当前位置:网站首页>MySQL common commands
MySQL common commands
2022-07-28 06:52:00 【[dissimilarity space]】
MySQL Common commands
- One 、 Database operation
- Two 、 Database table operation
One 、 Database operation
1. Create database
CREATE DATABASE Database name ;
-- Create a name for python_test The database of
CREATE DATABASE python_test;
2. Delete database
DROP DATABASE Database name ;
-- Delete name as python_test The database of
DROP DATABASE python_test;
3. View all databases
SHOW DATABASES;
4. Switch database
use Database name ;
-- Select the name as python_test The database of
use python_test;
5. sign out mysql
exit;
6. View the of creating the database SQL sentence
SHOW CREATE DATABASE Database name ;
-- Show create database python_test Of SQL sentence
SHOW CREATE DATABASE python_test;
Two 、 Database table operation
1. Create table
CREATE TABLE students(id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,NAME VARCHAR(20) NOT NULL,age TINYINT DEFAULT 0,gender ENUM(" male "," Woman ") DEFAULT " male ");
2. Delete table
DROP TABLE students;
3. View the tables in the selected database
SHOW TABLES;
4. View table structure
DESC Table name ;
5.ALTER operation
(1) Add a field to the table
ALTER TABLE Table name ADD Field name Field type ;
-- to students Table add a birthday Field
ALTER TABLE students ADD birthday DATETIME NOT NULL;
(2) Modify the field type specified in the table
ALTER TABLE Table name MODIFY Field name Field type ;
-- modify students In the table birthday Type of field
ALTER TABLE students MODIFY birthday DATE;
(3) At the same time, modify the field name and field type of the table
ALTER TABLE Table name CHANGE Original field name new field name New field type ;
-- modify students The original field in the table is birthday Field name and type
ALTER TABLE students CHANGE birthday birth DATETIME NOT NULL;
(4) Delete table specified fields
ALTER TABLE Table name DROP Field name ;
-- Delete students In the table birth Field
ALTER TABLE students DROP birth;
6. Displays the... That created the specified table SQL sentence
SHOW CREATE TABLE Table name ;
-- According to create students Tabular SQL sentence
SHOW CREATE TABLE students;
7.INSERT The insert
INSERT INTO Table name VALUES( Field 1 value , Field 2 value ,..., Field n value );
-- to students Table inserts a piece of data ( Insert one line )
-- The primary key column can be specified as 0,null,default
INSERT INTO students VALUES(0,'Tom',18,DEFAULT);
INSERT INTO students VALUES(DEFAULT,'Yellow',28,DEFAULT);
INSERT INTO students VALUES(NULL,'LiLei',38,DEFAULT);
-- Some columns are inserted in a single row
INSERT INTO students(NAME,age) VALUES('Bob',26);
-- Insert all columns and rows
INSERT INTO students VALUES(0,'Lisa',22,' Woman ');
-- to students The table inserts multiple pieces of data at one time ( Multi line insertion )
-- Partial column multi row insertion
INSERT INTO students(NAME,age) VALUES('Jian',23),('Anla',40);
-- Insert all columns and multiple rows
INSERT INTO students VALUES(0,'Linda',25,' Woman '),(0,'Daive',27,' male ');
8.UPDATE update operation
UPDATE Table name SET Field 1= value 1, Field 2= value 2,..., Field 2= value n WHERE Field m= value m;
-- modify students In the table NAME The field values for ‘Anla’ The data of
UPDATE students SET age=33,gender=' Woman ' WHERE NAME='anla';
9.DELETE Delete operation
DELETE FROM students [WHERE Field m= value m];
-- Delete all table data ( Physical delete )
DELETE FROM Table name ;
-- Delete the specified “ Field m= value m” The data of ( Physical delete )
DELETE FROM Table name WHERE Field m= value m;
-- Physical delete students In the table "id=8" The data of
DELETE FROM students WHERE id=8;
-- Set delete field is_del( Logical deletion )
UPDATE Table name SET is_del=1 WHERE Field m= value m;
-- Logical deletion students In the table NAME=‘Lisa’ The data of
UPDATE students SET is_del=1 WHERE NAME='Lisa';
10.SELECT Query operation
Include : Conditions of the query , Range queries , Fuzzy query , Empty judgment query , Sort , Paging query , Aggregate functions , Group query , Link query , Subquery
-- as and distinct The use of keywords in queries
-- as names Fields and tables can be used as Alias keywords
SELECT Field name 1 AS Alias 1, Field name 2 AS Alias 2,..., Field name n AS Alias n FROM Table name AS Table alias ;
-- as Alias query example
SELECT NAME AS full name ,age AS Age FROM students;
SELECT NAME AS full name ,age AS Age FROM students AS s;
--as Keyword ellipsis form
SELECT NAME full name FROM students s;
-- distinct duplicate removal Remove the data in the same field name in the query , Keep only one piece of data
SELECT DISTINCT Field name 1[, Field name 2,..., Field name n] from Table name ;
-- distinct Example of de duplication query
SELECT DISTINCT gender FROM students;
SELECT DISTINCT age,gender FROM students;
--------------------------------------------------
-- Conditions of the query :where
-- Comparison operator : > , >= , < , <= , !=
SELECT * FROM students WHERE id>3; -- Inquire about id>3 Student information
SELECT * FROM students WHERE id<=4;
SELECT * FROM students WHERE NAME!='Bob'; -- Inquire about name No Bob Information about
-- Logical operators :and , or , not
SELECT * FROM students WHERE id>3 AND gender=' Woman '; -- Inquire about id Range greater than 3 And the gender is ‘ Woman ’ Information about
SELECT * FROM students WHERE id<4 OR is_del=0; -- Interlude id Range is less than 4 Or delete the tag 0 Information about
SELECT * FROM students WHERE NOT (age>=18 AND age<=30);
--------------------------------------------------
-- Range queries :between ... and , in
SELECT * FROM students WHERE id BETWEEN 3 AND 8; -- Inquire about id The scope is 3-8 Information about
SELECT * FROM students WHERE NOT (id BETWEEN 3 AND 8);
SELECT * FROM students WHERE id IN (1,3,5,7); -- Inquire about id The scope is (1,3,5,7) Information about
SELECT * FROM students WHERE id NOT IN (1,3,5,7);
--------------------------------------------------
-- Fuzzy query :like [ With regular expressions "%"( Any number of keywords ) , "_"( Any keyword ) ]
SELECT * FROM students WHERE NAME LIKE 'L%';
SELECT * FROM students WHERE NAME LIKE 'L___';
SELECT * FROM students WHERE NAME LIKE '%w' OR NAME LIKE 'L%';
--------------------------------------------------
-- Empty judgment query :is null , is not null
SELECT * FROM students WHERE height IS NULL; -- Search for information with height empty
SELECT * FROM students WHERE height IS NOT NULL; -- Query the information that the height is not empty
--------------------------------------------------
-- Sort :order by Ascending (order by Field name ASC) Descending (order by Field name DESC)
SELECT * FROM students WHERE gender=' male ' AND is_del=0 ORDER BY id DESC; -- Query the information that the gender is male and has not been deleted , Descending order
SELECT * FROM students ORDER BY age DESC;
SELECT * FROM students ORDER BY age DESC,height DESC; -- First, in descending order of age , If the age is the same, they will be arranged in descending order of height
SELECT * FROM students ORDER BY age ASC;
--------------------------------------------------
-- Paging query
select * from Table name limit start,count; -- start Is the start row index ,count Is the number of queries
SELECT * FROM students WHERE gender=' male ' LIMIT 0,3; -- The first three data show that gender is male
SELECT * FROM students WHERE gender=' male ' LIMIT 3; -- Abbreviation : The first three data show that gender is male
-- For the first n Page data
SELECT * FROM students LIMIT (n-1)*m,m;
--------------------------------------------------
-- Aggregate functions ( Generally, statistics are carried out in combination with grouping incorrect null Make statistics )
-- count(col) Indicates the total row number of the specified column
SELECT COUNT(id) FROM students;
SELECT COUNT(*) FROM students;
-- max(col) Indicates the maximum value of the specified column
SELECT MAX(id) FROM students WHERE gender=' Woman ';
-- min(col) Means to find the minimum value of the specified column
SELECT MIN(id) FROM students WHERE is_del=0;
-- sum(col) Indicates the sum of the specified columns
SELECT SUM(height) FROM students WHERE gender=' male ';
-- avg(col) Indicates that the average value of the specified column
SELECT AVG(height) FROM students WHERE gender=' male ';
SELECT AVG(IFNULL(height,0)) FROM students WHERE gender=' male '; -- Null value found as 0 Make statistics
--------------------------------------------------
-- Group query
--group by Field name ( After grouping according to the specified field, you can only query according to the specified field )
SELECT gender FROM students GROUP BY gender;
SELECT gender,name FROM students GROUP BY gender,name;
-- group_concat( Field name ) ( Count the information set of the specified field name according to the specified grouping field name )
-- according to gender Grouping ( It's actually divided into male Woman Two sets ), And statistics name The distribution of fields in male and female sets , Use ',' Segmentation
SELECT gender,GROUP_CONCAT(NAME) FROM students GROUP BY gender;
SELECT gender,AVG(age) FROM students GROUP BY gender;
SELECT gender,COUNT(*) FROM students GROUP BY gender;
-- Use having
SELECT gender,COUNT(*) FROM students GROUP BY gender HAVING COUNT(*)>3;
-- with rollup Add summary information to the last line Only the column of set function will be summarized
SELECT gender,COUNT(*) FROM students GROUP BY gender WITH ROLLUP;
SELECT gender,GROUP_CONCAT(age) FROM students GROUP BY gender WITH ROLLUP;
--------------------------------------------------
-- Link query ( Realize the joint table query of multiple tables )
-- Internal connection Public data that meets the conditions of multiple tables intersection
select Field from surface 1 inner join surface 2 on surface 1. Field = surface 2. Field
SELECT * FROM students INNER JOIN class ON students.c_id=class.id;
SELECT s.name,c.name FROM students AS s INNER JOIN class AS c ON s.c_id=c.id;
-- Left connection Query the data in the right table based on the left table , If the data in the right table does not exist according to the query criteria, use null fill left On the left is the left watch , On the right is the right watch
select Field from surface 1 left join surface 2 on surface 1. Field 1= surface 2. Field 2;
SELECT * FROM students LEFT JOIN class ON students.c_id=class.id;
SELECT * FROM students AS s LEFT JOIN class AS c ON s.c_id=c.id;
-- Right link Query the data of the left table based on the right table , If the data in the left table does not exist according to the query criteria, use NULL fill right On the left is the left watch , On the right is the right watch
select Field from surface 1 right join surface 2 on surface 1. Field 1= surface 2. Field 2
SELECT * FROM students AS s RIGHT JOIN class AS c ON s.c_id=c.id;
-- Self join The left table and the right table are the same table Is to connect a table inside
CREATE TABLE areas(id VARCHAR(20) PRIMARY KEY NOT NULL,title VARCHAR(20) NOT NULL,pid VARCHAR(20));
-- Subquery ( stay select Nested in a statement select sentence )
SELECT * FROM students WHERE age>(SELECT AVG(IFNULL(age,0)) FROM students);
边栏推荐
- rancher部署实战
- HDU-1097-A hard puzzle(快速幂)
- NFS shared storage service
- Elastic common high frequency commands
- KVM hot migration
- Explain in detail
- Which brand of air conduction earphones is good and highly praised
- raid磁盘阵列
- It is recommended to wear air conduction earphones, which do not need to wear in ear
- Small tips
猜你喜欢

DNS domain name resolution service

RayMarching realizes volume light rendering

NFS 共享存储服务

Technology sharing | sending requests using curl

Prometheus monitoring Nacos

技术分享 | 使用 cURL 发送请求
![[untitled]](/img/54/660667e528729cc87796d972dc0b17.png)
[untitled]

Compilation and preprocessing of C language

Installation and configuration of unit test framework jest with typescript

QGraphicsView提升为QChartView
随机推荐
进程和线程的区别
Code tidiness (I)
PKU-2739-Sum of Consecutive Prime Numbers(筛素数法打表)
HDU-1097-A hard puzzle(快速幂)
测试面试题集锦(五)| 自动化测试与性能测试篇(附答案)
等保3.0-服务器三权分立配置
About the collation of shader keyword
HDU-1159-CommonSubsequence(LCS最长公共子序列)
Project compilation nosuch*** error problem
Yapi vulnerability hanging horse program chongfu.sh processing
---栈&队列---
MySQL master master
网络——数据链路层
yapi漏洞挂马程序chongfu.sh处理
HDU-2036-改革春风吹满地(多边形面积模板)
redis缓存设计与性能优化
Question brushing record -- binary tree
OSI七层模型
Hdu-2036-reform spring breeze blowing all over the ground (polygon area template)
Hdu-5806-nanoapelovesequence Ⅱ (ruler method)