How does mysql establish constraints
This article mainly introduces how to establish constraints in mysql, which can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.
Mysql establishes the constraint method: 1, when creating the table, the code is [constraint index name foreign key (foreign key column)]; 2, after the table is created, the primary key constraint [alter table table_name add primary key (field)].
Mysql's method of establishing constraints:
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 listing set default 'value' deletion: alter table table_name alter listing drop default thank you for carefully reading this article, I hope the editor to share how to establish constraints on mysql content to help everyone, but also hope that you support a lot, pay attention to the industry information channel, find problems, detailed solutions waiting for you to learn!