forked from hajali-amine/cheat-sheets
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
5,711 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
|
||
# Nested Queries | ||
|
||
| With Select | With Update | With Insert | With Delete | | ||
| --- | --- | --- | --- | | ||
| ``SELECT (SELECT query that returns one value) FROM table WHERE conditions`` <br /> <br /> ``SELECT columns FROM (SELECT query that retuns multiple rows ) WHERE condition`` <br /> <br /> `` SELECT column_name FROM tables WHERE column_name OPERATOR (SELECT Query)`` | ``UPDATE table SET column_name = new_value WHERE OPERATOR (SELECT Query)``| ``INSERT INTO table_name [ (columns..) ] VALUES (SELECT Query that reurns one value , [val , ...])`` | ``DELETE FROM TABLE_NAME WHERE OPERATOR (SELECT Query)`` | | ||
<!--## With Select | ||
SELECT ( SELECT Query that returns one value | ||
// Don't forget to make the join condition with the table called in the main select ) | ||
FROM table | ||
WHERE conditions | ||
------------------------------ | ||
SELECT columns | ||
FROM (SELECT query that retuns multiple values ) | ||
WHERE condition | ||
--------------------------------------- | ||
SELECT column_name | ||
FROM tables | ||
WHERE column_name OPERATOR (SELECT Query) | ||
## With Update | ||
UPDATE table | ||
SET column_name = new_value | ||
WHERE OPERATOR [ VALUE ] (SELECT Query) | ||
## With Insert | ||
INSERT INTO table_name [ (columns..) ] | ||
VALUES (SELECT Query that reurns one value , [val , ...]) | ||
## With Delete | ||
DELETE FROM TABLE_NAME | ||
WHERE OPERATOR (SELECT Query) | ||
--> | ||
**OPERATOR** : EXISTS / NOT EXISTS -- BETWEEN -- IN | ||
|
||
|
||
# Views | ||
### Creation | ||
#### Virtual View | ||
- ``CREATE VIEW view_name(col1,col2,..) AS query [WITH CHECK OPTION] [WITH READ ONLY] `` | ||
- `` REPLACE VIEW view_name(cols) AS query [WITH CHECK OPTION] [WITH READ ONLY]`` | ||
#### Concrete View | ||
- ``CREATE CONCRETE VIEW view_name(col1,col2,..) AS query [WITH CHECK OPTION] [WITH READ ONLY] `` | ||
|
||
### Delete | ||
- `` DROP VIEW view_name `` | ||
# Recursive Queries (^SQL3) | ||
|
||
WITH [RECURSIVE] name AS ( | ||
Initialisation Query | ||
UNION ALL | ||
Recursive Query with terminate condition in where | ||
) | ||
SELECT * from name; -- Displaying result data | ||
- PostgreSQL requires the RECURSIVE keyword in recursive definitions but it is optional for other databases. | ||
|
||
<img height=300 src='https://cdn.sqlservertutorial.net/wp-content/uploads/SQL-Server-Recursive-CTE-execution-flow.png' > | ||
|
||
# PL/SQL | ||
|
||
``DECLARE``<br /> ``VarName [constant] type [:= val];``<br /> ``BEGIN``<br />``EXCEPTION``<br />``END`` | ||
|
||
|
||
# Functions and Procedures | ||
## Functions | ||
|Oracle | PostgreSQL | | ||
| --- | --- | | ||
| ``CREATE FUNCTION func_name[(in / out / in out parms)] RETURN returnType AS``<br /> ``DECLARE``<br /> ``VarName [constant] type [:= val];``<br /> ``BEGIN``<br />``-- SELECT must always be used with INTO``<br />``RETURN statement``<br />``EXCEPTION``<br />``END``|``CREATE FUNCTION func_name() RETURNS returnType AS $$``<br />``DECLARE``<br />``VarName [constant] type [:= val];``<br /> ``BEGIN``<br />``RETURN statement``<br />``EXCEPTION``<br />``END``<br />``$$ LANGUAGE plpgsql;``| | ||
## Procedures | ||
|
||
|Oracle | PostgreSQL | | ||
| --- | --- | | ||
| ``CREATE procedure proc_name[(in / out / in out parms)] AS ``<br /> ``DECLARE``<br /> ``VarName [constant] type [:= val];``<br /> ``BEGIN``<br />``-- SELECT must always be used with INTO``<br />``RETURN statement``<br />``EXCEPTION``<br />``END proc_name;``|``CREATE Procedure prod_name() AS $$``<br />``DECLARE``<br />``VarName [constant] type [:= val];``<br /> ``BEGIN``<br />``EXCEPTION``<br />``END``<br />``$$ LANGUAGE plpgsql;``| | ||
|
||
|
||
|
||
- Var Types | ||
- RECORD, INTEGER , VARCHAR(n) , CHAR ,DATE , DECIMAL ... | ||
- TABLES , LINE .. | ||
- %TYPE %ROWTYPE | ||
### Custom Type : | ||
CREATE TYPE type_name AS ( | ||
var_name var_type , | ||
); | ||
- Return Types: | ||
- INTEGER , VARCHAR(n) , CHAR ,DATE , DECIMAL ... | ||
- setof (type) : return more then one row | ||
|
||
**Example:** | ||
|
||
CREATE OR REPLACE function cat() RETURNS setof varchar language plpgsql as $$ | ||
DECLARE | ||
liste CURSOR for SELECT tablename FROM pg_tables where tableowner='root'; | ||
tablename RECORD ; | ||
BEGIN | ||
|
||
for tablename in liste | ||
loop | ||
return NEXT tablename.tablename ; | ||
end loop; | ||
|
||
END | ||
$$; | ||
|
||
|
||
# Triggers | ||
|
||
CREATE TRIGGER trigger_name | ||
{BEFORE | AFTER} {INSERT | UPDATE| DELETE } [OF colums] | ||
ON table_name | ||
[REFERENCING {OLD | NEW | PARENT } [ROW] [AS] alias] | ||
[FOR EACH ROW] | ||
--trigger_body (pl/sql bloc) | ||
or | ||
--calling a function (EXECUTE PROCEDURE func_name();) ; | ||
this function should RETURN a TRIGGER | ||
|
||
# Cursor | ||
- It helps you store multiple lines and Mange access to them | ||
|
||
### Creation | ||
|
||
|
||
|Oracle |PostgreSQL| | ||
| ----- | ----- | | ||
|``CURSOR cursorname IS SELECT ..FROM .. WHERE..``<br />``FOR UPDATE/DELETE [OF Column list ]][NOWAIT WAIT duration]``|``cursorname CURSOR FOR Query``| | ||
|
||
### Iterating Rows | ||
|
||
|
||
|Oracle |PostgreSQL| | ||
| ----- | ----- | | ||
|``FOR record_variable IN cursor_variable`` <br/>``LOOP``<br/>``...``<br/> ``END LOOP;``|``LOOP``<br/>``FETCH FROM cursor_variable INTO record_variable;``<br/>``EXIT WHEN NOT FOUND;``<br/>``...``<br />``END LOOP;``| | ||
|
||
In PostgreSQL : we should open the Cursor after Declation | ||
|
||
# Exceptions | ||
|
||
we add exceptions within a PL/SQL bloc : | ||
|
||
DECLARE | ||
-- Variables, curseurs, exceptions définies par l'utilisateur | ||
BEGIN * | ||
|
||
-- Instructions | ||
|
||
EXCEPTION (facultatif) | ||
-- Actions à effectuer lorsque des erreurs se produisent | ||
END; * | ||
------------------------------------- | ||
EXCEPTION | ||
WHEN DefinedException | costum exception THEN | ||
statement; | ||
... | ||
|
||
WHEN OTHERS THEN | ||
statement; | ||
|
||
|
||
- Defined exception | ||
- NO_DATA_FOUND | ||
- TOO_MANY_ROWS | ||
- .. | ||
- Custom exception in oracle: | ||
- **Declaration** : ``exception_name EXCEPTION; `` | ||
- **Raising an exception** : ``RAISE exception_name;`` | ||
|
||
- Custom exception in PostgreSQL: | ||
``raise exception '%','Exception text';`` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
|
||
# Managing Users | ||
- ``GRANT privileges ON table TO user (PUBLIC) [WITH GRANT OPTION ]`` | ||
- ``REVOKE privileges ON table FROM user`` | ||
- Privileges: | ||
|
||
|
||
|`` ALL PREVILEGES``|``UPDATE (columns)``|``SELECT (columns)``|``INSERT (columns)``|``DELETE (columns)``|``ALTER (columns)``| `` INDEX``| | ||
| --- | --- | --- | --- | --- | --- | --- | | ||
|
||
|
||
### ROLES : | ||
- `` | ||
CREATE ROLE role_name; | ||
`` | ||
- `` | ||
GRANT previlege ON table TO role; | ||
`` | ||
- `` | ||
GRANT role TO user; | ||
`` | ||
|
||
# Managing Tables | ||
|
||
## Creation | ||
- ``CREATE TABLE table_name ( column_name [TYPE] [COLUMN_CONSTRAINT] , [TABLE CONSTRAINT] [REFRENTIAL CONSTAINT]);`` | ||
|
||
- **TABLE CONSTRAINT** : | ||
- ``CONSTRAINT`` const_name ``CONST_TYPE`` (Column1, column2,... ) | ||
- **COLUMN CONSTRAINT**: | ||
[CONSTRAINT constraint_name] | ||
- `` AUTO_INCREMENT `` | ||
- ``UNIQUE`` | ||
- ``NULL`` / ``NOT NULL`` | ||
- ``DEFAULT``'val' | ||
- ``PRIMARY KEY`` | ||
- ``REFRENCES`` 'table_name' | ||
- ``CHECK`` 'constraint' | ||
|
||
- **REFERENCIAL CONSTRAINT** : | ||
- ``FOREIGN KEY``(column_name) ``REFERENCES`` table_name(primarykey) [FOREIGN KEY CONSTRAINT] | ||
- **FOREIGN KEY CONSTRAINT**: | ||
- ``ON DELETE`` {CASCADE/SET NULL/ SET DEFAULT} | ||
- ``ON UPDATE`` {CASCADE/SET NULL/ SET DEFAULT} | ||
|
||
- **TYPE**: | ||
- ``VARCHAR(n)`` | ||
- ``INTEGER`` | ||
- ``BOOL`` | ||
- ``DATE`` | ||
- ``DATETIME`` | ||
|
||
##### FIND MORE: `https://www.w3schools.com/sql/sql_datatypes.asp` | ||
## Modification | ||
- ``ALTER TABLE table_name [OPERATION];`` | ||
|
||
|
||
- OPERATIONS : | ||
- `` RENAME TO`` new_table_name | ||
- ``RENAME COLUMN`` column_name ``TO`` new_name | ||
- ``DROP COLUMN`` column_name | ||
- ``DROP CONSTRAINT`` const_name | ||
- ``ADD COLUMN`` column_name | ||
- ``ADD CONSTRAINT`` constraint_details | ||
- ``MODIFY COLUMN`` column_name | ||
|
||
|
||
## Delete | ||
- ``DROP TABLE Table_name {CASCADE CONSTRAINT | RESTRICT };`` | ||
# Managing Data | ||
## SELECT | ||
|
||
SELECT {DISTINCT | ALL} columns | ||
FROM tables | ||
WHERE conditions | ||
GROUP BY columns | ||
HAVING conditions | ||
[UNION | INTERSECT | MINUS <sub-Query>] | ||
ORDER BY columns | ||
### Operations can be used in ``WHERE`` and ``HAVING`` clause | ||
|
||
|< , > , = ,<= , >= , <>|LIKE|IS NULL|IN (va1, val)|BETWEEN n1 AND n2| | ||
| --- | --- | --- | --- | --- | | ||
|
||
|
||
### Functions can be used in ``SELECT`` statement, ``WHERE`` and ``ORDER BY`` clause | ||
|
||
1 - **Multiple-Row Functions** : Aggregate Functions | ||
|
||
| SUM|AVG|MIN|MAX|COUNT| | ||
| --- | --- | --- | --- | --- | | ||
|
||
|
||
2 - **Single-Row functions** | ||
|
||
- UPPER, LOWER , INITCAP | ||
- CONCAT, LENGTH, SUBSTR, INSTR, LPAD, RPAD, TRIM , REPLACE. | ||
- MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY, CURTIME , DATE_FORMAT(date, format) ,YEAR(date) | ||
- ROUND, TRUNC, MOD. | ||
|
||
See More : https://docs.oracle.com/database/121/SQLRF/functions002.htm#SQLRF51178 | ||
|
||
## UPDATE | ||
- `` | ||
UPDATE table | ||
SET changes | ||
WHERE condition | ||
`` | ||
## INSERT | ||
|
||
- ``INSET INTO table_name (col1, col2, ...) VALUES (val1, val2 ,...);`` | ||
|
||
## DELETE | ||
- ``DELETE FROM table_name WHERE condition`` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.