mysql 外键sql语言编写

MySQL表创建、数据添加及外键操作
博客介绍了MySQL数据库操作,包括创建班级表和学生表,向表中添加数据,将学生表外键关联到班级表主键,还涉及删除外键、添加外键以及查看外键是否添加成功等操作。

在这里插入图片描述
先创建一个班级表
create table c_class(id int(5) primary key auto_increment,name varchar(10));
创建好后向里面添加数据
insert into c_class values(0,‘python1’),(0,‘python2’);
再创建一个学生表 将学生表的外键加到班级表的主键中
create table t_student(id int primary key auto_increment,name varchar(10),gender enum(‘男’,‘女’),c_id int(5) ,foreign key(c_id) references c_class(id));
向学生表添加数据
#添加的c_id不能超过c_class的范围
inset into t_student (0,‘tom’,‘男’,1),(0,‘jack’,‘女’,2);
删除外键
先查看外键名
show create table t_student;
alter table t_student drop foreign key 外键名;
添加外键名
alter table t_student add foreign key(c_id) references c_class(id);
查看是否加入外键
show create table t_student;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值