当前位置:网站首页>MySQL笔记3_约束_主键约束
MySQL笔记3_约束_主键约束
2022-04-23 06:07:00 【小叶很笨呐!】
约束——主键约束(primary key)
声明,本文仅为学习记录笔记,借鉴于—>原文地址
在MySQL中,”约束“指的是对表中数据的一种限制约束,它能够确保数据库中数据的准确性和有效性
例如:学号、手机号、身份证号这种唯一的值,就需要约束其唯一性
在MySql中主要有6中约束:主键约束、外键约束、唯一约束、检查约束、非空约束和默认值约束
主键分为”单字段主键“和”多字段联合主键“
在使用主键的时候需要注意以下几点:
- 一个表中只能定义一个主键
- 主键值必须唯一标识表中的每一行,且不能为null,即表中不能存在有相同主键的两行或两行以上的数据,严格遵守唯一性原则;
- 一个字段名只能在联合主键字段表中出现一次;
- 联合主键不能包含不必要的字段。当把联合主键的某一字段删除后,如果剩下的字段构成的主键仍满足唯一性原则,那么这个联合主键是错误的。这是最小化原则。
- 在建表的时候设置主键约束 (单字段主键)
- 在定义字段的时候设置主键约束
create table <数据表名> (<字段名> <数据类型> primary key [默认值]);
例如:在数据库中创建学习信息数据表info_study,主键为_id,SQL语句以及运行结果如下:
mysql> create table info_student (
-> _id int(10) primary key,
-> name varchar(20),
-> class varchar(10),
-> age int(2));
Query OK, 0 rows affected (0.03 sec)
- 在定义完所有字段之后指定部件为主键约束(单字段主键/联合主键)
create table <数据表名> (<字段名> <数据类型>...,primary key [字段1,字段2...]);
mysql> create table boss_info(
-> _id int(10),
-> username varchar(50),
-> phone_number varchar(100),
-> primary key(_id,phone_number));
Query OK, 0 rows affected (0.02 sec)
注意:在设置联合主键的时候,不能在每个字段后面直接声明主键约束。
- 在修改表的时候添加主键约束(可以多个)
方法1:alter table <数据表名> modify <字段> <字段类型> primary key;
方法2:alter table <数据表名> add primary key(<字段名>,<字段名>,...);
方法1:mysql> alter table personal modify _id int(10) primary key;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
方法2:mysql> alter table personal add primary key(_id);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
注意:mysql> alter table personal add primary key(phone);
ERROR 1068 (42000): Multiple primary key defined
// 需要注意的是如果原表中存在约束,那么将报以上错误,多个主键定义
- 删除主键约束
alter table <数据表名> drop primary key;
mysql> alter table personal drop primary key;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
- 如何删除带有自增长的主键约束
1、alter table <数据表的名称> modify column <字段名称> <新的类型(新的长度)>
2、alter table <数据表的名称> drop primary key;
mysql> alter table student_info modify column _id int(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table student_info drop primary key;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
使用DOS出现以下错误输入';
即可
mysql> ' '>
'> '> ';
总结
主键约束时使用最频繁的约束,一般情况下,都会在表中设置一个主键
在建立数据表的时候,一般情况下,为了方便地查找表中的记录,都会要求设置一个”主键“。
”主键“是表里面的一个特殊字段,这个字段能够唯一标识该表中的每条信息。
设置成主键约束的字段不允许有空值。
版权声明
本文为[小叶很笨呐!]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_50957373/article/details/120929140
边栏推荐
猜你喜欢
Build a cloud blog based on ECS (send blessings on the cloud Xiaobao code and draw iphone13 for free)
Binder机制原理
AVD Pixel_2_API_24 is already running.If that is not the case, delete the files at C:\Users\admi
组件化学习(3)ARouter中的Path和Group注解
Memcached source code analysis
Explore how @ modelandview can forward data and pages through the source code
Cause: dx.jar is missing
基于ECS搭建云上博客(体验有礼)
Construire un blog Cloud basé sur ECS (bénédiction sur le Code Cloud Xiaobao, explication détaillée de la tâche iphone13 gratuite)
Using Prom label proxy to implement label based multi tenant reading of Prometheus thanos
随机推荐
Abnormal record-16
Django::Did you install mysqlclient?
EMR Based offline data analysis - polite feedback
补补网络缺口
iTOP4412 HDMI显示(4.0.3_r1)
Abnormal record-20
Encapsulate a set of project network request framework from 0
从0开始封装一套项目的网络请求框架
iTOP4412内核反复重启
Implementation of multi tenant read and write in Prometheus cortex
【机器学习】笔记 4、KNN+交叉验证
Component learning
Typical application scenarios of alicloud log service SLS
Dolphinscheduler集成Flink任务踩坑记录
ARGB透明度换算
利用栈实现队列的出队入队
发布自定义插件到本地服务器
Chaos带你快速上手混沌工程
Exception record-14
19C RAC修改VIP及SCANIP步骤-同网段