Database-multi-table design and multi-table query


1. Multi-table design-foreign key constraints

  • Single table constraint : primary key constraint; unique constraint; non-empty constraint;
  • Multi-table constraints : foreign key constraints;

Example: Add a foreign key
to the employee table (Note: employee is the employee table, dept is the department table, did is the primary key of the department table, and dno is the department field of the employee table)

alter table employee add foreign key (dno) references dept(did);

Two, multi-table design-table relationship introduction

  • One-to-many: Example: There are multiple employees in a department, and an employee can only belong to one department;
  • Many-to-many: Example: One student can choose multiple courses, and one course can be selected by multiple students;
  • One-to-one: Example: A company can have one registered address, and one registered address can only have one company;

Three, multi-table design-one-to-many relationship

  • The principle of table creation: create a primary key with a foreign key pointing to one on the multiple side;

Four, multi-table design-many-to-many relationship

  • The principle of table creation: a third table (intermediate table) needs to be created. At least two fields in the intermediate table are used as foreign keys, pointing to the primary keys of many-to-many parties;

Five, multi-table design-one-to-one relationship

  • Unique foreign key correspondence:
    Assuming it is one-to-many, the more party creates a primary key whose foreign key points to one, and sets the foreign key to unique;
  • Primary key correspondence:
    the primary keys of the two tables can be established correspondingly;

Six, multi-table design-multi-table analysis and creation

Seven, multi-table query-overview

1. Multi-table query classification:

(1) Connect query

  • Cross-connection : What is cross join
    queried is the Cartesian product of the two tables;
    select * from 表1 cross join 表2;
    select * from 表1, 表2;

  • Inner connection : inner join
    explicit inner connection: select * from 表1 inner join 表2 on 关联条件;
    implicit inner connection:select * from 表1, 表2 where 关联条件;

  • Outer connection : outer join
    Left outer connection: select * from 表1 left outer join 表2 on 关联条件;
    Right outer connection:select * from 表1 right outer join 表2 on 关联条件;

(2) Subquery
: The condition of a query statement depends on the result of another query statement;

Eight, multi-table query-data preparation

Nine, multi-table query-cross join

(There are few applications in development)

Ten, multi-table query-inner join

11. Multi-table query-outer join

12. Multi-table query-the difference between inner join and outer join

Insert picture description here

Thirteen, multi-table query-sub query

  • With insub-queries;
    Insert picture description here

  • With exisitsub-queries;
    Insert picture description here

  • With anysub-queries;
    Insert picture description here

  • With allsub-queries;
    Insert picture description here

Guess you like

Origin blog.csdn.net/pary__for/article/details/111166412