DDL
---- Data Definition Language database definition language such as create procedure
Create a database
CREATE DATABASE [IF NOT EXISTS] DBNAME [CHARACTER SET 'CHAR_NAME'] [COLLATE 'COLL_NAME']
Modify: ALTER deleted: DROP
DML
---- Data Manipulation Language Data Manipulation Language
Such as insert, delete, update, select (insert, delete, modify, retrieve) into modified data
mysql>INSERT INTO students (Name,Gender,teacher) VALUE ('lujunyi','M','mage'),('wusong','M','zhuima'); mysql>INSERT INTO students SET Name='lujunyi',Gender='M',tearcher='zhuima';
update data
mysql>UPDATE tb_name SET column=value WHERE column=value; mysql>UPDATE students SET Course='mysql' WHERE Name='lujunyi';
Replace data:
UPDATE and use, as long as you can REPLACE will replace UPDATE
delete data:
mysql>DELETE FROM tb_name WHERE conditions; mysql>DELETE FROM students WHERE Course='mysql';
Empty Table:
mysql>TRUNCATE tb_name
Query data
Single-table query:
mysql>SELECT [DISTINCT] column FROM tb_name WHERE CONDITION;
EXAMPLE:
# Basic projection inquiry
mysql>SELECT Name,teacher FROM students WHERE Name='wusong';
# Repeatable results show only once
mysql>SELECT DISTINCT Gender FROM students;
# Combination of conditions, use AND, OR, NOT, XOR combination of the plurality of conditions
mysql>SELECT * FROM students WHERE Age>20 AND Gender='M';
# Using BETWEEN ... AND ... filter out data between the ages of 20-25
mysql>SELECT * FROM students WHERE Age BETWEEN 20 AND 25;
# Name starts with a query of data Y,% represents any character of any length, represents any single character _
mysql>SELECT * FROM student WHERE Name LIKE 'Y%';
# Use regular expressions to match the query, or keywords for the RLINK REGEXP
mysql> SELECT * FROM students WHERE Name RLINK '^[MNY].*$';
# Using IN keyword, the condition is defined in a list. IS with keywords, indicating whether the condition is zero (IS NULL or IS NOT NULL)
mysql>SELECT * FROM students WHERE Age IN (20,22,24);
# The results of the query to sort
mysql>SELECT * FROM students ORDER BY Name {ASC|DESC};
# Alias display query results
mysql>SELECT Name AS Stu_Name FROM students;
After several #LIMIT defined query results, LIMIT 2,3 represents the offset data 2, data 3 taken
mysql>SELECT * FROM students LIMIT 2;
Averaging #: AVG (), maximum: MAX () minimum value MIN () Number: COUNT () sum: SUM ()
mysql>SELECT AVG(age) FROM students;
# GROUP BY grouping
mysql>SELECT Age, Gender FROM students GROUP BY Gender;
# Alias: AS
mysql>SELECT COUNT(Age) AS Num,Age FROM students GROUP BY Age;
# Filter: HAVING
mysql>SELECT COUNT(Age) AS Num,Age FROM students GROUP BY Age HAVING Num>2;
Multi-table query:
# Specify which fields have been connected to two tables
mysql>SELECT students.Name,courses.Cname FROM students,courses WHERE students.CID1 = courses.CID;
# Alias connection
mysql>SELECT students.Name,courses.Cname FROM students,courses WHERE students.CID1 = courses.CID;
# Left outer join ... LEFT JOIN ... ON ...
mysql>SELECT s.Name,c.Cname FROM students AS s LEFT JOIN courses AS c ON s.CID1=c.CID;
# Right outer join ... RIGHT JOIN ... ON ...
mysql>SELECT s.Name,c.Cname FROM students AS s RIGHT JOIN courses AS c ON s.CID1=c.CID;
Subqueries
# Query data older than the average age of
mysql>SELECT * FROM students WHERE Age > (SELECT AVG(Age) FROM students);
# Using subqueries in the FROM
mysql>SELECT Name,Age FROM (SELECT * FROM students WHERE CID IN (2,3)) AS t WHERE Age>20;
# Joint inquiry
mysql>(SELECT Name,Age FROM students) UNION (SELECT Tname,Age FROM tutors);
Create a view
CREATE VIEW VIEW_NAME AS SELECT....
DCL
---- Data Control Language database control language such as grant, deny, revoke, etc. Only administrators have permission to do so.
Create a user
mysql>CREATE USER 'USERNAME'@'HOST' IDENTIFIED BY 'PASSWORD'
delete users
MySQL > DROP the USER ' USERNAME ' @ ' HOSHOST support wildcards
_: Any single character
%: Any number of characters
Authorize
mysql>GRANT pri1,pri2...ON DB_NAME.TB_NAME TO 'USERNAME'@'HOST' [IDENTIFIED BY 'PASSWORD']
Deauthorize
mysql>REVOKE pri1,pri2...ON DB_NAME.TB_NAME FROM 'USERNAME'@'HOST';
Check Authorization
mysql>SHOW GRANTS FOR 'USERNAME'@'HOST';
EXAMPLE:
mysql>CREATE USER 'lujunyi'@'%' IDENTIFIED BY '123456'; mysql>SHOW GRANTS FOR 'lujunyi'@'%'; mysql>GRANT ALL PRIVILEGES ON testdb.* TO 'lujunyi'@'%';