· 7 years ago · Nov 25, 2018, 05:40 PM
1-- Database-Level
2DROP DATABASE databaseName -- Delete the database (irrecoverable!)
3DROP DATABASE IF EXISTS databaseName -- Delete if it exists
4CREATE DATABASE databaseName -- Create a new database
5CREATE DATABASE IF NOT EXISTS databaseName -- Create only if it does not exists
6SHOW DATABASES -- Show all the databases in this server
7USE databaseName -- Set the default (current) database
8SELECT DATABASE() -- Show the default database
9SHOW CREATE DATABASE databaseName -- Show the CREATE DATABASE statement
10
11-- Table-Level
12DROP TABLE [IF EXISTS] tableName, ...
13CREATE TABLE [IF NOT EXISTS] tableName (
14 columnName columnType columnAttribute, ...
15 PRIMARY KEY(columnName),
16 FOREIGN KEY (columnNmae) REFERENCES tableName (columnNmae)
17)
18SHOW TABLES -- Show all the tables in the default database
19DESCRIBE|DESC tableName -- Describe the details for a table
20ALTER TABLE tableName ... -- Modify a table, e.g., ADD COLUMN and DROP COLUMN
21ALTER TABLE tableName ADD columnDefinition
22ALTER TABLE tableName DROP columnName
23ALTER TABLE tableName ADD FOREIGN KEY (columnNmae) REFERENCES tableName (columnNmae)
24ALTER TABLE tableName DROP FOREIGN KEY constraintName
25SHOW CREATE TABLE tableName -- Show the CREATE TABLE statement for this tableName
26
27-- Row-Level
28INSERT INTO tableName
29 VALUES (column1Value, column2Value,...) -- Insert on all Columns
30INSERT INTO tableName
31 VALUES (column1Value, column2Value,...), ... -- Insert multiple rows
32INSERT INTO tableName (column1Name, ..., columnNName)
33 VALUES (column1Value, ..., columnNValue) -- Insert on selected Columns
34DELETE FROM tableName WHERE criteria
35UPDATE tableName SET columnName = expr, ... WHERE criteria
36SELECT * | column1Name AS alias1, ..., columnNName AS aliasN
37 FROM tableName
38 WHERE criteria
39 GROUP BY columnName
40 ORDER BY columnName ASC|DESC, ...
41 HAVING groupConstraints
42 LIMIT count | offset count
43
44-- Others
45SHOW WARNINGS; -- Show the warnings of the previous statement