My sql cheat sheet


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

My sql cheat sheet

  1. 1. Column Types STRING COMPARISON FUNCTIONS SELECT[NATIONAL] CHAR(M) [BINARY] LONGBLOB expr LIKE pat [ESCAPE escape-char] SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT][NATIONAL] VARCHAR(M) [BINARY] LONGTEXT expr NOT LIKE pat [ESCAPE escape-char] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE]BIGINT[(M)] [UNSIGNED] [ZEROFILL] MEDIUMBLOB expr NOT REGEXP pat [SQL_CALC_FOUND_ROWS] [HIGH_PRIORITY]BIT MEDIUMINT[(M)] [UNSIGNED] expr NOT RLIKE pat [DISTINCT | DISTINCTROW | ALL]BLOB [ZEROFILL] expr REGEXP pat select_expression,...BOOL MEDIUMTEXT expr RLIKE pat [INTO {OUTFILE | DUMPFILE} file_name export_options]CHAR NUMERIC(M,D) [ZEROFILL] MATCH (col1,col2,...) AGAINST (expr IN BOOLEAN MODE) [FROM table_referencesDATE REAL[(M,D)] [ZEROFILL] STRCMP() returns 0 if the strings are the same, -1 if the MATCH [WHERE where_definition]DATETIME SET(value1,value2,...) (col1,col2,...) AGAINST (expr) [GROUP BY {unsigned_integer | col_name | formula} [ASC | DESC],DECIMAL[(M[,D])] [ZEROFILL] SMALLINT[(M)] [UNSIGNED] STRCMP(expr1,expr2) ...]DOUBLE PRECISION[(M,D)] [ZEROFILL] [HAVING where_definition] [ZEROFILL] TEXT ARITHMETIC OPERATIONS [ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC]DOUBLE[(M,D)] [ZEROFILL] TIME + - * / ,...]ENUM(value1,value2,...) TIMESTAMP[(M)] [LIMIT [offset,] rows]FLOAT(precision) [ZEROFILL] TINYBLOB DATE AND TIME FUNCTIONS [PROCEDURE procedure_name]FLOAT[(M,D)] [ZEROFILL] TINYINT[(M)] [UNSIGNED] [FOR UPDATE | LOCK IN SHARE MODE]]INT[(M)] [UNSIGNED] [ZEROFILL] [ZEROFILL] ADDDATE(date,INTERVAL expr type) MONTHNAME(date)INTEGER[(M)] [UNSIGNED] TINYTEXT CURDATE() NOW() JOIN [ZEROFILL] YEAR[(2|4)] CURRENT_DATE PERIOD_ADD(P,N) CURRENT_TIME PERIOD_DIFF(P1,P2) table_reference, table_reference CURRENT_TIMESTAMP QUARTER(date) table_reference [CROSS] JOIN table_reference FUNCTIONS TO USE IN SELECT AND WHERE CLAUSES table_reference INNER JOIN table_reference join_condition CURTIME() SEC_TO_TIME(seconds) DATE_ADD(date,INTERVAL expr type) SECOND(time) table_reference STRAIGHT_JOIN table_referenceCOMPARISON OPERATORS table_reference LEFT [OUTER] JOIN table_reference join_condition DATE_FORMAT(date,format) SUBDATE(date,INTERVAL expr type) = <> != <= < >= > <=> DATE_SUB(date,INTERVAL expr type) SYSDATE() table_reference LEFT [OUTER] JOIN table_reference DAYNAME(date) TIME_FORMAT(time,format) table_reference NATURAL [LEFT [OUTER]] JOIN table_referenceCOALESCE(list) expr NOT IN IS NOT NULL DAYOFMONTH(date) TIME_TO_SEC(time) { oj table_reference LEFT OUTER JOIN table_reference ONexpr BETWEEN min AND (value,...) IS NULL DAYOFWEEK(date) TO_DAYS(date) conditional_expr } max INTERVAL(N,N1,N2,N3,.. ISNULL(expr) DAYOFYEAR(date) UNIX_TIMESTAMP() table_reference RIGHT [OUTER] JOIN table_reference join_conditionexpr IN (value,...) .) EXTRACT(type FROM date) UNIX_TIMESTAMP(date) table_reference RIGHT [OUTER] JOIN table_reference FROM_DAYS(N) WEEK(date) table_reference NATURAL [RIGHT [OUTER]] JOIN table_referenceLOGICAL OPERATORS FROM_UNIXTIME(unix_timestamp) WEEK(date,first) HANDLERAND (&&) NOT (!) OR (||) FROM_UNIXTIME(unix_timestamp,form WEEKDAY(date) at) YEAR(date) HANDLER table OPEN [ AS alias ]CONTROL FLOW FUNCTIONS HOUR(time) YEARWEEK(date) HANDLER table READ index { = | >= | <= | < } (value1, value2, ... )CASE value WHEN [compare-value] THEN result [WHEN [compare-value] MINUTE(time) YEARWEEK(date,first) [ WHERE ... ] [LIMIT ... ] THEN result ...] [ELSE result] END MONTH(date) HANDLER table READ index { FIRST | NEXT | PREV | LAST } [ WHERE ... ] [LIMIT ... ]CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] CAST FUNCTIONS HANDLER table READ { FIRST | NEXT } [ WHERE ... ] [LIMIT ... ] [ELSE result] ENDIF(expr1,expr2,expr3) CAST(expression AS type) CONVERT(expression,type) HANDLER table CLOSEIFNULL(expr1,expr2) BIT FUNCTIONS UPDATENULLIF(expr1,expr2) | & << >> ~ BIT_COUNT(N) UPDATE [LOW_PRIORITY] [IGNORE] tbl_nameSTRING FUNCTIONS SET col_name1=expr1, [col_name2=expr2, ...] MISCELLANEOUS FUNCTIONS [WHERE where_definition]ASCII(str) LPAD(str,len,padstr)BIN(N) LTRIM(str) BENCHMARK(count,expr) GET_LOCK(str,timeout) [LIMIT #]BIT_LENGTH(str) MAKE_SET(bits,str1,str2,...) CONNECTION_ID() INET_ATON(expr) DELETECHAR(N,...) MID(str,pos,len) DATABASE() INET_NTOA(expr)CHAR_LENGTH(str) OCT(N) DECODE(crypt_str,pass_str) LAST_INSERT_ID([expr]) DELETE [LOW_PRIORITY | QUICK] FROM table_nameCHARACTER_LENGTH(str) OCTET_LENGTH(str) des_decrypt(string_to_decrypt [, MASTER_POS_WAIT(log_name, [WHERE where_definition]CONCAT(str1,str2,...) ORD(str) key_string]) log_pos) [ORDER BY ...]CONCAT_WS(separator, str1, POSITION(substr IN str) des_encrypt(string_to_encrypt, flag, MD5(string) [LIMIT rows] str2,...) REPEAT(str,count) [, (key_number | key_string) ] ) PASSWORD(str) DELETE [LOW_PRIORITY | QUICK] table_name[.*] [,table_name[.*] ...]CONV(N,from_base,to_base) REPLACE(str,from_str,to_str) ENCODE(str,pass_str) RELEASE_LOCK(str) FROM table-references [WHERE where_definition]ELT(N,str1,str2,str3,...) REVERSE(str) ENCRYPT(str[,salt]) SESSION_USER() DELETE [LOW_PRIORITY | QUICK]EXPORT_SET(bits,on,off,[separator RIGHT(str,len) FORMAT(X,D) SYSTEM_USER() FROM table_name[.*], [table_name[.*] ...] ,[number_of_bits]]) RPAD(str,len,padstr) FOUND_ROWS() USER() USING table-references [WHERE where_definition]FIELD(str,str1,str2,str3,...) RTRIM(str) TRUNCATEFIND_IN_SET(str,strlist) SOUNDEX(str) FUNCTONS FOR USE WITH GROUP BY CLAUSESHEX(N_or_S) SPACE(N) COUNT(expr) AVG(expr) STD(expr) TRUNCATE TABLE table_nameINSERT(str,pos,len,newstr) SUBSTRING(str FROM pos FOR len) COUNT(DISTINCT MIN(expr) STDDEV(expr)INSTR(str,substr) SUBSTRING(str FROM pos) REPLACE expr,[expr...]) MAX(expr) BIT_OR(expr)LCASE(str) SUBSTRING(str,pos) SUM(expr) BIT_AND(expr) REPLACE [LOW_PRIORITY | DELAYED]LEFT(str,len) SUBSTRING(str,pos,len) [INTO] tbl_name [(col_name,...)]LENGTH(str) SUBSTRING_INDEX(str,delim,count) DATA MANIPULATION LANGUAGE VALUES (expression,...),(...),...LOAD_FILE(file_name) TRIM([[BOTH | LEADING | TRAILING] REPLACE [LOW_PRIORITY | DELAYED]LOCATE(substr,str) [remstr] FROM] str) INSERT [INTO] tbl_name [(col_name,...)]LOCATE(substr,str,pos) UCASE(str) SELECT ...LOWER(str) UPPER(str) INSERT [LOW_PRIORITY | DELAYED] [IGNORE] REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name [(col_name,...)] [INTO] tbl_nameMATHEMETICAL FUNCTIONS VALUES (expression,...),(...),... SET col_name=expression, col_name=expression,...- COS(X) LOG10(X) ROUND(X) INSERT [LOW_PRIORITY | DELAYED] [IGNORE]ABS(X) COT(X) MOD(N,M) ROUND(X,D) [INTO] tbl_name [(col_name,...)] UNIONACOS(X) DEGREES(X) PI() SIGN(X) SELECT ... INSERT [LOW_PRIORITY | DELAYED] [IGNORE] SELECT ....ASIN(X) EXP(X) POW(X,Y) SIN(X) SELECT ....ATAN(X) FLOOR(X) POWER(X,Y) SQRT(X) [INTO] tbl_name SET col_name=expression, col_name=expression, ... [UNION SELECT ...]ATAN(Y,X) GREATEST(X,Y,...) RADIANS(X) TAN(X) UNION [ALL] INSERT [LOW_PRIORITY] [IGNORE] [INTO] tbl_name [(column list)]ATAN2(Y,X) LEAST(X,Y,...) RAND() TRUNCATE(X,D)CEILING(X) LOG(X) RAND(N) SELECT ... INSERT DELAYED ...
  2. 2. LOAD DATA INFILE create_definition:LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE file_name.txt { col_name type [NOT NULL | NULL] [DEFAULT default_value] [REPLACE | IGNORE] [AUTO_INCREMENT] [PRIMARY KEY] [reference_definition] INTO TABLE tbl_name | PRIMARY KEY (index_col_name,...) [FIELDS | KEY [index_name] (index_col_name,...) [TERMINATED BY t] | INDEX [index_name] (index_col_name,...) [[OPTIONALLY] ENCLOSED BY ] | UNIQUE [INDEX] [index_name] (index_col_name,...) [ESCAPED BY ] ] | FULLTEXT [INDEX] [index_name] (index_col_name,...) [LINES TERMINATED BY n] | [CONSTRAINT symbol] FOREIGN KEY [index_name] [IGNORE number LINES] (index_col_name,...) [reference_definition] [(col_name,...)] | CHECK (expr) } Quick Reference DATA DEFINITION LANGUAGE reference_definition: REFERENCES tbl_name [(index_col_name,...)]CREATE DATABASE [MATCH FULL | MATCH PARTIAL]CREATE DATABASE [IF NOT EXISTS] db_name [ON DELETE reference_option] [ON UPDATE reference_option]DROP DATABASE reference_option:DROP DATABASE [IF EXISTS] db_nameCREATE TABLE { RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT } table_options: MySQLCREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name Version 4.0.2-alpha [(create_definition,...)] [table_options] [select_statement] { TYPE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM } | AUTO_INCREMENT = #ALTER TABLE | AVG_ROW_LENGTH = # | CHECKSUM = {0 | 1}ALTER [IGNORE] TABLE tbl_name alter_spec [, alter_spec ...] | COMMENT = "string"RENAME TABLE | MAX_ROWS = # | MIN_ROWS = #RENAME TABLE tbl_name TO new_table_name[, tbl_name2 TO | PACK_KEYS = {0 | 1 | DEFAULT} new_table_name2,...] | PASSWORD = "string"DROP TABLE | DELAY_KEY_WRITE = {0 | 1} | ROW_FORMAT= { default | dynamic | fixed | compressed }DROP TABLE [IF EXISTS] tbl_name [, tbl_name,...] [RESTRICT | | RAID_TYPE= {1 | STRIPED | RAID0 } RAID_CHUNKS=#RAID_CHUNKSIZE=# CASCADE] | UNION = (table_name,[table_name...]) | INSERT_METHOD= {NO | FIRST | LAST }CREATE INDEX | DATA DIRECTORY="absolute path to directory" Table of Contents:CREATE [UNIQUE|FULLTEXT] INDEX index_name ON tbl_name | INDEX DIRECTORY="absolute path to directory" } (col_name[(length)],... ) select_statement:DROP INDEX Column Types [IGNORE | REPLACE] SELECT ... (Some legal select statement)DROP INDEX index_name ON tbl_name alter_spec: Functions to use in SELECT and WHERE clauses Basic MySQL User Utility Commands { ADD [COLUMN] create_definition [FIRST | AFTER column_name ] | ADD [COLUMN] (create_definition, create_definition,...)USE | ADD INDEX [index_name] (index_col_name,...) Data Manipulation Language | ADD PRIMARY KEY (index_col_name,...)USE db_name | ADD UNIQUE [index_name] (index_col_name,...)DESCRIBE | ADD FULLTEXT [index_name] (index_col_name,...) Data Definition Language | ADD [CONSTRAINT symbol] FOREIGN KEY index_name{DESCRIBE | DESC} tbl_name {col_name | wild} (index_col_name,...) [reference_definition] | ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} Basic MySQL User Utility Commands MySQL Transactional and Locking Commands | CHANGE [COLUMN] old_col_name create_definitionBEGIN/COMMIT/ROLLBACK [FIRST | AFTER column_name] | MODIFY [COLUMN] create_definition MySQL Transactional and Locking CommandsBEGIN; [FIRST | AFTER column_name] ... | DROP [COLUMN] col_nameCOMMIT; | DROP PRIMARY KEY | DROP INDEX index_nameLOCK/UNLOCK TABLES | DISABLE KEYSLOCK TABLES tbl_name [AS alias] | ENABLE KEYS {READ | [READ LOCAL] | [LOW_PRIORITY] WRITE} | RENAME [TO] new_tbl_name [, tbl_name {READ | [LOW_PRIORITY] WRITE} ...] ... | ORDER BY colUNLOCK TABLES | table_options }SET TRANSACTIONS table-reference: table_name [[AS] alias] [USE INDEX (key_list)]SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL [READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE] [IGNORE INDEX (key_list)] where_definition: GENERAL REPLACEMENTStype: (NOT) { where_expr | where_expr [ AND | OR ] where_expr } deepX Ltd. where_expr:see COLUMN TYPES Dublin, Irelandindex_col_name: { column_name [> | >= | = | <> | <= | < ] column_name_or_constant | column_name LIKE column_name_or_constantcol_name [(length)] | column_name IS NULL info@deepX.comjoin_condition: | column_name IS NOT NULL | ( where_definition ) }{ ON conditional_expr | USING (column_list) }