当前位置:网站首页>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
边栏推荐
- 19C中ASM network未自动启动的处理
- Itop4412 cannot display boot animation (4.0.3_r1)
- JS 比较2个数组中不同的元素
- iTOP4412 SurfaceFlinger(4.0.3_r1)
- Practice using polardb and ECs to build portal websites
- Abnormal record-16
- [exynos4412] [itop4412] [android-k] add product options
- Build an OSS based image sharing website - polite feedback
- js时间获取本周一、周日,判断时间是今天,今天前、后
- 测试oracle库700万数据量普通方式创建索引所需时间
猜你喜欢
随机推荐
oracle通过触发器和序列来定义自增主键,并且设置定时任务每秒钟插入一条数据到目标表
pg库查看某模式下某表的分布键
How does thanos configure different data retention durations for different tenants
oracle创建表空间和修改用户默认表空间
Using Prom label proxy to implement label based multi tenant reading of Prometheus thanos
19C RAC修改VIP及SCANIP步骤-同网段
oracle视图相关
Prometheus alarm record persistence (historical alarm saving and Statistics)
Android-Room数据库快速上手
組件化學習
同时解决高度塌陷和外边距重叠问题
开篇:双指针仪表盘的识别
JS function package foreach use return can not jump out of the outer function
oracle数据库将多个列的查询结果集合并到一行中
Chaos带你快速上手混沌工程
oracle分区的相关操作
iTOP4412 SurfaceFlinger(4.0.3_r1)
10g数据库使用大内存主机时不能启动的问题
EMR Based offline data analysis - polite feedback
Apprentissage par composantes