当前位置:网站首页>MySQL database constraints, table design
MySQL database constraints, table design
2022-07-31 00:33:00 【drhrht】
Author: 老九
个人博客:老九的CSDN博客
?? 个人名言:不可控之事 乐观面对
?? 系列专栏:MySQL通关系列
文章目录
数据库约束
not null
指定某列的存储不能为null值
create table student (id int not null,name varchar(20));
Query OK, 0 rows affected (0.01 sec)mysql> desc student;
±------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±------------±-----±----±--------±------+
| id | int(11) | NO | | NULL | |
| name | varchar(20) | YES | | NULL | |
±------±------------±-----±----±--------±------+
2 rows in set (0.00 sec)
unique
- 保证某列必须有唯一的值,插入重复的值就会报错
default
规定给列赋值时的默认值
create table student(id int,name varchar(20) default ‘匿名’);
primary key 主键
- 主键约束,是not null 与unique的结合,确保某列的赋值不能为null,并且是唯一的
auto_increment
自增特点:
1.如果表中没有记录,自增从1开始
2.如果有数据,从上一条记录往下自增
3.插入再删掉数据,自增的值不会重复利用,会按删掉的那条开始自增create table student (id int primary key auto_increment,name varchar(20));
Query OK, 0 rows affected (0.01 sec)mysql> desc student;
±------±------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------±------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
±------±------------±-----±----±--------±---------------+
2 rows in set (0.00 sec)mysql> insert into student values(null,‘张三’);
Query OK, 1 row affected (0.00 sec)mysql> select * from student;
±—±-------+
| id | name |
±—±-------+
| 1 | 张三 |
±—±-------+
1 row in set (0.00 sec)
foreign key 外键
外键约束,在表一中的数据必须在表二中存在,要参照完整性准则
外键约束描述的是两张表的两个列之间的“依赖关系”
外键约束会影响表的删除,例如下面的实例的class表被关联,所以它不能被轻易删除
mysql> create table class (
-> id int primary key,
-> name varchar(20) not null
-> );
Query OK, 0 rows affected (0.04 sec)mysql> create table student (
-> id int primary key,
-> name varchar(20) not null,
-> email varchar(20) default ‘unknow’,
-> QQ varchar(20) unique,
-> classId int , foreign key (classId) references class(id)
-> );
Query OK, 0 rows affected (0.03 sec)mysql> desc class;
±------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±------------±-----±----±--------±------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
±------±------------±-----±----±--------±------+
2 rows in set (0.02 sec)mysql> desc student;
±--------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±--------±------------±-----±----±--------±------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| email | varchar(20) | YES | | unknow | |
| QQ | varchar(20) | YES | UNI | NULL | |
| classId | int(11) | YES | MUL | NULL | |
±--------±------------±-----±----±--------±------+
5 rows in set (0.00 sec)
check
指定一个条件,通过条件来对值进行判定
但是mysql并不支持
create table test_user (
id int,
name varchar(20),
sex varchar(1),
check (sex =‘男’ or sex=‘女’)
);
表的设计
一对一
- 一对一设计表就比如学生表和账户表,一个账户对应到一个学生,一个学生也只有一个账户
- 表示方法
1.可以把这两个实体用一张表来表示
2.可以用两张表来表示,其中一张表包含了另一个表的id
一对多
- 一个学生应该处于一个班级中,一个班级可以包含多个学生
- 表示方法:
1.在班级表中,新增一列,表示这个班级里的学生id都有啥(mysql没有数组类型,redis可以)
2.班级表不变,学生表中,新增一列classId
多对多
- 多对多设计表就好比学生表和课程表,一个学生可以选多个课程,一个课程也可以被多个学生选择
- 表示方法 :
使用一个关联表,来表示两个实体之间的关系
多对多建表实例
-- 学生表
mysql> create table test_student (
-> id int primary key,
-> name varchar(10) default 'unknow'
-> );
Query OK, 0 rows affected (0.03 sec)
-- 选课表
mysql> create table test_course (
-> id int primary key,
-> name varchar(20) default 'unknow'
-> );
Query OK, 0 rows affected (0.02 sec)
-- 成绩表
mysql> create table test_score (
-> studentId int,
-> courseId int,
-> score int,
-> foreign key (studentId) references test_student(id),
-> foreign key (courseId) references test_course(id)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc test_student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(10) | YES | | unknow | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> desc test_coures;
ERROR 1146 (42S02): Table 'java_5_27.test_coures' doesn't exist
mysql> desc test_course;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | unknow | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> desc test_score;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| studentId | int(11) | YES | MUL | NULL | |
| courseId | int(11) | YES | MUL | NULL | |
| score | int(11) | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
插入数据到实例实现多对多
mysql> insert into test_student values (1, 'listen');
Query OK, 1 row affected (0.01 sec)
mysql> insert into test_course values (1, '数学');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test_student values (2, 'Faker');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test_course values (2, '数学');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test_score values(1, 1, 90);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test_score values (1, 2, 99);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test_score values (2, 1, 50);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test_score values (2, 2, 60);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test_student;
+----+--------+
| id | name |
+----+--------+
| 1 | listen |
| 2 | Faker |
+----+--------+
2 rows in set (0.00 sec)
mysql> select * from test_course;
+----+--------+
| id | name |
+----+--------+
| 1 | 数学 |
| 2 | 语文 |
+----+--------+
2 rows in set (0.00 sec)
mysql> select * from test_score;
+-----------+----------+-------+
| studentId | courseId | score |
+-----------+----------+-------+
| 1 | 1 | 90 |
| 1 | 2 | 99 |
| 2 | 1 | 50 |
| 2 | 2 | 60 |
+-----------+----------+-------+
4 rows in set (0.00 sec)
————————————————————————
码字不易,大家的支持就是我坚持下去的动力
版权声明:本文为CSDN博主「浦上青天」的原创文章
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- 加密传输过程
- What are the efficient open source artifacts of VSCode
- Go study notes (84) - Go project directory structure
- Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
- pytorch双线性插值
- MySQL筑基篇之增删改查
- Bypass of xss
- [Deep learning] Detailed explanation of Transformer model
- MySQL triggers
- IOT跨平台组件设计方案
猜你喜欢

Strict Mode for Databases

joiplay模拟器不支持此游戏类型怎么解决

消息队列存储消息数据的MySQL表设计

Learn Scope from a Compilation Perspective!

h264和h265解码上的区别
![45. [Application of list linked list]](/img/7a/ca026cafeceffd2daee68fe66e1882.png)
45. [Application of list linked list]

C language force buckles the rotating image of the 48th question.auxiliary array

WMware Tools安装失败segmentation fault解决方法

asser利用蚁剑登录

【唐宇迪 深度学习-3D点云实战系列】学习笔记
随机推荐
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
[Deep learning] Detailed explanation of Transformer model
pytorch bilinear interpolation
从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术
Gabor滤波器学习笔记
background对float的子元素无效
binglog log tracking: data backup and backup tracking
Asser uses ant sword to log in
firewalld
Meeting OA project pending meeting, all meeting functions
DNS解析过程【访问网站】
Go 学习笔记(84)— Go 项目目录结构
MySQL的grant语句
The difference between substring and substr in MySQL
ES 中时间日期类型 “yyyy-MM-dd HHmmss” 的完全避坑指南
46.
How to use joiplay emulator
从两个易错的笔试题深入理解自增运算符
Thesis understanding: "Designing and training of a dual CNN for image denoising"
【深入浅出玩转FPGA学习14----------测试用例设计2】