Вы находитесь на странице: 1из 3

SELECT cl, c2 FROM t;

FROM t1

CREATE TABLE t (

Query data in columns cl, c2 from a table

FULL OUTER JOIN t2 ON condition;


Perform full outer join

id INT PRIMARY KEV,

SELECT * FROM t;

name VARCHAR NOT NULL, price INT


DEFAULT 0

Query all rows and columns from a table


SELECT cl, c2 FROM t1 CROSS JOIN t2;

);

Produce a Cartesian product of rows in tables

Create a new table with three columns

SELECT cl, c2 FROM tl, t2;

DROP TABLE t;

SELECT DISTINCT cl FROM t WHERE


condition;

Another way to perform cross join

Delete the table from the database

Query distinct rows from a table

SELECT cl, c2 FROM tl A

ALTER TABLE t ADD column;

SELECT cl, c2 FROM t ORDER BY cl ASC


[DESC];

INNER JOIN t2 B ON condition;

Add a new column to the table

Join t1 to itself using INNER JOIN clause

ALTER TABLE t DROP COLUMN c;

SELECT cl, c2 FROM t1 UNION [ALL]

Drop column c from the table

SELECT cl, c2 FROM t2;

ALTER TABLE t ADD constraint;

Combine rows from two queries

Add a constraint

SELECT cl, c2 FROM t1 INTERSECT

ALTER TABLE t DROP constraint;

SELECT cl, c2 FROM t2;

Drop a constraint

Return the intersection of two queries

ALTER TABLE t1 RENAME TO t2;

SELECT cl, c2 FROM t1 MINUS

Rename a table from t1 to t2

SELECT cl, c2 FROM t2;

ALTER TABLE t1 RENAME cl TO c2;

Subtract a result set from another result set

Rename column cl to c2

SELECT cl, c2 FROM t1 WHERE cl [NOT]


LIKE pattern;

TRUNCATE TABLE t;

SELECT cl, c2 FROM t WHERE condition;


Query data and filter rows with a condition

Sort the result set in ascending or descending


order
SELECT cl, c2 FROM t ORDER BY cl
LIMIT n OFFSET offset;
Skip offset of rows and return the next n rows
SELECT cl, aggregate(c2) FROM t
GROUP BY cl;
Group rows using an aggregate function
SELECT cl, aggregate(c2) FROM t
GROUP BY cl
HAVING condition;
Filter groups using HAVING clause

Remove all data in a table


Query rows using pattern matching%,_

SELECT cl, c2 FROM t1

CREATE TABLE t(
SELECT cl, c2 FROM t

INNER JOIN t2 ON condition; Inner join t1


and t2
SELECT cl, c2 FROM t1
LEFT JOIN t2 ON condition; Left join t1 and
t1
SELECT cl, c2
FROM t1
RIGHT JOIN t2 ON condition; Right join t1
and t2
SELECT cl, c2

WHERE cl [NOT] IN value_list

cl INT, c2 INT, c3 VARCHAR, PRIMARY


KEV (cl,c2)

Query rows in a list

);

SELECT cl, c2 FROM t

Set cl and c2 as a primary key

WHERE cl BETWEEN low AND high;

CREATE TABLE tl(

Query rows between two values

cl INT PRIMARY KEV, c2 INT,

SELECT cl, c2 FROM t WHERE cl IS


[NOT] NULL;

FOREIGN KEV (c2) REFERENCES t2(c2)


);

Check if values in a table is NULL or not

Set c2 column as a foreign key

CREATE TABLE t
( cl INT, cl INT,

CREATE UNIQUE INDEX idx_name ON


t(c3,c4);

UNIQUE(c2,c3) );
Create a unique index on c3, c4 of the table t
Make the values in cl and c2 unique

DELETE FROM t
DROP INDEX idx_name;

CREATE TABLE t

WHERE condition;

( cl INT, c2 INT,

Delete subset of rows in a table

CHECK(cl> 0 AND cl > = c2) );

CREATE VIEW v(cl,c2)

Drop an index
AVG returns the average of a list

Ensure cl > 0 and values in cl > = c2

AS

CREATE TABLE t(

SELECT cl, c2 FROM t;

cl INT PRIMARY KEV,

Create a new view that consists of cl and c2

c2 VARCHAR NOT NULL );

CREATE VIEW v(cl,c2)

Set values in c2 column not NULL

AS

INSERT INTO t(column_list)


VALUES(value_list);

SELECT cl, c2

COUNT returns the number of elements of a


list
SUM returns the total of a list `
MAX returns the maximum value in a list
MIN returns the minimum value in a list

CREATE OR MODIFY TRIGGER


trigger_name WHEN EVENT
ON table_name TRIGGER_ TYPE
EXECUTE stored_procedure;

FROM t;
Insert one row into a table
INSERT INTO t(column_list)

WITH [CASCADED I LOCAL] CHECK


OPTION;

VALUES (value_list), (value_list), .... ;

Create a new view with check option

Insert multiple rows into a table

CREATE RECURSIVE VIEW v

INSERT INTO tl(column_list) SELECT


column_list

AS

Create or modify a trigger


WHEN
BEFORE - invoke before the event occurs
AFTER - invoke after the event occurs
EVENT

select-statement -- anchor part UNION [ALL]


FROM t2;

INSERT- invoke for INSERT

Insert rows from t2 into t1

select-statement; -- recursive part Create a


recursive view

UPDATEt

CREATE TEMPORARY VIEW v

SET cl = new_value;

AS

Update new value in the column cl for all rows

SELECT cl, c2

UPDATEt

FROM t;

SET cl = new_value,

Create a temporary view

c2 = new_value WHERE condition;

DROP VIEW view_name;

Update values in the column cl, c2 that match


the condition

Delete a view
CREATE INDEX idx_name

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

ON t(cl,c2);
DELETE FROM t;

DROP TRIGGER trigger_name;


Create an index on cl and c2 of the table t

Delete all data in a table

Delete a specific trigger

Вам также может понравиться