SQL Cheatsheet - adonisv79/bytecommander.com GitHub Wiki

-- Comment uses --

WHERE OPERATORS

<, > , <=, >=, =, <>, IN(ENUM), AND, OR

SQL COMMON DATA TYPES

  • INT - WHOLE NUMBERS
  • TINYINT(N) - Smaller sized INT used mostly to store boolean (0 or 1) where N is set to 1
  • DECIMAL(M,N) - Floating point numbers with a precision of M digits and N of those digits represent the decimals
  • VARCHAR(X) - Strings of X Length
  • BLOB - Binary Large Objects
  • DATE - 'YYYY-MM-DD'
  • TIMESTAMP - 'YYYY-MM-DD HH:MM:SS'

DDL (Data Definition Language)

CREATE - Creates a table

CREATE TABLE { TABLE_NAME } ( [ COLUMN_NAME { DATA_TYPE}] {NOT NULL | UNIQUE | DEFAULT {DEFAULT_VALUE} } )

CREATE TABLE table1 ( field1 INT AUTO_INCREMENT, field2 VARCHAR(10) UNIQUE NOT NULL, field3 VARCHAR(10) DEFAULT '12345', date_created TIMESTAMP, PRIMARY KEY(field1), FOREIGN KEY (field2) REFERENCES (table2) ON DELETE SET NULL, FOREIGN KEY (field3) REFERENCES (table3) ON DELETE CASCADE, );

DESCRIBE - Describes a table

DESCRIBE [ TABLE_NAME ]

DROP - Delete a table

DROP TABLE [ TABLE_NAME ]

ALTER - modifies a Table

ALTER TABLE [ TABLE_NAME ] [ FUNCTION ]

ALTER TABLE table1 ADD field3 DECIMAL (5,2);

ALTER TABLE table1 DROP COLUMN field3;

DML (Data Manipulation Language)

INSERT

INSERT INTO { TABLE_NAME } VALUES ([ VALUE_ASSIGNMENTS])

` INSERT INTO table1 VALUES (1, 'TEST', '2014-02-13');

or

INSERT INTO table1(field1, field1, date_created) VALUES (1, 'TEST', '2014-02-13'); `

UPDATE

UPDATE { TABLE_NAME } SET { FIELD_NAME = {NEW_VALUE} } { WHERE [ CONDITIONS ] }

UPDATE table1 SET field2 = 'TEST2' WHERE field1 = 1;

DELETE

DELETE FROM { TABLE_NAME } { WHERE [ CONDITIONS ] }

DELETE FROM table1 WHERE field1 = 3 }

DQL (Data Query Language)

Select

SELECT {* | { DISTINCT } | [ FIELD_NAMES ] | COUNT(* | FIELD_NAME) } FROM [ TABLE_NAMES { AS ALIAS } ] { WHERE [ CONDITIONS] } { ORDER BY [ FIELDNAME { ASC | DESC }] } { LIMIT NUMBER }

SELECT field1 AS id, field2 AS name FROM table1 AS tb1 WHERE field2 = "TEST";

SELECT WITH AGGREGATION

` SELECT SUM (field1) FROM table1

SELECT COUNT (gender), gender FROM students GROUP BY gender `

WILDCARDS

SELECT * FROM table1 WHERE field1 LIKE 'ADO%' AND field2 LIKE 'V_LLA%'

JOINS AND UNIONS

SELECT field1 FROM table1 UNION SELECT field1 FROM table2

SELECT table1.field1, table2.field1 FROM table1 JOIN table2 ON table1.field2 = table2.field2

SELECT table1.field1, table2.field1 FROM table1 LEFT JOIN table2 ON table1.field2 = table2.field2

NESTED

SELECT field1 FROM table1 WHERE field2 in ( SELECT field1 FROM table2 WHERE field 2 = 1 );

SELECT field1 FROM table1 WHERE field2 = ( SELECT field1 FROM table2 WHERE field 2 = 1 );

DCL (Data Control Language)

TRIGGERS

DELIMITER $$ CREATE TRIGGER my_trigger BEFORE INSERT ON table1 FOR EACH ROW BEGIN IF NEW.field1 = 'M' THEN INSERT INTO table2 VALUES('A male record has been inserted on table1 for ' + NEW.field2); ELSEIF NEW.field1 = 'F' THEN INSERT INTO table2 VALUES('A female record has been inserted on table1 for ' + NEW.field2); ELSE INSERT INTO table2 VALUES('A unknown gender record has been inserted on table1 for ' + NEW.field2); END IF; END$$ DELIMETER;

⚠️ **GitHub.com Fallback** ⚠️