cmd操作MySQL 创建约束(日记 day2)

这篇博客记录一下我学习创建约束的过程。
总结一下知识点,还有排雷。

首项,要明白几个概念:
约束:
主键约束、外键约束、自增约束、唯一约束、非空约束

下篇介绍设计范式、表的查询

一、 主键约束

这个字段能唯一确定表中的一条记录。比如说,有张学生信息表,有字段id,每个学生都有自己特有的id,不重复,不为空。通过id能映射有且仅有一个学生这个id就是主键。
创建主键约束:create table 表名(字段 类型 primary key,下个字段等等);
这个主键字段传值的时候是不能传重复或者null的,不然会报错。

create table stu(id int ,name char,class int,primary key(id,name));
//联合主键,只要这连个加起来的内容不重复、不为空就可

mysql> create table stu(id int primary key,name char,class int); 

mysql> desc stu;
+-------+---------+------+-----+---------+-------+
| Field |Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id   | int     | NO    | PRI | NULL |     |  //(有个标记PRI)
| name | char(1) | YES   |     |NULL  |       |
| class|int      | YES   |     |NULL  |       |
+-------+---------+------+-----+---------+-------+
3 rows in set
(0.12 sec)


*****这几个的意思分别是
Field 字段|Type字段类型  | Null是否允许为空 | Key有没有键 | Default有没有默认值 | Extra 备注

字段|类型|空|键|默认|额外

1。删除主键:
alter table 表名 drop primary key ; //因为一张表只能有一个主键,所以不用指定删哪个字段的主键

mysql> alter table stu drop primary key; 
mysql> desc
stu;
+-------+---------+------+-----+---------+-------+
| Field |Type    | Null | Key | Default | Extra |  
+-------+---------+------+-----+---------+-------+
| id    | int    | NO   |     | NULL   |       |
| name  | char(1) | YES  |     |NULL    |       |
| class |int     | YES  |     |NULL    |       |
+-------+---------+------+-----+---------+-------+
//你发现了什么没有,删完了主键它的null规定那里居然是NO。

**注意:这个被我们定义了主键又删除的字段是不能传空值null的

二、自增约束
顾名思义就是自己加东西,如果你没有给他传参的话。举个栗子,有两个字段,一个是 class(自增约束),一个是name,当我传name值而不传class值的时候,它就会自动帮我加上一个class

一个表只能有一个自增列
自增列必须是整型的
自增列必须是键列,例如:主键,唯一键

自增的三种方法:

insert into stu (id,name) values(1,‘A’);
insert into stu values(1,‘A’,null);
insert into stu (id,name) values(1,‘A’,0);

还是用这个stu表,我用修改约束的方法,把class改为自增约束。

扫描二维码关注公众号,回复: 11037293 查看本文章

1。修改约束:
alter table stu modify class int primary key auto_increment;
这一条字段的属性就变成了:
| class | int | NO | PRI | NULL | auto_increment |


mysql>
insert into stu values(1,'A');
//如果这样没有指定的传参是会报错的
ERROR 1136 (21S01): Column count doesn't match value count at row 1
//可以传null进去

mysql>
insert into stu (id,name) values(1,'A');
Query
OK, 1 row affected (0.26 sec)
mysql>select * from stu;
+----+------+-------+
| id |name | class |
+----+------+-------+
|  1 | A   |     1 |
+----+------+-------+

三、唯一约束
可以这样理解,唯一约束就是可以传空值的主键。唯一约束的原则就是不能有重复的记录
一山容不得二虎,除非是一公一母,也可以没有老虎。
create table shan(laohu int,sex char unique);

//同样这里也可以像主键的格式一样写成联合唯一约束。
create table shan(laohu int,sex char, unique(laohu,sex));


mysql> create table shan(laohu int,sex char unique);
Query OK, 0 rows affected (0.52 sec)

mysql> desc shan;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| laohu | int     | YES |     | NULL    |      |
| sex   | char(1) | YES  | UNI | NULL    |      |
+-------+---------+------+-----+---------+-------+

mysql> insert into shan values(1,'x');
mysql> insert into shan values(1,'x');
//当我输入2次x给sex(唯一约束)时就会报错
ERROR 1062 (23000): Duplicate entry 'x' for key 'shan.sex'


四、非空约束
非空约束跟唯一约束组合起来就是主键约束,所以非空约束就是不能是空值。

create table student (TEL int not null,class,int);
//字段TLE电话就是不能是空的,不给联系方式不让走


mysql> create table student(TEL int not null,class int);
Query OK, 0 rows affected (0.80 sec)
mysql> desc student ;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra|
+-------+------+------+-----+---------+-------+
| TEL  | int  | NO   |    | NULL    |       |
| class | int | YES  |     | NULL   |       |
+-------+------+------+-----+---------+-------+
//这里的NULL是NO

五、默认约束
传数据到字段中,如果没有数据,就默认给个值
create table teacher(age int default 40,id int);


mysql> insert into teacher values(null,1);
Query OK, 1 row affected (0.15 sec)
mysql> insert into teacher (id) values(2);
Query OK, 1 row affected (0.14 sec)

mysql> select*from teacher;

+------+------+
| age  |id   |
+------+------+
| NULL |   1 |
|   40|    2 |
+------+------+
2 rows in set (0.00 sec)
//传入了两条数据

六、外键约束
这个就关于到多个表了。
假如有两个表,student、class,外键是student表的一个字段,不是主键,但 是class表的主键。
student表里有个字段叫班级,设置成外键,在class就有班级号(主键)与之对应。


mysql> create table student(class_id int,id int);
Query OK, 0 rows affected (0.97 sec)
mysql> create table class(id int primary key, name varchar(20),foreign key(class_id)references class(id));
ERROR 1072 (42000): Key column 'class_id' doesn't exist in table
//要搞清楚顺序,得先创建主键

mysql> create table class(id int primary key, name varchar(20));
Query OK, 0 rows affected (0.57 sec) 
mysql> create table student(class_id int,id int ,foreign key(class_id)references class(id));
Query OK, 0 rows affected (0.77 sec)
//这样两个表就关联了起来

student不能增加class没有的班级号,而class也不能删除有学生的班级号
这是一个双向制约的关系

本来想跟范式一起写了,但是有点长,可能会影响日后的阅读体验,所以范式在下篇写入。

发布了12 篇原创文章 · 获赞 9 · 访问量 546

猜你喜欢

转载自blog.csdn.net/weixin_45219512/article/details/105128167