当前位置:网站首页>What is MySQL? What is the learning path of MySQL

What is MySQL? What is the learning path of MySQL

2022-07-06 09:02:00 Automated test seventh uncle

Preface

Today, I want to tell you about MySql, This article is divided into two parts , Namely mysql Basic part and advanced part , I won't say more nonsense , Let's go straight to the topic .

 

One . Basics

1.1 Database operation

Show databases; Show all databases 
Show tables;  Show all database tables 
Use databasename; Switch database 
Desc tablename;  Display all field information in the table 

explain : Used in notes sql Statements use tables websites and access_log

surface 1:websites

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  |  TaoBao           | https://www.taobao.com/   | 13    | CN      |
| 3  |  Novice tutorial       | http://www.runoob.com/    | 4689  | CN      |
| 4  |  Microblogging           | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/ |   0 | IND     |
+----+---------------+---------------------------+-------+---------+

surface 2:access_log

+-----+---------+-------+------------+
| aid | site_id | count | date       |
+-----+---------+-------+------------+
|   1 |       1 |    45 | 2022-5-10|
|   2 |       3 |   100 | 2022-05-13|
|   3 |       1 |   230 | 2022-05-14 |
|   4 |       2 |    10 | 2022-05-14 |
|   5 |       5 |   205 | 2022-05-14 |
|   6 |       4 |    13 | 2022-05-15 |
|   7 |       3 |   220 | 2022-05-15 |
|   8 |       5 |   545 | 2022-05-16 |
|   9 |       3 |   201 | 2022-05-17 |
+-----+---------+-------+------------+

1.2 LIKE The operator

example :

 select * from websites where url like 'https%';-- url contain https The data of 
 select * from websites where name like 'G%'; -- name With G start 
 select * from websites where url like '%h%';-- url contain h Field data of 
 select * from websites where name like '_o%';-- name in o Data in the second character 

1.3ORDER BY keyword

explain : Sort the result set by one or more columns

example :

 select name,alexa from websites 
 order by alexa desc; --  The default time is ascending  desc  Representation of descending order 
     
 select * from websites
 order by country, alexa; --  In multi column sorting , First row country  In the platoon alexa

1.4 INSERT INTO

explain : Insert data into table

example :

 insert into websites 
 values (6,' Baidu ','http://www.baidu.com',22,'ZG');--  Columns can be omitted , But to insert all values 
     
 insert into websites (id ,name,url,alexa,country)
 values ('7',' NetEase ','www.wangyi.com','25','ZG');--  You can specify which column to insert 

1.5 UPDATE

explain : Update the data in the table

example :

update websites 
set url = 'http://www.wangyi.com'
where id = 7;
    
update websites
set alexa = 5000, country = 'USA'
where id = 3;

1.6 DISTINCT

explain : Filter duplicate data

example :

SELECT DISTINCT country FROM Websites;

1.7 DELETE

explain : Delete data or delete tables

example :

--  Delete a row of data 
SELECT FROM WEBSITES 
WHERE ID = 7;
--  Delete the entire table , But the structure of the table still exists 
SELECT FROM WEBSITES;

1.8 AND, OR ,IN

explain : Are used to filter data

example :

 SELECT * FROM WEBSITES 
 WHERE ID BETWEEN 1 AND 3;
 
 SELECT * FROM WEBSITES
 WHERE ID = 1 OR ID =3;--  Screening id by 1 and 3  The data of 

 SELECT * FROM WEBSITES
 WHERE ID IN (1,3);

That's all Mysql The basic part of , Some simple operations of adding, deleting, modifying and querying , Maybe the content is not very complete , But I hope for beginners Mysql Students can play a certain reference and reference role , Next, I will talk about the database building that should be included in the basic part , Table building, etc .. These are added to the advanced part by me .

Two 、 senior

2.1 CREATE DATABASE Building database

 CREATE DATABASE BOKEYUAN;--  Create a name for BOKEYUAN The database of 

2.2 CREATE TABLE Build table

 USE BOKEYUAN;--  Use the newly created database 
 CREATE TABLE MYTABLE --  Create a name for MYTABLE Table of 
 (
 ID INT(4) PRIMARY KEY COMMENT ' Serial number - The only key ' , 
 NAME VARCHAR(255) NOT NULL COMMENT ' Website name ',
 URL VARCHAR(255) UNIQUE COMMENT ' website ',
 ALEXA INT(5) COMMENT ' Traffic volume ',
 COUNTRY CHAR(20) COMMENT ' The country of the website '
 );

2.3 INSER INTO SELECT

explain : We have created the table structure above , But the data has not been inserted yet , Then the basic part of the previous section has written about inserting data SQL sentence , In this part, we use another method to insert data , It is actually copying data from other tables

example 1: hypothesis MYTABLE Table does not exist , We can use the following method to copy the structure and data of other tables ( Because we use the same table data , So I will consider this method , If you are a new table, you can only create a table first and then insert data )

 --  Suppose our websites  Table in MYDATABASE In the database 
 CREATE TABLE MYTABLE SELECT * FROM MYDATABASE.WEBSITES;

example 2: We need a watch MYTABLE Already exist , Then the above table has been established , Then we can use INSERT INTO SELECT Statement to insert data directly ( Here is also a copy websites Data in )

 INSERT INTO MYTABLE SELECT * FROM MYDATABASE.WEBSITES;

example 3: Insert a column of data

--  Insert only one column of data 

INSERT INTO MYTABLE(NAME) 
SELECT NAME FROM MYDATABASE.WEBSITES;

2.4 LIMIT

explain : Return record line , above SQL After execution, we will generate the following data table

# ID, NAME, URL, ALEXA, COUNTRY
'1', 'Google', 'https://www.google.cm/', '1', 'USA'
'2', ' TaoBao ', 'https://www.taobao.com/', '13', 'CN'
'3', ' Novice tutorial ', 'http://www.runoob.com/', '5000', 'USA'
'4', ' Microblogging ', 'http://weibo.com/', '20', 'CN'
'5', 'Facebook', 'https://www.facebook.com/', '3', 'USA'
'6', ' Baidu ', 'http://www.baidu.com', '22', 'ZG'

example 1:

 --  Before acquisition 3 Row data  2 SELECT * FROM MYTABLE LIMIT 3;
--  obtain 3,4,5 Row data 
SELECT * FROM MYTABLE LIMIT 2,3;

2.5 BETWEEN AND

explain : coordination WHERE Query condition statements used , Here is 3 Implementation acquisition 1 To 3 Method of row data , But in general, examples 1 It's more convenient , It's logical , And simple and easy to understand

example :

 --  obtain 1 To 3 Row data 
 SELECT * FROM MYTABLE WHERE ID BETWEEN 1 AND 3;

example 2:

 --  obtain 1 To 3 Row data can also be WHERE IN  To achieve 
 SELECT * FROM MYTABLE WHERE ID IN (1,2,3);

example 3:

 --  obtain 1 To 3 Another implementation of row data 
 SELECT * FROM MYTABLE WHERE ID < 4;

2.6 AS The operator

explain : Give column , Table alias

example 1:

 --  Alias a column 
 SELECT NAME  AS ' name ', URL AS ' website ' FROM MYTABLE;

example 2:

 --  Merge 2 Columns , names   need CONCAT keyword 
 SELECT NAME AS ' name ', CONCAT(URL,COUNTRY) AS ' Website country ' FROM MYTABLE;

example 3:

 --  Aliasing tables , You can use aliases to access field elements 
SELECT M.ID,M.NAME,M.COUNTRY FROOM MYTABLE AS M;

2.7 JOIN

explain : Connect two tables , Split left connection , Right connection and full connection , Here we need another table , Suppose the table is ACCESS_LOG, The structure is as follows :

# aid, site_id, count, date
'1', '1', '45', '2016-05-10'
'2', '3', '100', '2016-05-13'
'3', '1', '230', '2016-05-14'
'4', '2', '10', '2016-05-14'
'5', '5', '205', '2016-05-14'
'6', '4', '13', '2016-05-15'
'7', '3', '220', '2016-05-15'
'8', '5', '545', '2016-05-16'
'9', '3', '201', '2016-05-17'

example 1:

-- INNER JOIN  Here we put MYTABLE Look at the left table ACCESS_LOG It's the right table , Below sql Statement will return the line that meets the condition , It can be imagined as the intersection of two sets in a set 
 SELECT M.*,A.SITE_ID FROM MYTABLE AS M
 INNER JOIN ACCESS_LOG AS A
 ON M.ID = A.SITE_ID
 ORDER BY M.ID;

example 2:

 --  Left connection , All rows of the left table will be returned , If there is no match, it will also return 
 SELECT M.* FROM MYTABLE AS M
 LEFT JOIN ACCESS_LOG AS A
 ON M.ID = A.SITE_ID
 ORDER BY M.ID;

example 3:

 --  The right connection , Return the rows of all right tables that meet the conditions 
 SELECT *  FROM MYTABLE AS M
 RIGHT JOIN ACCESS_LOG AS A
 ON M.ID = A.SITE_ID
 ORDER BY A.SITE_ID;

example 4:

 -- Full connection 
 SELECT M.name, A.count, A.date
 FROM MYTABLE AS M
 FULL OUTER JOIN access_log
 ON M.id=A.site_id
 ORDER BY A.count DESC; 

2.8 UNION

explain : The operator is used to merge two or more SELECT The result set of the statement . Be careful ,UNION Everyone inside SELECT Statements must have the same number of columns . Columns must also have similar data types . meanwhile , Every SELECT The order of the columns in the statement must be the same .

Hypothetical existence table MYSOFTW

# id, soft_name, url, country
'1', 'QQ APP', 'http://im.qq.com/', 'CN'
'2', ' Microblogging  APP', 'http://weibo.com/', 'CN'
'3', ' TaoBao  APP', 'https://www.taobao.com/', 'CN'

example 1:

 --  Screening all countries , Do not include duplicate data 
 SELECT M.COUNTRY FROM MYTABLE AS M
 UNION
 SELECT W.COUNTRY FROM MYSOFTW AS W;

example 2:

--  The query country is “cn” All websites and software 
SELECT M.URL ,W.COUNTRY FROM MYTABLE AS W
WHERE M.COUNTRY = 'CN'
UNION
SELECT S.APP_NAME,A.COUNTRY FROM MYSOFTW AS S
WHERE S.COUNTRY = 'CN';

summary

Today's content is a little more , But the feeling is still quite simple , As the saying goes, who makes perfect , Do more, write more, practice more , All in front SQL Statements are relatively simple , Because there is no practical significance , Just to learn , At work SQL Statements are more complex , A complex SQL Statements are simply a combination of several statements , As long as you comb your thoughts carefully .

I hope this article can help you , Favorite friends can like collection comments and pay attention , Of course, I also prepare a teaching video about database for you , Friends in need can privately send keywords “ Information ” Get it .

 

原网站

版权声明
本文为[Automated test seventh uncle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060856581188.html