当前位置:网站首页>12.约束
12.约束
2022-04-23 06:23:00 【阿呆布衣酷】
12.约束
一:添加约束
1.在创建表时添加约束
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.查看表所有信息包括约束
show create table t_student;
3.为约束指定名称
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.在创建表后再添加约束
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);
二:删除约束
1.删除主键约束
alter table 表名 drop primary key
2.删除外键约束
alter table 表名 drop foreign key 约束名称
3.删除唯一约束
alter table 表名 drop index 约束名称
4.删除非空约束
alter table 表名 modify 列名 数据类型 null
三:常见五种约束:主键,非空,检查,唯一,外键
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;
版权声明
本文为[阿呆布衣酷]所创,转载请带上原文链接,感谢
https://adbycool.blog.csdn.net/article/details/124351654
边栏推荐
- 技能点挖坑
- 5.SQL99标准:内连接和外连接
- [CodeForces - 208E] Blood Cousins(k代兄弟问题)
- Failed to install Tui editor, quick solution
- [Educational Codeforces Round 80] 解题报告
- Us photo cloud editing helps BiliBili upgrade its experience
- anaconda3安装
- 关于素数的不到100个秘密
- Mysql 数据库从设计上的优化
- Discussion on frame construction and technology selection of short video platform
猜你喜欢

Patrol inspection intercom communication system in power industry

USO technology was invited to share the technical framework and challenges of AI synthetic virtual characters at lvson2020 conference

如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列

可视化之路(十一)matplotlib颜色详解

H5 case development

Meishe helps Baidu "Duka editing" to make knowledge creation easier

el-date-picker中自定义快捷选项picker-options,动态设置禁用日期

manjaro安装与配置(vscode,微信,美化,输入法)

关于'enum'枚举类型以及结构体的问题。

可视化之路(十二)Collection类详解
随机推荐
华为云MVP邮件
13.用户和权限管理
可视化之路(十二)Collection类详解
Us photo cloud editing helps BiliBili upgrade its experience
5.SQL99标准:内连接和外连接
状态同步与帧同步
P1390 公约数的和(莫比乌斯反演)
可视化常见绘图(一)堆叠图
获取字符格式的当前时间
可视化之路(十)分割画布函数详解
推导式与正则式
Moment.js中format方法函数的格式
数据分析入门 | kaggle泰坦尼克任务(三)—>探索数据分析
青龙面板拉库命令更新【2022/4/20】收藏不走丢
Flexible blind patch of ad hoc network | Beifeng oil and gas field survey solution
C语言的指针符号到底靠近变量类型还是变量名?
直观理解熵
图论入门——建图
安装tui-editor失败,快速解决方案
Pycharm