One, foreign key
- Now we have two tables, "Classification Table" and "Commodity Table". In order to indicate which category the product belongs to, we usually add a column to the product table to store the classified cid information. This column is called: external Key .
- At this time, the
"category category" is called the primary table, and the cid is called the primary key.
"Products table products" is called: from the table, category_id is called a foreign key.
We describe the primary and foreign key relationship through the primary key of the primary table and the foreign key of the secondary table, which presents a one-to-many relationship. - Foreign key characteristics:
The value of the foreign key from the table is a reference to the primary key of the main table.
The type of the foreign key from the table must be consistent with the primary key type of the primary table - Declare foreign key constraints
Syntax: alter table from the table add [constraint] [foreign key name] foreign key (from the table foreign key field name) references the main table (the primary key of the primary table);
where [foreign key constraint] is used to delete foreign key constraints, generally It is recommended that "_fk" end
alter table from the table drop foregin key foreign key name;
- Purpose of using foreign keys: to
ensure data integrity
Two, the relationship between the table and the table
The relationship between the table and the table is the relationship between the table and the table data.
- One-to-one relationship: (understand)
- There are not many applications in actual development, because one to one can create a table.
- One-to-many relationship:
- Common examples: customers and orders, categories and commodities, departments and employees.
- The principle of one-to-many table creation: create a field in the slave table (many side), and the field is used as a foreign key to point to the primary key of the primary table (one side).
- Many-to-many relationship:
-
Common examples: students and courses, products and orders, people and roles.
-
The principle of many-to-many relationship table creation : You need to create a third table with at least two fields in the middle table, and these two fields are used as foreign keys to point to the primary key of each . (That is to split one many-to-many into two one-to-many)
-
Both tables are the master table, and the third table is the slave table, providing two fields, both of which are foreign keys.
Three, create a table to achieve
1. One-to-many: categories and commodities
#创建数据库
create database web01;
#使用数据库
create web01;
#创建分类表
create table category(
cid int primary key auto_increment, #主表的主键
cname varchar(20) #分类名称
);
#商品表
create table products(
pid int primary key auto_increment,
name varchar(20),
price double
);
#添加外键字段category_id
alter table products add category_id int;
#添加外键约束
alter table products add foreign key(category_id) references category(cid);
2. Many to many: orders and commodities
#订单表
create table orders(
oid int primary key auto_increment,
totalprice double #总计
);
#订单项表
create table orderitem(
oid int primary key, #订单id
pid int primary key #商品id
);
#订单表和订单项表的主外键关系
alter table orderitem add foreign key(oid) references orders(oid);
#商品表和订单项表的主外键关系
alter table orderitem add foreign key(pid) references products(pid);
to sum up:
- Add data: master table: add at will; slave table: limited by the master table, you cannot add data that does not exist in the master table.
- Delete data: Main table: If a row of data is dependent on the slave table and has been used in the slave table, it cannot be deleted ; From the table: delete at will.
- If you want to delete the data in the main table that is related to the slave table, you can do this:
1. Lift the constraint relationship of the master-slave table
2. First delete the data in the slave table that is related to the main table, and then delete the data in the main table .