FOREIGN KEY foreign key constraints; UNIQUE and PRIMARY KEY primary key constraints, the use of CREATE INDEX to build indexes

1) The foreign key is a constraint, which means that if you set a foreign key constraint on the A field, and you want to insert data into the A field in the future, the data must be the value that exists in the field following the foreign key. The meaning of this is to constrain the integrity of the data and not insert wrong data.

Modify to add t_topic foreign key constraint:

ALTER TABLE t_topic
ADD CONSTRAINT fk_userTopics
FOREIGN KEY (user_id)
REFERENCES t_users(id)

Delete table t_topic foreign key:

ALTER TABLE t_topic
DROP FOREIGN KEY fk_userTopics

 

2) UNIQUED is nullable and can be defined in one or more fields in a table

Add unique key:

ALTER TABLE t_topic
ADD CONSTRAINT un_filed UNIQUE (id,title)

 

Remove unique keys:

ALTER TABLE t_topic
DROP INDEX un_filed

 

3) PRIMARY KEY cannot be empty or repeatable. There can only be one Primary Key in a table, and multiple Unique Keys can exist at the same time. 

Create a unique primary key:

ALTER TABLE t_topic
ADD PRIMARY KEY (id)

Delete the primary key:

ALTER TABLE t_topic
DROP PRIMARY KEY

 

4) CREATE INDEX creates the unique index of the table create unique index and create index

CREATE UNIQUE INDEX index_topic_id
ON t_topic (id)

 

Guess you like

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