Structured Query Language (SQL) : By: Shahinaz Azab 2009
Structured Query Language (SQL) : By: Shahinaz Azab 2009
Language(SQL)
by:
Shahinaz Azab
2009
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
A. Create Command
6
7
B. Drop Table
Sometimes we may decide that we need to get rid of a table
in the database for some reason.
8
C. Alter Command
ALTER TABLE statement is used to add or drop columns in an
existing table.
- ALTER TABLE table_name ADD column_name datatype
Result:
9
Data Manipulation Language
A. INSERT Command
INSERT INTO "table_name" ("column1", "column2", ...)
VALUES ("value1", "value2", ...)
Example(1)
INSERT INTO Store_Information (store_name, Sales, Date)
VALUES ('Los Angeles', 900, 'Jan-10-1999')
10
Example (2)
INSERT INTO table_name (column1, column2,...) VALUES (value1,
value2,....)
Insert a New Row
This “Students" table:
12
B. Update Command
UPDATE "table_name"
SET "column_1" = [new value]
WHERE {condition}
UPDATE Person SET Address = ‘241 El-haram ', City = ‘Giza' WHERE
LastName = ‘El-Sayed'
Result:
14
C. Delete Command
DELETE FROM "table_name"
WHERE {condition}
Table Store_Information
store_name Sales Date
15
Truncate Table
16
Simple Queries
Select <attribute list >
From < table list>
Where <condition>
- select *
from departments;
17
Simple Queries (Cont’d)
18
Comparison Conditions
= Equal
> greater than
>= greater than or equal
< less than
<= less than or equal
<>not equal
19
Other Comparison Conditions
20
Other Comparison Conditions (Cont’d)
- Select last_name, salary
from employees
where salary between 1000 and 3000;
- Select first_name
from employees
where first_name Like ‘_s%’;
21
Logical Conditions
• AND
• OR
• NOT
22
Arithmetic Expressions
- Order of precedence: * , / , +, -
You can enforce priority by adding parentheses
23
Order by Clause
(ASC, DESC)
24
Types of Join
25
Join Queries (Inner Join)
26
Ambiguous Column Names
27
SQL Alias
28
Self Join
29
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
30
Outer Join
31
Equijoins and Non-Equijoins
32
Sub-Queries
Select name
from Employee
where Dno in
( select Dnumber from dept
where location=‘giza’)
33
Sub-Queries (Cont’d )
34
Nested Queries (Cont’d)
35
Union Operator
Select dnumber
From department where MRGSSN= 10 One
Union Result
Select dnumber
From dept_locations
Where dlocation=‘GIZA’
36
Union Operator ( Cont’d)
37
Exists Condition
38
Exists Condition
Select name
From employee
Where Not Exists ( select * from dependent where
ssn=Essn)
39
Exists Condition With DML
40
Aggregate Functions
COUNT , SUM , MAX, MIN and AVG
41
Grouping
42
Grouping (Cont’d)
43
C. Data Control Language
Grant Revoke
45
Views
46
Advantages of Views
47
Views
48
Views (Cont’d)
49
Views ( cont’d)
50
Views with Check option
51
Indexes
52
Index
S2 Jones 10 Paris
London
53