Lesson 4
Lesson 4
Language(SQL)
Lecturer: Rana
Salah
[email protected]
Room:3005
Made by:
Shahinaz S. Azab
Edited by:
Mona Saleh
Structured Query Language
(SQL)
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
• Data Control Language (DCL)
2
Database Schema
3
Data types
4
Database Constraints
• Not Null
• Unique Key
• Referential Integrity ( FK )
• Check
5
Data Definition Language (DDL)
• CREATE command
• ALTER command
• DROP command
• TRUNCATE command
6
CREATE Command
• Syntax
CREATE TABLE table_name
(column1 DATA_TYPE [CONS_TYPE CONS_NAME],
column2 DATA_TYPE [CONS_TYPE
CONS_NAME],... )
• Example
CREATE TABLE Students
(ID NUMBER(15) PRIMARY KEY, First_Name CHAR(50) NOT
NULL, Last_Name CHAR(50), Address CHAR(50), City
CHAR(50), Country CHAR(25), Birth_Date DATE);
7
CREATE TABLE DEPARTMENT
(DNUMBER NUMBER(15) CONTSRAINT DEPT_ID_PK PRIMARY
KEY, DNAME VARCHAR(30) NOT NULL, MGRSTARTDATE
DATE, MGRSSN NUMBER(15) CONTSRAINT DEPT_SSN_FK
FOREIGN KEY REFERENCES EMPLOYEE (SSN) );
• Syntax
DROP TABLE table_name
• Example
DROP TABLE Students
9
ALTER Command
• Syntax
- ALTER TABLE table_name ADD column_name datatype
CONSTRAINT
- ALTER TABLE table_name DROP COLUMN column_name
10
ALTER Example
City
LastName FirstName Address
(NN)
Pettersen Kari Storgt 20
11
Data Manipulation Language
(DML)
• INSERT Command
• UPDATE Command
• DELETE Command
• SELECT Command
12
INSERT Command
• Syntax
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)
• Example
INSERT INTO Store_Information (store_name, Sales, Date)
VALUES ('Los Angeles', 900, 'Jan-10-1999')
13
INSERT Example 2
14
INSERT Example 3
15
UPDATE Command
• Syntax
UPDATE table_name
SET column_1= new value, column_2= new value
WHERE condition
16
UPDATE Example
UPDATE Store_Information
SET Sales = 500
WHERE store_name = ‘Los Angeles’ AND Date = ‘Jan-08-1999’
Before After
store_name Sales Date store_name Sales Date
Jan-05- Jan-05-
Los Angeles $1500 Los Angeles $1500
1999 1999
Jan-07- Jan-07-
San Diego $250 San Diego $250
1999 1999
Jan-08- Jan-08-
Los Angeles $300 Los Angeles $500
1999 1999
Jan-08- Jan-08-
Boston $700 Boston $700
1999 1999
17
UPDATE Example 2
Result:
18
DELETE Command
• Syntax
DELETE FROM table_name
WHERE condition
19
DELETE Example
Before After
20
TRUNCATE Vs DELETE
• Syntax
22
Examples
• SELECT *
FROM departments;
23
DISTINCT Keyword
Output
DNo
• SELECT DISTINCT DNo
2
FROM employees; 3
24
DISTINCT Keyword (cont.)
• Example
Employees table
EmpNo Name DNo JobID
100 Ahmed 2 Sales_Rep
200 Mai 2 IT_PROG
300 Ali 2 Sales_Rep
400 Mahmoud 3 Sales_Rep
25
Comparison Conditions
= Equal
> greater than
>= greater than or equal
< less than
<= less than or equal
<>not equal
26
Other Comparison Conditions
27
Examples
• SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 1000 AND 3000;
• SELECT first_name
FROM employees
WHERE first_name LIKE ‘_s%’;
WHERE first_name LIKE ‘?s*’;
28
Logical Conditions
• AND
• OR
• NOT
29
Arithmetic Expressions
• Order of precedence: * , / , +, -
You can enforce priority by adding parentheses
30
Order by Clause
(ASC, DESC)
32
Join Queries (Inner Join)
33
Ambiguous Column Names
• When you query two tables with the same column name
There is a column called “ name “ in both tables.
There is a column called “ ID “ in both tables.
34
Example
35
Table Alias
36
Self Join
37
Outer Join
• LEFT JOIN: Return all rows from the left table, even if
there are no matches in the right table
• RIGHT JOIN: Return all rows from the right table,
even if there are no matches in the left table
• FULL JOIN: Return rows when there is a match in
one of the tables
38
Outer Join
39
Equijoins and Non-Equijoins
40
Example (Non-Equijoin)
job_grades employees
LowSal HighSal Grade EmpNo Name Salary
1000 4000 B 100 Ahmed 5000
4000 7000 A 200 Mai 3000
41
Sub-Queries
42
Examples
SELECT name
FROM Employee
WHEREDno IN (SELECT Dnumber
FROM dept
WHERE location=‘giza’)
43
Examples (Cont’d )
SELECT dname
FROM dept
WHERE deptno = (SELECT deptno
FROM emp
WHERE sal =
(SELECT MAX(sal)
FROM EMP));
44
Examples (Cont’d)
45
Union Operator
46
Examples
SELECT dnumber
FROM department WHERE MRGSSN= 10
One
Union Result
SELECT dnumber
FROM dept_locations
WHERE dlocation=‘GIZA’
47
Examples (Cont’d)
SELECT Name
FROM Employees
UNION
SELECT Name
FROM Employees_retired
48
Correlated Sub-Query
49
Exists Keyword
• Syntax
SELECT columns
FROM tables
WHERE EXISTS (sub-query );
50
Example
SELECT *
FROM suppliers
WHERE EXISTS
(SELECT *
FROM orders
WHERE
suppliers.supplier_id=
orders.supplier_id);
51
Example 2
SELECT name
FROM employee
WHERE NOT EXISTS (SELECT *
FROM dependent
WHERE ssn=Essn)
52
Exists Condition With DML
53
Aggregate Functions
54
Examples
• Find the sum, maximum, minimum and average
salaries of all employees
SELECT SUM (salary) , MAX (salary), MIN (salary),
AVG (salary)
FROM Employees
56
Grouping Examples
57
Grouping Examples (Cont’d)
58
C. Data Control Language
Grant Revoke
59
Views
60
Advantages of Views
61
Simple Views and Complex Views
62
Creating Views
63
Creating Views (Cont’d)
64
Retrieving Data from a View
• SELECT *
FROM vw_work_hrs
65
Views with Check option
66
Modifying a View
• Syntax
CREATE OR REPLACE VIEW view_name
AS
Sub-query
• Example
CREATE OR REPLACE VIEW vw_work_hrs
AS
SELECT Fname , Lname , Pname , Hours
FROM Employee, Project , Works_on
WHERE SSN=ESSN AND PNO=PNUMBER AND Dno = 5;
67
Removing a View
• Syntax
DROP VIEW view_name;
• Example
DROP VIEW vw_work_hrs
68
Indexes
69
Indexes (Cont’d)
S2 Jones 10 Paris
London
70
Index Creation Guidelines
The table is small or most queries are expected to retrieve more than 2%
to 4% of the rows in the table
The table is updated frequently
71
Indexes (Cont’d)
• Creation
CREATE INDEX index_name ON Table_name (column_name);
• Removing
DROP INDEX index_name;
72
SQLTutorials
73
Questions?
74