SQL Notes
SQL Notes
SQL Notes
DATA TYPES
-VARCHAR2(size): variable-length character data
-CHAR(size): fixed-length character data
-NUMBER(p,s): p digits, s decimal digits
-DATE: date and time value between Jan 1 4712 BC and Dec 31 AD 9999
-LONG: variable-length character data (up to 2 GB)
-CLOB: character data (up to 4 GB)
-RAW and LONG RAW: raw binary data
-BLOB: binary data (up to 4 GB)
-BFILE: binary data stored in an external file (up to 4 GB)
-ROWID: a base-64 number system representing the unique address of a row in its
table
LOGICAL OPERATORS
-AND Syntax: { ... WHERE ... AND ... }
-OR Syntax: { ... WHERE ... OR ...}
-NOT Syntax: { ... WHERE ... NOT (IN, BETWEEN, LIKE)}
----------------------------
LIKE = '%': represents any sequence of zero or more characters '_': represents any
single character
IS NULL, IS NOT NULL: tests for nulls. Syntax: { ... WHERE ... IS (NOT) NULL; }
BETWEEN = displays values in a range. Syntax: { ... WHERE ... BETWEEN ...
AND ...; }
ORDER BY = sorts datas -ASC: ascending order(min first) -DESC: descending order
(max first)
DEFINE = assigns a value to a var. UNDEFINE = removes assigned value
----------------------------------
Case-Coversion Functions
-LOWER: converts to lower case ex: LOWER('SQL Course'):
sql course
-UPPER: converst to upper case ex: UPPER('SQL Course'):
SQL COURSE
-INITCAP: only converts first char to upper, rest is lower ex: INITCAP('SQL
Course'): Sql Course
Character-Manipulation Functions
-CONCAT: same as || ex:
CONCAT('Hello', 'World'): HelloWorld
-SUBSTR: returns part of the value. m: index, n: count
ex: SUBSTR('HelloWorld',1,5): Hello
-LENGTH: returns length ex:
LENGTH('HelloWorld'): 10
-INSTR: returns numeric position of a named string m: starting index, n: nth
occurence ex: INSTR('HelloWorld', 'W'): 6
-LPAD | RPAD: returns an expression left or right padded m:length, n: blank
ex: LPAD(salary, 10, '*'): *****24000, RPAD(salary, 10, '*'): 24000*****
-TRIM: trims ex: TRIM('H'
FROM 'HelloWorld'): elloWorld
-REPLACE: replaces specified string ex:
REPLACE('JACK and JUE', 'J', 'BL'): BLACK and BLUE
Numeric Functions
-ROUND: rounds value to a specified decimal ex: ROUND(45.925, 2): 45.93,
ROUND(45.924, 2): 45.92, ROUND(45.923, 0): 46, ROUND(45.923, -1): 50
-TRUNC: truncates value to a specified decimal ex: TRUNC(45.929, 2): 45.92,
TRUNC(45.923): 45, TRUNC(45.923, -1): 40
-MOD: returns remainder of division ex: MOD(1600, 300): 100
Date-Manipulation Functions
Group Functions
-AVG: average value of n
-MAX: maximum value of expr
-MIN: minimum value of expr
-STDDEV: standart deviation of n
-SUM: sums all values
-VARIANCE: variance of n
-COUNT: counts num of rows
-------------------------------------
CASE = for conditional expressions Syntax: {... CASE ... WHEN ... THEN ...
WHEN ... THEN ... ELSE ... END "..." ...;}
-------------------------------------
JOINS
-NATURAL JOIN: joins table columns with same column name Syntax: { ... natural join
<table name>; }
-CROSS JOIN: cross-product of two tables (table 1 has 10 rows, table 2 has 8 rows)
total row 8*10 = 80
-------------------------------------
Set Operators
-------------------------------------
-ROLLBACK: discards all pending changes and returns last committed point or default
table
-ROLLBACK TO SAVEPOINT 'name': returns to savepoint
-------------------------------------
Database Objects
----
Constraints
-NOT NULL: specifies that the column cannot contain a null value
-UNIQUE: specifies a column or combination of columns whose values must be unique
for all rows in the table
-PRIMARY KEY: uniquely identifies each row of the table
-FOREIGN KEY: used to match values between tables
-CHECK: specifies a condition that must be true
-ON DELETE CASCADE: deletes row in the child table when a row in parent table
deleted
-ON DELETE SET NULL: converts dependent foreign key values to null
-----------------------------------------
-CREATE VIEW: represents subsets of data from one or more tables SYNTAX: { CREATE
(OR REPLACE) VIEW table_name (column_name) AS SELECT
DROP VIEW view_name: removes view column_name
FROM table_name WHERE ...; }
-------------------------------------------
-----DBA----
CREATE ROLE: role is named group of related privilages that can be granted to the
user
Syntax: { CREATE ROLE role_name; }
note: you can change your password by 'ALTER USER user_name IDENTIFIED BY
password;'!!!
---
Object Privilages
-------------------------------
ALTER TABLE: used to add, modify or drop columns Syntax: { ALTER TABLE
table_name (ADD|MODIFY|DROP)
(column_name datatype); }
Adding Constraints
ex: ALTER TABLE emp2 ADD CONSTRAINTS emp_mgr_fk FOREIGN KEY (manager_id) REFERENCES
emp2(employee_id);
ex: ALTER TABLE emp2 MODIFY employee_id PRIMARY KEY;
ex: ALTER TABLE emp2 ADD CONSTRAINT emp_dt_fk FOREIGN KEY (department_id)
REFERENCES departments(department_id) ON DELETE CASCADE;
ex: ALTER TABLE emp2 ADD CONSTRAINT emp_dt_fk FOREIGN KEY (department_id)
REFERENCES departments(department_id) ON DELETE SET NULL;
Dropping Constraints
Cascading Constraints
---
FLASHBACK TABLE: recovers tables from bin Syntax: { FLASHBACK TABLE table_name TO
BEFORE DROP; }
note: to see dropped tables use 'SELECT * FROM recyclebin;'!
-----------------------------------
View Naming
ex: insert into (select location_id, city, country_id from locations where
country_id in
(select country_id from countries natural join regions where region_name =
'Europe')
with check option) values (3600, 'Washington', 'US');
note: this is not going to work because of logical mistakes
-----
Insert Types
-Unconditional INSERT: for each row returned by the subquery, a row is inserted
into each of the target tables
-Conditional INSERT ALL: for each row returned by the subquery, a row is inserted
into each target table if the specified condition is met
-Pivoting INSERT: This is a special case of the unconditional INSERT ALL
-Conditional INSERT FIRST: for each row returned by the subquery, a row is inserted
inte the very first target table in which
the condition is met