Experiment 1 SQL DDL language and single table query

Level 1: Create the supplier tableS(SNO,SNAME,STATUS,CITY)

Task description
Create supplier table S(SNO,SNAME,STATUS,CITY)
related knowledge
The supplier table is composed Sof supplier code ( SNO), supplier name ( SNAME), supplier status ( STATUS), supplier city ( ); the table is as follows:CITY
S
insert image description here

CREATE TABLE S(
    SNO CHAR(3),
    SNAME CHAR(10),
    `STATUS` INT,
    CITY CHAR(8)
); 

INSERT INTO S
VALUES('S1', '精益', 20, '天津'),
    ('S2', '盛锡', 10, '北京'),
    ('S3', '东方红', 30, '北京'),
    ('S4', '丰泰盛', 20, '天津'),
    ('S5', '为民', 30, '上海');

Level 2: Increase the weight of all red parts in the P table by 6

Task description
Add relevant knowledgeP to the weight of all red parts in the table. The parts table is composed of part code ( ), part name ( ), color ( ), and weight ( ); the table is as follows:6

PPNOPNAMECOLORWEIGHTP
insert image description here

update P
set WEIGHT=WEIGHT+6
where COLOR='红';

Level 3: Change the color of all red parts in the P-list to blue

Task description Change the color of all red parts in the table to blue
Relevant knowledge The parts table is composed of part code ( ), part name ( ), color ( ), and weight ( ); :P
PPNOPNAMECOLORWEIGHTP
insert image description here
P
insert image description here

update P
set COLOR='蓝'
where COLOR='红';

Level 4: Change the part P6 supplied by S5 to J4 in the SPJ table to be supplied by S3

Task description
Change the part from supply SPJin the table to supply related knowledge. The supply situation table is composed of supplier code ( ), part code ( ), engineering item code ( ), supply quantity ( ), and identifies a certain supplier supplying a certain part The quantity for a project is . The table is as shown in the figure below: the table has been built , and the structure information is as follows:S5J4P6S3

SPJSNOPNOJNOQTYQTYSPJ
insert image description here
SPJ
insert image description here

UPDATE SPJ
SET SNO='S3'
WHERE SNO='S5' 
AND JNO='J4' 
AND PNO='P6';

Level 5: Decrease the QTY attribute value of all Tianjin suppliers in the SPJ table by 11 (using subquery method)

Task description
Reduce the attribute values SPJ​​of all Tianjin suppliers in the table (using subquery method)QTY11

Relevant knowledge
1. The supplier table is composed Sof supplier code ( ) SNO, supplier name ( SNAME), supplier status ( ), and supplier STATUScity ( CITY). Reduce relevant knowledge (using sub-query method) 1. The supplier table is composed of supplier code ( ), supplier name ( ), supplier status ( ), and supplier city ( ). The table is as follows:
S
SPJQTY11

SSNOSNAMESTATUSCITY
S

insert image description here

The table has been built S, and the structure information is as follows:

insert image description here

2. The supply situation table is composed SPJof supplier code ( SNO), part code ( PNO), engineering item code ( JNO), and supply quantity ( QTY), which identifies the quantity of a certain part supplied by a supplier to an engineering item QTY.
SPJThe table is as follows:

insert image description here

The table has been built SPJ, and the structure information is as follows:

insert image description here

UPDATE SPJ
set QTY=QTY-10
where '天津'=(select CITY from S where SPJ.SNO=S.SNO);

Level 6: Delete the records of all engineering projects in Table J in Tianjin

Task description
Delete Jall the records of engineering projects in the table in Tianjin.
Relevant knowledge engineering project table J is composed of engineering project code ( JNO), engineering project name ( JNAME), and the city where the engineering project is located ( CITY).
JThe table is as shown in the figure below: the table
insert image description here
has been built J, and the structure information is as follows:
insert image description here

DELETE FROM J WHERE CITY= '天津';

Level 7: Delete the record whose supplier number is S2 from the SPJ table

Task Description Delete the record with the supplier number
from the tableSPJS2

Relevant knowledge
The supply situation table is composed SPJof supplier code ( SNO), part code ( PNO), project code ( JNO), and supply quantity ( QTY), which identifies the quantity that a certain supplier supplies a certain part to a certain project QTY.
SPJThe table is as shown in the figure below: the table
insert image description here
has been built SPJ, and the structure information is as follows:
insert image description here

DELETE FROM SPJ WHERE Sno= 'S2';

Level 8: Delete the record with the part name screwdriver from the SPJ table (using subquery)

Task description
Delete the record whose part name is screwdriver from SPJthe table (using subquery method)

DELETE FROM SPJ WHERE PNO in(SELECT PNO FROM P where PNAME= '螺丝刀');

Level 9: Delete all records in the P list

Task Description
Delete Pall records in the table

Relevant knowledge
The parts list is composed Pof part code ( PNO), part name ( PNAME), color ( COLOR), and weight ( WEIGHT);
Pthe table is as shown in the figure below: the table
insert image description here
has been constructed P, and the structural information is as follows:
insert image description here

DELETE FROM P;

Level 10: Please insert (S2, J6, P4, 200) into the supply situation relationship table SPJ

Task description
Please insert ( S2, J6, P4, 200) into the supply situation relationship tableSPJ

Relevant knowledge
The supply situation table is composed SPJof supplier code ( SNO), part code ( PNO), project code ( JNO), and supply quantity ( QTY), which identifies the quantity that a certain supplier supplies a certain part to a certain project QTY.
SPJThe table is as shown in the figure below: the table
insert image description here
has been built SPJ, and the structure information is as follows:
insert image description here

INSERT INTO SPJ
VALUES('S2','P4','J6,200);

Level 11: Ask for the supplier number SNO of the J1 parts of the supply engineering

Task description Seek knowledge about supplier numbers (note de-duplication) for
supplying engineering parts The supply situation table is composed of supplier code ( ), part code ( ), engineering item code ( ), supply quantity ( ), and identifies a supplier supplying a certain The quantity of this kind of parts for a certain engineering project is . The table is as shown in the figure below: the table has been built , and the structure information is as follows:J1SNO

SPJSNOPNOJNOQTYQTY
SPJ
insert image description here
SPJ

insert image description here

SELECT DISTINCT SNO from SPJ where JNO = 'J1';

Level 12: Find the supplier number SNO of the supply engineering J1 part P1

Task description Find the number of suppliers
supplying engineering J1partsP1SNO

Relevant knowledge
The supply situation table is composed SPJof supplier code ( SNO), part code ( PNO), project code ( JNO), and supply quantity ( QTY), which identifies the quantity that a certain supplier supplies a certain part to a certain project QTY.
SPJThe table is as shown in the figure below: the table
insert image description here
has been built SPJ, and the structure information is as follows:
insert image description here

select SNO from SPJ where JNO ='J1' and PNO = 'P1';

Level 13: Find the names and cities of all suppliers

Task Description
Find the names and cities of all suppliers

Relevant knowledge
The supplier table is composed Sof supplier code ( SNO), supplier name ( SNAME), supplier status ( STATUS), supplier city ( CITY).
SThe table is as follows:

insert image description here
The table has been built S, and the structure information is as follows:
insert image description here

SELECT SNAME,CITY FROM S;

Level 14: Find out the names, colors, and weights of all the parts

Task Description
Find out the name, color, weight of all parts

Relevant knowledge
The parts list is composed Pof part code ( PNO), part name ( PNAME), color ( COLOR), and weight ( WEIGHT);
Pthe table is as shown in the figure below: the table
insert image description here
has been constructed P, and the structural information is as follows:
insert image description here

SELECT PNAME,COLOR,WEIGHT FROM P;

Level 15: Find out the project number using the part supplied by supplier S1

Task Description Find out the project number that uses the part supplied by
the supplierS1

Relevant knowledge
The supply situation table is composed SPJof supplier code ( SNO), part code ( PNO), project code ( JNO), and supply quantity ( QTY), which identifies the quantity that a certain supplier supplies a certain part to a certain project QTY.
SPJThe table is as shown in the figure below: the table
insert image description here
has been built SPJ, and the structure information is as follows:
insert image description here

SELECT DISTINCT JNO FROM SPJ WHERE SNO='S1';

Level 16: Add a new user in the users table, user_id is 2019100904 student number, name is '2019-Internet of Things-Li Ming'

Task description
Add a new user in the users table, user_id is 2019100904 student number, name is '2019-Internet of Things-Li Ming'

Relevant knowledge
users is the player information table
insert image description here
;
insert image description here

insert into users(user_id,name) values ('2019100904','2019-物联网-李明');

Level 17: Update the information of the user user_id as robot_2 in the users table, and set the name to 'robot 2'

Task description
In the users table, update the information that the user user_id is robot_2, and the name is set to 'Robot No. 2'
related knowledge
users is the player information table; the users table is as follows (only the first few items are displayed):

insert image description here

The users table has been built, and the structural information is as follows:
The users table has been built, and the structural information is as follows:

UPDATE users
set name='机器人二号'
where user_id='robot_2';

Level 18: Set the answer result (result) of all questions with problem_id 1003 in the solution table to 6

Task description
Set the answer results (result) of all questions whose problem_id is 1003 in the solution table to 6
Relevant knowledge
solution: The solution table of the questions submitted by the contestants is as follows (only the first few items are displayed):
insert image description here
The solution table has been built, and the structural information is as follows :
insert image description here

Level 18: Set the answer result (result) of all questions with problem_id 1003 in the solution table to 6

Task description
Delete all the answers in the solution table where the contest_id is 1001
Related knowledge solution: The solution table of the questions submitted by the contestants is as follows (only the first few items are displayed):

UPDATE solution
set result=6
where problem_id=1003;

Level 19: Delete all solutions with contest_id 1001 in the solution table

Task description
Delete all the answers in the solution table where contest_id is 1001
Related knowledge
solution: The solution table of the questions submitted by the contestants is shown in the figure below (only the first few items are shown):
insert image description here
The solution table has been built, and the structural information is as follows:
insert image description here

delete from solution where contest_id = 1001;

Level 20: Query the title and end_time of all contests

Task description
Query the title and end_time of all contests.
Relevant knowledge
contest is the competition information table;
the contest table is as follows (only the first few items are displayed):
insert image description here
the contest table has been built, and the structural information is as follows:
insert image description here

SELECT title, end_time FROM contest;

Level 21: Query which players' user_ids have submitted solutions, and require that the user_ids in the results are not repeated

Task description
Query which players' user_ids have submitted solutions, and require that the user_ids in the results are not repeated


Answers to questions submitted by relevant knowledge solution contestants. The solution table is as shown in the figure below (only the first few items are displayed):
insert image description here
The solution table has been built, and the structural information is as follows:
insert image description here

SELECT DISTINCT user_id from solution;

Level 22: Query the contest_id whose end_time is later than '2020-11-21 17:30:00'

Task description Query contest_id related knowledge
whose end_time is later than '2020-11-21 17:30:00' contest is the contest information table ;


insert image description here

insert image description here

SELECT  contest_id  from contest where end_time > '2020-11-21 17:30:00';

Level 23: Query the title of the problem whose problem_id is between 1005 and 1009

Task description
Query the title of the problem whose problem_id is between 1005 and 1009

Relevant knowledge
problem is the title table; the problem table is as follows (only the first few items are displayed):
insert image description here
the problem table has been built, and the structural information is as follows:
insert image description here

SELECT title from problem where problem_id between 1005 and 1009;

Level 24: Query the code_length of the solution whose language is not in 0, 1, or 3

Task description
Query the code_length of the solution whose language is not in 0, 1, or 3

Relevant knowledge
solution: The solution table of the questions submitted by the contestants is as follows (only the first few items are displayed):

insert image description here

The solution table has been built, and the structure information is as follows:
insert image description here

SELECT code_length from solution where language !=0 and language !=1 and language !=3;

Level 25: Query the information of the 2018 players (the first 4 digits of the user whose user_id is the student number are the grade)

Task description
Query the information of 2018 players (the first 4 digits of the user whose user_id is the student ID is the grade)

Relevant knowledge
users is the player information
table
insert image description here
;
insert image description here

SELECT * from users where user_id>=20180000000 and user_id<=20189999999

Level 26: Query the information of professional players of 'Biomedical' (players with 'Biomedical' in their name)

Task description
Query the information of professional players of 'Biomedical' (players with 'Biomedical' in name)

Relevant knowledge
users is the player information table
insert image description here
;
insert image description here

select * from users where name like '%生医%';

Level 27: Query solution_id and in_date of solutions that do not belong to any competition (contest_id is NULL)

Task description
Query the solution_id and in_date of the solution that does not belong to any competition (contest_id is NULL)

Relevant knowledge
solution: The solution table of the questions submitted by the contestants
is as follows (only the first few items are displayed):
insert image description here
The solution table has been built, and the structural information is as follows:
insert image description here

select solution_id,in_date from solution where contest_id is NULL;

Level 28: Query the solution_id and language whose result is 6 and whose problem_id is greater than 1010

Task description
Query the solution_id and language whose result is 6 and whose problem_id is greater than 1010

Relevant knowledge
solution: The solution table of the questions submitted by the contestants is as follows (only the first few items are displayed):
insert image description here

The solution table has been built, and the structure information is as follows:

insert image description here

select solution_id ,language from solution where result=6 and problem_id > 1010;

Level 29: Query the number of players who have submitted a solution

Task description
Query the number of players who have submitted a solution

Relevant knowledge
solution:
The solution table of the questions submitted by the contestants is as follows (only the first few items are displayed):
insert image description here
The solution table has been built, and the structural information is as follows:
insert image description here

SELECT COUNT(DISTINCT solution.user_id) FROM solution;

Level 30: Query the memory consumption size and solution_id of the solution that consumes the most memory (memory)

Task description
Query the number of players who have submitted a solution

Relevant knowledge
solution: The solution table of the questions submitted by the contestants
is as follows (only the first few items are displayed):
insert image description here
The solution table has been built, and the structural information is as follows:
insert image description here

select memory,solution_id from solution
where memory in(
    select max(distinct memory) from solution
);

Level 31: Query the number of submissions for each topic

Task description
Query the number of submissions for each topic

Relevant knowledge solution: The solution table of the questions submitted by the contestants is as follows (only the first few items are displayed):

insert image description here

The solution table has been built, and the structure information is as follows:
insert image description here

SELECT distinct problem_id, COUNT(solution.problem_id) FROM solution GROUP BY problem_id HAVING COUNT(*) > 0;

Level 32: Query the question number of the question with more than 20 submissions

Task description
Query the question number of the question with more than 20 submissions

Relevant knowledge
solution: The solution table of the questions submitted by the contestants
is as follows (only the first few items are displayed):
insert image description here
The solution table has been built, and the structural information is as follows:
insert image description here

SELECT distinct problem_id FROM solution where problem_id in (SELECT problem_id FROM solution  GROUP BY problem_id HAVING COUNT(problem_id)>20);

Level 33: Find all solutions with problem_id 1001 or solutions with empty contest_id

Task description
Find all solutions with problem_id 1001 or with contest_id empty

Relevant knowledge
solution: The solution table of the questions submitted by the contestants
is as follows (only the first few items are displayed):

insert image description here
The solution table has been built, and the structure information is as follows:
insert image description here

select * from solution where problem_id=1001 or contest_id is null;

Guess you like

Origin blog.csdn.net/weixin_51571728/article/details/127439544