search
HomeDatabaseMysql TutorialWhat is the meaning of restrict in mysql

In mysql, restrict means constraint, which refers to a restriction on the data in the table. It can help the database administrator better manage the database and ensure that the data in the database correctness, validity and completeness. In MySQL, there are six types of constraints available, including primary key constraints, foreign key constraints, unique constraints, check constraints, non-null constraints and default value constraints.

What are constraints in MySQL?

restrict (constraint) refers to a restriction on the data in the table, which can help the database administrator better manage the database and ensure the correctness, validity and integrity of the data in the database .

If invalid values, such as 200, 300, are stored in the data table as the age value, then these values ​​​​are meaningless. Therefore, it is necessary to use constraints to limit the range of data in the table.

Constraints in MySQL are used to detect the correctness and rationality of data. Common errors include: 1. The same duplicate data exists; 2. Wrong data; 3. Data loss errors (empty data) ; 4. Refers to unclear errors (tables cannot be accurately linked), etc.

MySQL's constraints are designed to solve some problems, such as prohibiting duplicate ID numbers and forcing the delivery address to be non-empty. If a constraint is violated, the data manipulation behavior is suppressed.

In MySQL, the following 6 types of constraints are mainly supported:

1) Primary key constraints

Primary key constraints are the most frequently used constraints. When designing a data table, it is generally required to set a primary key in the table.

The primary key is a special field in the table that uniquely identifies each piece of information in the table. For example, the student number in the student information table is unique.

2) Foreign key constraints

Foreign key constraints are often used together with primary key constraints to ensure data consistency.

For example, a fruit stall has only four kinds of fruits: apples, peaches, plums, and watermelons. Then, when you come to the fruit stall to buy fruits, you can only choose apples, peaches, plums, and watermelons, and you cannot buy other fruits.

3) Unique constraints

Unique constraints and primary key constraints have something in common, that is, they both guarantee the uniqueness of the column. Different from primary key constraints, there can be multiple unique constraints in a table, and the column where the unique constraint is set is allowed to have null values, although there can only be one null value.

For example, in the user information table, to avoid duplicate user names in the table, you can set the user name column as a unique constraint.

4) Check constraints

Check constraints are a means to check whether the field values ​​in the data table are valid.

The age field in the student information table does not contain negative numbers and has numerical restrictions. If you are a college student, your age should generally be between 18 and 30 years old. In order to reduce the input of invalid data, when setting the check constraints of the field, it is necessary to make appropriate settings according to the actual situation.

5) Non-null constraint

Non-null constraint is used to constrain the fields in the table that they cannot be empty. If the student's name is not filled in the student information form, then this record is meaningless.

6) Default value constraint

Default value constraint is used to constrain that when no value is entered in a field in the data table, an already set value is automatically added to it.

For example, when registering student information, if the student's gender is not entered, a gender will be set by default or "unknown" will be entered.

Default value constraints are usually used on columns that have set non-null constraints, which can prevent errors when entering data into the data table.

Among the above 6 constraints, there can only be one primary key constraint in a data table, and there can be multiple other constraints.

Operation table constraints

1. Add primary key constraints to a table

A table can only have one primary key. There are two ways to add a primary key.

Single primary key: Use one column as the primary key column. When the values ​​of this column are repeated, the unique constraint is violated.

Union primary key: Use multiple columns as primary key columns. When the values ​​of multiple columns are the same, the unique constraint is violated. It refers to: using a combination of multiple columns to determine the primary key. When a combination is repeated, it will be invalid, such as a combination of 00,01,10,11,12,22.

Use DDL statements to add primary key constraints. Example:

ALTER TABLE 表名 ADD PRIMARY KEY(列名);
alter table emp add primary key(employee_id);//选取employee_id作为主键
alter table emp add primary key(employee_id,shenfen_id);//联合主键

2. Add auto-increment primary key constraint

auto_increment to a table. When the database management system automatically maintains the table, it will automatically add the primary key according to the natural number increment method. Similarly, self-increasing primary keys also use a certain column as the primary key. When data is added to the field where the primary key is added to the table, the primary key is automatically incremented by 1.

Auto-increasing primary keys and general primary key constraints can be converted into each other.

1, Only one column in a table can automatically grow . 2. The type of the automatically growing column must be integer type. 3. Automatic growth can only be added to columns with primary key constraints and unique constraints. 4. Delete the primary key constraint or unique constraint. If the column has automatic growth capability, you need to remove the automatic growth first and then delete the constraint.

       注意点:如果之前给某个字段添加了自增长属性,则删除这个字段时要先删除自增长属性。

alter table 表名 modify 主键 类型 auto_increment;
alter table emp modify employee_id int auto_increment;

或者在Navicat中添加主键:

What is the meaning of restrict in mysql

3、删除一张表中的主键约束

使用DDL语句删除主键。

ALTER TABLE 表名 DROP PRIMARY KEY;
alter table emp drop primary key;

       注意:删除主键时,如果主键列具备自动增长能力,需要先去掉自动增长,然后在删除主键。

alter table emp modify employee_id int;//去掉自增长,与添加自增长代码地区别就是没有auto_increament
alter table emp drop primary key;

4、给一张表添加外键约束

使用DDL语句添加外键约束。可以给一张表设置多个外键。

ALTER  TABLE 表名 ADD CONSTRAINT 约束名 FOREIGN  KEY(列名) REFERENCES 参照的表名(参照的列名);
alter table emp add constraint emp_fk
foreign key(dept_id) references
departments(department_id);

或在在Navicat中添加外键。外键名称常使用:表名_fk,表名_fk1表示。

What is the meaning of restrict in mysql

5、删除一张表的外键约束

外键约束可以通过使用DDL语句来删除。或者在Navicat中主动删除外键。

ALTER TABLE 表名 DROP FOREIGN KEY 约束名;
alter table emp drop foreign key emp_fk;

What is the meaning of restrict in mysql

6、给表中某字段添加唯一性约束

使用DDL语句添加唯一性约束。或在在Navicat中添加唯一性约束。

ALTER TABLE 表名 ADD CONSTRAINT 约束名 UNIQUE(列名);
alter table emp add constraint emp_uk unique(name);

What is the meaning of restrict in mysql

7、删除表中某字段的唯一性约束

使用DDL语句删除唯一性约束。删除之前需要弄清楚删除的约束名是对哪一个字段进行约束的。

ALTER TABLE 表名 DROP KEY 约束名;
alter table emp drop key emp_uk;

8、给表中某个字段添加非空约束

使用DDL语句添加非空约束。或者Navicat。

ALTER TABLE 表名 MODIFY 列名 类型 NOT NULL;
alter table emp modify salary float(8,2) not NULL;

What is the meaning of restrict in mysql

9、删除表中某个字段的非空约束

使用DDL语句删除非空约束。

ALTER TABLE 表名 MODIFY 列名 类型 NULL;
alter table emp modify salary float(8,2) NULL;

What is the meaning of restrict in mysql

创建表的时候添加约束

上面介绍了当一张表已经存在之后如何添加约束。下面介绍在一开始新建表的时候如何添加约束。

1、查询表中加了哪些约束?

 SHOW KEYS FROM 表名;

2、创建表时添加约束

       创建 depts 表包含 department_id 该列为主键且自动增长,department_name 列不允许重复,location_id 列不允含有空值。

create table depts
(
department_id int primary key auto_increment,
department_name varchar(30) unique,
location_id int not null;
);

The above is the detailed content of What is the meaning of restrict in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)