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

The method of establishing constraints in mysql

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article will explain in detail the methods of establishing constraints on mysql. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Methods: 1, when creating a table, use primary key and foreign key keywords after the field to establish primary key and foreign key constraints; 2, after building the table, use "alter table" sentence with primary key, not null, unique and other keywords to establish constraints.

MYSQL add constraint

The first one: 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)

The second kind: after the completion of the table

1. Primary key constraint add: alter table table_name add primary key (field) delete: alter table table_name drop primary key2. Non-empty constraint add: alter table table_name modify column name data type not null delete: alter table table_name modify column name data type null3. Unique constraint add: alter table table_name add unique constraint name (field) delete: alter table table_name drop key constraint name 4. Automatic growth add: alter table table_name modify column name int auto_increment delete: alter table table_name modify column name int 5. Foreign key constraint add: alter table table_name add constraint constraint name foreign key (foreign key column) references primary key table (primary key column) delete: first step: delete foreign key alter table table_name drop foreign key constraint name second step: delete index alter table table_name drop index index name [^ 1]: constraint name and index name are the same 6. Default value add: alter table table_name alter column name set default 'value' deletion: alter table table_name alter column name drop default on the method of establishing constraints on mysql is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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