SQL CHEATSHEET
@yosracodes
Create used to create a new database or table
CREATE DATABASE <DATABASE NAME>
CREATE TABLE <TABLE NAME>
Drop used to delete an existing database
or table Data Types
Numeric
-
N SMALL N DE MAL(i j)
I T, I T, CI ,
DROP DATABASE <DATABASE NAME> String
-
CHAR HAR(n) VAR HAR(n)
, C , C
DROP TABLE <TABLE NAME>
Bit String
-
B B (n)
IT, IT
Date and time
-
DA E ME ME(i)
T , TI , TI
used to delete information Timestamp MES AMP
Truncate
- TI T
in the table but doesn’t
delete the table itself Referential Triggered Action
TRUNCATE TABLE <TABLE NAME> used to set what happens on updating or deleting a
tuple (row) in the database that references another
row
Alter used to delete, add or modify
constraints or columns in a table OPTIONS:
ON DELETE <OPTION> SE NULL
ALTER TABLE <TABLE NAME>
ON UPDATE <OPTION> SE DEFAULT
ADD <COLUMN NAME> <DATA TYPE> CAS ADE C
ALTER TABLE <TABLE NAME>
Renaming (Aliasing)
DROP COLUMN <COLUMN NAME>
Relation and attribute names can be renamed for
ALTER TABLE <TABLE NAME>
conenience or to remove ambiguity using the keyword
ALTER COLUMN <COLUMN NAME> <DATA TYPE> AS
Backup used to create a backup on <TABLE NAME> AS <NEW TABLE NAME>
an existing database (<NEW ATTRIBUTE 1 NAME>, .....)
BACKUP DATABASE <DATABASE NAME>
TO DISK = ‘<PATH>’
Cross Product (,)
used to produce a result table that has the number of
rows of the first table multiplied by the number of
Insert used to insert new tuples
(rows) in a table
rows of the second table
SELECT <ATTRIBUTE LIST>
INSERT INTO <TABLE NAME> (<COLUMN1>, .... )
FROM <TABLE 1>, <TABLE 2>
VALUES V
(< ALUE1>, ....)
*you do not need to specify all columns if you will add values
for all the columns Duplicates
Delete used to delete tuples (rows)
from a table
DISTINCT is used to eliminate duplicate
ALL is used to allow duplicates
DELETE FROM <TABLE NAME>
SELECT ALL <ATTRIBUTE LIST>
WHERE <CONDITION>
FROM <TABLE NAME>
*if you don’t add the WHERE clause all rows will be deleted
,
SELECT DISTINCT <ATTRIBUTE LIST>
Update used to modify existing
records in a table
FROM <TABLE NAME>
*SELE CT without ALL or DISTINCT is equivalent to ALL
UPDATE <TABLE NAME>
SET <COLUMN NAME> = <NEW VALUE>
String Comparisons
WHERE <CONDITION> I E is used for string compariso
L K
(%) replaces an arbitary number of characters
(_) replaces one character
Select used to select data from a
table
<ATTRIBUTE> LIKE <PATTERN>
SELECT <ATTRIBUTE LIST>
FROM <TABLE NAME>
WHERE <CONDITION> Arithmetic Operators
*if you want all attributes of a table use (*) (+) add (*) multipl
(-) subtract (/) divide
Union, Intersect, Except Ordering
equivalent to the set operations: union,
intersection and difference. ORDER BY is used to order the resulting tuple
The keyword ASC (ascending) and DESC can be
<FIRST SELECT STATEMENT>
used.
UNION / INTERSECT / E CEP X T
<SECOND SELECT STATEMENT> <SELECT STATEMENT>
ORDER BY <ATTRIBUTE> <ASC / DESC>
compares a value with a set of
In values, returns true if the value is one
of the elements of the set. Set Comparisons
* he default is AS (ascending)
T C
ANY and ALL can be used with (=, >, >=, <, <=, <>) to
compare a value with a set
SELECT <ATTRIBUTE LIST>
FROM <TABLE NAME>
V
WHERE < ALUE> IN <ANOTHER SELECT QUERY>
SELECT <ATTRIBUTE LIST>
Null used to check whether a FROM <TABLE NAME>
value is NULL V
WHERE < ALUE> > ALL / ANY <ANOTHER SELECT QUERY>
CONTAINS - Compares two sets and returns true
if one set contains the othe
<ATTRIBUTE NAME> IS (NOT) NULL
EXISTS - It checks whether the result of a nested
Join used to join two tables based on
a related column between them
query is empty or no
UNIQUE - checks if the table has duplicates
SELECT <ATTRIBUTES LIST>
FROM <TABLE 1> JOIN <TABLE 2>
Types of Join
ON <JOIN CONDITION>
WHERE <SELECTION CONDITION>
Assertion I nner Join Left Join Right Join Full Outer Join
used to ensure a certain condition is always
met in the database Aggregate Functions
COUNT - Counts how many rows in a particular
CREATE ASSERTION <ASSERTION NAME>
colum
CHECK (<CONDITION>)
SUM - adds together all the values in a particular
colum
Trigger MIN - returns the minumum value in a colum
MAX - returns the maximum value in a colum
Triggers are activated when a defined action is AVG - returns the average of a group of selected
executed for the table values
CREATE TRI GGER <TRI GGER NAME>
BEFORE / AFTE R
INSERT / UPDATE / DELET E
ON <TABLE NAME>
FOR EACH RO W
<TRIGGER BODY>