Home >Database >Mysql Tutorial >How to understand one-to-many relationship in mysql
1. One-to-many relationship means that one row of data in the master table can correspond to multiple rows of data in the slave table at the same time. In turn, multiple rows of data in the slave table point to the same row of data in the master table.
2. The application scenarios include classification table and product table, class table and student table, user table and order table, etc.
Example
-- 创建分类表 CREATE TABLE category( cid INT PRIMARY KEY AUTO_INCREMENT, cname VARCHAR(50) ); -- 创建商品表 CREATE TABLE product( pid INT PRIMARY KEY AUTO_INCREMENT, pname VARCHAR(50), price DOUBLE, cid INT ) -- 给商品表添加一个外键 alter table product add foreign key(cid) references category(cid)
The above is the detailed content of How to understand one-to-many relationship in mysql. For more information, please follow other related articles on the PHP Chinese website!