MYSQL study notes-punch in 2
2.1 MySQL Basics (2)-Table Operations
#Learning Content#
- MySQL table data type
- Create table with SQL statement
Statement explanation
Set column type, size, constraint
Set primary key - Use SQL statements to add data to the table
Statement explanation
Multiple ways to add (specify column names; do not specify column names) - Use SQL statement to delete a table. The
statement explains the difference between the different ways of
DELETE
DROP
TRUNCATE - Modify the table with SQL statement Modify the
column name
Modify the data in the table
Delete row
Delete column
New column
New row
Item three
Create the courses table as shown below, there are: student (student) and class (course).
For example, table:
±--------±-----------+
| student | class |
±--------±--------- --+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
| A | Math |
±--------±-----------+
Write a SQL query to list all classes with more than or equal to 5 students.
Should output:
±--------+
| class |
±--------+
| Math |
±--------+
Note:
Students should not be Repeated calculation.
-Create table
CREATE TABLE courses
(student VARCHAR(255) NOT NULL,
class VARCHAR(255) NOT NULL);
-Insert data
INSERT INTO courses VALUES ('A', 'Math');
INSERT INTO courses VALUES ('B', 'English');
INSERT INTO courses VALUES ('C', 'Biology');
INSERT INTO courses VALUES ('D', 'Math');
INSERT INTO courses VALUES ('E', 'Math');
INSERT INTO courses VALUES ('F', 'Computer');
INSERT INTO courses VALUES ('G', 'Math');
INSERT INTO courses VALUES ('H', 'Math');
INSERT INTO courses VALUES ('I', 'Math');
INSERT INTO courses VALUES ('A', 'Math');
-Code (write a SQL query to list all classes with more than or equal to 5 students)
SELECT class FROM courses
GROUP BY class
HAVING COUNT(DISTINCT student) >= 5;
-result
Project 4: Exchange wages (difficulty: easy)
Create a salary table, as shown below, with values of m=male and f=female.
E.g:
id | name | sex | salary |
---|---|---|---|
1 | A | m | 2500 |
2 | B | f | 1500 |
3 | C | m | 5500 |
4 | D | f | 500 |
Swap all f and m values (for example, change all f values to m and vice versa). An update query is required and there is no intermediate temporary table.
After running the query statement you have written, you will get the following table:
id | name | sex | salary |
---|---|---|---|
1 | A | f | 2500 |
2 | B | m | 1500 |
3 | C | f | 5500 |
4 | D | m | 500 |
-Create table
CREATE TABLE salary
(id INT PRIMARY KEY NOT NULL,
NAME VARCHAR ( 10 ) NOT NULL,
sex VARCHAR ( 10 ) NOT NULL,
salary INT NOT NULL
);
-Insert data
INSERT INTO salary
VALUES
( 1, "A", "m", 2500 ),
( 2, "B", "f", 1500 ),
( 3, "C", "m", 5500 ),
( 4, "D", "f", 500 );
--Code (update data)
UPDATE salary
SET sex =
CASE
sex
WHEN 'm' THEN
'f' ELSE 'm' END;
-result
2.2 MySQL Basics (3)-Table Join
#Learning content#
MySQL alias
INNER JOIN
LEFT JOIN
CROSS JOIN
self-connection
UNION
The difference and connection of the above methods
Item 5: Combine two tables (difficulty: easy)
Create Table 1 and Table 2 in the database, and insert three rows of data (made by yourself)
Table 1: Person
±------------±--------+
| Column Name | Type |
±------------±--------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
±-------- ----±--------+
PersonId is the primary key of the above table
Table 2: Address
±------------±--------+
| Column Name| Type|
±------------±--- -----+t
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
±------------±--------+
AddressId is the primary key of the above table
Write a SQL query to meet the conditions: Regardless of whether the person has address information, the following information of the person needs to be provided based on the above two tables: FirstName, LastName, City, State
-Create table (person)
CREATE TABLE person(PersonId INT,
FirstName VARCHAR(10),
LastName VARCHAR(10));
-Create table (address)
CREATE TABLE address(AddressId INT,
PersionId INT,
City VARCHAR(255),
State VARCHAR(255));
-Insert data (person and address)
INSERT INTO person VALUES (1,'kobe','bryant'),
(2,'lebron','james'),
(3,'chris','paul');
INSERT INTO address VALUES(001,1,'Los Angeles','california'),
(002,2,'Cleveland Cavaliers','Ohio'),
(003,2,'Houston','Texas');
– Code (Regardless of whether the person has address information or not, the following information of person needs to be provided based on the above two tables: FirstName, LastName, City, State)
SELECT FirstName, LastName, City, State FROM
person LEFT JOIN address ON person.PersonId=address.PersionId;
Project 6: Delete duplicate mailboxes (difficulty: easy)
Write a SQL query to delete all duplicate email addresses in the email table, and only keep the one with the smallest Id in the duplicate mailboxes.
±—±--------+
| Id | Email |
±—±--------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
±—±--------+
Id is the primary key of this table.
For example, after running your query, the Person table above should return the following rows:
±—±-----------------+
| Id | Email |
±—±- ----------------+
| 1 | [email protected] |
| 2 | [email protected] |
±—±------------ -----+
-Create table
CREATE TABLE email
( id INT PRIMARY KEY, Email VARCHAR ( 255 ) );
-Insert data
INSERT INTO email ( id, Email )
VALUES( 1, '[email protected]' ),
( 2, '[email protected]' ),
( 3, '[email protected]' );
-Code (write a SQL query to delete all duplicate email addresses in the email table, and only keep the one with the smallest Id in the duplicate mailboxes)
DELETE FROM email
WHERE id NOT IN
( SELECT a.min_id FROM ( SELECT MIN( id ) AS min_id FROM email GROUP BY Email ) a );
-result