当前位置:网站首页>MySQL约束
MySQL约束
2022-06-22 18:24:00 【火眼猊】
约束
作用:在表设计的时候加入约束的目的就是为了保证表中的记录完整性和有效性,比如有些用户表有些列的额值不能为空,有些列的值不能重复。
分类:
1.主键约束(primary key)
2.自增长约束(auto_increment)
3.非空约束(not null)
4.唯一性约束(unique)
5.默认约束(default)
6.零填充约束(zerofill)
7.外键约束(foreign key)
1. 主键约束(primary key)
概念:MySQL主键约束是一个列或者多个列的组合,其值能唯一地表示表中的每一个,方便在RDBMS中尽快找到某一行;
作用:
1.主键约束相当于唯一约束+非空约束的组合,不允许重复也不允许出现空值;
2.每个表最多只允许有一个主键;
3.当创建主键的约束时,系统默认会在所在的列和列组合上建立对应的唯一索引。
添加单列主键
方法一:
create table 表名(
……
<字段名><数据类型>primary key
……
)
例如:
create table emp1(
eid int primary key,
name varchar(20),
deptId int,
salary double
);
方法二:
create table 表名(
……
[constraint<约束名>] primary key[ 字段名]
);
例如:
create table emp2(
eid int ,
name varchar(20),
deptId int,
salary double,
constraint pk1 primary key(id)
);
注:constraint pk1可以不写
添加联合主键
概念:所谓的联合主键,就是这个主键由一张表中多个字段组成。
注:1.当主键是由多个字段组成是,不能直接在字段名后面声明主键约束
2.一张表只能有一个主键,联合主键也是一个主键
3.联合主键中的主键不完全一样即可
4.联合主键的割裂,每一列都不能为空
方法:
creat table 表名(
……
primary key(字段11,字段2,……,字段n)
);
例如:
create table emp3(
eid int,
name varchar(20),
deptId int,
salary double,
constraint pk2 primary key(name,deptId)
);
修改表结构添加主键
方法:
create table 表名(
……
);
alter table <表名>add primary key(字段列表);
例如:
添加单列主键
create table emp4(
eid int,
name varchar(20),
deptId int,
salary double
);
alter table emp4 add primary key(eid);
添加多列主键
create table emp5(
eid int,
name varchar(20),
deptId int,
salary double
);
alter table emp5 add primary key(name,deptId);
删除主键约束
方法:
alter table <数据表名> drop primary key;
方法:
删除单列主键
alter table emp1 drop primary key;
删除联合主键
alter table emp5 drop primary key;
2. 自增长约束(auto_increment)
概念:在MySQL中,当主键定义为自增长后,这个主键的值就不再需要用户输入数据了,而由数据库系统根据定义自动赋值。每增加一条记录,主键就会自动以相同的步长进行增长。
特点:1.默认情况下,auto_increment的初始值为1,每新增一条记录,字段值自动加1
2.一个表中只能有一个字段使用auto_increment约束,且该字段必须有唯一索引,以避免序号重复
3.auto_increment月数字段必须具备NOT NULL属性
4.auto_increment约束的字段只能是整数类型(TINYINT、SMALLINT、INT、BIGINT等)
5.auto_increment约束字段的最大值受该字段的数据类型约束,如果达到上限,auto_increment会失效
添加自增长约束
方法:
字段名 数据类型 auto_increment
例如:
CREATE TABLE t_user1(
id INT PRIMARY KEY auto_increment,
name VARCHAR(20)
);
从指定位置开始依次向上增加
方法一:创建表时指定
例如:
CREATE TABLE t_user2 (
id int PRIMARY KEY auto_increment,
name VARCHAR(20)
)auto_increment = 100;
方法二:创建表之后指定
例如:
CREATE TABLE t_user3 (
id int PRIMARY KEY auto_increment,
name VARCHAR(20)
);
alter TABLE t_user3 auto_increment = 200;
删除自增长约束
delete数据之后自动增长从断点开始
truncate数据之后自动增长从默认起始值开始
注:delete删除之后,比方说删除前五条数据(即自增长的数为5),删除后在新增数据时数据从6开始。
3.非空约束(not null)
概念:MySQL非空约束(not null)指字段的值不能为空。对于使用了非空约束的字段,如果用户在添加数据时没有指定值,数据库系统就会报错。
特点:
1.添加非空约束的列不能为空
2.真正的空是NULL,不是’NULL’,也不是’’
方法一:
<字段名><数据类型>not null
例如:
CREATE TABLE t_user6(
id INT,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL
);
方法二:
alter table 表名 modify 字段 类型 not null
例如:
CREATE TABLE t_user7(
id INT,
name VARCHAR(20) ,
address VARCHAR(20)
);
ALTER TABLE t_user7 MODIFY name VARCHAR(20) NOT NULL;
ALTER TABLE t_user7 MODIFY address VARCHAR(20) NOT NULL;
4.唯一约束(unique)
概念:唯一约束(Unique Key)是指所有记录中字段的值不能重复出现。
特点:
1.添加唯一约束之后,该列的值不能重复
2.虽然不能重复,但是添加唯一约束的列可以为NULL,因为NULL不等于NULL
方法一:<字段名><数据类型>unqiue
例如:
CREATE TABLE t_user8(
id INT,
name VARCHAR(20) ,
phone_number VARCHAR(20) UNIQUE
);
方法二:alter table 表名 add constraint 约束名 unique(列);
例如:
CREATE TABLE t_user9(
id INT,
name VARCHAR(20) ,
phone_number VARCHAR(20)
);
ALTER TABLE t_user9 add CONSTRAINT unique_pn unique(phone_number);
注:在MySQL中NULL和任何值都不通,甚至和自己都不相同(即如果在唯一约束中出现NULL则可以有多可NULL不会报错)
删除唯一约束
方法:
alter table <表名> drop index <唯一约束名>
例如:
ALTER TABLE t_user9 DROP INDEX unique_pn;
注:如果没有写约束名则列名即为约束名。
5.默认约束(default)
概念:MySQL默认值约束用来指定某列的默认值
特点:
添加约束的列如果没有指定值,则为默认值
方法一:
<字段名><数据类型>default<默认值>;
例如:
CREATE TABLE t_user10(
id INT,
name VARCHAR(20) ,
address VARCHAR(20) DEFAULT '北京'
);
方法二:
alter table 表名 modify 列名 类型 default 默认值;
例如:
CREATE TABLE t_user11(
id INT,
name VARCHAR(20) ,
address VARCHAR(20)
);
ALTER TABLE t_user11 MODIFY address VARCHAR(20) DEFAULT '深圳';
删除默认约束
方法:
alter table<表名> change column <字段名><类型>default null;
例如:
ALTER TABLE t_user11 MODIFY address VARCHAR(20) DEFAULT NULL;
6.零填充约束(zerofill)
概念:1.插入数据时,当该字段的值长度小于定义的长度时,会在该值的前面补上相应的0
2.zerofill默认为int(10)
3.当使用zerofill时,默认会自动加unsigned(无符号)属性,使用unsigned属性后,数值范围是原值的两倍
特点:
1.当给数值类型设置该约束,左边不够指定的整形长度时,则自动补充0
2.默认零填充约束的数字int(10)
3.指定零填充约束的列为unsigned类型,即无符号类型
方法:
<字段名><数据类型>zerofill
例如:
CREATE TABLE t_user12(
id INT ZEROFILL,
name VARCHAR(20)
);
删除约束
ALTER TABLE t_user12 MODIFY id int;
边栏推荐
猜你喜欢

Detailed explanation of shell script (x) -- how to use sed editor
![K个一组翻转链表[链表拆解/翻转/拼装]](/img/70/fb783172fa65763f031e6bd945cbd9.png)
K个一组翻转链表[链表拆解/翻转/拼装]

Follow up course supplement of little turtle teacher "take you to learn C and take you to fly"

K8s deploy MySQL

1.4-----PCB设计?(电路设计)确定方案

Adapter mode of structural mode

深入浅出聊布隆过滤器(Bloom Filter)

Implementing Domain Driven Design - using ABP framework - solution overview

Digital enabling machinery manufacturing industry, supply chain collaborative management system solution helps enterprises upgrade their supply chain
![[nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql](/img/15/cbb95ec823cdde5fb8f032dc45cfc7.png)
[nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql
随机推荐
2.什么是机械设计?
NLP-D57-nlp比赛D26&刷题D13&读论文&&找了一个多小时bug
3D打印机耗材受潮
Modify the antd tree component so that its subclasses are arranged horizontally.
结构型模式之代理模式
使用 Order by 与 rownum SQL 优化案例一则
Flutter series - build a flutter development environment
Decorator mode of structural mode
Xintang nuc980 usage record: basic description of development environment preparation and compilation configuration
知识蒸馏之Focal and Global Knowledge Distillation for Detectors
Do you use thread or service?
510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming
数组对象实现一 一对比(索引和id相同的保留原数据,原数组没有的数据从默认列表加进去)
Fault analysis | from data_ Free exception
故障分析 | 从 data_free 异常说起
Digital supply chain centralized purchase platform solution for mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
数组对象根据id一 一对应填入(将arr1填入arr2中)
C #, introductory tutorial -- a little knowledge about function parameter ref and source program
Pat a 1093 count Pat's (25 points)
Human Pose Estimation浅述