sql server2005中用语句创建数据库和表
来源:易贤网 阅读:620 次 日期:2014-09-15 09:15:04
温馨提示:易贤网小编为您整理了“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--默认约束,

更多信息请查看IT技术专栏

更多信息请查看数据库
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标