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

How mysql adds constraints

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

Share

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

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!

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

Database

Wechat

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

12
Report