Get the App
SLTechnology News&Howtos  ›  Database  › 

How mysql adds constraints

Shulou Source: shulou.com Published: 2022-06-01 03:26:13 10月26日 Update

Editor to share with you how to add constraints mysql, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!

What is restraint?

A constraint is actually a constraint on the data in a table; the purpose is to ensure that the records in the table are complete and valid.

Common constraints are:

1. Non-null constraint (not null)

2. Unique constraint (unique)

3. Primary key constraint (primary key)

4. Foreign key constraint (foreign key)

5. Check constraints (currently not supported by MySQL, supported by Oracle)

How does mysql add and remove constraints?

1. Add constraints when creating a table

Create table table_name (column name 1 data type (int) primary key auto_increment, column name 2 data type not null, column name 3 data type unique, column name 4 data type default 'value', constraint index name foreign key (foreign key column) references primary key table (primary key column) on delete cascade | on delete set null)

2. Add and delete constraints after the table is built

1), non-empty constraint

Add a non-empty constraint

Alter table table_name modify column name data type not null

Delete a non-empty constraint

Alter table table_name modify column name data type null

2), unique constraint

Add unique constraint

Alter table table_name add unique constraint name (field)

Delete unique constraint

Alter table table_name drop key constraint name

3), primary key constraint

Add primary key constraint

Alter table table_name add primary key (field)

Delete primary key constraint

Alter table table_name drop primary key

4), foreign key constraint

Add a foreign key constraint

Alter table table_name add constraint constraint name foreign key (foreign key column)

Delete foreign key constraint

Alter table table_name drop foreign key constraint name

5) automatic growth constraint

Add automatic growth constraint

Alter table table_name modify column name int auto_increment

Delete automatic growth constraint

Alter table table_name modify list int above is all the content of how to add constraints to mysql, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Data type growth content fields articles support effective not so much most actually that is commonly used time more conditions purpose knowledge index Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple OPPO Reno MariaDB NVidia