分享

MySQL表的约束

 流楚丶格念 2022-01-14

表的约束

*定义主键约束 primary key:不允许为空,不允许重复
*定义主键自动增长 auto_increment
*定义唯一约束 unique
*定义非空约束 not null
*定义外键约束 constraint ordersid_FK foreign key(ordersid) references orders(id)
*删除主键:alter table tablename drop primary key ;

MySQL中约束举例:

create table myclass(
id INT(11) primary key auto_increment,
name varchar(20) unique
);
create table student (
id INT(11) primary key auto_increment, 
name varchar(20) unique,
passwd varchar(15) not null,
classid INT(11),  
constraint stu_classid_FK foreign key(classid) references myclass(id)
);

check约束在MySQL中语法保留,但没有效果。

可以通过

SELECT * FROM information_schema.`TABLE_CONSTRAINTS`;

查看表的约束。
在这里插入图片描述

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多