-- 创建一个数据库 students--
create database students;
-- 使用数据库 students --
use students;
-- 删除表student Course Student--
drop table Student,Course,Score;
-- 创建一个表 student --
create table Student(
sno int not null primary key auto_increment comment "学号",
sname varchar(5) not null comment "姓名",
ssex varchar(1) not null comment "性别",
sage int not null comment "年龄",
stel int not null comment "联系方式",
saddress varchar(50) not null default "地址不详" comment "住址"
)auto_increment=5;
-- 创建一个 Course 表 --
create table Course(
cno int not null primary key auto_increment comment "课程号",
cname varchar(5) not null comment "课程名",
ccredit varchar(1) not null default "5" comment "学分"
)auto_increment=1;
-- 创建一个成绩表 --
-- 设置外键 例如--
-- alter table 外键表名 add constraint 约束名称 foreign key (外键字段) references 主键表名(约束列名)
-- 如果表A中的Ids是主键,要约束表B中得Aid列,那么语句应该是:
-- alter table B add constraint A_B_Ids foreign key(Aid) references A(Ids)
create table Score(
cno int,
sno int,
score float not null comment "学分"
);
alter table Score add constraint cno foreign key(cno) references Course(cno);
alter table Score add constraint sno foreign key(sno) references Student(sno);
create database students;
-- 使用数据库 students --
use students;
-- 删除表student Course Student--
drop table Student,Course,Score;
-- 创建一个表 student --
create table Student(
sno int not null primary key auto_increment comment "学号",
sname varchar(5) not null comment "姓名",
ssex varchar(1) not null comment "性别",
sage int not null comment "年龄",
stel int not null comment "联系方式",
saddress varchar(50) not null default "地址不详" comment "住址"
)auto_increment=5;
-- 创建一个 Course 表 --
create table Course(
cno int not null primary key auto_increment comment "课程号",
cname varchar(5) not null comment "课程名",
ccredit varchar(1) not null default "5" comment "学分"
)auto_increment=1;
-- 创建一个成绩表 --
-- 设置外键 例如--
-- alter table 外键表名 add constraint 约束名称 foreign key (外键字段) references 主键表名(约束列名)
-- 如果表A中的Ids是主键,要约束表B中得Aid列,那么语句应该是:
-- alter table B add constraint A_B_Ids foreign key(Aid) references A(Ids)
create table Score(
cno int,
sno int,
score float not null comment "学分"
);
alter table Score add constraint cno foreign key(cno) references Course(cno);
alter table Score add constraint sno foreign key(sno) references Student(sno);