Uploaded by Devendra Singh

SQL-Basic-Cheat-Sheet

advertisement
S E Q U E N T I A L Q U E RY
LANGUAGE
C H E AT S H E E T
SQL Basics
Function
TO_DATE
COALESCE
CURRENT_TIME
STAMP
SQL
=
Used to compare a value with the given input
SELECT C1 FROM t1
Returns the correct time on the database server
It is a special operator used with WHERE to search for a
LIKE
GROUP BY
Functions
SQL Data Types
Aggregate Functions: It is a function where the values of multiple rows are
It is used to subtract the second result set
MINUS
MINUS
from the first
SELECT C1 FROM t2
meets the specifies condition must be returned
It is used to return all rows where key records of one table
It is used to return all rows from the left table with the
LEFT JOIN
It is used to return all rows from right table with the
FULL OUTER
It is used to return rows that match either in the left or
JOIN
right table
Trigger
It is a special type of stored procedure that automatically executes when a user tries to modify
•
INTEGER
•
SMALLINT
•
CHARACTER VARYING (VARCHAR)
UNION: A set operation can be used on the returned results called ‘UNION’
Syntax:
•
BIGINT
•
CHARACTER LARGE OBJECT
which can append the result of one query to another
•
•
NUMERIC
•
NATIONAL CHARACTER
•
DECIMAL
•
NATIONAL CHARACTER VARYING
•
NATIONAL CHARACTER LARGE
SELECT col1, col2 FROM table1
OBJECT
UNION
•
DOUBLE PRECISION
Date times:
•
FLOAT
•
DATE
•
DECFLOAT
•
TIME WITHOUT TIMEZONE
Binary Strings:
•
TIMESTAMP WITHOUT TIMEZONE
•
BINARY
•
TIME WITH TIMEZONE
•
BINARY VARYING
•
TIMESTAMP WITH TIMEZONE
•
BINARY LARGE OBJECT
SQL Query
combined to form a single value
Query Language Processor
DBMS Engine
ARRAY
Syntax:
Intervals:
•
MULTISET
To create an index: CREATE INDEX index_name ON t(c1, c2)
•
INTERVAL DAY
Other Types:
To create an unique Index: CREATE UNIQUE INDEX index_name ON t(c3, c4)
•
INTERVAL YEAR
•
ROW
•
To drop an index: DROP INDEX index_name
XML
o
BEFORE: Invokes before an event occurs
o
AFTER: Invokes after an event occurs
ON table_name TRIGGER_TYPE
o
INSERT: Invoke for Insert
EXECUTE stored_procedure
o
UPDATE: Invoke for Update
o
DELETE: Invoke for Delete
Stored Procedure
View
Physical database
Primary Key: Set c1 and c2 as primary key
c1 INT, c2 INT, c3 VARCHAR,
reused by multiple programs
Syntax: To create Procedure
CREATE PROCEDURE procedure_name
);
EVENT:
TRIGGER TYPE:
o
FOR EACH ROW
o
FOR EACH STATEMENT
To delete or drop a trigger: Used to delete a specific trigger
Using SQL
constraints
PRIMARY KEY (c1,c2)
It is a set of SQL statements with assigned names that can be shared and
•
WHEN EVENT
•
Syntax: CREATE TABLE t(
Syntax:
File manager +
Transaction
manager
WHEN:
trigger_name
Syntax: DROP TRIGGER trigger_name
of database pages to be visited
•
•
CREATE OR MODIFY TRIGGER
•
Collection Types:
mechanism letting users to access the data through the views
Parser+Optimizer
Explanation:
To create or modify trigger
It is used to speed up the performance of the queries by reducing the number
Boolean:
It is a virtual table which is a result of a query. It is often used as a security
through a DML event
SELECT col3, col4 FROM tabl2
INDEXES
To check if the values are NULL or NOT NULL
WHERE c1 IS [NOT] NULL
CHARACTER
REAL
and MAX
NOT NULL
•
•
It returns the rows where c1 is between MIN
WHERE c1 BETWEEN min AND max
matching rows in the left table
Character Strings:
Approximate Numeric’s:
matching pattern
SELECT c1 FROM t
SELECT c1 FROM t
Exact Numeric’s:
Syntax:
It is used to return the query of rows using the
WHERE c1 [NOT] LIKE pattern
BETWEEN
matching rows in the right table
RIGHT JOIN
SELECT c1 FROM t
NOT LIKE
is same as that of the other table
It is used to transform values from a group of rows into a
delimited string
queries
SELECT C1 FROM t1
It is used to specify that rows with aggregate values which
An aggregate function to return the largest/smallest value
INTERSECT
SELECT C1 FROM t2
It is used to group identical data
INNER JOIN
among the result set
It is used to return the intersection of two
INTERSECT
specific pattern from a column or row
HAVING
set
LISTAGG
programming and querying a database
two queries
It is used to define a condition
To compute the mean average of the values in the result
Structured query language (SQL) is a domain specific language used for
from table t2 and combine the rows of these
SELECT C1 FROM t2
It is used to declare the table to select from
WHERE
set
MIN/MAX
UNION [ALL]
FROM
columns that contain NULL
UNION
Description
Selecting column 1 for table t1 and column 2
Returns the first non NULL results, when querying with the
An aggregate function that sums up the values in a result
AVG
Syntax
SELECT C1 FROM t1
It is used to specify which column to query. Use * for all
the result set
SUM
Operator
Explanation
SELECT
An aggregate function that returns the number of rows in
COUNT
Keywords
Description
It is used to convert a string to date.
Unique
Making the values in C1 and C2 as unique
Syntax: CREATE TABLE t(
c1 INT, c1 INT,
UNIQUE (c2,c3)
);
Foreign Key: Set c2 column as a foreign key
Syntax: CREATE TABLE t1(
c1 INT PRIMARY KEY,
CREATE VIEW view1 AS
@variable AS datatype = value
SELECT c1,c2
AS
c2 INT,
FROM t1
-- Comments
FOREIGN KEY (c2) REFERENCES t2(c2)
WHERE condition
SELECT * FROM t GO
);
FURTHERMORE:
SQL Certification Training Course
Download