mysql创建表格1warning_MySQLMySQL创建表及相关约束
第一个表创建:
create table class(
cid int not null auto_increment primary key,
caption char(20) not null
)engine=innodb default charset=utf8;
插入数据:
insert into class(caption) values('三年二班');
insert into class(caption) values('一年三班');
insert into class(caption) values('三年一班');
第二个表创建:
create table student(
sid int not null auto_increment primary key,
sname char(20) not null,
gender char(20) not null,
class_id int
)engine=innodb default charset=utf8;
增加约束(外键):
alter table student add constraint foreign key student(class_id) references class(cid);
插入数据:
insert student(sname,gender,class_id) values('钢弹','女',1);
insert student(sname,gender,class_id) values('铁锤','女',1);
insert student(sname,gender,class_id) values('山炮','男',2);
第三个表创建:
create table teacher(
tid int not null auto_increment primary key,
tname char(20) not null
)engine=innodb default charset=utf8;
插入数据:
insert teacher(tname) values('波多');
insert teacher(tname) values('苍空');
insert teacher(tname) values('饭岛');
第四个表创建:
create table course(
cid int not null auto_increment primary key,
cname char(20) not null,
tearch_id int
)engine=innodb default charset=utf8;
增加约束:
alter table course add constraint foreign key course(tearch_id) references teacher(tid);
插入数据:
insert course(cname,tearch_id) values('生物',1);
insert course(cname,tearch_id) values('体育',1);
insert course(cname,tearch_id) values('物理',2);
第五个表创建:
create table score(
sid int not null auto_increment primary key,
student_id int not null,
corse_id int not null,
number int not null
)engine=innodb default charset=utf8;
增加约束:
alter table score add constraint foreign key score(student_id) references student(sid);
alter table score add constraint foreign key (corse_id) references course(cid);
可能存在的问题:
假设第二句写成:alter table score add constraint foreign key score(corse_id) references course(cid);
会报错:ERROR 1061 (42000): Duplicate key name 'score'
原因是:外键名称重复,在key后面增加表名会默认作为外键名,因此如果写2条,就会出现外键名称重复
解决办法:删除key后面的表名,mysql会默认增加索引
插入数据:
insert score(student_id,corse_id,number) values(1,1,60);
insert score(student_id,corse_id,number) values(1,2,59);
insert score(student_id,corse_id,number) values(2,2,100);
mysql创建表格1warning_MySQLMySQL创建表及相关约束相关推荐
- MySQL之数据类型、建表和六大约束
前言:昨天跟大家分享了MySQL的账号管理.建库及四大引擎,今天与大家分享的知识是MySQL之数据类型.建表和六大约束. 一.数据类型介绍 1.作用:MySQL中定义数据字段的类型对你数据库的优化是非 ...
- html中jquery创建表格,jQuery创建表格
创建表格 table{ border:#0099FF 1px solid; width:600px; border-collapse:collapse; } table td{ border:#009 ...
- mysql 自连接 树形_自连接表的相关问题(树形结构)
问题一: 自连接表肯定是相似 Id(PK),parentId(FK),name -.了 Id假如是主键的话,parentId就是外键了, 可是树形结构肯定是有根节点了,那么根节点的parentId按 ...
- 使用WPS文档创建表格怎么调整行间距
在WPS文档中可以绘制表格,表格绘制完成后,大家可在表格中输入想要展现的表格数据,表格数据可以根据需要进行调整,比如调整表格数据行间距,调整表格宽度等. 由于是手动绘制的表格,在调整表格时可能会出现牵 ...
- 【ArcGIS Pro二次开发】(14):使用字典(Dictionary)创建表格(Table)
在规划工作中,经常需要输入表格数据,如[用地用海分类表]. 之前的经验是做一个Excel表格,通过导入Excel,转换成数据库表格(Tabel). 但这样有一点不好的地方,Excel表格作为输入数据, ...
- 用html5创建表格
1.表格的基本结构 <table>:一个表格只允许出现一对<table>,html5不再支持它的任何属性 <tr>:表示表格的行数,h ...
- Python代码创建表格
Python代码创建表格 简介 在数据分析和处理的工作中,常常需要将数据整理成表格的形式,方便查看和分析.Python作为一门流行的编程语言,提供了多种创建表格的工具和库,极大地方便了数据处理的工作. ...
- mysql select查询2个表_mysql – 为SELECT查询合并2个表?
首先..这里是我创建的两个表(没有相关的列)- CREATE TABLE users_history1 ( circuit tinyint(1) unsigned NOT NULL default ' ...
- mysql创建表格1warning_MySQL:创建、修改和删除表
在说创建.修改和删除表前,我们还是要进行一个操作的简单说明: 1.登陆数据库系统 在命令行中登陆MySQL数据库管理系统,输入一下内容: mysql -h localhost -u root -p 很 ...
最新文章
- SAP云采购解决方案入华在即
- VC(MFC、ATL)中 得到2个SYSTEMTIME时间差
- java中list去除空值_Java –从列表中删除所有空值
- [洛谷P1231] 教辅的组成
- 为什么 Go 的泛型一拖再拖?(转)
- 查找数组中特定元素_Leetcode 540 题 有序数组中的单一元素
- DP 转移方程 —— 单调队列优化 斜率优化 李超树优化
- 2020年上海将初步建成“泛在化、融合化、智敏化”智慧城市
- 中缀表达式转前缀表达式
- SwitchResX for Mac(屏幕分辨率修改工具)
- 【光学】基于matlab相互垂直的光波叠加仿真【含Matlab源码 2071期】
- 浅析 HLS 流媒体协议
- 一文读懂沃尔玛、腾讯、京东、浙商银行在供应链领域的区块链应用实例
- 《Cinema 4D + After Effects动态图形设计案例解析》——1.2 动态图形的历史和发展...
- NB-IoT窖井井盖解决方案
- 国内外IP黑名单查询网站和邮件相关DNS的查询大全
- DesignWare 加密文件 综合成GTECH 以便FPGA使用
- Markdown/KaTeX/LaTeX语法、工具及模板大全(持续更新ing...)
- 【功能实现】qrcode生成二维码Demo
- Centos 7 新版安装mysql
热门文章
- BZOJ.3227.[SDOI2008]红黑树tree(树形DP 思路)
- Oracle的 listagg() WITHIN GROUP ()函数使用
- PAT-1124. Raffle for Weibo Followers (20)
- GameBryo Shader 组织方式
- 用Eclipse开发PHP项目
- 101.编译是如何把一个机器的语言拿到另一台机器语言机器上执行的?
- 重磅!2021年考研国家线正式公布,部分学科分数线比去年更低!
- 生活在AI的世界是种什么体验?
- matlab计算运行时间方法
- ios linux时间戳转时间,将UNIX时间戳转换为数据 – iOS