关键字:constraint
约束是表级限制,它通过限制表的数据来确保数据的完整性和一致性。
[En]
A constraint is a table-level restriction that ensures the integrity and consistency of the data by restricting the data of the table.
常见约束:
用途:就是用来约束其中的一列,作为所有列中的标识符(这一列的唯一代表),
您可以通过主键准确定位表中的列。可以避免列中的数据重复。
[En]
You can accurately locate a column in a table through the primary key. Duplication of data in the column can be avoided.
主键的特性:
1.唯一约束
2.非空约束
语法:
1.create table [库名].表名 (列名1 数据类型1(长度)
primary key,列名2 数据类型2(长度));
2.create table [库名].表名 (列名1 数据类型1(长度),
列名2 数据类型2(长度),primary key(列名1));
第一种创建主键方式
create table school.bbq(
id int(2) primary key,
name varchar(3),
tall int(4),
age int(3)
);
insert into school.bbq values
(1001,’小贺’,170,20),
(1002,’小窦’,184,20),
(1003,’小张’,175,20),
(1004,’小王’,170,20);
— 验证主键的唯一性约束
insert into school.bbq(id, name
) values (1002,’小周’);
— 验证主键的非空约束
insert into school.bbq name
) values (‘小杨’);
— 第二种主键创建方式
create table school.qqq(
id int(2),
name
varchar(3),
age int(2),
sex varchar(2),
primary key(id)
);
2.唯一约束(unique)
用途:用来约束一列中的所有数据,不能重复。
create table school.aaa(
id int(3) unique,
name
varchar(3),
age int(4)
);
3.非空约束(not null)
用途:用来约束一列中的所有数据,不能为null。
注意:所有数据类型都可以为空。
[En]
Note: all data types can be empty.
create table school.bbb(
id int(3) not null,
name
varchar(3),
age int(4)
);
用途:在规定了的默认值约束的列时,不向该列插入其他数据,则该数据为默认数据
语法:create table 表名(列名1 数据类型1(长度)default 默认值,列名2 数据类型2(长度));
用途:也能确保数据的完整性也能展现和其他表的关系,
一个表可以一个或多个外键,每个外键必须(references)另一个表的主键或唯一键。
语法:
create table 表名(列名1 数据类型1(长度),列名2 数据类型2(长度),
forrign key(本表外键列名) references 被引用的表(被引用的列));
create table school.bbp(
id int(3),
tall int(3),
brithday date,
foreign key(id) references bbq(id)
);
该约束在mysql上停用了,语句不会报错,但没有实际用处
作用:用于限制列中的值的范围,比如 check了一列,那么该列只允许特定的值。
语法:
create table 表名 (列名1 数据类型1(长度),列名2,
数据类型2(长度),check(表达式))
例如:check(id>0)
create table school.qqq(
id int(2),
name
varchar(3),
check(id>0)
);
语法:show keys from 表名;
表的修改
语法:alter table 表名 add primary key(列名);
create table school.bbq(
id int(2),
name
varchar(3),
age int(2)
);
desc school.bbq;
— 查找展示主键数量
show keys from school.bbq;
— 为表添加主键
alter table school.bbq add primary key(id);
show keys from school.bbq;
desc school.bbq;
语法:alter table 旧表名 rename to 新表名;
alter table school.bbq rename to school.bbb;
语法:alter table 表名 drop primary key;
alter table school.bbq drop primary key;
语法:alter table 表名 modify 列名 数据类型(长度) not null;
语法: alter table 表名 modify 列名 数据类型(长度) 列约束;
alter table school.bbq — 修改的表
modify name
varchar(4) — 修改表中的某个对象列
not null; — 不为空
alter table school.bbq
modify name
varchar(3);
语法:alter table 表名 change 旧列名 新列名 数据类型(长度);
注:空字符串不等于空字符串。空字符串是字符串类型。
[En]
Note: empty string is not equal to empty string. Empty string is a string type.
alter table school.student change id Sid int(3);
alter table school.student change Sid sid int(3);
alter table school.student change sid id int(2);
alter table school.student change id ID int(5);
desc school.student;
alter table school.student change ID Id int(5);
语法:alter table 表名 add column 列名 数据类型(长度);
alter table school.student add column hahaha int(2);
— 改变表中某列的列名
alter table school.student change hahaha card int(2);
语法:alter table 表名 add column
(列名1 数据类型1(长度),列名2 数据类型2(长度));
alter table school.student add column (idcard1 int(2),idcard2 int(3));
语法1:alter table 表名 modify [column]列名 新数据类型(长度);
alter table school.student modify column card varchar(3);
语法:alter table 表名 drop column 列名;
语法;alter table 表名 drop column 列名1,
drop column 列名2,drop column 列名3;
alter table school.student drop column idcard1;
alter table school.student drop column id1;
alter table school.student drop column id2;
alter table school.student drop column idcard;
alter table school.student drop column id1,drop id2,drop id3;
alter table school.student add column(id1 int(2),id2 int(2),id3 int(2));
语法:create table 表名 (列名1 数据类型1(长度) primary key,
列名2 数据类型2(长度) primary key)
语法:create table 表名 (列名1 数据类型1(长度),
列名2 数据类型2(长度) primary key(列名1,列名2));
语法: create table 表名(列名1 数据类型1(长度),列名2 数据类型2(长度),
foreign key(本表列) references 被引用的表(被引用的列);
在添加外键时指定约束的名称
[En]
Specify the name of the constraint when adding a foreign key
语法:alter table 表名 表名(列名1 数据类型1(长度),列名2 数据类型2(长度),
constraint 约束名 foreign key(本表列) references 被引用的表(被引用的列);
注意:引用被引用列的数据类型(包括约束)需要一致
[En]
Note: data types (including constraints) that reference referenced columns need to be consistent
create table school.student1 (
id int(2),
name
varchar(2),
age int(2),
constraint id foreign key (id) references student(id)
);
1.删除外键
语法:alter table 表名 drop foreign key 外键名;
2.删除索引
语法:drop index 索引名 on 表名;
alter table school.student1 drop foreign key student1_ibfk_1;
drop index id on school.student1;
show keys from school.student1;
Original: https://www.cnblogs.com/cn-zhouchao/p/16455523.html
Author: 小胖子学编程
Title: 主键约束,唯一约束,默认约束,检查约束,删除主键,删除外键按,删除列,添加列
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/505252/
转载文章受原作者版权保护。转载请注明原作者出处!