Getting Started {.cols-2}
Connect MySQL
mysql -u <user> -p
mysql [db_name]
mysql -h <host> -P <port> -u <user> -p [db_name]
mysql -h <host> -u <user> -p [db_name]
Commons {.row-span-2}
Database
- | - |
---|---|
CREATE DATABASE db ; | Create database |
SHOW DATABASES; | List databases |
USE db; | Switch to db |
CONNECT db ; | Switch to db |
DROP DATABASE db; | Delete db |
Table
- | - |
---|---|
SHOW TABLES; | List tables for current db |
SHOW FIELDS FROM t; | List fields for a table |
DESC t; | Show table structure |
SHOW CREATE TABLE t; | Show create table sql |
TRUNCATE TABLE t; | Remove all data in a table |
DROP TABLE t; | Delete table |
Process
- | - |
---|---|
show processlist; | List processes |
kill pid; | kill process |
Other
- | - |
---|---|
exit or \q | Exit MySQL session |
Backups
Create a backup
mysqldump -u user -p db_name > db.sql
Export db without schema
mysqldump -u user -p db_name --no-data=true --add-drop-table=false > db.sql
Restore a backup
mysql -u user -p db_name < db.sql
MySQL Examples
Managing tables
Create a new table with three columns
CREATE TABLE t (
id INT,
name VARCHAR DEFAULT NOT NULL,
price INT DEFAULT 0
PRIMARY KEY(id)
);
Delete the table from the database
DROP TABLE t ;
Add a new column to the table
ALTER TABLE t ADD column;
Drop column c from the table
ALTER TABLE t DROP COLUMN c ;
Add a constraint
ALTER TABLE t ADD constraint;
Drop a constraint
ALTER TABLE t DROP constraint;
Rename a table from t1 to t2
ALTER TABLE t1 RENAME TO t2;
Rename column c1 to c2
ALTER TABLE t1 RENAME c1 TO c2 ;
Remove all data in a table
TRUNCATE TABLE t;
Querying data from a table
Query data in columns c1, c2 from a table
SELECT c1, c2 FROM t
Query all rows and columns from a table
SELECT * FROM t
Query data and filter rows with a condition
SELECT c1, c2 FROM t
WHERE condition
Query distinct rows from a table
SELECT DISTINCT c1 FROM t
WHERE condition
Sort the result set in ascending or descending order
SELECT c1, c2 FROM t
ORDER BY c1 ASC [DESC]
Skip offset of rows and return the next n rows
SELECT c1, c2 FROM t
ORDER BY c1
LIMIT n OFFSET offset
Group rows using an aggregate function
SELECT c1, aggregate(c2)
FROM t
GROUP BY c1
Filter groups using HAVING clause
SELECT c1, aggregate(c2)
FROM t
GROUP BY c1
HAVING condition
Querying from multiple tables {.row-span-2}
Inner join t1 and t2
SELECT c1, c2
FROM t1
INNER JOIN t2 ON condition
Left join t1 and t1
SELECT c1, c2
FROM t1
LEFT JOIN t2 ON condition
Right join t1 and t2
SELECT c1, c2
FROM t1
RIGHT JOIN t2 ON condition
Perform full outer join
SELECT c1, c2
FROM t1
FULL OUTER JOIN t2 ON condition
Produce a Cartesian product of rows in tables
SELECT c1, c2
FROM t1
CROSS JOIN t2
Another way to perform cross join
SELECT c1, c2
FROM t1, t2
Join t1 to itself using INNER JOIN clause
SELECT c1, c2
FROM t1 A
INNER JOIN t1 B ON condition
Using SQL Operators Combine rows from two queries
SELECT c1, c2 FROM t1
UNION [ALL]
SELECT c1, c2 FROM t2
Return the intersection of two queries
SELECT c1, c2 FROM t1
INTERSECT
SELECT c1, c2 FROM t2
Subtract a result set from another result set
SELECT c1, c2 FROM t1
MINUS
SELECT c1, c2 FROM t2
Query rows using pattern matching %, _
SELECT c1, c2 FROM t1
WHERE c1 [NOT] LIKE pattern
Query rows in a list
SELECT c1, c2 FROM t
WHERE c1 [NOT] IN value_list
Query rows between two values
SELECT c1, c2 FROM t
WHERE c1 BETWEEN low AND high
Check if values in a table is NULL or not
SELECT c1, c2 FROM t
WHERE c1 IS [NOT] NULL
Using SQL constraints
Set c1 and c2 as a primary key
CREATE TABLE t(
c1 INT, c2 INT, c3 VARCHAR,
PRIMARY KEY (c1,c2)
);
Set c2 column as a foreign key
CREATE TABLE t1(
c1 INT PRIMARY KEY,
c2 INT,
FOREIGN KEY (c2) REFERENCES t2(c2)
);
Make the values in c1 and c2 unique
CREATE TABLE t(
c1 INT, c1 INT,
UNIQUE(c2,c3)
);
Ensure c1 > 0 and values in c1 >= c2
CREATE TABLE t(
c1 INT, c2 INT,
CHECK(c1> 0 AND c1 >= c2)
);
Set values in c2 column not NULL
CREATE TABLE t(
c1 INT PRIMARY KEY,
c2 VARCHAR NOT NULL
);
Modifying Data
Insert one row into a table
INSERT INTO t(column_list)
VALUES(value_list);
Insert multiple rows into a table
INSERT INTO t(column_list)
VALUES (value_list),
(value_list), …;
Insert rows from t2 into t1
INSERT INTO t1(column_list)
SELECT column_list
FROM t2;
Update new value in the column c1 for all rows
UPDATE t
SET c1 = new_value;
Update values in the column c1, c2 that match the condition
UPDATE t
SET c1 = new_value,
c2 = new_value
WHERE condition;
Delete all data in a table
DELETE FROM t;
Delete subset of rows in a table
DELETE FROM t
WHERE condition;
Managing Views
Create a new view that consists of c1 and c2
CREATE VIEW v(c1,c2)
AS
SELECT c1, c2
FROM t;
Create a new view with check option
CREATE VIEW v(c1,c2)
AS
SELECT c1, c2
FROM t;
WITH [CASCADED | LOCAL] CHECK OPTION;
Create a recursive view
CREATE RECURSIVE VIEW v
AS
select-statement -- anchor part
UNION [ALL]
select-statement; -- recursive part
Create a temporary view
CREATE TEMPORARY VIEW v
AS
SELECT c1, c2
FROM t;
Delete a view
DROP VIEW view_name;
Managing triggers
Create or modify a trigger
CREATE OR MODIFY TRIGGER trigger_name
WHEN EVENT
ON table_name TRIGGER_TYPE
EXECUTE stored_procedure;
WHEN
- | - |
---|---|
BEFORE | invoke before the event occurs |
AFTER | invoke after the event occurs |
EVENT
- | - |
---|---|
INSERT | invoke for INSERT |
UPDATE | invoke for UPDATE |
DELETE | invoke for DELETE |
TRIGGER_TYPE
- | - |
---|---|
FOR EACH ROW | |
FOR EACH STATEMENT |
Managing indexes
Create an index on c1 and c2 of the t table
CREATE INDEX idx_name
ON t(c1,c2);
Create a unique index on c3, c4 of the t table
CREATE UNIQUE INDEX idx_name
ON t(c3,c4)
Drop an index
DROP INDEX idx_name;
MySQL Data Types
Strings
- | - |
---|---|
CHAR | String (0 - 255) |
VARCHAR | String (0 - 255) |
TINYTEXT | String (0 - 255) |
TEXT | String (0 - 65535) |
BLOB | String (0 - 65535) |
MEDIUMTEXT | String (0 - 16777215) |
MEDIUMBLOB | String (0 - 16777215) |
LONGTEXT | String (0 - 4294967295) |
LONGBLOB | String (0 - 4294967295) |
ENUM | One of preset options |
SET | Selection of preset options |
Date & time
Data Type | Format |
---|---|
DATE | yyyy-MM-dd |
TIME | hh:mm:ss |
DATETIME | yyyy-MM-dd hh:mm:ss |
TIMESTAMP | yyyy-MM-dd hh:mm:ss |
YEAR | yyyy |
Numeric
- | - |
---|---|
TINYINT x | Integer (-128 to 127) |
SMALLINT x | Integer (-32768 to 32767) |
MEDIUMINT x | Integer (-8388608 to 8388607) |
INT x | Integer (-2147483648 to 2147483647) |
BIGINT x | Integer (-9223372036854775808 to 9223372036854775807) |
FLOAT | Decimal (precise to 23 digits) |
DOUBLE | Decimal (24 to 53 digits) |
DECIMAL | "DOUBLE" stored as string |
MySQL Functions & Operators
Strings {.row-span-2}
- ASCII() (opens in a new tab){data-tooltip="Return numeric value of left-most character"}
- BIN() (opens in a new tab){data-tooltip="Return a string containing binary representation of a number"}
- BIT_LENGTH() (opens in a new tab){data-tooltip="Return length of argument in bits"}
- CHAR() (opens in a new tab){data-tooltip="Return the character for each integer passed"}
- CHARACTER_LENGTH() (opens in a new tab){data-tooltip="Synonym for CHAR_LENGTH()"}
- CHAR_LENGTH() (opens in a new tab){data-tooltip="Return number of characters in argument"}
- CONCAT() (opens in a new tab){data-tooltip="Return concatenated string"}
- CONCAT_WS() (opens in a new tab){data-tooltip="Return concatenate with separator"}
- ELT() (opens in a new tab){data-tooltip="Return string at index number"}
- EXPORT_SET() (opens in a new tab){data-tooltip="Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string"}
- FIELD() (opens in a new tab){data-tooltip="Index (position) of first argument in subsequent arguments"}
- FIND_IN_SET() (opens in a new tab){data-tooltip="Index (position) of first argument within second argument"}
- FORMAT() (opens in a new tab){data-tooltip="Return a number formatted to specified number of decimal places"}
- FROM_BASE64() (opens in a new tab){data-tooltip="Decode base64 encoded string and return result"}
- HEX() (opens in a new tab){data-tooltip="Hexadecimal representation of decimal or string value"}
- INSERT() (opens in a new tab){data-tooltip="Insert substring at specified position up to specified number of characters"}
- INSTR() (opens in a new tab){data-tooltip="Return the index of the first occurrence of substring"}
- LCASE() (opens in a new tab){data-tooltip="Synonym for LOWER()"}
- LEFT() (opens in a new tab){data-tooltip="Return the leftmost number of characters as specified"}
- LENGTH() (opens in a new tab){data-tooltip="Return the length of a string in bytes"}
- LIKE (opens in a new tab){data-tooltip="Simple pattern matching"}
- LOAD_FILE() (opens in a new tab){data-tooltip="Load the named file"}
- LOCATE() (opens in a new tab){data-tooltip="Return the position of the first occurrence of substring"}
- LOWER() (opens in a new tab){data-tooltip="Return the argument in lowercase"}
- LPAD() (opens in a new tab){data-tooltip="Return the string argument, left-padded with the specified string"}
- LTRIM() (opens in a new tab){data-tooltip="Remove leading spaces"}
- MAKE_SET() (opens in a new tab){data-tooltip="Return a set of comma-separated strings that have the corresponding bit in bits set"}
- MATCH (opens in a new tab){data-tooltip="Perform full-text search"}
- MID() (opens in a new tab){data-tooltip="Return a substring starting from the specified position"}
- NOT LIKE (opens in a new tab){data-tooltip="Negation of simple pattern matching"}
- NOT REGEXP (opens in a new tab){data-tooltip="Negation of REGEXP"}
- OCT() (opens in a new tab){data-tooltip="Return a string containing octal representation of a number"}
- OCTET_LENGTH() (opens in a new tab){data-tooltip="Synonym for LENGTH()"}
- ORD() (opens in a new tab){data-tooltip="Return character code for leftmost character of the argument"}
- POSITION() (opens in a new tab){data-tooltip="Synonym for LOCATE()"}
- QUOTE() (opens in a new tab){data-tooltip="Escape the argument for use in an SQL statement"}
- REGEXP (opens in a new tab){data-tooltip="Whether string matches regular expression"}
- REGEXP_INSTR() (opens in a new tab){data-tooltip="Starting index of substring matching regular expression"}
- REGEXP_LIKE() (opens in a new tab){data-tooltip="Whether string matches regular expression"}
- REGEXP_REPLACE() (opens in a new tab){data-tooltip="Replace substrings matching regular expression"}
- REGEXP_SUBSTR() (opens in a new tab){data-tooltip="Return substring matching regular expression"}
- REPEAT() (opens in a new tab){data-tooltip="Repeat a string the specified number of times"}
- REPLACE() (opens in a new tab){data-tooltip="Replace occurrences of a specified string"}
- REVERSE() (opens in a new tab){data-tooltip="Reverse the characters in a string"}
- RIGHT() (opens in a new tab){data-tooltip="Return the specified rightmost number of characters"}
- RLIKE (opens in a new tab){data-tooltip="Whether string matches regular expression"}
- RPAD() (opens in a new tab){data-tooltip="Append string the specified number of times"}
- RTRIM() (opens in a new tab){data-tooltip="Remove trailing spaces"}
- SOUNDEX() (opens in a new tab){data-tooltip="Return a soundex string"}
- SOUNDS LIKE (opens in a new tab){data-tooltip="Compare sounds"}
- SPACE() (opens in a new tab){data-tooltip="Return a string of the specified number of spaces"}
- STRCMP() (opens in a new tab){data-tooltip="Compare two strings"}
- SUBSTR() (opens in a new tab){data-tooltip="Return the substring as specified"}
- SUBSTRING() (opens in a new tab){data-tooltip="Return the substring as specified"}
- SUBSTRING_INDEX() (opens in a new tab){data-tooltip="Return a substring from a string before the specified number of occurrences of the delimiter"}
- TO_BASE64() (opens in a new tab){data-tooltip="Return the argument converted to a base-64 string"}
- TRIM() (opens in a new tab){data-tooltip="Remove leading and trailing spaces"}
- UCASE() (opens in a new tab){data-tooltip="Synonym for UPPER()"}
- UNHEX() (opens in a new tab){data-tooltip="Return a string containing hex representation of a number"}
- UPPER() (opens in a new tab){data-tooltip="Convert to uppercase"}
- WEIGHT_STRING() (opens in a new tab){data-tooltip="Return the weight string for a string"} {.cols-2}
Date and Time {.row-span-2}
- ADDDATE() (opens in a new tab){data-tooltip="Add time values (intervals) to a date value"}
- ADDTIME() (opens in a new tab){data-tooltip="Add time"}
- CONVERT_TZ() (opens in a new tab){data-tooltip="Convert from one time zone to another"}
- CURDATE() (opens in a new tab){data-tooltip="Return the current date"}
- CURRENT_DATE() (opens in a new tab){data-tooltip="Synonyms for CURDATE()"}
- CURRENT_TIME() (opens in a new tab){data-tooltip="Synonyms for CURTIME()"}
- CURRENT_TIMESTAMP() (opens in a new tab){data-tooltip="Synonyms for NOW()"}
- CURTIME() (opens in a new tab){data-tooltip="Return the current time"}
- DATE() (opens in a new tab){data-tooltip="Extract the date part of a date or datetime expression"}
- DATE_ADD() (opens in a new tab){data-tooltip="Add time values (intervals) to a date value"}
- DATE_FORMAT() (opens in a new tab){data-tooltip="Format date as specified"}
- DATE_SUB() (opens in a new tab){data-tooltip="Subtract a time value (interval) from a date"}
- DATEDIFF() (opens in a new tab){data-tooltip="Subtract two dates"}
- DAY() (opens in a new tab){data-tooltip="Synonym for DAYOFMONTH()"}
- DAYNAME() (opens in a new tab){data-tooltip="Return the name of the weekday"}
- DAYOFMONTH() (opens in a new tab){data-tooltip="Return the day of the month (0-31)"}
- DAYOFWEEK() (opens in a new tab){data-tooltip="Return the weekday index of the argument"}
- DAYOFYEAR() (opens in a new tab){data-tooltip="Return the day of the year (1-366)"}
- EXTRACT() (opens in a new tab){data-tooltip="Extract part of a date"}
- FROM_DAYS() (opens in a new tab){data-tooltip="Convert a day number to a date"}
- FROM_UNIXTIME() (opens in a new tab){data-tooltip="Format Unix timestamp as a date"}
- GET_FORMAT() (opens in a new tab){data-tooltip="Return a date format string"}
- HOUR() (opens in a new tab){data-tooltip="Extract the hour"}
- LAST_DAY (opens in a new tab){data-tooltip="Return the last day of the month for the argument"}
- LOCALTIME() (opens in a new tab){data-tooltip="Synonym for NOW()"}
- LOCALTIMESTAMP() (opens in a new tab){data-tooltip="Synonym for NOW()"}
- MAKEDATE() (opens in a new tab){data-tooltip="Create a date from the year and day of year"}
- MAKETIME() (opens in a new tab){data-tooltip="Create time from hour, minute, second"}
- MICROSECOND() (opens in a new tab){data-tooltip="Return the microseconds from argument"}
- MINUTE() (opens in a new tab){data-tooltip="Return the minute from the argument"}
- MONTH() (opens in a new tab){data-tooltip="Return the month from the date passed"}
- MONTHNAME() (opens in a new tab){data-tooltip="Return the name of the month"}
- NOW() (opens in a new tab){data-tooltip="Return the current date and time"}
- PERIOD_ADD() (opens in a new tab){data-tooltip="Add a period to a year-month"}
- PERIOD_DIFF() (opens in a new tab){data-tooltip="Return the number of months between periods"}
- QUARTER() (opens in a new tab){data-tooltip="Return the quarter from a date argument"}
- SEC_TO_TIME() (opens in a new tab){data-tooltip="Converts seconds to 'hh:mm:ss' format"}
- SECOND() (opens in a new tab){data-tooltip="Return the second (0-59)"}
- STR_TO_DATE() (opens in a new tab){data-tooltip="Convert a string to a date"}
- SUBDATE() (opens in a new tab){data-tooltip="Synonym for DATE_SUB() when invoked with three arguments"}
- SUBTIME() (opens in a new tab){data-tooltip="Subtract times"}
- SYSDATE() (opens in a new tab){data-tooltip="Return the time at which the function executes"}
- TIME() (opens in a new tab){data-tooltip="Extract the time portion of the expression passed"}
- TIME_FORMAT() (opens in a new tab){data-tooltip="Format as time"}
- TIME_TO_SEC() (opens in a new tab){data-tooltip="Return the argument converted to seconds"}
- TIMEDIFF() (opens in a new tab){data-tooltip="Subtract time"}
- TIMESTAMP() (opens in a new tab){data-tooltip="With a single argument, this function returns the date or datetime expression; with two arguments, the sum of the arguments"}
- TIMESTAMPADD() (opens in a new tab){data-tooltip="Add an interval to a datetime expression"}
- TIMESTAMPDIFF() (opens in a new tab){data-tooltip="Subtract an interval from a datetime expression"}
- TO_DAYS() (opens in a new tab){data-tooltip="Return the date argument converted to days"}
- TO_SECONDS() (opens in a new tab){data-tooltip="Return the date or datetime argument converted to seconds since Year 0"}
- UNIX_TIMESTAMP() (opens in a new tab){data-tooltip="Return a Unix timestamp"}
- UTC_DATE() (opens in a new tab){data-tooltip="Return the current UTC date"}
- UTC_TIME() (opens in a new tab){data-tooltip="Return the current UTC time"}
- UTC_TIMESTAMP() (opens in a new tab){data-tooltip="Return the current UTC date and time"}
- WEEK() (opens in a new tab){data-tooltip="Return the week number"}
- WEEKDAY() (opens in a new tab){data-tooltip="Return the weekday index"}
- WEEKOFYEAR() (opens in a new tab){data-tooltip="Return the calendar week of the date (1-53)"}
- YEAR() (opens in a new tab){data-tooltip="Return the year"}
- YEARWEEK() (opens in a new tab){data-tooltip="Return the year and week"}
- GET_FORMAT() (opens in a new tab){data-tooltip="'%m.%d.%Y'"} {.cols-2}
Numeric
- %, MOD (opens in a new tab){data-tooltip="Modulo operator"}
- * (opens in a new tab){data-tooltip="Multiplication operator"}
- + (opens in a new tab){data-tooltip="Addition operator"}
- - (opens in a new tab){data-tooltip="Minus operator"}
- - (opens in a new tab){data-tooltip="Change the sign of the argument"}
- / (opens in a new tab){data-tooltip="Division operator"}
- ABS() (opens in a new tab){data-tooltip="Return the absolute value"}
- ACOS() (opens in a new tab){data-tooltip="Return the arc cosine"}
- ASIN() (opens in a new tab){data-tooltip="Return the arc sine"}
- ATAN() (opens in a new tab){data-tooltip="Return the arc tangent"}
- ATAN2(), ATAN() (opens in a new tab){data-tooltip="Return the arc tangent of the two arguments"}
- CEIL() (opens in a new tab){data-tooltip="Return the smallest integer value not less than the argument"}
- CEILING() (opens in a new tab){data-tooltip="Return the smallest integer value not less than the argument"}
- CONV() (opens in a new tab){data-tooltip="Convert numbers between different number bases"}
- COS() (opens in a new tab){data-tooltip="Return the cosine"}
- COT() (opens in a new tab){data-tooltip="Return the cotangent"}
- CRC32() (opens in a new tab){data-tooltip="Compute a cyclic redundancy check value"}
- DEGREES() (opens in a new tab){data-tooltip="Convert radians to degrees"}
- DIV (opens in a new tab){data-tooltip="Integer division"}
- EXP() (opens in a new tab){data-tooltip="Raise to the power of"}
- FLOOR() (opens in a new tab){data-tooltip="Return the largest integer value not greater than the argument"}
- LN() (opens in a new tab){data-tooltip="Return the natural logarithm of the argument"}
- LOG() (opens in a new tab){data-tooltip="Return the natural logarithm of the first argument"}
- LOG10() (opens in a new tab){data-tooltip="Return the base-10 logarithm of the argument"}
- LOG2() (opens in a new tab){data-tooltip="Return the base-2 logarithm of the argument"}
- MOD() (opens in a new tab){data-tooltip="Return the remainder"}
- PI() (opens in a new tab){data-tooltip="Return the value of pi"}
- POW() (opens in a new tab){data-tooltip="Return the argument raised to the specified power"}
- POWER() (opens in a new tab){data-tooltip="Return the argument raised to the specified power"}
- RADIANS() (opens in a new tab){data-tooltip="Return argument converted to radians"}
- RAND() (opens in a new tab){data-tooltip="Return a random floating-point value"}
- ROUND() (opens in a new tab){data-tooltip="Round the argument"}
- SIGN() (opens in a new tab){data-tooltip="Return the sign of the argument"}
- SIN() (opens in a new tab){data-tooltip="Return the sine of the argument"}
- SQRT() (opens in a new tab){data-tooltip="Return the square root of the argument"}
- TAN() (opens in a new tab){data-tooltip="Return the tangent of the argument"}
- TRUNCATE() (opens in a new tab){data-tooltip="Truncate to specified number of decimal places"} {.cols-2}
Aggregate
- AVG() (opens in a new tab){data-tooltip="Return the average value of the argument"}
- BIT_AND() (opens in a new tab){data-tooltip="Return bitwise AND"}
- BIT_OR() (opens in a new tab){data-tooltip="Return bitwise OR"}
- BIT_XOR() (opens in a new tab){data-tooltip="Return bitwise XOR"}
- COUNT() (opens in a new tab){data-tooltip="Return a count of the number of rows returned"}
- COUNT(DISTINCT) (opens in a new tab){data-tooltip="Return the count of a number of different values"}
- GROUP_CONCAT() (opens in a new tab){data-tooltip="Return a concatenated string"}
- JSON_ARRAYAGG() (opens in a new tab){data-tooltip="Return result set as a single JSON array"}
- JSON_OBJECTAGG() (opens in a new tab){data-tooltip="Return result set as a single JSON object"}
- MAX() (opens in a new tab){data-tooltip="Return the maximum value"}
- MIN() (opens in a new tab){data-tooltip="Return the minimum value"}
- STD() (opens in a new tab){data-tooltip="Return the population standard deviation"}
- STDDEV() (opens in a new tab){data-tooltip="Return the population standard deviation"}
- STDDEV_POP() (opens in a new tab){data-tooltip="Return the population standard deviation"}
- STDDEV_SAMP() (opens in a new tab){data-tooltip="Return the sample standard deviation"}
- SUM() (opens in a new tab){data-tooltip="Return the sum"}
- VAR_POP() (opens in a new tab){data-tooltip="Return the population standard variance"}
- VAR_SAMP() (opens in a new tab){data-tooltip="Return the sample variance"}
- VARIANCE() (opens in a new tab){data-tooltip="Return the population standard variance"} {.cols-2}
JSON {.row-span-4}
- -> (opens in a new tab){data-tooltip="Return value from JSON column after evaluating path; equivalent to JSON_EXTRACT()."}
- ->> (opens in a new tab){data-tooltip="Return value from JSON column after evaluating path and unquoting the result; equivalent to JSON_UNQUOTE(JSON_EXTRACT())."}
- JSON_ARRAY() (opens in a new tab){data-tooltip="Create JSON array"}
- JSON_ARRAY_APPEND() (opens in a new tab){data-tooltip="Append data to JSON document"}
- JSON_ARRAY_INSERT() (opens in a new tab){data-tooltip="Insert into JSON array"}
- JSON_CONTAINS() (opens in a new tab){data-tooltip="Whether JSON document contains specific object at path"}
- JSON_CONTAINS_PATH() (opens in a new tab){data-tooltip="Whether JSON document contains any data at path"}
- JSON_DEPTH() (opens in a new tab){data-tooltip="Maximum depth of JSON document"}
- JSON_EXTRACT() (opens in a new tab){data-tooltip="Return data from JSON document"}
- JSON_INSERT() (opens in a new tab){data-tooltip="Insert data into JSON document"}
- JSON_KEYS() (opens in a new tab){data-tooltip="Array of keys from JSON document"}
- JSON_LENGTH() (opens in a new tab){data-tooltip="Number of elements in JSON document"}
- JSON_MERGE() (deprecated) (opens in a new tab){data-tooltip="Merge JSON documents, preserving duplicate keys. Deprecated synonym for JSON_MERGE_PRESERVE()"}
- JSON_MERGE_PATCH() (opens in a new tab){data-tooltip="Merge JSON documents, replacing values of duplicate keys"}
- JSON_MERGE_PRESERVE() (opens in a new tab){data-tooltip="Merge JSON documents, preserving duplicate keys"}
- JSON_OBJECT() (opens in a new tab){data-tooltip="Create JSON object"}
- JSON_OVERLAPS() (introduced 8.0.17) (opens in a new tab){data-tooltip="Compares two JSON documents, returns TRUE (1) if these have any key-value pairs or array elements in common, otherwise FALSE (0)"}
- JSON_PRETTY() (opens in a new tab){data-tooltip="Print a JSON document in human-readable format"}
- JSON_QUOTE() (opens in a new tab){data-tooltip="Quote JSON document"}
- JSON_REMOVE() (opens in a new tab){data-tooltip="Remove data from JSON document"}
- JSON_REPLACE() (opens in a new tab){data-tooltip="Replace values in JSON document"}
- JSON_SCHEMA_VALID() (introduced 8.0.17) (opens in a new tab){data-tooltip="Validate JSON document against JSON schema; returns TRUE/1 if document validates against schema, or FALSE/0 if it does not"}
- JSON_SCHEMA_VALIDATION_REPORT() (introduced 8.0.17) (opens in a new tab){data-tooltip="Validate JSON document against JSON schema; returns report in JSON format on outcome on validation including success or failure and reasons for failure"}
- JSON_SEARCH() (opens in a new tab){data-tooltip="Path to value within JSON document"}
- JSON_SET() (opens in a new tab){data-tooltip="Insert data into JSON document"}
- JSON_STORAGE_FREE() (opens in a new tab){data-tooltip="Freed space within binary representation of JSON column value following partial update"}
- JSON_STORAGE_SIZE() (opens in a new tab){data-tooltip="Space used for storage of binary representation of a JSON document"}
- JSON_TABLE() (opens in a new tab){data-tooltip="Return data from a JSON expression as a relational table"}
- JSON_TYPE() (opens in a new tab){data-tooltip="Type of JSON value"}
- JSON_UNQUOTE() (opens in a new tab){data-tooltip="Unquote JSON value"}
- JSON_VALID() (opens in a new tab){data-tooltip="Whether JSON value is valid"}
- JSON_VALUE() (introduced 8.0.21) (opens in a new tab){data-tooltip="Extract value from JSON document at location pointed to by path provided; return this value as VARCHAR(512) or specified type"}
- MEMBER OF() (introduced 8.0.17) (opens in a new tab){data-tooltip="Returns true (1) if first operand matches any element of JSON array passed as second operand, otherwise returns false (0)"} {.cols-1}
Cast
- BINARY (opens in a new tab){data-tooltip="Cast a string to a binary string"}
- CAST() (opens in a new tab){data-tooltip="Cast a value as a certain type"}
- CONVERT() (opens in a new tab){data-tooltip="Cast a value as a certain type"} {.cols-2}
Flow Control
- CASE (opens in a new tab){data-tooltip="Case operator"}
- IF() (opens in a new tab){data-tooltip="If/else construct"}
- IFNULL() (opens in a new tab){data-tooltip="Null if/else construct"}
- NULLIF() (opens in a new tab){data-tooltip="Return NULL if expr1 = expr2"} {.cols-2}
Information
- BENCHMARK() (opens in a new tab){data-tooltip="Repeatedly execute an expression"}
- CHARSET() (opens in a new tab){data-tooltip="Return the character set of the argument"}
- COERCIBILITY() (opens in a new tab){data-tooltip="Return the collation coercibility value of the string argument"}
- COLLATION() (opens in a new tab){data-tooltip="Return the collation of the string argument"}
- CONNECTION_ID() (opens in a new tab){data-tooltip="Return the connection ID (thread ID) for the connection"}
- CURRENT_ROLE() (opens in a new tab){data-tooltip="Return the current active roles"}
- CURRENT_USER() (opens in a new tab){data-tooltip="The authenticated user name and host name"}
- DATABASE() (opens in a new tab){data-tooltip="Return the default (current) database name"}
- FOUND_ROWS() (opens in a new tab){data-tooltip="For a SELECT with a LIMIT clause, the number of rows that would be returned were there no LIMIT clause"}
- ICU_VERSION() (opens in a new tab){data-tooltip="ICU library version"}
- LAST_INSERT_ID() (opens in a new tab){data-tooltip="Value of the AUTOINCREMENT column for the last INSERT"}
- ROLES_GRAPHML() (opens in a new tab){data-tooltip="Return a GraphML document representing memory role subgraphs"}
- ROW_COUNT() (opens in a new tab){data-tooltip="The number of rows updated"}
- SCHEMA() (opens in a new tab){data-tooltip="Synonym for DATABASE()"}
- SESSION_USER() (opens in a new tab){data-tooltip="Synonym for USER()"}
- SYSTEM_USER() (opens in a new tab){data-tooltip="Synonym for USER()"}
- USER() (opens in a new tab){data-tooltip="The user name and host name provided by the client"}
- VERSION() (opens in a new tab){data-tooltip="Return a string that indicates the MySQL server version"} {.cols-2}
Encryption and Compression
- AES_DECRYPT() (opens in a new tab){data-tooltip="Decrypt using AES"}
- AES_ENCRYPT() (opens in a new tab){data-tooltip="Encrypt using AES"}
- COMPRESS() (opens in a new tab){data-tooltip="Return result as a binary string"}
- MD5() (opens in a new tab){data-tooltip="Calculate MD5 checksum"}
- RANDOM_BYTES() (opens in a new tab){data-tooltip="Return a random byte vector"}
- SHA1(), SHA() (opens in a new tab){data-tooltip="Calculate an SHA-1 160-bit checksum"}
- SHA2() (opens in a new tab){data-tooltip="Calculate an SHA-2 checksum"}
- STATEMENT_DIGEST() (opens in a new tab){data-tooltip="Compute statement digest hash value"}
- STATEMENT_DIGEST_TEXT() (opens in a new tab){data-tooltip="Compute normalized statement digest"}
- UNCOMPRESS() (opens in a new tab){data-tooltip="Uncompress a string compressed"}
- UNCOMPRESSED_LENGTH() (opens in a new tab){data-tooltip="Return the length of a string before compression"}
- VALIDATE_PASSWORD_STRENGTH() (opens in a new tab){data-tooltip="Determine strength of password"} {.cols-1}
Locking
- GET_LOCK() (opens in a new tab){data-tooltip="Get a named lock"}
- IS_FREE_LOCK() (opens in a new tab){data-tooltip="Whether the named lock is free"}
- IS_USED_LOCK() (opens in a new tab){data-tooltip="Whether the named lock is in use; return connection identifier if true"}
- RELEASE_ALL_LOCKS() (opens in a new tab){data-tooltip="Release all current named locks"}
- RELEASE_LOCK() (opens in a new tab){data-tooltip="Release the named lock"} {.cols-1}
Bit
- & (opens in a new tab){data-tooltip="Bitwise AND"}
- >> (opens in a new tab){data-tooltip="Right shift"}
- << (opens in a new tab){data-tooltip="Left shift"}
- ^ (opens in a new tab){data-tooltip="Bitwise XOR"}
- BIT_COUNT() (opens in a new tab){data-tooltip="Return the number of bits that are set"}
- | (opens in a new tab){data-tooltip="Bitwise OR"}
- ~ (opens in a new tab){data-tooltip="Bitwise inversion"} {.cols-2}
Miscellaneous
- ANY_VALUE() (opens in a new tab){data-tooltip="Suppress ONLY_FULL_GROUP_BY value rejection"}
- BIN_TO_UUID() (opens in a new tab){data-tooltip="Convert binary UUID to string"}
- DEFAULT() (opens in a new tab){data-tooltip="Return the default value for a table column"}
- GROUPING() (opens in a new tab){data-tooltip="Distinguish super-aggregate ROLLUP rows from regular rows"}
- INET_ATON() (opens in a new tab){data-tooltip="Return the numeric value of an IP address"}
- INET_NTOA() (opens in a new tab){data-tooltip="Return the IP address from a numeric value"}
- INET6_ATON() (opens in a new tab){data-tooltip="Return the numeric value of an IPv6 address"}
- INET6_NTOA() (opens in a new tab){data-tooltip="Return the IPv6 address from a numeric value"}
- IS_IPV4() (opens in a new tab){data-tooltip="Whether argument is an IPv4 address"}
- IS_IPV4_COMPAT() (opens in a new tab){data-tooltip="Whether argument is an IPv4-compatible address"}
- IS_IPV4_MAPPED() (opens in a new tab){data-tooltip="Whether argument is an IPv4-mapped address"}
- IS_IPV6() (opens in a new tab){data-tooltip="Whether argument is an IPv6 address"}
- IS_UUID() (opens in a new tab){data-tooltip="Whether argument is a valid UUID"}
- MASTER_POS_WAIT() (opens in a new tab){data-tooltip="Block until the replica has read and applied all updates up to the specified position"}
- NAME_CONST() (opens in a new tab){data-tooltip="Cause the column to have the given name"}
- SLEEP() (opens in a new tab){data-tooltip="Sleep for a number of seconds"}
- UUID() (opens in a new tab){data-tooltip="Return a Universal Unique Identifier (UUID)"}
- UUID_SHORT() (opens in a new tab){data-tooltip="Return an integer-valued universal identifier"}
- UUID_TO_BIN() (opens in a new tab){data-tooltip="Convert string UUID to binary"}
- VALUES() (opens in a new tab){data-tooltip="Define the values to be used during an INSERT"} {.cols-2}
Also see {.cols-1}
- Regex in MySQL (cheatsheets.zip)