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

关键字: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)

大家都在看

  • BASE64处理文件

    Base64: Base64是一种编码方式,基于64个ASCII字符来表示二进制数据,Base64将8个bit为一个单位的字节数据拆分为以6个bit为一个单位的二进制片段,每6个b…

    数据库 2023年6月9日
    077
  • 什么是真正的HTAP?(二)挑战篇

    上一篇文章中,我们从技术和商业角度分析了 HTAP 系统缘起的背景,本篇文章中,我们将从 HTAP 定义及其相关核心技术等方面来讨论:构建一个 HTAP 所面临的核心问题和挑战有哪…

    数据库 2023年5月24日
    052
  • [LeetCode]剑指 Offer 17. 打印从1到最大的n位数

    输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。 示例 1: 输入: n = 1输出: [1,2,3…

    数据库 2023年6月9日
    080
  • 深入浅出分析 ArrayDeque

    作者:炸鸡可乐原文出处:www.pzblog.cn 一、摘要 在 jdk1.5 中,新增了 Queue 接口,代表一种队列集合的实现,咱们继续来聊聊 java 集合体系中的 Que…

    数据库 2023年6月14日
    086
  • 一个校验接口引发的思考–我真的了解Response吗

    一个校验接口 最近,我需要对接一个外部接口,基本功能是:校验指定的门店是否完善了货运信息。接口大致是这样的: POST https://******/Dealer/CheckCar…

    数据库 2023年6月6日
    095
  • Java面试题(九)–Spring MVC

    1、Spring MVC中的拦截器和Servlet中的filter有什么区别? 过滤器:依赖于servlet容器,在实现上基于函数回调,可以对几乎所有请求进行过滤 拦截器:依赖于w…

    数据库 2023年6月16日
    068
  • 相同执行计划,为何有执行快慢的差别

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。 GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。 前言 今天遇到一个很神奇的现象,…

    数据库 2023年5月24日
    075
  • JVM-类加载

    JVM JAVA技术交流群:737698533 类加载 推荐视频 https://www.bilibili.com/video/BV1PJ411n7xZ JVM系列笔记结合此视频和…

    数据库 2023年6月16日
    076
  • 译文 | MySQL 8.0 密码管理策略(一)

    MySQL 8.0 在密码管理方面有很多改善,本文将介绍以下两个特性。 密码重用策略 生成随机密码 简单地说,当您设置新密码时,您可以限制使用以前使用的密码。有两种策略: [En]…

    数据库 2023年5月24日
    072
  • MySQL 关于 only_full_group_by 限制

    先上结论 如果 only_full_group_by 被启用,那么在查询时,如果某个列不在group by 列表中,此时如果不对该列进行聚合处理,则该列不能出现在 select 列…

    数据库 2023年6月16日
    087
  • MySQL之多表查询、Navicat及pymysql

    一、多表查询 1.1 数据准备 — 建表 create table dep( id int primary key auto_increment, name varchar(20…

    数据库 2023年5月24日
    090
  • gitlab-runner浅谈——【git fetch-pack: expected shallow list】解决方法

    配置完gitlab-runner后执行job总是失败,如下: 解决方法 分析原因发现是git的版本太低了,我用的是系统自带的1.8.3的版本。后来更新为:2.31.1 后job可以…

    数据库 2023年6月11日
    070
  • MySQL实战45讲 16

    16 | “order by”是怎么工作的? 以市民表为例,假设要查询城市是”杭州”的所有人名字,并且按照姓名 排序返回前 1000…

    数据库 2023年6月14日
    083
  • 第06章 MySQL多表查询

    第06章 MySQL多表查询 多表查询,也称为关联查询,是指两个或多个表一起完成查询操作。 [En] A multi-table query, also known as an a…

    数据库 2023年5月24日
    075
  • [springmvc]mvc的多种方式实现请求转发与重定向

    3.restful风格 RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。 RESTFUL适用于移动互联网厂商作为业务接…

    数据库 2023年6月16日
    073
  • Linux–>开关机+用户管理指令

    shutdown关机 语法: shutdown -h 关机时间 now 立刻1 1分种后 s…

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