当前位置:网站首页>12. Constraints
12. Constraints
2022-04-23 07:40:00 【Ah Dai cloth clothes cool】
12. constraint
One : Adding constraints
1. Add constraints when creating tables
create table student
(
id int primary key,
name varchar(10) not null,
age int check(age between 1 and 120),
sex varchar(8) not null check(sex in ('male','female')),
IDCard varchar(18) unique,
class_id int,
foreign key (class_id) references class (c_id)
)charset=utf8;
create table class
(
c_id int primary key,
c_name varchar(20) not null,
c_info varchar(200)
)charset=utf8;
2. View all information in the table, including constraints
show create table t_student;
3. Specify a name for the constraint
create table student
(
id int,
name varchar(10) not null,
age int,
sex varchar(8),
IDCard varchar(18),
class_id int,
constraint pk_id primary key (id),
constraint ck_age check(age between 1 and 120),
constraint ck_sex check(sex in ('male','female')),
constraint uq_IDCard unique (IDCard),
constraint fk_class_id foreighn key (class_id) references class(c_id)
)charset=utf8;
4. Add constraints after creating the table
create table student
(
id int,
name varchar() not null,
age int,
sex varchar(8),
IDCard varchar(18),
class_id int
);
alter table student add constraint pk_id primary key (id);
alter table student add constraint ck_age check(age between 1 and 120);
alter table student add constraint ck_sex check(sex in ('male','female'));
alter table student add constraint uq_IDCard unique(IDCard);
alter table student add constraint fk_class_id foreign key (class_id)
references class (c_id);
Two : Delete constraints
1. Delete primary key constraint
alter table Table name drop primary key
2. Delete foreign key constraint
alter table Table name drop foreign key Constraint name
3. Delete unique constraint
alter table Table name drop index Constraint name
4. Delete non empty constraints
alter table Table name modify Name data type null
3、 ... and : Five common constraints : Primary key , Non empty , Check , only , Foreign keys
create table student
(
id int primary key,
name varchar(10) not null,
age int check(age between 1 and 120),
sex varchar(8) not null check(sex in ('male','female')),
IDCard varchar(18) unique,
class_id int,
foreign key (class_id) references class (c_id) on delete cascade
)charset=utf8;
版权声明
本文为[Ah Dai cloth clothes cool]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230622239142.html
边栏推荐
猜你喜欢

数据分析入门 | kaggle泰坦尼克任务(三)—>探索数据分析

Solutions to common problems in visualization (VII) solutions to drawing scale setting

如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列
![[COCI]Lampice (二分+树分治+字符串哈希)](/img/7b/fe2a45d960a6d3eb7dc25200304adc.png)
[COCI]Lampice (二分+树分治+字符串哈希)

反思 | Android 音视频缓存机制的系统性设计

Discussion on arrow function of ES6

Visualization Road (IX) detailed explanation of arrow class

Javscript gets the real suffix of the file

MySQL index

安装配置淘宝镜像npm(cnpm)
随机推荐
ogldev-读书笔记
npm 安装踩坑
ESP32学习-GPIO的使用与配置
超级宝典&编程指南(红蓝宝书)-读书笔记
What is a closure?
9. Common functions
【TED系列】一个习惯是如何改变我的一生
MySQL isolation level
h5本地存储数据sessionStorage、localStorage
[2020WC Day2]F.采蘑菇的克拉莉丝(子树和查询、轻重儿子思想)
7. sub query
Transformer的pytorch实现
配置npm
技能点挖坑
如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)
redis连接出错 ERR AUTH <password> called without any password configured for the default user.
学习笔记5-梯度爆炸和梯度消失(K折交叉验证)
快速下载vscode的方法
[Educational Codeforces Round 80] 解题报告
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)