当前位置:网站首页>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
边栏推荐
猜你喜欢

SAP RFC_CVI_EI_INBOUND_MAIN BP主数据创建示例(仅演示客户)

快速下载vscode的方法

keytool: command not found

Implementation of MySQL persistence

BTREE, B + tree and hash index

王者荣耀-unity学习之旅

Django使用mysql数据库报错解决

数据分析入门 | kaggle泰坦尼克任务(四)—>数据清洗及特征处理

How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)

SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)
随机推荐
What is a closure?
Applet Wx Previewmedia related problem solving - Daily stepping on the pit
数据分析入门 | kaggle泰坦尼克任务(三)—>探索数据分析
[CodeForces - 208E] Blood Cousins(k代兄弟问题)
Authorization+Token+JWT
11. Table and library management
页面实时显示当前时间
[self motivation series] you'll never be ready
SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)
ogldev-读书笔记
1D/1D动态规划学习总结
11.表和库的管理
常用的DOS命令
NPM installation stepping pit
2022.3.14 Ali written examination
FSM有限状态机
推导式与正则式
C语言的指针符号到底靠近变量类型还是变量名?
13.用户和权限管理
Dirichlet 前缀和(数论优化式子复杂度利器)