MySQL_cheatsheet
MySQL_cheatsheet
The SQL cheat sheet provides you with the most commonly used SQL statements for your reference.
# Getting Started
Connect MySQL - Commons
Database
mysql -u <user> -p
CREATE DATABASE db ; Create database
mysql [db_name]
SHOW DATABASES; List databases
mysql -h <host> -P <port> -u <user> -p [db_name]
USE db; Switch to db
mysql -h <host> -u <user> -p [db_name]
CONNECT db ; Switch to db
Table
Other
# MySQL Examples
- Managing tables - Querying data from a table - Querying from multiple tables
Create a new table with three columns Query data in columns c1, c2 from a table Inner join t1 and t2
ALTER TABLE t ADD constraint; Skip offset of rows and return the next n rows
Produce a Cartesian product of rows in tables
SELECT c1, c2
Remove all data in a table SELECT c1, aggregate(c2)
FROM t1 A
FROM t
INNER JOIN t1 B ON condition
GROUP BY c1
TRUNCATE TABLE t;
HAVING condition
Using SQL Operators Combine rows from two queries
DELETE FROM t
WHERE condition;
Create a new view that consists of c1 and c2 Create or modify a trigger Create an index on c1 and c2 of the t table
CREATE VIEW v(c1,c2) CREATE OR MODIFY TRIGGER trigger_name CREATE INDEX idx_name
AS WHEN EVENT ON t(c1,c2);
SELECT c1, c2 ON table_name TRIGGER_TYPE
FROM t; EXECUTE stored_procedure; Create a unique index on c3, c4 of the t table
WHEN
Create a new view with check option CREATE UNIQUE INDEX idx_name
BEFORE invoke before the event occurs ON t(c3,c4)
CREATE VIEW v(c1,c2)
AS AFTER invoke after the event occurs
Drop an index
SELECT c1, c2
EVENT
FROM t;
DROP INDEX idx_name ON t;
WITH [CASCADED | LOCAL] CHECK OPTION; INSERT invoke for INSERT
Delete a view
TINYTEXT String (0 - 255) DATETIME yyyy-MM-dd hh:mm:ss MEDIUMINT x Integer (-8388608 to 8388607)
TEXT String (0 - 65535) TIMESTAMP yyyy-MM-dd hh:mm:ss INT x Integer (-2147483648 to 2147483647)
Aggregate
REGEXP_SUBSTR() REPEAT() SUBTIME() SYSDATE()
JSON_ARRAY()
Information Encryption and Compression
JSON_ARRAY_APPEND()
BENCHMARK() CHARSET() AES_DECRYPT()
JSON_ARRAY_INSERT()
COERCIBILITY() COLLATION() AES_ENCRYPT()
JSON_CONTAINS()
CONNECTION_ID() CURRENT_ROLE() COMPRESS()
JSON_CONTAINS_PATH()
CURRENT_USER() DATABASE() MD5()
JSON_DEPTH()
FOUND_ROWS() ICU_VERSION() RANDOM_BYTES()
JSON_EXTRACT()
LAST_INSERT_ID() ROLES_GRAPHML() SHA1(), SHA()
JSON_INSERT()
ROW_COUNT() SCHEMA() SHA2()
JSON_KEYS()
SESSION_USER() SYSTEM_USER() STATEMENT_DIGEST()
JSON_LENGTH()
USER() VERSION() STATEMENT_DIGEST_TEXT()
JSON_MERGE() (deprecated)
UNCOMPRESS()
JSON_MERGE_PATCH()
UNCOMPRESSED_LENGTH()
JSON_MERGE_PRESERVE()
VALIDATE_PASSWORD_STRENGTH()
JSON_OBJECT()
JSON_PRETTY()
GET_LOCK() & >>
JSON_QUOTE()
IS_FREE_LOCK() << ^
JSON_REMOVE()
IS_USED_LOCK() BIT_COUNT() |
JSON_REPLACE()
RELEASE_ALL_LOCKS() ~
JSON_SCHEMA_VALID() (introduced 8.0.17)
RELEASE_LOCK()
JSON_SCHEMA_VALIDATION_REPORT() (introduced
8.0.17)
Miscellaneous
JSON_SEARCH()
ANY_VALUE() BIN_TO_UUID()
JSON_SET()
DEFAULT() GROUPING()
JSON_STORAGE_FREE()
INET_ATON() INET_NTOA()
JSON_STORAGE_SIZE()
INET6_ATON() INET6_NTOA()
JSON_TABLE()
IS_IPV4() IS_IPV4_COMPAT()
JSON_TYPE()
IS_IPV4_MAPPED() IS_IPV6()
JSON_UNQUOTE()
IS_UUID() MASTER_POS_WAIT()
JSON_VALID()
NAME_CONST() SLEEP()
JSON_VALUE() (introduced 8.0.21)
UUID() UUID_SHORT()
MEMBER OF() (introduced 8.0.17)
UUID_TO_BIN() VALUES()
# Also see