当前位置:网站首页>MySQL notes 3_ Restraint_ Primary key constraint
MySQL notes 3_ Restraint_ Primary key constraint
2022-04-23 07:12:00 【Xiaoye is stupid!】
constraint —— Primary key constraint (primary key)
Statement , This article is only for learning notes , In view of —> Original address
stay MySQL in ,” constraint “ It refers to a restriction on the data in the table , It can ensure the accuracy and effectiveness of the data in the database
for example : Student number 、 cell-phone number 、 ID number is the only value. , We need to restrict its uniqueness
stay MySql There are mainly 6 Medium constraint : Primary key constraint 、 Foreign key constraints 、 Unique constraint 、 Check constraint 、 Non null and default constraints
Primary keys are divided into ” Single field primary key “ and ” Multi field union primary key “
When using primary keys, you should pay attention to the following points :
- Only one primary key can be defined in a table
- The primary key value must uniquely identify each row in the table , And cannot be null, That is, there cannot be two or more rows of data with the same primary key in the table , Strictly observe The principle of uniqueness ;
- A field name can only appear once in the federated primary key field table ;
- A federated primary key cannot contain unnecessary fields . When a field in the union primary key is deleted , If the primary key composed of the remaining fields still meets the uniqueness principle , Then the joint primary key is wrong . This is a The principle of minimization .
- Set primary key constraints when creating tables ( Single field primary key )
- Set primary key constraints when defining fields
create table < Data table name > (< Field name > < data type > primary key [ The default value is ]);
for example : Create a learning information data table in the database info_study, The primary key is _id,SQL The statement and running results are as follows :
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)
- After defining all fields, specify the part as the primary key constraint ( Single field primary key / Combined the primary key )
create table < Data table name > (< Field name > < data type >...,primary key [ Field 1, Field 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)
Be careful : When setting the federated primary key , Primary key constraints cannot be declared directly after each field .
- When modifying a table, add a primary key constraint ( Can be more )
Method 1:alter table < Data table name > modify < Field > < Field type > primary key;
Method 2:alter table < Data table name > add primary key(< Field name >,< Field name >,...);
Method 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
Method 2:mysql> alter table personal add primary key(_id);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
Be careful :mysql> alter table personal add primary key(phone);
ERROR 1068 (42000): Multiple primary key defined
// It should be noted that if there are constraints in the original table , Then the above error will be reported , Multiple primary key definitions
- Delete primary key constraint
alter table < Data table name > drop primary key;
mysql> alter table personal drop primary key;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
- How to delete a primary key constraint with self growth
1、alter table < The name of the data table > modify column < Field name > < New type ( New length )>
2、alter table < The name of the data 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
Use DOS The following error occurred: enter ';
that will do
mysql> ' '>
'> '> ';
summary
Primary key constraint is the most frequently used constraint , In general , A primary key will be set in the table
When creating a data table , In general , In order to easily look up the records in the table , Will ask to set up a ” Primary key “.
” Primary key “ Is a special field in the table , This field uniquely identifies each piece of information in the table .
Fields set as primary key constraints cannot have null values .
版权声明
本文为[Xiaoye is stupid!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230606586437.html
边栏推荐
- 组件化学习
- Oracle和mysql批量查询用户下所有表名和表名注释
- 解决:You have 18 unapplied migration(s). Your project may not work properly until you apply
- MySQL notes 1_ database
- Tiny4412 HDMI display
- 窗口分析函数LAST_VALUE,FIRST_VALUE,lag,lead
- Oracle Job定时任务的使用详解
- Personal blog website construction
- Dolphinscheduler集成Flink任务踩坑记录
- MySQL notes 2_ data sheet
猜你喜欢
【2021年新书推荐】Practical IoT Hacking
杂七杂八的学习
Build an OSS based image sharing website - polite feedback
oracle表的约束详解
【机器学习】笔记 4、KNN+交叉验证
Itop4412 HDMI display (4.0.3_r1)
AVD Pixel_2_API_24 is already running.If that is not the case, delete the files at C:\Users\admi
Encapsulate a set of project network request framework from 0
组件化学习(2)Arouter原理学习
Dolphinscheduler集成Flink任务踩坑记录
随机推荐
MySQL notes 2_ data sheet
组件化学习(1)思想及实现方式
个人学习相关
Component learning
C#新大陆物联网云平台的连接(简易理解版)
BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView 滑动问题
Using queue to realize stack
Antd Design Form表单检验
Abnormal record-10
[SM8150][Pixel4]LCD驱动
测试oracle库700万数据量普通方式创建索引所需时间
杂七杂八的学习
实习做了啥
取消远程依赖,用本地依赖
统一任务分发调度执行框架
记录webView显示空白的又一坑
PG SQL截取字符串到指定字符位置
Binder机制原理
同时解决高度塌陷和外边距重叠问题
oracle中生成32位uuid