第二章--数据库的实现

知识点梳理:
   1.使用SQL语句创建和删除数据  
     创建:  create database S2226
on primary
(
name= '...',
filename='...._data.mdf',
size=5mb,
maxsize=100mb,
filegrowth=15%


)


log on
(
name='..._log',
filename='..._log.ldf',
size=2mb,
maxsize=100mb,
filegrowth=1mb
)
删除: DROP DATABASE 数据库名
   2.使用SQL语句创建和删除表
        创建: CREATE TABLE 表名
        (
          列1 数据类型 列的特征,
          .......
       )
        删除:  DROP TABLE 表名
   
   3.使用SQL语句创建和删除约束 
      创建:    ALTER TABLE 表名
     ADD CONSTRAINT 约束名 约束类型 具体的约束说明 
   删除: ALTER TABLE 表名
        DROP CONSTRAINT 约束名

--添加数据库
use master

go
if exists (select * from sysdatabases where name='S2226')
drop database S2226

create database S2226
on primary
(
name='S2226_data',
filename='E:\S2226_data.mdf',
size=5mb,
maxsize=100mb,
filegrowth=15%

)

log on
(
name='S2226_log',
filename='E:\S2226_log.ldf',
size=2mb,
maxsize=100mb,
filegrowth=1mb
)

--添加表
use S2226
go
if exists (select * from sysobjects where name='Student')
drop table Student

create table Student
(
StudentNo int identity(1,1),
LoginPwd  nvarchar(20) not null,
StudentName  nvarchar(20) not null,
Sex bit not null,
GradeID int not null,
Phone nvarchar(50) null,
Address nvarchar(200) null,
BornDate datetime not null,
Email nvarchar(50) null,
IdentityCard varchar(18) not null

)

go
if exists (select * from sysobjects where name='Grade')
drop table Grade

create table Grade
(
GradeID int identity(1,1),
GradeName nvarchar(50) not null
)

go
if exists (select * from sysobjects where name='Subject')
drop table Subject
create table Subject
(
SubjectNo int identity(1,1),
SubjectName nvarchar(50)  null,
ClassHour int null,
GradeID INT NULL

)

go
if exists (select * from sysobjects where name='Result')
drop table Result
create table Result
(
StudentNo int not null,
SubjectNo int not null,
ExamDate datetime not null,
StudentResult int not null

)


--添加约束

--年级表的年级号为主键
alter table Grade
add constraint PK_GradeID primary key (GradeID)

--学生表的id为主键
alter table Student
add constraint PK_StudentNo primary key (StudentNo)

--身份证唯一约束
alter table Student
add constraint UQ_IdentityCard unique (IdentityCard)

--地址默认约束
alter table Student
add constraint DF_Address default ('地址不详') for Address

--出生日期的检查约束
alter table Student
add constraint CK_BornDate check (BornDate>='1980-1-1')

--学生表与成绩表的外键约束

alter table Result
add constraint FK_StudentNo foreign key (StudentNo)
references Student(StudentNo)

--课程表中课程编号主键约束

alter table Subject
add constraint PK_SubjectNo primary key (SubjectNo)

--课时的检查约束
alter table Subject
add constraint CK_ClassHour check (ClassHour>='0')

--年级表与科目表外键约束
alter table Subject
add constraint FK_GradeID foreign key (GradeID)
references Grade(GradeID)

--成绩表主键约束

alter table Result
add constraint PK_Result primary key (StudentNo,SubjectNo,ExamDate)

--考试日期默认约束
alter table Result
add constraint DF_ExamDate default (GETDATE()) for ExamDate

--成绩的检查约束
alter table Result
add constraint CK_StudentResult check (StudentResult>=0 and StudentResult<=100 )

insert into Grade(GradeName) values ('S1'),('S2'),('Y2')


insert into Subject (SubjectName,ClassHour,GradeID) values ('JAVA',5,1)
insert into Subject (SubjectName,ClassHour,GradeID) values ('SQL',4,2)
insert into Subject (SubjectName,ClassHour,GradeID) values ('C#',6,3)

insert into Student (StudentName,Sex,LoginPwd,Phone,IdentityCard,BornDate,GradeID,Email) values ('徐小倩',1,'1','1364654364','413026199701120498','1997-1-12',2,'xuxiaoqianqq.com')

--科目表与成绩表
alter table Result
add constraint FK_Subject_Result_SubjectNo foreign key (SubjectNo)
references Subject(SubjectNo)

--学生表与成绩表
alter table Result
add constraint FK_Student_Result_StudentNo foreign key (StudentNo)
references Student(StudentNo)

insert into Result (StudentNo,SubjectNo,StudentResult) values (2,1,80)

你可能感兴趣的:(SQL,数据库设计)