Mysql practice questions 12.1

Mysql practice questions 12.1

1. Create two tables as follows:

School table: id primary key increment, school name, school address

create table menpai(
id int primary key auto_increment,
mname varchar (20),
msite varchar (20)
);

Personnel table: id primary key increment, name, age, mpid, number

create table mppeople (
id int primary key auto_increment,
name var char (20),
age int (3),
mpid int (2),
num int(10)
);

2. Create a foreign key link for the two tables

alter table mppeople add constraint m_id foreign key (mpid) references menpai (id) ;

3. Insert the following data into the two tables:

Huashan Huashan
Beggar gang Luoyang
Emei Mount Emei
Wudang Wudang Mountain
Mingjiao Guangmingding
Shaolin Shaolin Temple
Clear wind 90 1 100001
Yue Buqun 50 1 100002
Linghu Chong 24 1 100003
Hong Qigong 70 2 100004
Qiao Feng 35 2 100005
Madam Kill-all 70 3 100006
Zhou Zhiruo 30 3 100007
Wei Xiaobao 20 null 100010
insert into menpai(mname,msite)
values('华山','华山'),
('丐帮','洛阳'),
('峨眉','峨眉山'),
('武当','武当山',
('明教','光明顶'),
('少林','少林寺');
insert into mppeople(name,age,mpid,num)
values('风清扬',90,1,100001)('岳不群',50,1,100002),
('令狐冲',24,1,100003),
('洪七公',70,2,100004),
('乔峰',35,2,100005),
('灭绝师太',70,3,100006),
('周芷若',20,3,100007),
('韦小宝',18, null, 100010);

4. Create the following query

(1) Query all the personnel information of the martial arts

select * from menpai left join mppeople on 
menpai.id=mppeople.mpid;

(2) Query all personnel and display their martial arts information

select * from mppeople left join menpai on 
mppeople.mpid=menpai.id;

(3) List the information of people who do not have martial arts

select menpai.mname,mppeople.name from mppeople left join menpai on 
mppeople.mpid=menpai.id where mppeople.mpid is null;

(4) List the sects that no one has joined

select menpai.mname,mppeople.name from menpai left join
mppeople on menpai.id=mppeop le.mpid where mppeople. id
is null;

Guess you like

Origin blog.csdn.net/m0_46653702/article/details/109546513