Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysql checks the role of constraints

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the role of mysql check constraints, the article is very detailed, has a certain reference value, interested friends must read it!

In MySQL, an CHECK check constraint is provided to specify the range of values available for a column, which enforces the integrity of the field by restricting the values entered into the column. However, the current version of MySQL only analyzes and handles CHECK constraints, but it will be directly ignored and will not report an error.

MySQL check constraints (CHECK) can be implemented through CREATE TABLE or ALTER TABLE statements, defined according to the user's actual integrity requirements. It can enforce CHECK constraints on columns or tables, respectively.

Set check constraints when creating a table

Basic syntax:

CHECK ()

Example: create a tb_emp7 data table in the test_db database, requiring the salary field value to be greater than 0 and less than 10000

Mysql > CREATE TABLE tb_emp7-> (- > id INT (11) PRIMARY KEY,-> name VARCHAR (25),-> deptId INT (11),-> salary FLOAT,-> CHECK (salary > 0 AND salary FOREIGN KEY (deptId) REFERENCES tb_dept1 (id)->); Query OK, 0 rows affected (0.37 sec)

Add check constraints when you modify a table

Basic syntax:

ALTER TABLE tb_emp7 ADD CONSTRAINT CHECK ()

Example: modify the tb_dept data table to require the id field value to be greater than 0

Mysql > ALTER TABLE tb_emp7-> ADD CONSTRAINT check_id-> CHECK (id > 0); Query OK, 0 rows affected (0.19 sec) Records: 0 Duplicates: 0 Warnings: 0

Delete check constraint

Basic syntax:

ALTER TABLE DROP CONSTRAINT; this is all the content of the article "the role of mysql check constraints". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report