QUERYING DATA FROM A TABLE QUERYING FROM MULTIPLE TABLES USING SQL OPERATORS
SELECT c1, c2 FROM t ; SELECT c1, c2 SELECT c1, c2 FROM t1
Query data in columns c1, c2 from a table FROM t1 UNION [ALL]
INNER JOIN t2 ON condition; SELECT c1, c2 FROM t2;
SELECT * FROM t ; Inner join t1 and t2 Combine rows from two queries
Query all rows and columns from a table
SELECT c1, c2 SELECT c1, c2 FROM t1
SELECT c1, c2 FROM t FROM t1 INTERSECT
WHERE condition; LEFT JOIN t2 ON condition; SELECT c1, c2 FROM t2;
Query data and filter rows with a condition Left join t1 and t1 Return the intersection of two queries
SELECT DISTINCT c1 FROM t SELECT c1, c2 FROM t1
SELECT c1, c2
WHERE condition; MINUS
FROM t1
Query distinct rows from a table
RIGHT JOIN t2 ON condition; SELECTc1, c2 FROM t2;
Right join t1 and t2 Subtract a result set from another result set
SELECT c1, c2 FROM t
ORDER BY c1ASC [DESC]; SELECT c1, c2 FROM t1
Sort the result set in ascending or descending order SELECT c1, c2
FROM t1 WHERE c1[NOT] LIKE pattern;
FULL OUTER JOIN t2 ON condition; Query rows using pattern matching %, _
SELECT c1, c2 FROM t
ORDER BY c1 Perform full outer join
SELECT c1, c2 FROM t
LIMIT n OFFSET offset;
SELECT c1, c2 WHERE c1 [NOT] IN value list;
Skip offset of rows and return the next n rows
FROM t1 Query rows in a list
SELECT c1, aggregate(c2) CROSS JOIN t2;
FROM t Produce a Cartesian product of rows in tables SELECT c1, c2 FROM t
GROUP BY c1; WHERE c1 BETWEEN low AND high;
Group rows using an aggregate function Query rows between two values
SELECT c1, c2
FROM t1, t2;
SELECT c1, aggregate(c2) SELECT c1, c2 FROM t
Another way to perform cross join
FROM t WHERE c1 IS [NOT] NULL;
GROUP BY c1 Check if values in a table is NULL or not
SELECT c1, c2
HAVING condition; FROM t1 A
Filter groups using HAVING clause INNER JOIN t2 BON condition;
Join t1 to itself using INNER JOIN clause
MANAGING TABLES USING SQL CONTRAINTS MODIFYING DATA
CREATE TABLE t ( CREATE TABLE t( INSERT INTO t(column_list)
Id INT PRIMARY KEY, c1INT, c2INT, c3VARCHAR, VALUES(value_list);
Name VARCHAR NOT NULL, PRIMARY KEY (c1,c2) Insert one row into a table
Price INT DEFAULT 0 );
); Set c1 and c2 as a primary key INSERT INTO t(column_list)
Create a new table with three columns VALUES (value_list),
CREATE TABLE t1( (value_list), ….;
DROP TABLE t ; c1INT PRIMARY KEY, Insert multiple rows into a table
Delete the table from the database c2INT,
FOREIGN KEY (c2)REFERENCES t2(c2) INSERT INTO t1(column_list)
ALTER TABLE t ADD column; ); SELECT column_list
Add a new column to the table Set c2 column as a foreign key FROMt2;
ALTER TABLE t DROP COLUMN c ; Insert rows from t2 into t1
Drop column c from the table CREATE TABLE t(
c1INT, c1INT, UPDATE t
ALTER TABLE t DROP COLUMN c ; UNIQUE(c2,c3) SET c1= new_value;
Drop column c from the table ); Update new value in the column c1 for all rows
Make the values in c1 and c2 unique
ALTER TABLE t ADD constraint; UPDATE t
Add a constraint CREATE TABLE t( SET c1 = new_value,
c1INT, c2INT, c2 = new_value
ALTER TABLE t DROP constraint; CHECK(c1> 0 AND c1 >= c2) WHERE condition;
Drop a constraint ); Update values in the column c1, c2that match the
Ensure c1 > 0 and values in c1 >= c2 condition
DELETE FROM t;
ALTER TABLE t1 RENAME TO t2; CREATE TABLE t( Delete all data in a table
Rename a table from t1 to t2 c1INT PRIMARY KEY, DELETE FROM t
c2VARCHAR NOT NULL WHERE condition;
ALTER TABLE t1 RENAME c1TO c2; ); Delete subset of rows in a table
Rename column c1 to c2 Set values in c2 column not NULL
TRUNCATE TABLE t;
Remove all data in a table
MANAGING VIEWS MANAGING INDEXES MANAGING TRIGGERS
CREATE VIEW v(c1,c2) CREATE INDEX idx_name CREATE OR MODIFY TRIGGER trigger_name
AS ON t(c1,c2); WHEN EVENT
SELECT c1, c2 Create an index on c1 and c2 of the table t ON table_name TRIGGER_TYPE
FROM t; EXECUTE stored procedure;
Create a new view that consists of c1 and c2 CREATE UNIQUE INDEX idx_name Create or modify a trigger
ON t(c3,c4);
CREATE VIEW v(c1,c2) Create a unique index on c3, c4 of the table t WHEN
AS •BEFORE –invoke before the event occurs
SELECT c1, c2 DROP INDEX idx_name; •AFTER –invoke after the event occurs
FROM t; Drop an index
EVENT
WITH [CASCADED | LOCAL] CHECK OPTION;
•INSERT –invoke for INSERT
Create a new view with check option
•UPDATE –invoke for UPDATE
SQL AGGREGATE FUNCTIONS •DELETE –invoke for DELETE
CREATE RECURSIVEVIEW v
AS
TRIGGER_TYPE
select-statement--anchor part AVG returns the average of a list
•FOR EACH ROW
UNION [ALL] •FOR EACH STATEMENT
select-statement;--recursive part COUNT returns the number of elements of a list
Create a recursive view
SUM returns the total of a list
CREATE TRIGGER before_insert_person
CREATE TEMPORARYVIEW v
BEFORE INSERT
AS MAX returns the maximum value in a list
ON person FOR EACH ROW
SELECT c1, c2
EXECUTE stored procedure;
FROM t; MIN returns the minimum value in a list
Create a trigger invoked before a new row is
Create a temporary view
inserted into the person table
DROP VIEW view_name
DROP TRIGGER trigger_name
Delete a view
Delete a specific trigger