mysql学习笔记03

本文详细介绍了MySQL中的各种约束,包括主键约束(确保唯一性和非空)、自增长约束(自动递增)、唯一约束(防止重复)、非空约束(字段不允许为空)、默认约束(提供默认值)以及零补充约束(填充不足的数值)。并提供了创建、修改和删除这些约束的SQL语句示例。
摘要由CSDN通过智能技术生成

mysql的约束

约束:constrain,实际上就是对表中数据的限制条件

目的:为了保证数据的完整性和有效性,比如有的唯一不能重复,不能为空

分类:
主键约束(primary key)

主键是唯一标识表中每一行的列或一组列 ,可以是一个列可以是好几个列组合在一起,方便确定某一行数据。

主键约束不允许空值也不允许重复,要有代表性比如身份证号唯一表示一个人

一个表只能有一个主键

主键约束相当于与 唯一约束+非空约束

创建时添加单列主键
-- 两种方式1:在创建表时,在定义数据结构后面写入 primary key
create table test01(
id int primary key, -- 此时将id设置为主键
name varchar(20),
age int 
);
​
--    方式2:在字段定义完成之后 写入constraint 主键名(自定义) primary key(字段) 
-- --  constraint 主键名(自定义)可以省略
create table test01(
id int,
name varchar(20),
age int,
primary key(id)  -- 此时将id设置为主键
);
​
创建时添加多列主键
-- 与上方法2相同,括号内写入多个字段即可
-- constraint 主键名(自定义) primary key(字段1,字段2,.....) 
-- constraint 主键名(自定义)可以省略
use test;
create table test_table02(
id int,
name varchar(20),
age int,
primary key (id,age) -- 此时将id和age组合成一个主键
    -- id和age可以各一相同,不能整体相同,都不能为空
)

修改表结构时添加主键
-- alter table 表名 add primary key(字段1,字段2....)
create table test_table03(
id int,
name varchar(20),
age int
);
alter table test_table03 add primary key(id,age);

删除主键
-- alter table 表名 drop primary key;
-- 删除主键,并没有删除它不能为空
create table test_table03(
id int,
name varchar(20),
age int
);
alter table test_table03 drop primary key(id,age);
drop table test_table03;

自增长约束(auto_increment)

当某字段定义为自增长之后,就不需要用户输入了,默认为1开始,每增加一条记录回增加1,通常与primary key 连用。auto_increment也是not null类型,增加范围是该定义的数据类型范围

定义及设置默认增长值方法
-- 语法:定义表时,字段名 数据类型 [primary key] auto_increment
create table test_table03(
id int primary key auto_increment,
name varchar(20),
age int
);
drop table test_table03;
-- 设置初始默认值
-- 方法1,创建表时:create table(   )auto_increment = 值;
create table test_table03(
id int primary key auto_increment,
name varchar(20),
age int
)auto_increment = 100 ;
drop table test_table03;
-- 方法2,修改表结构时:alter table 表名 auto_increment = 值;
create table test_table03(
id int primary key auto_increment,
name varchar(20),
age int
);
alter table test_table03 auto_increement = 100;
删除时delete 与truncate在自增长上的差异

​ delete会在断点位置继续自加,truncate完全从开始

非空约束(not null)

非空约束:指定字段不能为空,一张表可以有多个非空约束

插入数据primary key可以填null,not null 不能添加null

use test;
create table test_04(
id int primary key,
    name varchar(20) not null,-- 
    age int not null 
)
​

创建表时添加非空约束
use test;
create table test_04(
id int primary key,
    name varchar(20) not null,-- 
    age int not null 
)
insert into test_table04 VALUES(null,'女',22);-- 可以
insert into test_table04 VALUES(null,'',22); -- 可以,此时null作为空串添加的
insert into test_table04 VALUES(null,null,22);-- 不可以
insert into test_table04 VALUES(null,'null',22); -- 可以,此时null作为字符串添加的
修改表结构添加及删除非空约束
-- alter table 表名  modify 字段 数据类型 not null;修改表结构添加
--  alter table 表名  modify 字段 数据类型;删除非空约束
​
use test;
create table test_table04(
id int primary key auto_increment,
name varchar(20),
age int 
)auto_increment = 100 ;
alter table test_table04 modify age int not null;  -- 修改表结构添加
insert into test_table04 VALUES(null,'女',22);
TRUNCATE table test_table04;
alter table test_table04 modify age int; -- 删除非空约束
唯一约束(unique)

唯一约束:该字段下值不能相同,null和任何值都不相同,和自己也不相同

创建表时添加唯一约束
-- 格式:字段名 数据类型 unique;
use test;
create table test_table05(
    id int unique;
    name varchar(10);
    age int unique
);
drop table test_table05;
修改表时添加约束
-- alter table 表名 add constraint 约束名 unique(); 
use test;
create table test_table05(
    id int,
    name varchar(10),
    age int
);
alter table test_table05  add  constraint unique_01 unique(id,age); 
​
删除唯一约束
-- alter table 表名  drop index 约束名; 
alter table test_table05  drop index unique_01; -- 这是才用修改表结构时添加后的删除方式,有unique_01约束名
-- 如果是创建时添加的,字段名就是约束名,如果有两个唯一约束,一个一个删除就行
alter table test_table05  drop index id;
alter table test_table05  drop index age;

默认约束(default)

指定某列不输入时的默认值

创建表时添加
-- 格式:字段名 数据类型 default 默认值;
create table test_table06(
    id int ,
    name varchar(10) default '张三',
    age int
);
修改表结构时添加
-- 格式:alter table 表名 modify 字段名 数据类型 default 默认值
create table test_table05(
    id int,
    name varchar(10),
    age int
);
alter table test_table06 modify name varchar(10) dafault '张三';
删除默认约束
-- 格式:alter table 表名 modify 字段名 数据类型 default NULL
alter table test_table06 modify name varchar(10) dafault NULL;

零补充约束(zerofill)

插入数据时,值的长度小于定义的长度时,在前面补0

zerofill默认为int(10),补足10位,假如插入数为23,它会在表格上显示为0000000023

默认为无符号型

创建表时添加
-- 格式:字段名 数据类型 zerofill;
create table test_table05(
    id int zerofill,
    name varchar(10),
    age int
);
​

修改表结构时添加
-- 格式:alter table 表名 modify 字段名 数据类型 zerofill
create table test_table05(
    id int,
    name varchar(10),
    age int
);
alter table test_table06 modify name varchar(10) zerofill;

删除默认约束
-- 格式:alter table 表名 modify 字段名 数据类型 ;
create table test_table05(
    id int,
    name varchar(10),
    age int
);
alter table test_table06 modify name varchar(10);

约束的总结
  • 主键约束

    关键字:primary key

    添加格式:-- 字段名 数据类型 primary key

    ​ -- alter table 表名 add primary key(字段名)

    ​ -- alter table 表名 add primary key(字段名)

    删除格式:-- alter table 表名 drop primary key(字段名)

  • 自增长约束

    关键字:auto_increment

    添加格式:-- 字段名 数据类型 primary key auto_increment

    修改自增长初始值格式:-- create table ....(

    ​ ) auto_increment = 100;

    -- alter table 表名 auto_increment = 100

    删除格式:-- alter table 表名 modify 字段名 数据类型;

  • 唯一约束

    关键字:unique

    添加格式:-- 字段名 数据类型 unique

    ​ -- alter table 表名 modify add constraint 约束名 unique(字段名);

    删除格式:-- alter table 表名 drop index 约束名 ;)

  • 默认约束

    关键字:default

    添加格式:-- 字段名 数据类型 default 默认值

    ​ -- alter table 表名 modify 字段名 数据类型 default 默认值;

    删除格式:-- alter table 表名 modify 字段名 数据类型 ;

  • 零补充约束1

    关键字:zerofill

    添加格式:-- 字段名 数据类型 zerofill

    ​ -- alter table 表名 modify 字段名 数据类型 zerofill;

    删除格式:-- alter table 表名 modify 字段名 数据类型 ;

  • 25
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值