mysql 创建学生表

– 如果存在名为school的数据库就删除它

drop database if exists school;

– 创建名为school的数据库并设置默认字符集为utf8

create database school default charset utf8;

– 切换到school数据库

use school;

– 创建学院表

create table tb_college
(
colid int not null comment '学院编号',
colname varchar(20) not null comment '学院名称',
coltel varchar(20) not null comment '联系电话',
colwebsite varchar(255) comment'网站地址',
primary key(colid)

);

– 给学院网址加上唯一约束

alter table tb_college add constraint uni_website unique (colwebsite);

– 创建老师表

create table tb_teacher 
(
tid int not null,
colid int not null,
tod int not null,
tname varchar(4) not null,
primary key (tid)

);
alter table tb_teacher add constraint fk_teacher_colid foreign key (colid) references tb_college (colid);

– 创建学生表(tb_student)

create table tb_student
(
stuid int not null,
stuname varchar(4) not null,
stusex bit default 1,
stuaddr varchar(50),
colid int not null comment '学院编号',
primary key (stuid)
);
alter table tb_student add constraint fk_student_colid foreign key (colid) references tb_college (colid);
insert into tb_college (colid,colname,coltel) values 
(10, '计算机学院','028-88556678'),
(20, '外国学院','028-88556668'),
(30, '经济管理学院','028-88556658');

– 插入数据

insert into tb_student values 
(1001,'小强',1,'四川成都',30),
(1002,'花月',1,'四川成都',10),
(1003,'小红',1,'四川成都',20),
(1004,'小白',1,'四川成都',10),
(1005,'小青',1,'四川成都',30),
(1006,'小黑',1,'四川成都',10),
(1007,'白龙',1,'四川成都',20),
(1008,'小花',1,'四川成都',20),
(1009,'白马',1,'四川成都',30),
(1010,'冷面',1,'四川成都',30),
(1011,'白洁',1,'四川成都',20),
(1012,'紫薇',1,'四川成都',20),
(1013,'杨洋',1,'四川成都',20);

– 创建课程表

create table tb_course
(
cid int not null comment '课程编号',
cname varchar(20) not null comment '课程名称',
ccredit int not null comment'学分',
tid int not null,
primary key (cid)

);
alter table tb_course add constraint fk_course_tid foreign key (tid) references tb_teacher (tid);

– 创建学生选课表(tb_sc)

create table tb_sc
(
-- 自动增减
scid int not null auto_increment comment'选课记录号',
stuid int not null comment'学号',
cid int not null comment '课程编号',
scdate datetime default now(),
score decimal(4,1) comment'成绩',
primary key (scid)

);
alter table tb_sc add constraint fk_sc_stuid 
foreign key (stuid) references tb_student (stuid);

alter table tb_sc add constraint fk_sc_cid
foreign key (cid) references tb_course (cid);
  • 24
    点赞
  • 135
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值