mySQL设置外键

-- 创建一个数据库 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值