主键约束,唯一约束,默认约束,检查约束,删除主键,删除外键按,删除列,添加列

关键字:constraint
约束是表级限制,它通过限制表的数据来确保数据的完整性和一致性。

[En]

A constraint is a table-level restriction that ensures the integrity and consistency of the data by restricting the data of the table.

常见约束:

用途:就是用来约束其中的一列,作为所有列中的标识符(这一列的唯一代表),

您可以通过主键准确定位表中的列。可以避免列中的数据重复。

[En]

You can accurately locate a column in a table through the primary key. Duplication of data in the column can be avoided.

主键的特性:
1.唯一约束
2.非空约束
语法:
1.create table [库名].表名 (列名1 数据类型1(长度)
primary key,列名2 数据类型2(长度));
2.create table [库名].表名 (列名1 数据类型1(长度),
列名2 数据类型2(长度),primary key(列名1));

第一种创建主键方式
create table school.bbq(
id int(2) primary key,
name varchar(3),
tall int(4),
age int(3)
);
insert into school.bbq values
(1001,’小贺’,170,20),
(1002,’小窦’,184,20),
(1003,’小张’,175,20),
(1004,’小王’,170,20);
— 验证主键的唯一性约束
insert into school.bbq(id, name) values (1002,’小周’);
— 验证主键的非空约束
insert into school.bbq name) values (‘小杨’);

— 第二种主键创建方式
create table school.qqq(
id int(2),
name varchar(3),
age int(2),
sex varchar(2),
primary key(id)
);

2.唯一约束(unique)
用途:用来约束一列中的所有数据,不能重复。

create table school.aaa(
id int(3) unique,
name varchar(3),
age int(4)
);
3.非空约束(not null)
用途:用来约束一列中的所有数据,不能为null。
注意:所有数据类型都可以为空。

[En]

Note: all data types can be empty.

create table school.bbb(
id int(3) not null,
name varchar(3),
age int(4)
);

用途:在规定了的默认值约束的列时,不向该列插入其他数据,则该数据为默认数据
语法:create table 表名(列名1 数据类型1(长度)default 默认值,列名2 数据类型2(长度));
用途:也能确保数据的完整性也能展现和其他表的关系,

一个表可以一个或多个外键,每个外键必须(references)另一个表的主键或唯一键。
语法:
create table 表名(列名1 数据类型1(长度),列名2 数据类型2(长度),
forrign key(本表外键列名) references 被引用的表(被引用的列));

create table school.bbp(
id int(3),
tall int(3),
brithday date,
foreign key(id) references bbq(id)
);

该约束在mysql上停用了,语句不会报错,但没有实际用处
作用:用于限制列中的值的范围,比如 check了一列,那么该列只允许特定的值。
语法:

create table 表名 (列名1 数据类型1(长度),列名2,
数据类型2(长度),check(表达式))
例如:check(id>0)
create table school.qqq(
id int(2),
name varchar(3),
check(id>0)
);

语法:show keys from 表名;

表的修改

语法:alter table 表名 add primary key(列名);
create table school.bbq(
id int(2),
name varchar(3),
age int(2)
);
desc school.bbq;

— 查找展示主键数量
show keys from school.bbq;
— 为表添加主键
alter table school.bbq add primary key(id);
show keys from school.bbq;
desc school.bbq;

语法:alter table 旧表名 rename to 新表名;
alter table school.bbq rename to school.bbb;

语法:alter table 表名 drop primary key;

alter table school.bbq drop primary key;

语法:alter table 表名 modify 列名 数据类型(长度) not null;

语法: alter table 表名 modify 列名 数据类型(长度) 列约束;
alter table school.bbq — 修改的表
modify name varchar(4) — 修改表中的某个对象列
not null; — 不为空
alter table school.bbq
modify name varchar(3);

语法:alter table 表名 change 旧列名 新列名 数据类型(长度);
注:空字符串不等于空字符串。空字符串是字符串类型。

[En]

Note: empty string is not equal to empty string. Empty string is a string type.

alter table school.student change id Sid int(3);
alter table school.student change Sid sid int(3);
alter table school.student change sid id int(2);
alter table school.student change id ID int(5);
desc school.student;
alter table school.student change ID Id int(5);

语法:alter table 表名 add column 列名 数据类型(长度);
alter table school.student add column hahaha int(2);
— 改变表中某列的列名
alter table school.student change hahaha card int(2);

语法:alter table 表名 add column
(列名1 数据类型1(长度),列名2 数据类型2(长度));
alter table school.student add column (idcard1 int(2),idcard2 int(3));

语法1:alter table 表名 modify [column]列名 新数据类型(长度);

alter table school.student modify column card varchar(3);

语法:alter table 表名 drop column 列名;
语法;alter table 表名 drop column 列名1,
drop column 列名2,drop column 列名3;
alter table school.student drop column idcard1;
alter table school.student drop column id1;
alter table school.student drop column id2;
alter table school.student drop column idcard;
alter table school.student drop column id1,drop id2,drop id3;

alter table school.student add column(id1 int(2),id2 int(2),id3 int(2));

语法:create table 表名 (列名1 数据类型1(长度) primary key,

列名2 数据类型2(长度) primary key)
语法:create table 表名 (列名1 数据类型1(长度),
列名2 数据类型2(长度) primary key(列名1,列名2));

语法: create table 表名(列名1 数据类型1(长度),列名2 数据类型2(长度),

foreign key(本表列) references 被引用的表(被引用的列);
在添加外键时指定约束的名称

[En]

Specify the name of the constraint when adding a foreign key

语法:alter table 表名 表名(列名1 数据类型1(长度),列名2 数据类型2(长度),
constraint 约束名 foreign key(本表列) references 被引用的表(被引用的列);
注意:引用被引用列的数据类型(包括约束)需要一致

[En]

Note: data types (including constraints) that reference referenced columns need to be consistent

create table school.student1 (
id int(2),
name varchar(2),
age int(2),

constraint id foreign key (id) references student(id)
);

1.删除外键
语法:alter table 表名 drop foreign key 外键名;
2.删除索引
语法:drop index 索引名 on 表名;

alter table school.student1 drop foreign key student1_ibfk_1;

drop index id on school.student1;

show keys from school.student1;

Original: https://www.cnblogs.com/cn-zhouchao/p/16455523.html
Author: 小胖子学编程
Title: 主键约束,唯一约束,默认约束,检查约束,删除主键,删除外键按,删除列,添加列

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/505252/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Spring Boot启动流程

    Spring Boot启动流程 君生我未生,君生我已老。君恨我生迟,我恨君生早。 一、简述 Spring Boot启动流程分析使用版本SpringBoot VERSION:版本 2…

    数据库 2023年6月14日
    0107
  • SQL 版本号排序

    SQL 语句直接对内容为版本号格式的字段进行排序时,排序效果通常不是最终想要的效果,因为最终需要的效果,是需对版本号里的每一段(通常以小数点分隔)按数值进行排序。 解决这个问题,主…

    数据库 2023年5月24日
    079
  • 自动化测试练手项目推荐

    转载请注明出处❤️ 作者:测试蔡坨坨 原文链接:caituotuo.top/80599ac8.html 你好,我是测试蔡坨坨。 最近收到许多自学自动化测试的小伙伴私信,学习了理论知…

    数据库 2023年6月11日
    0118
  • 浅谈GTID及简单测试

    今天简单介绍一下GTID,并有部分相关实验。 GTID相信大家都不陌生,GTID的英文全称为Global Transaction Identifier,在MySQL主从架构中应用广…

    数据库 2023年6月16日
    071
  • mysql事物

    MySQL 事务主要用于处理操作量大,复杂度高的数据。比如说,在人员管理系统中,你删除一个人员,你既需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些…

    数据库 2023年6月9日
    085
  • Node.js安装

    nvm NVM: Node Version Manager 下载地址 Mac/Linux安装 nvm:https://github.com/nvm-sh/nvm Windows 安…

    数据库 2023年6月6日
    094
  • 小姐姐用动画图解Git命令,一看就懂!

    无论是开发、运维,还是测试,大家都知道Git在日常工作中的地位。所以,也是大家的必学、必备技能之一。之前公众号也发过很多git相关的文章: 但是呢,民工哥,也经常在后台看到读者说,…

    数据库 2023年6月9日
    099
  • POI操作EXCEL对象

    POI操作EXCEL对象HSSF:操作Excel 97(.xls)格式XSSF:操作Excel 2007 OOXML (.xlsx)格式,操作EXCEL内存占用高于HSSFSXSS…

    数据库 2023年6月16日
    085
  • 数据库设计案例

    简单构建设计数据库 数据库设计案例 描述:简单构建设计数据库 sql代码实现 /* 数据库设计案例 */ — 音乐表 CREATE TABLE Music ( title VAR…

    数据库 2023年6月16日
    0114
  • ShardingSphere-Proxy 前端协议问题排查方法及案例

    ShardingSphere-Proxy 是 Apache ShardingSphere 的接入端之一,其定位为透明化的数据库代理。ShardingSphere-Proxy 实现了…

    数据库 2023年6月16日
    089
  • 第16章 变量、流程控制与游标

    第16章 变量、流程控制与游标 1. 变量 在MySQL数据库的存储过程和函数中,可以使用变量来存储查询或计算的中间结果数据,或者输出最终的结果数据。 在 MySQL 数据库中,变…

    数据库 2023年6月6日
    090
  • 每个开发人员都应该关注的7个优秀的GitHub仓库

    1. FreeCodeCamp 2. Developer Roadmap 3. Awesome 4. Build Your Own X 5. Git Ignore 6. Syste…

    数据库 2023年6月11日
    0104
  • SQL注入学习

    SQL注入学习——资源、笔记整理 OWASP-top10(2021) SQL注入产生原因:注入产生的原因是接受相关参数未经处理直接带入数据库查询操作;注入攻击属于服务端攻击,他与操…

    数据库 2023年6月9日
    080
  • Qingcloud_MySQL Plus(Xenon) 高可用搭建实验

    实验:Xenon on 5.7.30 Xenon (MySQL Plus) 是青云Qingcloud的一个开源项目,号称金融级别强一致性的高可用解决方案,项目地址为 https:/…

    数据库 2023年6月16日
    0122
  • LeetCode刷题笔记-简单入门题

    分割平衡字符串 在一个 平衡字符串 中,’L’ 和 ‘R’ 字符的数量是相同的。 给你一个平衡字符串 s,请你将它分割成尽可能多的平…

    数据库 2023年6月11日
    095
  • 7、定时进行数据批处理任务

    一、StopWatch时间控制类: StopWatch 是spring工具包org.springframework.util下的一个工具类,主要用于计算同步 单线程执行时间。 1、…

    数据库 2023年6月6日
    0105
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球