SQLServe_使用T-SQL语句创建数据库、创建表以及表的约束

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

你可能感兴趣的:(SQLServe_使用T-SQL语句创建数据库、创建表以及表的约束)