SQLServe_使用T-SQL语句创建数据库、创建表以及表的约束
2017-01-21 09:51
801 查看
if exists(select * from sysdatabases where name='school') begin drop database school end go create database school on primary ( name='school_data', --主数据文件的逻辑名称 filename='F:\project\school_data.mdf', --主数据文件的物理名称及地址 size=5mb, --主数据文件的初始大小 maxsize=100mb, --主数据文件的最大值 filegrowth=15% --主数据文件的增长率 ) log on ( name='school_log.ldf', --数据日志文件的逻辑名称 filename='F:\project\school_log.mdf', --数据日志文件的物理名称及地址 size=2mb, --数据日志文件的初始大小 filegrowth=1mb --数据日志文件的增长速度 ) go --建表 use school go --1.年级表 if exists(select * from sysobjects where name='Grade') begin drop table Grade end go create table Grade ( GradeId int Identity(1,1) not null, GradeName nvarchar(50) not null ) go --2.学生表 --如果要创建的表已存在,那么就删除 if exists(select * from sysobjects where name='Student') begin drop table Student end go create table Student ( StudentNo nchar(8) not null, StudentName nvarchar(20) not null, LoginPwd nvarchar(20) not null, Sex nchar(1) not null, GradeId int not null, Phone nvarchar(20) not null, Address nvarchar(50) not null, BornDate nvarchar(50) not null, EMail nvarchar(50) not null ) --3.科目表 if exists(select * from sysobjects where name='Subject') begin drop table Subject end go create table Subject ( SubjectId int identity(1,1) not null, SubjectName nvarchar(20) not null, ClassHour int not null, GradeId int not null ) --4.成绩表 if exists(select * from sysobjects where name='Result') begin drop table Result end go create table Result ( Id int identity(1,1) not null, StudentNo nchar(8) not null, SubjectId int not null, StudentResult int not null, --0-100之间 ExamDate datetime not null --默认为当前日期 ) --约束 --1.年级表约束 alter table Grade add constraint PK_GradeId primary key(GradeId) --主键约束 go alter table Grade add constraint UQ_GradeName unique (GradeName) --唯一约束 go --2.学生表约束 alter table Student add constraint PK_StudentNo primary key(StudentNo) go alter table Student add constraint CK_StudentNocheck check(StudentNo like 'S20118[0-9][0-9]')--检查约束 go alter table Student add constraint CK_LoginPwd check (len(LoginPwd)>=6 and len(LoginPwd)<=12) go alter table Student add constraint FK_Student_GradeId foreign key(GradeId) references Grade(GradeId) go alter table Student add constraint CK_Sex check(sex in ('男','女')) go alter table Student add constraint CK_Phone check(Phone like '1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]') go alter table Student add constraint DF_Address default('学生宿舍') for Address go alter table Student add constraint CK_BornDate check(BornDate>1990-1-1) go alter table Student add constraint CK_Email check(Email like '%@%') go --科目表约束 alter table Subject add constraint PK_SubjectId primary key(SubjectId) go alter table Subject add constraint CK_ClassHour check(ClassHour>0) go alter table Subject add constraint FK_Subject_GradeId foreign key(GradeId) references Grade(GradeId) go --成绩表约束 alter table Result add constraint PK_Id primary key(Id) go alter table Result add constraint FK_Result_StudentNo foreign key(StudentNo) references Student(StudentNo) go alter table Result add constraint FK_Result_SujectId foreign key(SubjectId) references Subject(SubjectId) go
相关文章推荐
- SQL语句汇总(一)——数据库与表的操作以及创建约束
- 数据库常用的SQL 语句(一)创建数据库,表以及添加约束
- SQL语句汇总(一)——数据库与表的操作以及创建约束
- 创建数据库、表、主外键、各种约束、存储过程、视图、索引、事务使用、触发器、创建登录账号、数据库用户 Sql语句示例
- 使用SQL语句创建和删除约束
- 用Transact-SQL代码实现创建数据库、数据库表、以及设置数据库表的约束
- Sql语句在SqlServer中创建数据库、表格并添加约束
- 使用SQL语句创建和删除约束
- 在创建外键约束的时候, 设置级联删除为SET NULL执行sql语句的时候显示有错误,(数据库为sqlserver2000)?
- 使用SQL语句创建和删除约束
- 使用动态SQL语句 创建数据库
- ReportView如何使用构造SQL语句带参数的存储过程创建报表以及为rdlc传递参数
- 使用SQL语句创建数据库
- 使用SQL语句创建登录名,并为指定数据库分配制定权限
- 1.在sql server 2000 或者 2005中使用sql语句创建数据库
- SQL语句创建数据库表介绍以及在.net中创建介绍!
- 使用 SQL 语句创建 School 数据库(包括参数)、 TblClass 表、 TblStudent 表。
- 数据库sql语句——之sql语句创建数据库并增加约束
- 使用SQL语句对数据库以及数据库表的操作
- SQL Server 语法 使用SQL语句创建数据库、表