(SQL2) نظري 2 PDF
(SQL2) نظري 2 PDF
Advanced Database
LECTURE 2
Structured Query Language
(SQL)
Instructor Aisha Lusta
SQL Introduction
2
Integrity constraints
Create table
Alter table
o add (column, constraint)
o modify(column)
o drop(column,constraint)
Drop table
DML Commands
6
– Retrieval
• SELECT
– Update
• INSERT
• DELETE
• UPDATE
The select Clause
7
• SQL has one basic statement for retrieving information from a database;
• In the SELECT statement, users specify what the result of the query should be, and the DBMS decides
the operations and order of execution.
• SQL allows a table (relation) to have two or more tuples that are identical in all their attribute values
• SQL relations can be constrained to be sets by specifying PRIMARY KEY or by using the DISTINCT
option in a query
SELECT Basic Syntax
8
The where clause specifies conditions that the result must satisfy
To find all instructors in Comp. Sci. dept with salary > 800
select name
from instructor
where dept_name = ‘Comp. Sci' and salary > 80000
Comparison results can be combined using the logical connectives and, or, and not.
Comparisons can be applied to results of arithmetic expressions.
Select Example
12
find all instructors except the instructors in Comp. Sci dept, or the instructors in Music dept with
salaries = 70000
select name from instructor
where not (dept_name = ‘Comp. Sci’
Instructor
teaches
16
INNER JOIN vs. OUTER JOIN
17
INNER JOIN
These two queries are equivalent
1- SELECT emp_name, dname
FROM Employee INNER JOIN Department ON
Dno=dnum
Find the names of all instructors who have taught some courses and the course_id
Answer1: select name, course_id
from instructor , teaches
where instructor.ID = teaches.ID
The SQL allows renaming relations and attributes using the as clause:
old-name as new-name
Find the names of all instructors who have a higher salary than some
instructor in 'Comp. Sci’.
select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = 'Comp. Sci.’
Keyword as is optional and may be omitted
instructor as T ≡ instructor T
String Operations
22
SQL includes a string-matching operator for comparisons on character strings. The operator like uses
patterns that are described using two special characters:
percent ( % ). The % character matches any substring.
Find the names of all instructors whose name includes the substring “dar”.\
select name
from instructor
where name like '%dar%'
String Operations (Cont.)
23
Find the names of all instructors whose name starts with “A”
Select name
From instructor
Where name like “A%”;
Find the names of all instructors whose name starts with “A” , ends with “s”, and
includes only four letters.
Select name
From instructor
Where name like “A_ _s”;
25
Find the names of all instructors whose name starts with “A” , ends with
“d”, and includes at least five letters.
Select name
From instructor
Where name like “A_ _ _%d”
Find the names of all instructors whose name starts with “M” , the second
letter is “u” , ends with “a”, and includes at least four letters.
Select name
From instructor
Where name like “Mu_ %a”
Ordering the Display of Tuples
26
o union all,
o intersect all
o except all.
Null Values
31
It is possible for tuples to have a null value, denoted by null, for some of their
attributes
null signifies an unknown value or that a value does not exist.
The result of any arithmetic expression involving null is null
Example: 5 + null returns null
select name
from instructor
where salary is null
The predicate is not null succeeds if the value on which it is applied is not null.
The End