标题 | sql server2005中用语句创建数据库和表 |
范文 | 在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 : |
随便看 |
|
在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。