tutorial source
multi-table relationship
There are various association relationships between each table structure, which are basically divided into three types: one-to-many (many-to-one), many-to-many, and one-to-one
One-to-many (many-to-one)
example: the relationship between departments and employees. One department corresponds to multiple employees, and one employee corresponds to one department (regardless of the fact that one employee corresponds to multiple departments)
Realization: Create a foreign key on the many side, pointing to the primary key on the one side
Many-to-many
Example: Student-Course relationship. A student can choose multiple courses, and a course can also be chosen by multiple students
Realization: Create a third intermediate table. The intermediate table contains at least two foreign keys, which are associated with two primary keys
one-to-one
. For example: the relationship between users and user details. The one-to-one relationship is mostly used for single-table splitting. The basic fields of one table are placed in one table, and other detailed fields are placed in another table to improve operational efficiency.
For example: to improve query performance, when there is too much data in a table, the query will become slow; it can make the database structure clearer and the structure of each table simpler
You can add a foreign key on either side, associate the primary key on the other side, and set the foreign key to be unique.
Overview of multi-table queries
Refers to querying data from multiple tables.
Cartesian Product
Cartesian product: refers to all combinations of two sets A and B in mathematics. Need to eliminate invalid Cartesian
integral
-
connection query
- Inner join: equivalent to querying the intersection of A and B
- Outer join:
- Left outer join: Query all data in the left table, and some data in the intersection of two tables
- Right outer join: Query all data in the right table, and some data in the intersection of two tables
- Self-join: the connection query between the current table and itself, the self-join must use the table alias
-
subquery
inner join
The inner join query is the intersection of two tables
implicit inner join
select 字段 from 表1,表2 where 条件
Show Inner Links
select 字段列表 from 表1 join 表2 on 连接条件
Query the employee's job number, name and department name
// 隐式内连接
select emp.workno,emp.name,dept.dept_name from emp,dept where emp.dept_id = dept.id;
// 显示外连接
select emp.workno,emp.name,dept.dept_name from emp join dept on emp.dept_id = dept.id;
outer join
left outer join
select 字段 from 表1 left join 表2 on 条件
All the data in the query emp
table and the corresponding department information
select emp.*,dept.dept_name from emp left join dept on emp.dept_id = dept.id;
The difference between this and the inner join is that if there is a new employee who has just joined the job and does not have a corresponding department, the inner join will not query the employee's information, but the left outer join will query the information.
Inner joins must be present on both sides; left outer joins must be present on the left side, but not on the right side
right outer join
select 字段 from 表1 right join 表2 on 条件
All the data queried dept
, and the information of the corresponding employee table
self-join
select 字段列表 from 表A join 表A 别名B on 条件
A self-join query can be an inner join query or an outer join query.
For example, when the data in a table has a hierarchical structure, you can use a self-join to query parent-child or hierarchical relationships. For example, query the employee's superior or subordinate in the employee table.
Self-join can help us compare and analyze operations in the same table, and solve some complex query requirements. However, it should be noted that when using self-connection, the connection conditions need to be carefully handled to avoid infinite loops or performance problems.
select emp.*,a.name from emp join emp as a where emp.manager_id = a.id ;
joint query
Joint query is to combine the results of multiple queries to form a new query result set. For example, query the employees whose salary is less than 5000 and the employees whose age is greater than 60.
Keyword: union all
and union
, the latter is a deduplication of the former.
During joint query, the number of columns and field types of multiple tables must be consistent.
select 字段列表 from a 条件
union
select 字段列表 from b 条件
subquery
Nested statements in SQL select
are called nested queries, also known as subqueries.
select * from t1 where column1 = (select column1 from t2)
The outer statement of the subquery can be any of insert
, delete
, update
,select
According to the result of the subquery, it can be divided into:
- scalar subquery, the subquery result is a single value
- Column subquery, the result of the subquery is a column
- row subquery, the result of the subquery is one row
- Table subquery, the result of the subquery is multiple rows and multiple columns
scalar subqueries
A subquery that returns a single value (number, string, date, etc.) is called a scalar subquery.
Commonly used operators are: >
, <
, !=
,=
Query all employee information of the R&D center:
1) First query the department id of the R&D center
2) Query employee information according to the department id
select * from emp where dept_id = (select id from dept where dept_name = '研发中心')
column query
The result returned by a subquery is one column (can be multiple columns). This subquery is called a column subquery. The
commonly used operators are: in
, not in
, and
, some
,all
Query all employee information of the delivery center and R&D center
1) Query the department id of the delivery center and R&D center
2) Query employee information according to the department id
select * from emp where dept_id in (select id from dept where dept_name in ('交付中心','研发中心'))
row subquery
The result returned by a subquery is one row (can be multiple columns). This subquery is called a row subquery. The
commonly used operators are: >
, <
, !=
,=
Query employee information of the same department as Qin Yi's gender
1) Query Qin Yi's gender and department id
2) Query employee information based on gender and department id
select * from emp where (sex ,dept_id) = (select sex ,dept_id from emp where name = '秦一')
table subquery
The result returned by a subquery is multiple rows and multiple columns. This subquery is called a table subquery.
Commonly used operators:in
Query employee information with the same gender and department as Qin Yi and Li Er
select * from emp where (sex ,dept_id) in (select sex ,dept_id from emp where name = '秦一' or name = '李二')
Query the employee information after the entry date 2023-06-12
, and its department information
select e.*,d.* from (select * from emp where entrydata > '2023-06-12') as e
left join dept as d on e.dept_id = d.id