用户名: 密码: 免费注册 忘记密码? 网站地图 | 加入收藏 | 设为首页
首页 | 新闻 | 工具 | 系统 | 办公 | 聊天 | 多媒体 | 网页 | 运营 | 平面 | 欣赏 | 数据库 | 程序 | 服务器 | 组网
网页 | 3dmax | Ghost | Windows Xp| Dreamweaver | photoshop | Flash | office | Alexa | Css | QQ | Asp | PHP | Jsp | Access
Flash MX 2004入门 | 网站推广策略 | CorelDRAW入门 | ASP学习 | 网站建设大师功 | Word入门
  iTbulo.com > 学院 > 数据库开发教程 > MySQL教程 > Mysql基础教程 > 正文
SQL Server2005中用语句创建数据库和表
iTbulo.COM 2008-3-13 Alice()
在SQL Server2005中用语句创建数据库和表:

  具体示例如下:

  use master

  go

  if exists (select * from sysdatabases where name='Study')

  --判断Study数据库是否存在,如果是就进行删除

  drop database Study

  go

  EXEC sp_configure 'show advanced options', 1

  GO

  -- 更新当前高级选项的配置信息

  RECONFIGURE

  GO

  EXEC sp_configure 'xp_cmdshell', 1

  GO

  -- 更新当前功能(xp_cmdshell)的配置信息。

  RECONFIGURE

  GO

  exec xp_cmdshell 'mkdir D:\data', NO_OUTPUT

  --利用xp_cmdshell 命令创建文件夹,此存储过程的第一个参数为要执行的有效dos命令,第二个参数为是否输出返回信息。

  go

  create database Study--创建数据库

  on primary

  (

  name='Study_data',--主数据文件的逻辑名

  fileName='D:\data\Study_data.mdf',--主数据文件的物理名

  size=10MB,--初始大小

  filegrowth=10% --增长率

  )

  log on

  (

  name='Study_log',--日志文件的逻辑名

  fileName='D:\data\Study_data.ldf',--日志文件的物理名

  size=1MB,

  maxsize=20MB,--最大大小

  filegrowth=10%

  )

  go

  use Study

  go

  if exists (select * from sysobjects where name='Student')--判断是否存在此表

  drop table Student

  go

  create table Student

  (

  id int identity(1,1) primary key,--id自动编号,并设为主键

  [name] varchar(20) not null,

  sex char(2) not null,

  birthday datetime not null,

  phone char(11) not null,

  remark text,

  tId int not null,

  age as datediff(yyyy,birthday,getdate())--计算列。

  )

  go

  if exists (select * from sysobjects where name='Team')

  drop table Team

  go

  create table Team

  (

  id int identity(1,1) primary key,

  tName varchar(20) not null,

  captainId int

  )

  go

  alter table Student

  add

  constraint CH_sex check(sex in ('男','女')),--检查约束,性别必须是男或女

  constraint CH_birthday check(birthday between '1950-01-01' and '1988-12-31'),

  constraint CH_phone check(len(phone)=11),

  constraint FK_tId foreign key(tId) references Team(id),--外键约束,引用Team表的主键

  constraint DF_remark default('请在这里填写备注') for remark--默认约束,

  go

[1] [2] 下一页

文章搜索
相关资讯
相关文章 相关下载
没有相关文章
焦点信息