Notes: MySQL multi-table operation

The structure of the original table:
Insert picture description hereInsert picture description here

Maintenance of the relationship between multiple tables
Foreign key constraints: foreign key
alter table 外表名 add foreign key(列名) references 主表名(列名);
supplementation and deletion of foreign key constraints: the alter table 表名 drop foreign key 约束名
Insert picture description here
principle of table creation between multiple tables

  • One-to-many: products and categories
    • The principle of table creation: add a foreign key to the primary key of the one side
  • Many to many: teachers and students, students and courses
    • The principle of table building: create a middle and middle table, and the many-to-many relationship. The middle table must have at least two foreign keys, which point to another table.
  • One to one: class and monitor, citizen and ID card, country and national flag
    • Principles of table building:
      • Treat the one-to-one situation as a one-to-many situation, add a foreign key to any table, and this foreign key is unique, pointing to another table
      • Directly merge the two tables into one
      • Connect the primary keys of the two tables to make the primary keys of the two tables equal
    • Practical use: not a lot of use. (disassembly operation)
      • Blind date website
        • Personal information: name, gender, age, height, annual income, hobbies (specialties, education, occupation, requirements...)
        • Dismantling table operation: personal information and infrequently used information to reduce the bloat of the table,

Cross link query

Insert picture description here

Cartesian product
The result of the check is the product of the two tables, and the result of the check is meaningless
select * from product,category

Insert picture description here
_Filter out meaningful data,
select * from product,category where cno=cid;
avoid not knowing where the condition comes from, add aliases to the table
select * from prodyct as p,category as c where p.cno=c.cid;
Insert picture description here
Insert picture description here
__inner join
query___implicit inner join
: filter where conditions are done based on the results of the
select * from product p,category c where p.cno=c.cid;
Insert picture description here
query___ display inner join
: with Condition to query the results, the execution efficiency is high
select * from product p inner join categpry c on p.cno=c.cid;
Insert picture description here
__ Left outer link
: All data in the left table will be queried. If there is no corresponding data in the right table, replace
select * from product p left outer join category c on p.cno=c.cid;
Insert picture description here
__ Right outer link
: All the right table After the data is queried, if there is no corresponding data in the left table, replace the
select * from product p right outer join category c on p.cno=c.cid;
Insert picture description herepaging query with NULL,
select * from 表名 limit 起始索引,每页参数显示的个数
such as: select 8 from product limit 0,3;
Insert picture description here
subquery
select * from product where cno = (select cid from category where cname='手机数码');
Insert picture description here
select pname, (select cname from category c where p.cno=c.cid) as 商品分类名称 from product p;
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46083166/article/details/105430537