学生课程数据库
一、建库
create database xs
1.选择数据库右击,选择"新建数据库"

- 输入数据库名(版本不一样界面也不一样),大小默认,随后点击确定

二、建表
(1)学生信息表
create table student
(sno int primary key,
sname varchar(50),
ssex varchar(10) check(ssex='男' or ssex='女'),
sage int check(sage>17),
sdept varchar(10))
1.选择新建的数据库"xs"右击选着"新建表"

2.按照给定的列输入列名和数据类型

3.选择sno列右键添加主键

- 选择ssex列添加约束如图



5.选择sage列添加约束如图

6.保存命名

(2)建立course表
create table course
(cno int unique,
cname varchar(50),
cpno int foreign key references course(cno),
ccredit int)
- 按照上述方式建表,列名和数据类型如下(注意一点要√允许为空,本表不设主键)

2.选择第一列添加索引,改为"唯一值"


- 为cpno列添加外键


- 保存命名
(3)创建sc表
create table sc
(sno int foreign key references student(sno),
cno int foreign key references course(cno),
grade int check(grade>=0 and grade<=100))
1.按照上述过程建表,列名和数据类型如下图

- 选择sno列右键选择"关系"建立外键关系
- 选择cno列右键选择"关系"添加外键关系


- 选择grade列右键选择"check约束",按图所示添加约束


三、向表中添加数据
(1)
insert into student
values
(201215121,'李勇','男',20,'CS'),
(201215122,'刘晨','女',19,'CS'),
(201215124,'王敏','女',18,'MA'),
(201215125,'张立','男',19,'IS')

(2)course表中有参照完整性约束所以不能直接按顺序输入数据,需要将参照值为null的数据线录入
insert into course
values
(2,'数学',null,2),
(6,'数据处理',null,2),
(4,'操作系统',6,3),
(7,'PASCL语言',6,4),
(5,'数据结构',7,4),
(1,'数据库',5,4),
(3,'操作系统',1,4)

(3)
insert into sc
values
(201215121,1,92),
(201215121,2,85),
(201215121,3,88),
(201215122,2,90),
(201215121,3,80)
