Mysql table design [summary]

1. The relationship of table design:

1. One-to-one: Part of the data is frequently accessed, and some of the data is not accessed much, so you can create two tables.

  • What is one-to-one: There are two tables AB, where one piece of data in table A corresponds to one piece of data in table B, and one piece of data in table B also corresponds to one piece of data in table A.
  • Application scenarios: user table and user information extension table; product table and product information extension table;

Foreign key: The field used to establish a relationship in a table is called a foreign key. A table may have multiple foreign keys, but only one primary key.

  • If a relationship is established: add a foreign key from the table to the primary key of the main table.

Example: Create user and userinfo tables: If the user contains user name, password, nickname, age, address, and gender fields. We can store the commonly used user names and passwords in the user table without adding the primary key id, and at the same time create the secondary table userinfo, store other nicknames, age and other information, and add the foreign key userid corresponding to the primary key id of the primary table, so as to ensure In order to achieve a one-to-one relationship, when we need to query the userinfo of a user, we can use the id corresponding to match the query from the table foreign key userid.

2. One-to-many: There are two tables AB , one piece of data in table A corresponds to multiple pieces of data in table B, and one piece of data in table B corresponds to one piece of data in table A.

  • Application scenarios: user table and department table; product table and classification table (a category contains many products, but a product can only belong to one category).
  • Establish a relationship: add foreign keys in multiple segments, pointing to the primary key of another table.

Example: Create emp table and dept table: For example, we created the dept department table and added the primary key id. There are two departments, one technical department id = 1, and one design department corresponds to id=2. At this time, we create another emp table. And add the foreign key deptid = 1 corresponding to the primary key id of the main table dept. For example, we add Xiao Zhang deptid = 1, Xiao Li deptid = 1, Xiao Wang deptid = 2, Xiao Zhao deptid = 2 This is what we pass in the dept table The primary key id is 1 to find the matching 1 from the corresponding deptid in the table emp, you can find two people, Xiao Zhang and Xiao Li, which realizes one-to-many, a main table dept corresponds to the slave table emp Multiple pieces of data.

3. Many-to-many: There are two tables AB, one piece of data in table A corresponds to multiple pieces of data in table B, and one piece of data in table B corresponds to multiple pieces of data in table A.

  • Application scenarios: teacher table and student table; user table and role table; role table and permission table;
  • Establish a relationship: save the relationship between the two main tables through the third relationship table; the third relationship table records the primary keys of the two main tables;

Example: create teacher table, student table and relationship table:

create table teacher(id int primary key auto_increment,name varchar(10));
create table student(id int primary key auto_increment,name varchar(10));
create table t_s(tid int,sid int);

Query each student’s name and the corresponding teacher’s name:

select s.name,t.name from t_s ts join teacher t on ts.tid = t.id join student s on ts.sid = s.id;

Who are the students of Teacher Li:

select s.name,t.name from t_s ts join teacher t on ts.tid = t.id join student s on ts.sid = s.id where t.name = '李老师';

Check who is Xiaoli’s teacher:

select s.name,t.name from t_s ts join teacher t on ts.tid = t.id join student s on ts.sid = s.id where s.name = '小丽';

4. Self-association: Add a foreign key to the current table, and the value of the foreign key points to the primary key of the current table. This association method is called self-association;

  • Application scenario: In the user table, some users have a superior leader, and some do not. At this time, you can add a foreign key to the primary key id of the superior leader.

Example: create a person table, primary key id, name, mgr leader points to the primary key id

create table person(id int primary key auto_increment,name varchar(10),mgr int);

Save the following data: the effect is as follows:

insert into person values(null,'如来',null),(null,'唐僧',1),(null,'悟空',2),(null,'猴孙',3);

Insert picture description here
Query each person’s name and superior’s name:

select p.name,p2.name as 上级	 from person p left join person p2 on p.mgr = p2.id; 

Insert picture description here
5. Table design case: authority management

  • Implementation method: three main tables and two relational tables need to be prepared to realize the authority management function;
  • Application scenarios:
    • Main table: a user table, a role table, a role permission table
    • Relationship table: the correspondence between users and roles, and the correspondence between roles and role permissions

Exercise: Create a table:

create table user(id int primary key auto_increment,name varchar(10));
create table role(id int primary key auto_increment,name varchar(10));
create table module(id int primary key auto_increment,name varchar(10));
create table u_r(uid int,rid int);
create table r_m(rid int,mid int);

Insert data:

insert into user values(1,'刘德华'),(2,'凤姐');
insert into role values(1,'男游客'),(2,'男会员'),(3,'女游客'),(4,'女管理员'); 
insert into module values(1,'男浏览'),(2,'男发帖'),(3,'女浏览'),(4,'女发帖'),(5,'女删帖');

Save the relationship between users and roles Andy Lau: male members and female tourists Sister Feng: female administrators and male tourists:

insert into u_r values(1,2),(1,3),(2,1),(2,4);

Save roles and permissions:

insert into r_m values(1,1),(2,1),(2,2),(3,3),(4,3),(4,4),(4,5);

Query what are the permissions of each user:

select u.name 名字,m.name 权限 from user u join u_r on u.id = ur.id join r_m rm on ur.rid = rm.rid join module m on rm.mid = m.id;

Query the permissions of Sister Feng:

select u.name 名字,m.name 权限 from user u join u_r ur on u.id=ur.uid join r_m rm on rm.rid=ur.rid join module m on rm.mid=m.id where u.name='凤姐';

Query who has male preview permission:

select m.name 权限,u.name 预览 from user u join u_r ur on u.id=ur.uid join r_m rm on ur.rid=rm.rid join module m on m.id=rm.mid where m.name in('男浏览','女浏览');

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/108659615