[Database] Summary of table operation exercises

Table of contents

relational table

Execution order of database sql

How to write inner and outer joins

1. Design a product list

2. Design a teacher list

3. Design a book list

4. Query practice

5. Query practice

6. Design an attendance system

7. Design a school dormitory management system

8. Design a vehicle violation system

9. Design a school canteen management system

10. There is an employee table emp, with fields: name, gender, department, and salary. Query the following data:

11. Real interview questions

12.Real interview questions


relational table

One-to-many (1:n) creates a foreign key in the many table (the foreign key corresponds to the primary key in the one table).

Many-to-many (m:n) creates an additional intermediate table. The intermediate table has at least two fields, which serve as foreign keys pointing to the primary keys of both many-to-many parties.

Execution order of database sql

1. A sql statement usually includes:

 select
 from
 join
 where
 group by 
 having
 order by
 aggregate function
 limit 
 top

2. A brief discussion of the execution sequence:

1) First of all, make sure that it is not executed from left to right according to the order of statements we wrote.

2) Get the result set---->Specify certain fields of the query-->Sort according to certain contents

  • 1. First execute from and join to determine the relationship between tables and obtain a preliminary -----> result set 1
  • 2.where filter the result set 1 to get –> result set 2
  • ​Three.Group by -->Result set 3
  • 4. Filter result set 3 by having and get ----> result set 4
  • ​Specify the fields to query:
  • 5.Select specifies the field to be queried, or it can be an aggregate function -> deduplication of results
  • 6. Merge the grouped result sets and sort them according to the order by conditions
  • 7. If there is a limit or top , it will be executed at the end.

 

How to write inner and outer joins

inner join

1.A join B on A.x = B.x;

2.A inner join B on Ax = Bx; inner can be omitted

3.where A.x = B.x;

select * A join B on A.x = B.x where...

select * from A,B where A.x = B.x...

outer join

1.A left join B on A.x = B.x;

2.A right join B on A.x = B.x;

select * A left join B on A.x = B.x where...

1. Design a product list

Design a product table that contains the following fields: product name, product price, product inventory, and product description.

drop table if exists product;
create table product(  
	name varchar(20),  --商品名称  varchar() 使用字符集UTF8 一个汉字占三个字节
	price decimal(11,2), --商品价格  使用decimal() 比double精确
	storage int,       --商品库存
	description varchar(100) --商品描述。
);

2. Design a teacher list

Design a teacher table that contains the following fields: name, age, height, weight, gender, education, birthday, and ID number.

drop table if exists teacher;
create table teacher(
	name varchar(20),
	age int,
	height double,
	weight double,
	sex bit,
	birthday TIMESTAMP,  timestamp 时间日期型 精确到秒 格式 2000-1-1 10:05:00
	id_number varchar(18)
);

3. Design a book list

Design a book table, including the following fields: book name, book author, book price, book category

drop table if exists book;
create table book(
	name varchar(20),
	author varchar(20),
	price decimal(11,2),
	category varchar(20)
);

4. Query practice

Query in the article table for articles whose publication date create_date is from 10:30 am on January 1, 2019 to 4:02 pm on November 10, 2019.

select * from article where create_date between '2019-01-01 10:30:00' and '2019-11-10 16:02:00';

5. Query practice

In the query article table, the article title is empty, or the publication date create_date is after January 1, 2019.

 select * from article where title is null or create_date > '2019-01-01 00:00:00';

6. Design an attendance system

Attendance system, including employee table, attendance record table   employee and record one-to-many

-- 主要考虑记录表中的记录信息,是如何关联到员工表,员工与记录关系为1:m。

create table emp(
  id int primary key,
  name varchar(20)
);

create table info(
  id int primary key,
  emp_id int,
  info_date timestamp,
  foreign key (emp_id) references emp(id)
);

7. Design a school dormitory management system

The school dormitory management system is required to include dormitory information, student information, and daily dormitory inspection records. One-to-many between dormitories and students, one-to-many between dormitories and ward rounds

-- 主要考虑学生与宿舍的关系:m:1,宿舍的查房记录是根据宿舍来查的,与宿舍有关系,一个宿舍可以多次查房,宿舍与查房记录是1:m的关系


create table dormitory(
  id int primary key,
  number varchar(20)
);

create table student(
  id int primary key,
  name varchar(20),
  dormitory_id int,
  foreign key (dormitory_id) references dormitory(id)
);

create table info(
  id int primary key,
  dormitory_id int,
  status bit,
  info_date timestamp,
  foreign key (dormitory_id) references dormitory(id)
);

8. Design a vehicle violation system

The vehicle violation system includes user table, vehicle table, and violation information table. The violation information table contains the violation information of the user and the vehicle.

-- 用户可以拥有多辆车,关系为1:m,
题目已经说明违章信息包括用户和车辆,说明违章信息表中要记录用户和车辆
,一个用户可以有多次违章记录,用户与违章记录关系为1:m,
一辆车也可以有多次违章记录,车辆与违章记录关系也为1:m

create table user(
  id int primary key,
  name varchar(20)
);

create table cars(
  id int primary key,
  name varchar(20),
  user_id int,
  foreign key (user_id) references user(id)
);

create table info(
  id int primary key,
  user_id int,
  cars_id int,
  foreign key (user_id) references user(id),  --两个外键
  foreign key (cars_id) references cars(id)
);

9. Design a school canteen management system

The school canteen management system includes the canteen table, canteen warehouse table, and warehouse charge record table.

-- 一个食堂有多个仓口卖饭,关系为1:m,
--每个仓口卖饭可以有很多次,仓口与收费记录也是1:m

create table hall(
  id int primary key,
  name varchar(20)
);

create table hall_opening(
  id int primary key,
  name varchar(20),
  hall_id int,
  foreign key (hall_id) references hall(id)
);

create table info(
  id int primary key,
  price int,
  info_date timestamp,
  hall_opening_id int,
  foreign key (hall_opening_id) references hall_opening(id)
);

10. There is an employee table emp, with fields: name, gender, department, and salary. Query the following data:

1. Check the average salary of male and female employees

      select sex,avg(salsry) as 'average salary' from emp group by sex;

2. Check the total salary of each department

     select depart,sum(salsry) from emp group by depart;

3. Query the department with the second highest total salary

     select depart ,sum(salary) from emp group by depart order by sum(salary) desc limit 1,1;

4. Query employee information with duplicate names

      select name from emp group by name having count(name)>1;

5. Query the average salary of male employees in each department whose salary is greater than 10,000

     select depart,avg(salary) from emp where sex = '男' and salary>10000 group by depart;

11. Real interview questions

There are employee tables, department tables and salary tables. Write the corresponding SQL according to the query conditions [Tongcheng Yilong 2020 School Recruitment Written Test Questions]

Now there are employee table, department table and salary table. The fields of the department table department include department_id, name; the fields of the employee table staff include staff_id, name, age, depart_id; the fields of the salary table salary include salary_id, staff_id, salary, and month.

(Problem a): Find the total department salary of each department in the month '2016-09'

   select depart.name,sum(salary) from depart,staff,salary where depart.depart_id = staff.depart_id and staff.staff_id = salary.staff_id and month between '2016-09-01 00:00:00' and '2016-09-30 23:59:59' group by depart.depart_id;

select

        depart.name,sum(salary)

from

        depart join staff on depart.depart_id = staff.depart_id 

        join salary on staff.staff_id = salary.staff_id

where 

        month between '2016-09-01 00:00:00' and '2016-09-30 23:59:59'

group by

         depart.depart_id;

(Question b): Find the number of people in each department and require the output of the department name and number of people.

   select depart.depart_id,count(staff_id) from depart,staff where depart.depart_id = staff.depart_id group by depart.depart_id;

(Question c): Find the monthly salary expenditure of each department of the company, and require the output of the month and the total salary of this month.

   select depart.depart_id ,mouth,sum(salary) from depart,staff,salary where depart.depart_id = staff.depart_id and staff.staff_id = salary.staff_id group by depart.depart_id ;

12.Real interview questions

Write the query conditions for the following database [SJTU Sinuo 2020 written examination questions]

There are two tables as follows:

表A(varchar(32) name, int grade)

Data: zhangshan 80, lisi 60, wangwu 84

表B(varchar(32) name, int age)

Data: zhangshan 26, lisi 24, wangwu 26, wutian 26

Write the SQL statement to get the following query results:

| NAME   | GRADE | AGE |

| --------- | ----- | ---- |

| zhangshan | 80 | 26 |

| lisi   | 60  | 24  |

| wangwu | 84 | 26 |

| wutian | null | 26 |

 select B.name , grade,age from B left join A on B.name = A.name;

Guess you like

Origin blog.csdn.net/m0_73381672/article/details/132418145