当前位置:网站首页>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 .
边栏推荐
- CUDA realizes focal_ loss
- Show slave status \ read in G_ Master_ Log_ POS and relay_ Log_ The (size) relationship of POS
- [OC]-<UI入门>--常用控件-UIButton
- 可变长参数
- Computer graduation design PHP Zhiduo online learning platform
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
- Navicat Premium 创建MySql 创建存储过程
- To effectively improve the quality of software products, find a third-party software evaluation organization
- Deep anatomy of C language -- C language keywords
- LeetCode:673. Number of longest increasing subsequences
猜你喜欢
Nacos 的安装与服务的注册
[oc foundation framework] - < copy object copy >
KDD 2022 paper collection (under continuous update)
KDD 2022论文合集(持续更新中)
Unsupported operation exception
UML圖記憶技巧
LeetCode:498. Diagonal traversal
opencv+dlib实现给蒙娜丽莎“配”眼镜
LeetCode:124. Maximum path sum in binary tree
[MySQL] limit implements paging
随机推荐
LeetCode:214. Shortest palindrome string
可变长参数
UnsupportedOperationException异常
What is an R-value reference and what is the difference between it and an l-value?
UML图记忆技巧
TP-LINK 企业路由器 PPTP 配置
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
requests的深入刨析及封装调用
Simclr: comparative learning in NLP
Cesium draw points, lines, and faces
LeetCode:394. String decoding
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
自定义卷积注意力算子的CUDA实现
Computer graduation design PHP Zhiduo online learning platform
如何正确截取字符串(例:应用报错信息截取入库操作)
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
Promise 在uniapp的简单使用
Current situation and trend of character animation
Mongodb installation and basic operation
多元聚类分析