Home > Database > Mysql Tutorial > How to create many-to-many and one-to-one relationships in MySQL

How to create many-to-many and one-to-one relationships in MySQL

WBOY
Release: 2023-05-26 22:55:19
forward
1997 people have browsed it

1. Create many-to-many

1.Student table

create table students (
  
id int not null primary key auto_increment,
  
name varchar(45) not null
  
)engine=innodb default charset=utf8;
Copy after login

2.Course table

create table courses (
  
id int not null primary key auto_increment,
  
name varchar(45) not null 
  
)engine=innodb default charset=utf8;
Copy after login

3.Intermediate table

create table stu_cour (
  
id int not null primary key auto_increment
  
course_id int not null,
  
stu_id int not null,
  
constraint cour foreign key(course_id) references courses(id),
  
constraint stu foreign key(stu_id) references students(id)
  
)engine=innodb default charset=utf8;
Copy after login

4. Insert data

insert into students values (0,"小王");
insert into students values (0,"小宋");
insert into students values (0,"小李");


insert into courses values (0,"语文");
insert into courses values (0,"数学");
insert into courses values (0,"英语");


insert into stu_cour values (0,1,1);
insert into stu_cour values (0,1,2);
insert into stu_cour values (0,1,3);
insert into stu_cour values (0,2,1);
insert into stu_cour values (0,2,3);
insert into stu_cour values (0,3,2);
insert into stu_cour values (0,3,3);
Copy after login

5. Query which subjects student 1 chose

SELECT courses.id,courses.name FROM courses 

INNER JOIN stu_cour ON stu_cour.course_id=courses.id

INNER JOIN students ON students.id= 1 

and students.id = stu_cour.stu_id;
Copy after login

How to create many-to-many and one-to-one relationships in MySQL

##6. Query who chose id=2 mathematics

SELECT students.name FROM students 

INNER JOIN stu_cour ON stu_cour.stu_id =students.id

INNER JOIN courses ON courses.id= 2 

and stu_cour.course_id = courses.id;
Copy after login

How to create many-to-many and one-to-one relationships in MySQL

2. MySQL creates one-to-one relationship

1. One-to-one

Create user table:

CREATE TABLE users (

id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

name VARCHAR(50) NOT NULL

)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

User information table:

CREATE TABLE users_info (

id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

age int NOT NULL,

phone varchar(11) NOT NULL,

user_id int not null,
constraint user_info foreign key(user_id) references users(id)

)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

2. Insert data

insert into users values (0,"小王");
insert into users values (0,"小宋");


insert into users_info values (0,12,'13812345678',1);
insert into users_info values (0,14,'13812345679',2);
Copy after login

Query all the information of the person:

select * from users inner join users_info on
users_info.user_id =users.id;
Copy after login

The above is the detailed content of How to create many-to-many and one-to-one relationships in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template