Day13JavaWeb [Mybatis SQL review] Multi-table relationship

Introduction to the relationship between multiple tables

  • (1) Multiple tables in the project
    1: In the actual development, the project must be composed of multiple tables, and there is a relationship between these tables.
    2: The classification of the relationship between the tables:
    one to one, one to one Many, many to many
    (2) What is one to one?
    A row of table A corresponds to a row of table B, and vice versa, at this time, the two tables can be merged into one table
    Insert picture description here

(3) What is one-to-many?
A row of table A corresponds to multiple rows of table B, and the opposite is not true
Insert picture description here

(4) What is many-to-many?
A row of table A corresponds to multiple rows of table B, and a row of table B corresponds to multiple rows of table A
Insert picture description here

Multi-table relationship-one-to-many relationship

(1) Initialize data
(2) One-to-many creation process
Insert picture description here

"Create master table (category table)
" Create slave table (product table)
"Add foreign key constraints between the master table and the slave table
alter table 从表 add [constraint] [外键名称] foreign key (从表外键字段名) references 主表 (主表的主键);
" Add data to the master table (add it at will)
"Add data to the slave table (adding data is necessary Depend on the main table)
(3) One-to-many characteristics
Add data: main table: add at will, slave table:
delete data subject to the restrictions of the master table : master table: if a row of data is dependent on the slave table, it cannot be deleted. : You can delete
(4) analogy
province and city at will
Insert picture description here

create database day13_2
use day13_2
》创建主表(分类表)
create table category(
	cid int primary key auto_increment,
	cname varchar(20)
)
》创建从表(商品表)
create table product(
	pid int primary key auto_increment,
	pname varchar(20),
	price double,
	cid int -- 外键 表示属于哪个分类
)
》给主表和从表之间添加外键约束 
`alter table 从表 add [constraint] [外键名称] foreign key (从表外键字段名) references 主表 (主表的主键);`
alter table product add  foreign key (cid) references category(cid)
》给主表添加数据(随便添加)
insert into category value(null,'电子')
insert into category value(null,'服装')
》给从表添加数据(添加数据是必须依赖主表)
insert into product value(null,'联想',2000,1)
insert into product value(null,'华为',4000,1)	
insert into product value(null,'真维斯',100,2)	

Multi-table relationship-many-to-many relationship

  • (1) Use excel analysis
  • (2) Many-to-many creation process
    "Create order table (main table) order
    " "Create intermediate table (slave table) save cross line
    " "Create foreign key constraints for intermediate table (2 times)
    "" Add data to order table (Optional)
    》》Add data to the intermediate table (limited)
    Insert picture description here

Insert picture description here

》》创建订单表(主表) order
create table orders(
	oid int primary key auto_increment,
	money double
)
》》创建中间表(从表) 保存交叉线
create table orders_product(
	oid int , -- 必须存在 外键
	pid int  -- 必须存在 外键
)
》》给中间表建立外键约束(2)
alter table orders_product add  foreign key (oid ) references orders(oid);
alter table orders_product add  foreign key (pid ) references product(pid);
》》给订单表添加数据(随意)
insert into product value(null,'LV',30000,2)
》》给中间表添加数据(受限)	
insert into orders_product value(3,6)

Practice-one to many

(1) Analyze the relationship between the province table and the city table
(2) Create a table: one-to-many relationship (province table and city table)
"Create master table (province table)
"" Create slave table (city table)
"" Create external Key constraints
"Add data to the main table
" " Add data to the slave table
Insert picture description here


# 练习1
》》 创建主表(省表)
create table province(
	pid int primary key auto_increment,
	pname varchar(20)
)
》》 创建从表(市表)
create table city(
	cid int primary key auto_increment,
	cname varchar(20),
	pid_fk int
)
》》建立外键约束
alter table city add  foreign key (pid_fk ) references province(pid);
》》给主表添加数据
》》给从表添加数据

Practice-many to many-actors and roles

  • (1) Analyze the relationship between the cast table and the role table
    Insert picture description here

  • (2) Create a table: many-to-many relationship (actor table and role table)
    "" create a cast table (left main table)
    "" create a role table (right main table)
    "" create an intermediate table (from the table)
    " 》Create foreign key constraints (2 times)
    》》Add data to the cast table
    》》Add data to the role table
    》》Add data to the intermediate table


》》 创建演员表(左侧主表)
create table users(
	uid int primary key auto_increment,
	uname varchar(20)
)
》》 创建角色表(右侧主表)
create table role(
	rid int primary key auto_increment,
	rname varchar(20)
)
》》 创建中间表(从表)
create table users_role(
	rid int , -- 数据必须在role存在
	uid int   -- 数据必须在users存在
)
》》建立外键约束(2)
alter table users_role add  foreign key (rid ) references role(rid);
alter table users_role add  foreign key (uid) references users(uid);
》》给演员表添加数据
》》给角色表添加数据
》》给中间表添加数据

Exercise-role table and permission table

  • (1) Analyze the relationship between roles and permission tables
    Insert picture description here

  • (2) Create a table: many-to-many relationship (roles and permissions)
    Insert picture description here

》》Create permission table
》》Second intermediate table
》》Create foreign key constraint
》》Add data to permission table
》》Add data to intermediate table

》》 创建权限表
create table privilege(
	pid int primary key auto_increment,
	pname varchar(20)
)
》》 第二个中间表
create table privilege_role(
	pid_fk int , -- 外键
	rid_fk int -- 外键
)
》》建立外键约束
alter table privilege_role add  foreign key (pid_fk) references privilege(pid);
alter table privilege_role add  foreign key (rid_fk) references role(rid);
》》给权限表添加数据
》》给中间表添加数据

to sum up

  • (1) Analyze one-to-many or many-to-many
  • (2) Build a table, add foreign keys
  • (3) You don’t need to do all the questions, just choose one to do

Guess you like

Origin blog.csdn.net/u013621398/article/details/108751627