Mysql add constraints and view constraints through sql statements

One: add constraints

1. Add a unique constraint: alter table student add constraint uk_name unique(name);

2. Add foreign key constraints:

alter table table name

add constraint fk_fieldname

foreign key (field name) references associated table name (associated field name)

3. Add check constraint:

alter table table name

add constraint CK_fieldname

check (conditional expression)

4. Add a default value constraint:

alter table table name

add constraint DF_fieldname

default 'default value' for field name

5. Remove constraints:

alter table table name

drop constraint constraint name

Two: query constraints and triggers

1. Query constraints: SELECT * FROM information_schema.`TABLE_CONSTRAINTS`; (you can agree to only query a table by where table_name='student')

In fact, every time a constraint is added, the information of the added constraint is stored in the table_constraints table of the schema information_schema; (The `` in mysql is very similar to that of bash, and the expression in `` is also executed instead of using it as a string. )

2. View all triggers: select* from information_schema.triggers;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325257380&siteId=291194637