mysql database (multi-table)

Multi-table design_associated query

Database design: In the design of the database, there are often some problems (the content in the field is the same, also called data redundancy), such problems may lead to data destruction, in order to solve the data redundancy, it is necessary to design the database Follow certain rules (5 paradigms), usually follow 3 paradigms is enough

serial number Name nationality grade
1 Zhang San Chinese First grade
2 Li Si Chinese First grade

The first normal form:  the values ​​of all fields are indecomposable, that is, to maintain atomicity, which is the most basic normal form

The second normal form:  there is a primary key to ensure the integrity of the record

The third paradigm:   Eliminate transitive dependencies, convenience and understanding, that is, eliminate redundancy. For example:

order number order amount Product Number
555555 1000 22
Product Number product name unit price
22 nike

100

Foreign key:  A record that refers to another table is a field that associates a table with a table

(1) The foreign key column type is the same as the primary key type

(2) The association between data tables is realized by the dependence between the primary key and the foreign key

向某个suser表中添加专业编号,通过专业编号来关联专业信息
ALTER TABLE suser ADD COLUMN majorid INT 

The professional id in the suser table can be associated with the major, and the majorid can become a foreign key -- but there are two cases of
adding a foreign key, one is a foreign key without any constraints, and the other is a foreign key with a foreign key constraint added-
- No foreign key constraint association, 3 links between tables, no foreign key constraint association is also called weak association
-- no constraint foreign key, when deleting the main table (professional) information, there is no foreign key

-- Add a foreign key constraint to the foreign key column, then there will be a strong constraint relationship between tables.
Strongly associated foreign key:

ALTER TABLE suser ADD CONSTRAINT majorid_fk FOREIGN KEY(majorid) REFERENCES major(id)

Master table and slave table:  the foreign key is added to the slave table

(1) When there is no corresponding record in the master table, you cannot add records from the slave table casually
(2) You cannot change the value in the master table, resulting in the isolation of information in the slave table
(3) The slave table exists in the same way as the master table record, the corresponding record cannot be deleted from the master table
(4) Before deleting the master table, delete the slave table first

Associated relationship types:

(1) Many-to-one, one-to-many eg: the relationship between departments and employees

(2) Many-to-many eg: Create an intermediate table to associate the relationship between the two tables

(3) One-to-one eg: generally used for splitting multiple tables, students and details

Association query:  multi-table query, when the query fields come from multiple tables, connection query will be used

If there is no valid join condition, the Cartesian product phenomenon will be selected first

Classified by function:

inner join outer join

Equijoin, non-equijoin, self join, left outer join, right outer join

Equivalent connection: 

SELECT a.sname,b.cname FROM suser a INNER JOIN major b ON a.majorid=b.id

The result of table and table query may have several fields that are the same, and the contents of several fields are different:

SELECT 

     s.id,s.name,s.gender,m.name,GROUP_CONCAT(c.name)-- 分组连接,把同一个组中的多个进行连接,只适用于多对多的情况 
     FROM                         -- 将不相同的字段进行拼接
              suser s INNER JOIN major m ON s.id=m.id
                      INNER JOIN major_course mc ON m.id=mc.majorid
                      INNER JOIN course c ON mc.courseid=c.id
         GROUP BY 
               s.name,s.id,s.gender  -- 按相同的字段进行分组
                             

Left outer, right outer association:

SELECT a.sname,b.cname FROM suser a LEFT JOIN major b ON a.majorid=b.id



SELECT a.sname,b.cname FROM suser a RIGHT JOIN major b ON a.majorid=b.id

Non-equivalence join:

CREATE TABLE height(
nname VARCHAR(20),
low DOUBLE(3,2),
hei DOUBLE(3,2)
)
-- 非等值连接
SELECT s.name,s.gender FROM suser s INNER JOIN height h ON s.height BETWEEN h.low AND h.hei

Self join:

CREATE TABLE AREA(
  id INT PRIMARY KEY,
  NAME VARCHAR(20),
  pid INT      -- 父级id            
)
SELECT * FROM AREA WHERE pid=0
SELECT * FROM AREA WHERE pid=610
SELECT * FROM AREA WHERE pid=61001

-- 自连接
SELECT * FROM AREA a1 INNER JOIN AREA a2 ON a1.pid=a2.id
                      INNER JOIN AREA a3 ON a2.pid=a3.id
                       WHERE a1.id = 61001
                       -- 条件是区级的区域,依次往上查询父级

Subquery:  A select statement that appears within other statements

Where the subquery occurs:

(1) After select: support scalar subquery

(2) Support scalar subquery after from: support table subquery

(3) Support table subquery where: support scalar subquery, column subquery

The column subquery is different according to the function and the number of rows and columns of the result set:

(1) Scalar subquery (the result set has only one row and one column)

(2) Column subquery (the result set has only one column and multiple rows)

(3) Table subquery (the result set is generally multi-row and multi-column)

Guess you like

Origin blog.csdn.net/weixin_71243923/article/details/126709820