您的位置:首页 > 数据库

Sql server2005 创建数据和表的脚本

2015-07-22 08:17 232 查看
use master
go
if exists(select*
from sys.databases wherename='数据库名称')
drop database 数据库名称
go
--调用DOS命令创建文件夹(首先打开SQL
Server外围应用配置器——功能的外围应用配置器——启用xp_cmdshell)
exec xp_cmdshell'mkdir文件夹放置路径'
go
/*------------------------建库------------------------*/
create database 数据库名
/*----------------------数据文件----------------------*/
on primary                              
(
name='',                                ---主文件名称
filename='',                            ---主文件物理路径
size=10,                                 ---主文件初始值大小
maxsize=100,                             ---主文件增长最大值
filegrowth=100%                           ---主文件增长率
)
/*----------------------日志文件----------------------*/
log on                                  
(
name='',             
filename='',
size=20,
filegrowth=1
)
go
use 数据库名
go
if exists(select*
from sysobjectswhere
name='表明称')
drop table 表明称
go
/*------------------------建表------------------------*/
create table 表明称
(
--建表所需要的字段
)
go
/*----------------------添加约束----------------------*/
alter table 表明称addconstraint pk_idprimarykey(字段名称)  ---表设定主键
/*--------------------给表插入数据--------------------*/
--insert UserInfo(字段,字段,字段,字段)
--select'','','',0union
--select'','','',1union
insert into UserInfo (字段,字段,字段,字段) values (值,值,值,值)
select *
from UserInfo

--实例:
use master
go
if exists(select*from sysdatabaseswherename='usDB')
drop database usDB
go
exec xp_cmdshell'mkdir E:\DB'  --调用DOS命令创建文件夹
/*-------建库-------*/
create database usDB
on primary
(
/*--数据文件的具体描述--*/
name='Users',  --主文件名称
filename='E:\DB\Users.mdf',   --主文件物理路径
size=10,          --主文件初始值大小
maxsize=100,      --主文件增长的最大值
filegrowth=100%   --主文件增长率
)
log on
(
/*--日志文件的具体描述--*/
name='User',
filename='E:\DB\Users.ldf',
size=20,
filegrowth=1
)
go
use usDB
go
if exists(select*from sysobjectswherename='UserInfo')
drop table UserInfo
go
/*-------建表--------*/
create table UserInfo
(
Id int identity(1,1)notnull, 
--标识列
Uname varchar(20)notnull,    
--用户名
LogInId varchar(20)notnull,
Upassword varchar(20)notnull,
--用户密码
Usex bit
not null             --用户性别
)
go
/*-------添加约束-------*/
alter table userInfoadd
constraint pk_Id
primary key(Id),
constraint DF_Usex
default(1) for Usex
go
/*---表中插入数据---*/
insert UserInfo(Uname,LogInId,Upassword,Usex)
select'张可','zhangke','zhangke',0union
select'李杨','天堂浪子','liyang',1union
select'杨晓','绝美','yangxiao',1union
select'汤美','lulcy','tangmei',0
select *
from UserInfo

--主键约束(Primary Key constraint):要求主键列的数据唯一,并且不允许为空。

--唯一约束(Unique Constraint):要求该列唯一,允许为空,但只能出现一个空值。

--检查约束(Check Constraint):某列取值范围限制、格式限制等,如有关年龄的约束。

--默认约束(Default Constraint):某列的默认值,如我们的男性同学较多,性别默认为男。

--外键约束(Foreign Key):用于在两表之间建立关系需要制定引用主表的哪一列。

 

 

语法如下

 

alter table 表名

add constraint 约束名 约束类型具体的约束说明

 

 

示例:

--添加主键约束

alter table stuInfo

add constraint PK_stuNo primary key(stuNo)

--添加唯一键约束

alter table stuInfo

add constraint UQ_stuID unique(stuID)

--添加默认约束

alter table stuInfo

add constraint DF_stuAddress default('地址不详') for stuAddress

--添加检查约束

alter table stuInfo

add constraint CK_stuAge check(stuAge between 15 and 40)

--添加外键约束

alter table stuInfo

add constraint FK_stuNo foreign key(stuNo) references stuInfo(stuNo)

 

 

删除约束

 

alter table 表名 drop constraint 约束名
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: