The structure of the original table:
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 约束名
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,
- Blind date website
- Principles of table building:
Cross link query
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
_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;
__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;
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;
__ 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;
__ 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;
paging query with NULL,
select * from 表名 limit 起始索引,每页参数显示的个数
such as: select 8 from product limit 0,3;
subquery
select * from product where cno = (select cid from category where cname='手机数码');
select pname, (select cname from category c where p.cno=c.cid) as 商品分类名称 from product p;