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 约束名
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 约束名
相关文章推荐
- oracle insert &字符插入问题
- oracle sqlplus脚本建库总结
- 使用WinDbg调试SQL Server查询
- mysql集群的配置
- golang 下 mongodb 与 postgresql 插入速度的测试
- SQL 模糊查询
- flask-SQLAlchemy一些使用技巧(关联关系的创建)
- oracle11gR2 导出表报EXP-00011:table不存在。
- mysql的入门基础操作
- 将一些文档(.zip,.txt,.pdf)进行流处理,存储到mysql中blob字段中
- 安装PL/SQL Developer
- MySql数据库的导入_命令工具
- 好吧,使用sql实现Dijkstra算法
- Asp.mvc(一)~使用MongoDB来存储数据
- SQL Server 2008数据备份与还原(原理篇)
- 如何使用log miner分析oracle日志
- 如何使用MySQL查询某个列中相同值的数量统计
- SQL取分组后每组中的一条特定记录
- 严重注意MSSQL视图跨数据库复制的问题
- SQL日志文件长度过大的处理方法