MySQL database (internal connection) practice questions

One, create a database

1. Open the cmd window and execute the command:

mysql -u root -p

Press Enter and enter the password to log in to the MySQL environment

2. Execute the following command to create a database, the database name is: test (not case sensitive)

CREATE DATABASE test character set gbk;

3. Execute the following commands to use the created test database

use test;

4. Enter the data directory under the MySQL installation directory, and you will see a created test database

5. Connect to Navicat, you can also view the created Test database

Two, create a data table

1. Create a student table

(1) Enter the following command to create a students table

CREATE TABLE students(
id INT NOT NULL PRIMARY KEY,
name CHAR(20) NOT NULL,
score INT,
class INT);

(2) Connect Navicat to view the created students table

(3) Insert data into the created students table

INSERT INTO students VALUES
(1,'张三',80,1),
(2,'李四',70,2),
(3,'王五',90,1),
(4,'赵七',60,2);

(4) Connect Navicat to view the records after inserting data into the students table

2. Create a discipline table

(1) Enter the following command to create a class subject table

CREATE TABLE class(
id INT NOT NULL PRIMARY KEY,
name CHAR(20) NOT NULL
);

(2) Connect to Navicat and view the created class discipline table

(3) Insert data into the created class subject table

INSERT INTO class VALUES
(1,'语文'),
(2,'数学');

(4) Connect Navicat to view the records after inserting data into the class subject table

Three, practice questions

Find out the student with the highest score in each subject

(1) Enter the following command to execute the query result

SELECT C.name AS class,S.name,S.score
FROM students AS S 
	JOIN class AS C
	ON S.class = C.id
WHERE score IN(
	SELECT MAX(score)
	FROM students
	GROUP BY class);

(2) Connect to Navicat, execute the query statement, and view the query result

Four, analysis

SELECT C.name AS class,S.name,S.score

—> AS here means renaming. That is, the capital letter C is used as the class subject table, and the capital letter S is used to represent the students table.
C.name means the name column in the class subject table.
S.name means the name column in the students table.
S.score means the score column in the students data table.

 

FROM students AS S     

           JOIN class AS C

           ON S.class = C.id

—>The grammatical knowledge of inner join is used here: the keyword inner join is used, and inner can be omitted.

Syntax: display inner join: select * from A inner join B on condition;

The meaning of this sentence is to use JOIN to connect the students table and the class curriculum table, in which the capital letter S represents the students student table, and the capital letter C represents the class subject table.

—> The ON here is followed by the condition of the inner join query. The condition here is: the value of the class field in the students table is equal to the value of the id field in the class subject table.

 

WHERE score IN(

—>Here is the query condition, the condition is: score

 

             SELECT MAX(score)

             FROM  students

—>MAX() here is a database function used to find the maximum record in a record set. SELECT is used again here, indicating that this is a nested query, that is, the record with the highest score (score) is queried from the students table.

 

GROUP BY class);
—> Here, GROUP BY means grouping query and display, and the grouping condition is: class. After finding the largest record, use the GROUP BY grouping method to group and display it according to the class record.

● If you don’t add GROUP BY class ); this sentence will not be displayed in groups according to any conditions, and all the records with the highest score (score) will be displayed. The results are as follows:

 

● If you add GROUP BY class ); this sentence, here will show the maximum score (score) record of each department, the result is as follows:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_43184774/article/details/106142266