0% found this document useful (0 votes)
24 views

SQL Cheat Sheet

This document provides a cheat sheet on managing tables, triggers, modifying data, querying from multiple tables, using constraints, and SQL operators in a database. It includes summaries of commands for creating, altering, dropping, truncating, and renaming tables, as well as creating, modifying, and dropping triggers. It also summarizes commands for inserting, updating, deleting data and joining tables. Finally, it covers SQL constraints like primary keys, foreign keys, unique constraints, and check constraints as well as SQL operators like union, intersect, minus, like, in, between, and null checks.

Uploaded by

Shaurya Singru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

SQL Cheat Sheet

This document provides a cheat sheet on managing tables, triggers, modifying data, querying from multiple tables, using constraints, and SQL operators in a database. It includes summaries of commands for creating, altering, dropping, truncating, and renaming tables, as well as creating, modifying, and dropping triggers. It also summarizes commands for inserting, updating, deleting data and joining tables. Finally, it covers SQL constraints like primary keys, foreign keys, unique constraints, and check constraints as well as SQL operators like union, intersect, minus, like, in, between, and null checks.

Uploaded by

Shaurya Singru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CHEAT SHEET

MANAGING TABLES
CREATE TABLE t (
id INT PRI~ARY KEY ,
name VARCHAR NOT NUL. ,
price INT DEFAULT 0
);
Create a new table with three columns
DROP TABLE t ;
Delete the table from the database
ALTER TABLE t ADD column;
Add a new column to the table
ALTER TABLE t DROP COLUMN c ;
Drop column c from the table
ALTER TABLE t ADD constraint ;
Add a constraint
ALTER TABLE t DROP constraint ;
Drop a constraint
ALTER TABLE tl RENAME TO t2 ;
Rename a table from t1 to t2
ALTER TABLE tl RENAME cl TO c2 ;
Rename column c1 to c2
TRUNCATE TABLE t ;
Remove all data in a table
MANAGING TRIGGERS
CREATE OR MODIFY TRIGGER trigger_name
WHEN EVENT
ON table_name TRIGGER_TYPE
EXECUTE stored_procedure;
Create or modify a trigger
WHEN
• BEFOR[ - 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
CREATE TRIGGER before_insert_person
BEFORE INSERT
ON person FOR EACH ROW
EXECUTE stored_procedure;
Create a trigger invoked before a new row is
inserted into the person table
DROP TRIGGER trigger_name
Delete a specific trigger
MODIFYING DATA
INSERT INTO t(column_list)
VALUES( value_list );
Insert one row into a table
INSERT INTO t(column_list)
VALUES (value_list),
( value_list), ..•. ;
Insert multiple rows into a table
INSERT INTO tl(column_list)
SELECT column_list
FROM t2;
Insert rows from t2 into t1
UPDATE t
SET cl = new_value ;
Update new value in the column c1 for all rows
UPDATE t
SET cl= new_value,
c2 = new_value
WHERE condition ;
Update values in the column c1 , c2 that match
the condition
DELETE FROM t;
Delete all data in a table
DELETE FROM t
WHERE condition ;
QUERYING FROM MULTIPLE TABLES
SELECT cl, c2
FROM tl
INNER JOIN t2 ON condition;
Inner Join T1 And T2

SELECT cl, c2
FROM t1
LEFT JOIN t2 ON condition;
LeftJoin T1 And T1

SELECT cl, c2
FROM tl
RIGHT JOIN t2 ON condition;
Right Join T1 And T2
SELECT cl, c2
FROM tl
FULL OUTER JOIN t2 ON condition;
Perform Full Outer Join

SELECT cl, c2
FROM tl
CROSS JOIN t2;
Produce A Cartesian Product Of Rows In Tables

SELECT cl, c2
FROM t1, t2;
Another Way To Perform Cross Join

SELECT cl, c2
J:ROM t1 A
USING SQL CONSTRAINTS
CREATE TABLE t(
cl INT , c2 INT , c3 VARCHAR ,
PRIMARY KEY ( c1,c2 )
);
Set cl and c2 as a primary key
CREATE TABLE tl(
cl INT PRIMARY LEY ,
c2 INT ,
rOREIGN KEY (c2) REFERENCES t2(c2)
);
Set c2 column as a foreign key
CREATE TABLE t (
cl INT , cl INT ,
UNIQUE( c2,c3 )
);
Make the values in c1 and c2 unique
CREATE TABLE t (
cl INT , c2 INT ,
CHECK( cl> 0 AND cl ~ c2 )
);
Ensure c1 > O and values in c1 >= c2

CREATE TABLE t (
cl INT PRIMARY KEY ,
c2 VARCHAR NOT NULL
);
USING SQL OPERATORS
SELECT cl, c2 FROM t l
UNION [ALL]
SELECT cl, c2 FROM t2;
Combine Rows From Two Queries

SELECT cl, c2 FROM t l


INTERSECT
SELECT C1, C2 FROM T2;
Return The Intersection Of Two Queries
SELECT cl, c2 FROM t l
MINUS
SELECT cl, c2 FROM t2;
Subtract A Result Set From Another Result Set

SELECT cl, c2 FROM t l


WHERE cl [NOT] LIKE pattern;
Query Rows Using Pattern Matching%, _
SELECT cl, c2 FROM t
WHERE cl [NOT] IN value_list;
Query Rows In A List

SELECT cl, c2 FROM t


WHERE cl BETWEEN low AND high;
Query Rows Between Two Values
/
SELECT cl, c2 FROM t
WHERE cl IS [ NOT] NULL ;
Check If Values In A Table Is NULL Or Not
' , ~~~::.
-
~ -~
'-...__

You might also like