SQL Alias:: To Make Selected Columns More Readable.
SQL Alias:: To Make Selected Columns More Readable.
SQL Alias:: To Make Selected Columns More Readable.
Data Definition
Language (DDL)
Data Manipulation
Language (DML)
Transaction
Control Language
(TCL)
Data Control
Language (DCL)
Function
Creating, modifying,
and dropping the
structure of database
objects
Storing, retrieving,
modifying, and
deleting data
Managing changes
affecting the data.
Providing security to
database objects
Commands
CREATE, ALTER,
DROP, RENAME,
and TRUNCATE
SELECT, INSERT,
UPDATE, and
DELETE
COMMIT,
ROLLBACK, and
SAVEPOINT
GRANT and
REVOKE
first_name
AS
Name
or
SELECT first_name Name FROM student_details;
FROM
student_details;
SQL Operators:
Mainly used in the WHERE clause, HAVING clause to filter the data to be selected
2 types:
1. Comparison Operators
Used to compare the column data with specific values in a condition
Used along with the SELECT statement to filter data based on specific conditions
2. Logical Operators (AND, OR, NOT)
NOTE:
When logical operators are combined in a SELECT statement, then the order in which the
statement is processed is:
1) NOT
2) AND
3) OR
IN
Similar to OR Operator.
SELECT ENAME, SAL from EMP where SAL IN(2000, 3000, 4000)
BETWEEN...AND
SELECT ENAME, SAL from EMP where SAL BETWEEN 2000 AND 3000
IS NULL
o
Is used to display all the rows for columns that do not have a value.
LIKE
o
List all rows in a table whose column values match a specified pattern
select ENAME, SAL, COMM from EMP where SAL LIKE '2%' OR
SAL LIKE '_00%'
Descending Order
o SELECT ENAME, SAL from EMP ORDER BY SAL DESC
NOTE:
Column Numbers can also be used in place of Column Names
o SELECT EMPNO, ENAME from EMP ORDER BY 1, 2
Aliases defined in the SELECT Statement can be used in ORDER BY Clause (as shown in
the example below)
Using Expressions in ORDER BY clause
o SELECT ENAME, SAL, SAL*1.2 AS new_salary FROM EMP
WHERE SAL*1.2 > 3000
ORDER BY new_salary;
Built-in SQL functions that operate on groups of rows and return one value for the
entire group.
COUNT
o SELECT COUNT (*) from EMP where JOB = 'MANAGER'
MAX
o To get maximum value from a column (can be of any type)
SELECT MAX(JOB) from EMP
MIN
o To get minimum value from a column (can be of any type)
o SELECT MIN(SAL) from EMP
AVG
o To get average value of a numeric column
o SELECT AVG(SAL) from EMP
SUM
o To get average value of a numeric column
o SELECT SUM(SAL) from EMP
DISTINCT
o To get Number of distinct jobs
SELECT COUNT (DISTINCT JOB) from EMP
When WHERE, GROUP BY and HAVING clauses are used together in a SELECT
statement, then:
i.
ii.
Then the rows that are returned after the WHERE clause are grouped based on the
GROUP BY clause.
iii.
Finally, any condition(s) on the group function(s) in the HAVING clause is applied to
the grouped rows before the final output is displayed.
NOTE:
In the above scenario, when adding a new row, the data-type of the value and the column
must match in both the tables.
TRUNCATE
DROP
from a table
2.
(DML) until
(DDL)
transaction is
than DELETE
committed
3.
Grant or Access
privileges &
relationships with
other tables should
be established again.
4.
where E_UID = w
OR
DELETE from Emp
Add a Column:
ALTER TABLE Emp ADD Emp_Name varchar2(30)
Drop a Column:
ALTER TABLE Emp DROP Emp_Name varchar2(30)
Modify a Column:
ALTER TABLE Emp MODIFY Emp_ID integer
o Check
Can be defined in 2 ways:
o Column-level definition
o Table-level definition
PRIMARY KEY
o At Column level
Emp_ID integer CONSTRAINT p_key PRIMARY KEY
o At Table level
CREATE table Emp
(
id integer,
name varchar2(20),
CONSTRAINT p_key1 PRIMARY KEY(id)
);
At Column level
CREATE TABLE order_items
(
order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY,
prod_id number(5) CONSTRAINT pd_id_fk REFERENCES prod(prod_id)
prod_name char(20));
o At Table level
CREATE TABLE order_items
(
order_id number(5),
prod_id number(5),
prod_name char(20),
CONSTRAINT od_id_pk PRIMARY KEY (order_id),
NOT NULL
Ensures all rows in the table contain a definite value for the column which is
specified as not null
Emp_ID integer CONSTRAINT nn_key NOT NULL
UNIQUE
Ensures that a column or a group of columns in each row have a distinct value.
Column can have a null value but the values cannot be duplicated.
o At Column level
Emp_ID integer CONSTRAINT u_key UNIQUE
o At Table level
CREATE table Emp
(
id integer,
name varchar2(20),
CONSTRAINT u_key1 UNIQUE(id)
);
CHECK
o At Column level
Emp_Gender char(1) CHECK (Emp_Gender IN(M, F))
o At Table level
CREATE table Emp
(
id integer,
Emp_Gender char(1),
CONSTRAINT gender_chk CHECK (Emp_Gender IN(M,F)));
NOTE:
The integrity constraints can be defined at column level or table level.
Precision is the number of digits in a number. Scale is the number of digits to the right of
the decimal point in a number. For example, the number 123.45 has a precision of 5 and a
scale of 2.
DESCRIBE Emp or DESC Emp
SQL Joins:
Used to relate information in different tables.
Part of the SQL query that retrieves rows from two or more tables.
SQL Join condition is used in the SQL WHERE Clause of select, update, delete
statements.
If a SQL join condition is omitted or if it is invalid the join operation will result in a
Cartesian product. [For e.g., if the first table has 10 rows and the second table has 20
rows, the result will be 10 * 20, or 200 rows i.e. this query will take a long time to
execute.]
2 TYPES:
Equi Joins
Uses equal sign as comparison operator
o SQL OUTER JOIN
o SQL INNER JOIN
Equi Joins
I.
II.
SQL Views:
Selective amount of data can be seen from one or more tables
Used to restrict access to database (for security)
Hides complexity
CREATE VIEW ABC AS
Select Ename, Sal
FROM Emp