数据库第二次作业——表的约束

目录

1.单字段主键

2.多字段主键

3. 外键

4. 

     非空   not null

     唯一   unique

     默认   default"默认值“

     自增    auto_increment 

5.作业

(1)   教师表 teacher

(2)   学生表 student

(3)   课程表 course

(4)   授课表 school_teach

​ 

(5)    成绩表 grade

1.单字段主键

create table 表名(

字段名1 数据类型1 comment "备注信息" primary key,

字段名2 数据类型2 comment "备注信息",

字段名3 数据类型3 comment "备注信息"

);

create table student(
no varchar(8) COMMENT "学号" primary key,
name varchar(4) comment "姓名",
age int comment"年龄"

);

 

2.多字段主键

create table 表名(

字段名1 数据类型1 comment "备注信息" ,

字段名2 数据类型2 comment "备注信息",

字段名3 数据类型3 comment "备注信息",

primary key(字段名1,字段名2)

);

create table classroom(
build varchar(5) comment "楼栋",
classroom_no int comment "教室号",
seat int comment "座位数",
primary key(build,classroom_no)

 );

3. 外键

constraint 字段别名 foreign key (字段名) referrnces 表名 (字段名)

班级表中 主键:班级名  其他字段:人数、班长

create table class(

   name varchar(20) primary key,

   count int,

   monitor varchar(5) comment "班长”

);

create table class(
name varchar(20) primary key,
count int,
monitor varchar(5) comment "班长"
 );

课表 主键:班级名、教师号  其他字段:课程名、.....

create table timetable(

class_name varchar(20),

teacher_no varchar(8),

course varchar(20) comment "课程名",

constraint 字段别名一般为fk_本表字段名 foreign key (本表字段名) references 来源表名(来源表名字段名),

constraint 字段别名一般为fk_本表字段名 foreign key (本表字段名) references 来源表名(来源表名字段名)

);

create table timetable(
class_name varchar(20),
teacher_no varchar(8),
course varchar(20) comment "课程名",
constraint fk_class_name foreign key (class_name) references class(name),
constraint fk_teacher_no foreign key (teacher_no) references teacher(no)
 );

4. 

     非空   not null

     唯一   unique

     默认   default"默认值“

     自增    auto_increment 

 

 

5.作业

创建如下表:

(1)   教师表 teacher

 

 

(2)   学生表 student

 

 

(3)   课程表 course

(4)   授课表 school_teach

 

 

(5)    成绩表 grade


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值