Overview:
During project development, when designing the database table structure, the table structure will be analyzed and designed based on business needs and the relationship between business modules. Since businesses are interrelated, there are various connections between each table structure. There are basically three types:
one to many
many to many
one to one
one to many
Based on the page prototype and requirement documents, complete the table structure design of the department and employee modules
Foreign key constraints (syntax)
Specified when creating table
create table table name (
Field name data type,
...
[constraint] [foreign key name] foreign key (foreign key field name) references main table (field name);
)
After creating the table, add foreign keys
alter table table name add constraint foreign key name foreign (foreign key field name) referness main table (field name);
shortcoming:
Affects the efficiency of addition, deletion and modification
Only used for single-node databases, not suitable for distributed and cluster scenarios
It is easy to cause database deadlock problems and consume performance.
one to one
Relationship : One-to-one relationship, mostly used for single table splitting. Put the basic fields of one table in one table, other fields in another table, and other fields in another table to improve efficiency.
Implementation : Add a foreign key to either party, associate the primary key of the other party, and set the foreign key to be unique (UNIQUE)
many to many
Relationship: A student can take multiple courses, and a course can also be selected by multiple students.
Implementation : Create a third intermediate table. The intermediate table contains at least two foreign keys, which are associated with the primary keys of the two parties.