当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
What is a closure?
刨根问底---cocos2d源码的理解与分析
ESP32学习-向工程项目添加文件夹
FSM有限状态机
手游的热更方案与动态更新策略
关于素数的不到100个秘密
如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列
页面实时显示当前时间
Visualization Road (IX) detailed explanation of arrow class
2022.3.14 Ali written examination
页面动态显示时间(升级版)
14.事务处理
积性函数与迪利克雷卷积
Background management system framework, there is always what you want
SAP PI/PO Soap2Proxy 消费外部ws示例
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
[COCI]Lampice (二分+树分治+字符串哈希)
Django使用mysql数据库报错解决
12.约束
经典套路:一类字符串计数的DP问题









