create table class(
cid tinyint unsigned primary key auto_increment,
caption varchar(15) not null
)engine=innodb;

create table student(
sid smallint unsigned primary key auto_increment,
sname varchar(15) not null,
gender enum('male','female') default 'male',
class_id tinyint unsigned,
constraint fk_name foreign key(class_id)
references class(cid)
on delete cascade
on update cascade
)engine=innodb;

create table teacher(
tid tinyint unsigned primary key auto_increment,
tname varchar(15) not null
)engine=innodb;

create table course(
cid smallint unsigned primary key auto_increment,
cname varchar(15) not null,
teacher_id tinyint unsigned,
constraint fk_teacher_id foreign key(teacher_id)
references teacher(tid)
on delete cascade
on update cascade
)engine=innodb;

create table score(
sid int unsigned primary key auto_increment,
number tinyint unique not null,
student_id smallint unsigned,
course_id smallint unsigned,
constraint fk_student foreign key(student_id)
references student(sid),
constraint fk_course foreign key(course_id)
references course(cid)
on delete cascade
on update cascade
)engine=innodb;

mysql 添加外键的更多相关文章

  1. mysql添加外键无法成功的原因

    最近很忙,碰到很多问题都忘了发上来做个记录,现在又忘了,FUCK,现在碰到一个问题, 就是mysql添加外键总是无法成功,我什么都试了,就是没注意signed和unsigned,FUCK,因为我用my ...

  2. MySQL中MyISAM与InnoDB区别及选择,mysql添加外键

    InnoDB:支持事务处理等不加锁读取支持外键支持行锁不支持FULLTEXT类型的索引不保存表的具体行数,扫描表来计算有多少行DELETE 表时,是一行一行的删除InnoDB 把数据和索引存放在表空间 ...

  3. MySQL添加外键的方法

    为book表添加外键: <1>明确指定外键的名称: 语法:alter table 表名 add constraint 外键的名称 foreign key(你的外键字段名) REFERENC ...

  4. mysql 添加外键详解

    为已经添加好的数据表添加外键: 语法:alter table 表名 add constraint FK_ID foreign key(你的外键字段名) REFERENCES 外表表名(对应的表的主键字 ...

  5. MySQL添加外键报错 - referencing column 'xx' and referenced column 'xx' in foreign key constraint 'xx' are incompatible

    MySQL给两个表添加外键时,报错 翻译意思是:外键约束“xx”中的引用列“xx”和引用列“xx”不兼容 说明两个表关联的列数据类型不一致,比如:varchar 与 int,或者 int无符号 与 i ...

  6. Mysql添加外键约束

    简单说一下使用外键的好处 1.完整性约束 比如:用户表中有字段 用户编号(id) , 名称(username)设备表中有字段 设备编号(id) , 设备名称(devicename) 设备属于的用户编号 ...

  7. mysql添加外键的4种方式

    今天开始复习,在过后的几天里开始在博客上记录一下平时疏忽的知识点,温故而知新 屁话不多--直接上货 创建主表: 班级 CREATE TABLE class(cid INT PRIMARY KEY AU ...

  8. MYSQL添加外键关联

    SELECT * from stu st,course co,score sc where st.sid = sc.sid and sc.cid = co.cid 如果我们要给 sid 做一个约束,即 ...

  9. mysql添加外键语句

    sql语句格式: · 添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key (从表外键字段) references 主表(主 ...

  10. mysql添加外键约束变为索引

    今天有位自己填上一坑:mysql储存引擎 原因就是数据库表引擎为:MyISAM,建立主外键关系需要是InnoDB: 解决方案:alter  table table_name1  engine=inno ...

随机推荐

  1. 检测浏览器是否支持cookie功能

    <script> if(navigator.cookieEnabled) { document.write("你的浏览器支持cookie功能!"); } else{ d ...

  2. iframe多窗口

    Window 对象 浏览器会在其打开一个 HTML 文档时创建一个对应的 window 对象.但是,如果一个文档定义了一个或多个框架(即,包含一个或多个 frame 或 iframe 标签),浏览器就 ...

  3. hdu3826-Squarefree number-(欧拉筛+唯一分解定理)

    Squarefree number Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. Velocity Obstacle

    [Velocity Obstacle] Two circular objects A,B, at time t(0), with velocity V(A),V(B). A represent the ...

  5. MONGO的简单语法,新手实用

    window上启动方式:(简单略) mongod --dbpath E:\study_lib\mongodb\db --port=27000 show dababases; (创建配置文件的启动方式) ...

  6. awk——getline

    A.getline从整体上来说,应这么理解它的用法: 当其左右无重定向符 | 或 < 时,getline作用于当前文件,读入当前文件的第一行给其后跟的变量var 或$0(无变量):应该注意到,由 ...

  7. 中国剩余定理模板 51nod 1079

    题目链接:传送门 推荐博客:https://www.cnblogs.com/freinds/p/6388992.html (证明很好,代码有误). 1079 中国剩余定理  基准时间限制:1 秒 空间 ...

  8. 测试--错误java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=select], {ExactMatcher:fDisplayName=select(com.rjj.demo.DemoApplicationTests)]...

    异常这个错误java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=select], {ExactMatche ...

  9. POJ 2396 Budget(有源汇上下界网络流)

    Description We are supposed to make a budget proposal for this multi-site competition. The budget pr ...

  10. python + selenium 学习笔记 -摘要

    一.浏览器操作相关 from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() # 窗口最大 ...