Difference between primary key, foreign key and index

Definition :

Primary key: uniquely identifies a record, cannot have duplicates, and cannot be empty

Foreign key: the foreign key of a table is the primary key of another table, the foreign key can have duplicates, and can be null

Index: A structure that sorts the values ​​of one or more columns in a database table

 

 

Function :

 

Primary key: used to ensure data integrity

 

Foreign keys: used to establish connections with other tables

 

Index: is to improve the speed of query sorting

 

Number :

 

Primary key: There can only be one primary key

 

Foreign keys: A table can have multiple foreign keys

 

Indexes: A table can have multiple unique indexes

 

Add :

 

Primary key: ALTER TABLE "tablename" ADD PRIMARY KEY (fieldname)

 

Foreign keys: ALTER TABLE "tablename" ADD FOREIGN KEY (fieldname) REFERENCES "another tablename" (fieldname)

 

Index: ALTER TABLE "tablename" ADD INDEX (fieldname)

 

Guess you like

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