130 MySQL field integrity constraints (important)

Integrity constraints (important)

  • primary key: Primary key, a unique identifier, the table will have, not set as the default look for first is not empty, the only field that does not identify you create a hidden field
  • foreign key: Foreign keys, foreign keys to establish a link between the table and the table through a foreign key syntax
  • unique key: The only data, the value of the field need to ensure a unique piece, can not be repeated
  • auto_increment: Increment, int type field can only be applied to the key, as a secondary modification, only one table is provided a self-energizing field
  • not null: Is not empty - for a number of fields, such as user name registration, birth sex of the person and other fields under these needs, not only set to Null, it must be assigned
  • default: Default - when the field has a default value unexpected assignment, there is a default field values ​​are assigned default values
  • unsigned: Unsigned - stored digital starting from 0
  • zerofill: Zero padding - integer stored data length is less than the length of the range will be left filled with zeros in a digital
# not null 与 default 限制
# 不能为空,没有默认值的x,必须赋值
# y、z在没有赋值情况下,才有默认值,设置值后,采用默认值
mysql>: create table td1 (x int not null, y int default 0, z int default 100);



# 报错,auto_increment必须设置给 键字段
mysql>: create table td2 (x int auto_increment);
# 报错,auto_increment必须设置给 int字段
mysql>: create table td2 (x char(4) auto_increment);
# 报错,auto_increment字段最多出现 1次
mysql>: create table td2 (x int unique auto_increment, y int unique auto_increment);

# 正确,主键和唯一键分析
# x为主键:没有设置primary key时,第一个 唯一自增键,会自动提升为主键
mysql>: create table td21 (x int unique auto_increment, y int unique);
# y为主键:没有设置primary key时,第一个 唯一自增键,会自动提升为主键
mysql>: create table td22 (x int unique, y int unique auto_increment);
# x为主键:设置了主键就是设置的,主键没设置自增,那自增是可以设置在唯一键上的
mysql>: create table td23 (x int primary key, y int unique auto_increment);
# x为主键:设置了主键就是设置的,主键设置了自增,自增字段只能有一个,所以唯一键不能再设置自增了
mysql>: create table td24 (x int primary key auto_increment, y int unique);
# 默认主键:没有设置主键,也没有 唯一自增键,那系统会默认添加一个 隐式主键(不可见)
mysql>: create table td25 (x int unique, y int unique);

# 唯一键:确保一个字段,数据不能重复
# 主键:是一条记录的唯一标识(可以理解为数据的编号)



# 联合唯一
# ip在port不同时,可以相同,ip不同时port也可以相同,均合法
# ip和port都相同时,就是重复数据,不合法
mysql>: create table tu1 (ip char(16), port int, unique(ip, port));

# 也可以设置成 联合主键,道理同 联合唯一
mysql>: create table tu2 (ip char(16), port int, primary key(ip, port));
# sql可以多行书写
mysql>: 
create table t22(
    ip char(16),
    port int,
    primary key(ip,port)
);


# 通常自增字段的 自增索引 会被永久记录,想清空表并清空自增索引:
mysql>: truncate 数据库名.表名

Foreign key

A foreign key is a field associated with the table to establish the table, typically a foreign key to another table is the primary key table (key may be unique)

1、外键的 字段名 可以自定义(名字随意),通常命名规范(关联表_关联字段)

2、外键要通过 foreign key 语法建立表与表之间的关联

mysql>: foreign key(所在表的外键字段) references 关联表(关联表已有字段)
eg:foreign key(detail_id) references author_detail(id)

3、级联关系(更新一起更新,删一起删)
    级联更新 on update cascade
    级联删除 on delete cascade
    
# 重点:外键字段本身可以唯一或不唯一,但是外键字段必须唯一

Multi-table relations

One to One

For example: the husband - wife, user - id, author - Author Details

Foreign keys on either side are, at this time the foreign key to set the unique

No cascading relationship:

  • Increase: first to increase the association table records, and then add the associated table records
  • Delete: delete the associated table records, and then delete the table records are linked
  • Update: association table associated with the correlation can not be completed and a foreign key to update the primary key - (if the association table records is not bound to be modified)
"""
作者(author):id,name,sex,age,mobile
作者详情(author_detail): id,info,address,author_id
"""
## 建表
# 作者详情(author_detail): id,info,address
create table author_detail(
    id int primary key auto_increment,
    info char(50),
    address char(50)
);
# 作者表(author): id,name,sex,age,mobile, detail_id
create table author(
    id int primary key auto_increment,
    name char(10) not null,
    sex enum("男","女") default "男",
    age int not null,
    mobile char(13) unique key not null,
    detail_id int unique not null,  # 一对一的表必须添加唯一键,并且类型和被关联的字段类型一致
    foreign key(detail_id) references author_detail(id) # 设置外键detail_id,是author_detail表中的字段id
);
## 插入
# 必须先创建被关联表数据,有关联表外键关联的记录后,关联表才可以创建数据
mysql>: insert into author_detail(info,address) values('Tom_info','Tom_address');
mysql>: insert into author(name,age,mobile,detail_id) values('Tom',18,'13344556677', 1);


## 修改(在被关联表中已被关联的记录不可以直接修改,关联表修改的前提是被关联表必须有至少一条记录没有被其他表关联)先修改关联表重新指向被关联表中新的记录。在修改被关联表的原记录
# 修改关联表
mysql>: update author set detail_id=2 where detail_id=1;    # 当被关联表有id为2的既可以
# 修改被关联表
mysql>: update author_detail set id=10 where id=1;


## 删除(先删除关联表记录,再删除被关联表记录)
# 删除关联表记录
mysql>: delete from author where detail_id=1;  # 直接删除
# 删除被关联表记录
mysql>: delete from author_detail where id=2;  # 当被关联表中数据没有被关联是可以修改

A cascade relationship:

  • Update cascade relationship: on update cascade, to make a change together
  • Delete cascade relationship: on delete cascade, delete to delete together
"""
作者(author):id,name,sex,age,mobile
作者详情(author_detail): id,info,address,author_id
"""
## 建表
# 作者详情(author_detail): id,info,address
create table author_detail(
    id int primary key auto_increment,
    info char(50),
    address char(50)
);
# 作者表(author): id,name,sex,age,mobile, detail_id
create table author(
    id int primary key auto_increment,
    name char(10) not null,
    sex enum("男","女") default "男",
    age int not null,
    mobile char(13) unique key not null,
    detail_id int unique not null,  # 一对一的表必须添加唯一键,并且类型和被关联的字段类型一致
    foreign key(detail_id) references author_detail(id) # 设置外键detail_id,是author_detail表中的字段id
    on update cascade   # 创建更新级联关系
    on delete cascade   # 创建删除级联关系
);
## 插入
# 必须先创建被关联表数据,有关联表外键关联的记录后,关联表才可以创建数据
mysql>: insert into author_detail(info,address) values('Tom_info','Tom_address');
mysql>: insert into author(name,age,mobile,detail_id) values('Tom',18,'13344556677', 1);


## 修改(一起改)
# 修改关联表
mysql>: update author set detail_id=2 where detail_id=1;    # 当被关联表有id为2的既可以
# 修改被关联表
mysql>: update author_detail set id=10 where id=1;


## 删除(一起删)
# 删除关联表记录
mysql>: delete from author where detail_id=1;  # 直接删除
# 删除被关联表记录
mysql>: delete from author_detail where id=2;  # 当被关联表中数据没有被关联是可以修改

Many

For example: department - staff, classes - the students, the book - Press

Foreign key must be placed in multi-party, then the foreign key is not unique

## 建表
# 出版社(publish): id,name,address,phone
create table publish(
    id int primary key auto_increment,
    name varchar(64),
    address varchar(256),
    phone char(20)
);

# 书(book):id,name,price,publish_id, author_id
create table book(
    id int primary key auto_increment,
    name varchar(64) not null,
    price decimal(5, 2) default 0,
    publish_id int,  # 一对多的外键不能设置唯一
    foreign key(publish_id) references publish(id)
    on update cascade
    on delete cascade
);

# 增:先增加被关联表(publish)的数据,再增加关联表(book)的数据
mysql>: insert into publish(name, address, phone) values
('人民出版社', '北京', '010-110'),
('西交大出版社', '西安', '010-119'),
('淮南师范出版社', '上海', '010-120');

mysql>: insert into book(name, price, publish_id) values
('西游记', 6.66, 1),
('东游记', 8.66, 1),
('python从入门到入土', 2.66, 2),
('轮程序员修养之道', 3.66, 3),
('好好活着', 88.88, 3);
# 没有被关联的字段,插入依旧错误
mysql>: insert into book(name, price, publish_id) values ('打脸之道', 0.3, 4);  # 失败


# 更新:直接更新被关联表的(publish) 主键,关联表(book) 外键 会级联更新
mysql>: update publish set id=10 where id=1;
# 更新:直接更新关联表的(book) 外键,修改的值对应被关联表(publish) 主键 如果存在,可以更新成功,反之失败
mysql>: update book set publish_id=2 where id=4;  # 成功
mysql>: update book set publish_id=1 where id=4;  # 失败


# 删:
#   删被关联表,关联表会被级联删除
mysql>: delete from publish where id = 2;

#   删关联表,被关联表不会发生变化
mysql>: delete from book where publish_id = 3;

Many to many

For example: teachers - classes, courses - students, publishers - author

Be sure to create a third table (table), each foreign key value is not unique, you can see the establishment of a joint unique multiple foreign keys

# 多对多:一定要创建第三张表(关系表),每一个外键值不唯一,看可以多个外键建立联合唯一

# 作者(author):id, name, age
create table author(
    id int primary key auto_increment,
    name varchar(64),
    age int unsigned default 0
);

# 出版社(publish):id, name, address
create table publish(
    id int primary key auto_increment,
    name varchar(64),
    address varchar(256)
);

# 作者与出版社关系表:id, author_id, publish_id
create table author_publish(
    id int primary key auto_increment,
    # 关系表一定有多个外键,关联着多张表
    # 关联作者表
    author_id int,
    foreign key(author_id) references author(id)
    on update cascade
    on delete cascade,
    # 关联出版社表
    publish_id int,
    foreign key(publish_id) references publish(id)
    on update cascade
    on delete cascade,
    # 建立两个字段的联合唯一
    unique(author_id, publish_id)
);

# 注:关系表 关联着 作者 和 出版社 两张表,在表结构上 作者 与 出版社 两表键没有任何关系


# 增:两张被关联表,没有前后关系,但关系表必须在两个表都提供数据后才能进行 关系匹配
mysql>: insert into author(name, age) values('ruakei', 67),('engo', 76),('Lxx', 3);
mysql>: insert into publish(name, address) values('老男孩出版社', '上海'),('小女孩出版社', '北京');

# 操作关系表:
mysql>: insert into author_publish(author_id, publish_id) values(1,1),(1,2),(2,1),(2,2),(3,1);

# 关系表操作:增、删、改,只要两张被关系表有提供对应的操作数据,都可以操作成功,且对两张被关系表没有影响


# 操作两张被关系表:
#       增:不会影响关系表
mysql>: insert into publish(name, address) values('西交大出版社', '西安');
#       改:关系表都会级联更新
mysql>: update publish set id=10 where id=1;
#       删:关系表都会级联删除
mysql>: delete from author where name='ruakei';

Guess you like

Origin www.cnblogs.com/XuChengNotes/p/11588526.html