Chapter 3: Introduction To SQL: Database System Concepts, 7 Ed
Chapter 3: Introduction To SQL: Database System Concepts, 7 Ed
Database System Concepts - 7th Edition 3.2 ©Silberschatz, Korth and Sudarshan
History
Database System Concepts - 7th Edition 3.3 ©Silberschatz, Korth and Sudarshan
SQL Parts
Database System Concepts - 7th Edition 3.4 ©Silberschatz, Korth and Sudarshan
Data Definition Language
Database System Concepts - 7th Edition 3.5 ©Silberschatz, Korth and Sudarshan
Domain Types in SQL
char(n). Fixed length character string, with user-specified length
n.
varchar(n). Variable length character strings, with user-specified
maximum length n.
int. Integer (a finite subset of the integers that is machine-
dependent). -231 through 231 - 1
smallint. Small integer (a machine-dependent subset of the
integer domain type). -215 through 215 - 1
numeric(p,d). Fixed point number, with user-specified precision of
p digits, with d digits to the right of decimal point. (ex.,
numeric(3,1), allows 44.5 to be stores exactly, but not 444.5 or
0.32)
real, double precision. Floating point and double-precision
floating point numbers, with machine-dependent precision.
float(n). Floating point number, with user-specified precision of at
least n digits. -1.79E + 308 through 1.79E + 308.
More are covered in Chapter 4.
Database System Concepts - 7th Edition 3.6 ©Silberschatz, Korth and Sudarshan
Create Table Construct
An SQL relation is defined using the create table command:
create table r
(A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
Database System Concepts - 7th Edition 3.7 ©Silberschatz, Korth and Sudarshan
Create Table Construct
An SQL relation is defined using the create table command:
create table r
(A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
Example:
create table instructor (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
Database System Concepts - 7th Edition 3.8 ©Silberschatz, Korth and Sudarshan
Given the following schema of Emp table. Write
the SQL statement to create the Emp table.
Emp ( eid, ename ,age ,salary )
Database System Concepts - 7th Edition 3.9 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Emp (
eid INTEGER,
ename CHAR(10),
age INTEGER,
salary REAL
)
Database System Concepts - 7th Edition 3.10 ©Silberschatz, Korth and Sudarshan
Given the following schema of Dept
table. Write the SQL statement to
create the Dept table.
Dept ( did, budget, managerid )
Database System Concepts - 7th Edition 3.11 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Dept (
did INTEGER,
budget REAL,
managerid INTEGER
)
Database System Concepts - 7th Edition 3.12 ©Silberschatz, Korth and Sudarshan
Given the following schema of EMPLOYEE
table. Write the SQL statement to create the
Emp table.
EMPLOYEE ( FNAME , MINIT , LNAME ,
SSN , ADDRESS, Gander , SALARY, DNO)
Database System Concepts - 7th Edition 3.13 ©Silberschatz, Korth and Sudarshan
CREATE TABLE EMPLOYEE (
FNAME VARCHAR(15),
MINIT CHAR(1),
LNAME VARCHAR(15),
SSN CHAR(9),
ADDRESS VARCHAR(30),
Gander CHAR,
SALARY DECIMAL(10,2),
DNO INT
)
Database System Concepts - 7th Edition 3.14 ©Silberschatz, Korth and Sudarshan
Integrity Constraints in Create Table
Database System Concepts - 7th Edition 3.15 ©Silberschatz, Korth and Sudarshan
And a Few More Relation Definitions
create table student (
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_name) references department);
Database System Concepts - 7th Edition 3.16 ©Silberschatz, Korth and Sudarshan
And more still
Database System Concepts - 7th Edition 3.17 ©Silberschatz, Korth and Sudarshan
Given the following schema of Emp table. Write
the SQL statement to create the Emp table.
Make sure that your statement specifies the
PRIMARY KEY and enforce the constraint that
Employee name should not be null.
Emp ( eid, ename ,age ,salary )
CREATE TABLE Emp (
eid INTEGER,
ename CHAR(10),
age INTEGER,
salary REAL
)
Database System Concepts - 7th Edition 3.18 ©Silberschatz, Korth and Sudarshan
Emp ( eid, ename ,age ,salary )
CREATE TABLE Emp (
eid INTEGER,
ename CHAR(10) not null,
age INTEGER,
salary REAL,
PRIMARY KEY (eid)
)
Database System Concepts - 7th Edition 3.19 ©Silberschatz, Korth and Sudarshan
Given the following schema of Dept table. Write the
SQL statement to create the Dept table. Make sure
that your statement specifies the PRIMARY.
Database System Concepts - 7th Edition 3.20 ©Silberschatz, Korth and Sudarshan
Dept ( did, budget, managerid )
CREATE TABLE Dept (
did INTEGER,
budget REAL,
managerid INTEGER,
PRIMARY KEY (did)
)
Database System Concepts - 7th Edition 3.21 ©Silberschatz, Korth and Sudarshan
Given the following schema of Emp table and
the schema of Dept table. Write the SQL
statement to create the Emp and Dept tables.
Make sure that your statement specifies the
PRIMARY KEY and any FOREIGN KEYS.
Database System Concepts - 7th Edition 3.22 ©Silberschatz, Korth and Sudarshan
Emp ( eid, ename ,age ,salary )
Dept ( did, budget, managerid )
Database System Concepts - 7th Edition 3.23 ©Silberschatz, Korth and Sudarshan
Write the SQL statements that define the
relational schema (tables) for this database.
Assume that actor_id, play_id, year_born,
and year_written are all integers, and that
name, title, author, and character_name are
strings. Be sure to define primary keys and
foreign keys constraints.
Database System Concepts - 7th Edition 3.24 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Actor (
actor_id INTEGER PRIMARY KEY,
Name VARCHAR(100),
year_born INTEGER );
Database System Concepts - 7th Edition 3.25 ©Silberschatz, Korth and Sudarshan
ASS
Database System Concepts - 7th Edition 3.26 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 3.27 ©Silberschatz, Korth and Sudarshan
Updates to tables
Insert
insert into instructor values ('10211', 'Smith', 'Biology', 66000);
Delete
Remove all tuples from the student relation
delete from student
Drop Table
drop table r
Alter
alter table r add A D
where A is the name of the attribute to be added to relation
r and D is the domain of A.
All exiting tuples in the relation are assigned null as the
value for the new attribute.
alter table r drop A
where A is the name of an attribute of relation r
Dropping of attributes not supported by many databases.
Database System Concepts - 7th Edition 3.28 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Actor (
actor_id INTEGER PRIMARY KEY,
h t able
i n eac
Name VARCHAR(100),
ecor d
r ta r
year_born INTEGER ); I n se
Database System Concepts - 7th Edition 3.29 ©Silberschatz, Korth and Sudarshan
insert into Actor values (101, ‘abood’, 1999)
Database System Concepts - 7th Edition 3.30 ©Silberschatz, Korth and Sudarshan
Basic Query Structure
Ai represents an attribute
Ri represents a relation
P is a predicate.
The result of an SQL query is a relation.
Database System Concepts - 7th Edition 3.31 ©Silberschatz, Korth and Sudarshan
The select Clause
Database System Concepts - 7th Edition 3.32 ©Silberschatz, Korth and Sudarshan
select name
from instructor
instructor
Database System Concepts - 7th Edition 3.33 ©Silberschatz, Korth and Sudarshan
The select Clause
NOTE: SQL names are case insensitive (i.e., you may use
upper- or lower-case letters.)
E.g., Name ≡ NAME ≡ name
Some people use upper case wherever we use bold font.
select name
from instructor
Database System Concepts - 7th Edition 3.34 ©Silberschatz, Korth and Sudarshan
List the account numbers of
existing accounts
account
Database System Concepts - 7th Edition 3.35 ©Silberschatz, Korth and Sudarshan
List the account numbers of
existing accounts
SELECT account_number
FROM account
account
Database System Concepts - 7th Edition 3.36 ©Silberschatz, Korth and Sudarshan
Fined the firms and the number
of its employees
firm
Database System Concepts - 7th Edition 3.37 ©Silberschatz, Korth and Sudarshan
Fined the firms and the number
of its employees
SELECT firmName, employees
FROM firm
firm
Database System Concepts - 7th Edition 3.38 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
Database System Concepts - 7th Edition 3.39 ©Silberschatz, Korth and Sudarshan
select distinct dept_name
from instructor
dept_name
Physics
Finance
History
.Comp. Sci
.Elec. Eng
Biology
Music
instructor
Database System Concepts - 7 Edition
th
3.40 ©Silberschatz, Korth and Sudarshan
List all the cities that have branches
without duplication
branch
Database System Concepts - 7 Edition
th
3.41 ©Silberschatz, Korth and Sudarshan
List all the cities that have branches
without duplication
branch
Database System Concepts - 7 Edition
th
3.42 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
instructor
Database System Concepts - 7th Edition 3.43 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
instructor
Database System Concepts - 7th Edition 3.44 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)
Database System Concepts - 7th Edition 3.45 ©Silberschatz, Korth and Sudarshan
List the account numbers
each with quarter of its
balance with head “quarter”
account
Database System Concepts - 7th Edition 3.46 ©Silberschatz, Korth and Sudarshan
List the account numbers
each with quarter of its
balance with head “quarter”
SELECT account_number, balance/4 as quarter
FROM account
account
Database System Concepts - 7th Edition 3.47 ©Silberschatz, Korth and Sudarshan
List the account numbers
each with its balance added
to the balance 10% with head
“tbalace”
account
Database System Concepts - 7th Edition 3.48 ©Silberschatz, Korth and Sudarshan
List the account numbers
each with its balance added
to the balance 10% with head
“tbalace”
SELECT account_number, balance*1.1 as tbalance
FROM account
account
Database System Concepts - 7th Edition 3.49 ©Silberschatz, Korth and Sudarshan
List the branches each with the half of
its assets amount with header “hASS”
branch
Database System Concepts - 7 Edition
th
3.50 ©Silberschatz, Korth and Sudarshan
List the branches each with the half of
its assets amount with header “hASS”
branch
Database System Concepts - 7 Edition
th
3.51 ©Silberschatz, Korth and Sudarshan
The where Clause
The where clause specifies conditions that the result must satisfy
Corresponds to the selection predicate of the relational algebra.
To find all instructors in Comp. Sci. dept
select name
from instructor
where dept_name = 'Comp. Sci.'
instructor
Database System Concepts - 7th Edition 3.52 ©Silberschatz, Korth and Sudarshan
The where Clause
SQL allows the use of the logical connectives and, or, and not
The operands of the logical connectives can be expressions involving
the comparison operators <, <=, >, >=, =, and <>.
Comparisons can be applied to results of arithmetic expressions
To find all instructors in Comp. Sci. dept with salary > 80000
select name
from instructor
where dept_name = 'Comp. Sci.' and salary < 80000
Database System Concepts - 7th Edition 3.53 ©Silberschatz, Korth and Sudarshan
List the branches in “Brooklyn” City
branch
Database System Concepts - 7th Edition 3.54 ©Silberschatz, Korth and Sudarshan
List the branches in “Brooklyn” City
branch
SELECT branch_name
FROM branch
WHERE branch_city = 'Brooklyn'
Database System Concepts - 7th Edition 3.55 ©Silberschatz, Korth and Sudarshan
List the branches in “Brooklyn” City
and the branches whose assets are
exceeded 7000000 $
branch
Database System Concepts - 7 Edition
th
3.56 ©Silberschatz, Korth and Sudarshan
List the branches in “Brooklyn” City
and the branches whose assets are
exceeded 7000000 $
SELECT branch_name
FROM branch
WHERE branch_city = 'Brooklyn' or assets > 7000000
branch
Database System Concepts - 7 Edition
th
3.57 ©Silberschatz, Korth and Sudarshan
Translate the following operation to SQL
statement
course_id, year ( semester=“Spring” Λ year=2018 (section))
Find the records of all sections have been taken in Painter building at any of time slots (A or D) only
Database System Concepts - 7th Edition 3.61 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Actor (
actor_id INTEGER PRIMARY KEY,
Name VARCHAR(100),
year_born INTEGER );
Database System Concepts - 7th Edition 3.62 ©Silberschatz, Korth and Sudarshan
INSERT INTO Actor VALUES (10, ‘xxxxx’, 2000)
Database System Concepts - 7th Edition 3.63 ©Silberschatz, Korth and Sudarshan
Find the records of all sections have
been taken in Painter building
section
Database System Concepts - 7th Edition 3.64 ©Silberschatz, Korth and Sudarshan
SELECT * FROM section WHERE building = ‘Painter ’
Database System Concepts - 7th Edition 3.65 ©Silberschatz, Korth and Sudarshan
Find the records of all sections have
been taken in Painter building at any of
time slots (A or D) only
section
Database System Concepts - 7th Edition 3.66 ©Silberschatz, Korth and Sudarshan
SELECT * FROM section WHERE building = ‘Painter ’ AND ( time-
slot_id = ‘A’ OR time-slot_id = ‘D’ )
Database System Concepts - 7th Edition 3.67 ©Silberschatz, Korth and Sudarshan
Find the records of all sections have
been taken at Fall semester and take
place in Packard building
section
Database System Concepts - 7th Edition 3.68 ©Silberschatz, Korth and Sudarshan
SELECT * FROM section WHERE semester = ‘Fall’ AND building =
‘Packard ’
Database System Concepts - 7th Edition 3.69 ©Silberschatz, Korth and Sudarshan
List the account numbers
each with its balance added
to the balance 20% with head
“twbalance ”
account
Database System Concepts - 7th Edition 3.70 ©Silberschatz, Korth and Sudarshan
SELECT account_number, balance*1.2 as twbalance
FROM account
Database System Concepts - 7th Edition 3.71 ©Silberschatz, Korth and Sudarshan
salary >85000 (instructor)
instructor
Database System Concepts - 7th Edition 3.72 ©Silberschatz, Korth and Sudarshan
SELECT *
FROM instructor
WHERE salary >85000
Database System Concepts - 7th Edition 3.73 ©Silberschatz, Korth and Sudarshan
dept_name=“Physics” salary > 90,000 (instructor)
instructor
Database System Concepts - 7th Edition 3.74 ©Silberschatz, Korth and Sudarshan
SELECT *
FROM instructor
WHERE dept_name=“Physics” AND salary > 90,000
Database System Concepts - 7th Edition 3.75 ©Silberschatz, Korth and Sudarshan
Translate the following operation to SQL
statement
course_id, semester, year ( semester=‘Spring’ Λ year=2018 V building = ‘
Packard’ (section))
Database System Concepts - 7th Edition 3.77 ©Silberschatz, Korth and Sudarshan
The from Clause
Database System Concepts - 7th Edition 3.78 ©Silberschatz, Korth and Sudarshan
instructor X teaches
select
from instructor, teaches
instructor
teaches
Database System Concepts - 7th Edition 3.79 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 3.80 ©Silberschatz, Korth and Sudarshan
The from Clause
Database System Concepts - 7th Edition 3.81 ©Silberschatz, Korth and Sudarshan
Examples
Database System Concepts - 7th Edition 3.82 ©Silberschatz, Korth and Sudarshan
Examples
Database System Concepts - 7th Edition 3.83 ©Silberschatz, Korth and Sudarshan
Find the names of all employees who
work at marketing department
Dtable
Etable
Database System Concepts - 7th Edition 3.84 ©Silberschatz, Korth and Sudarshan
Find the names of all employees who
work at marketing department
SELECT ename
FROM etable, dtable
WHERE [Link] = [Link] and dname = ‘marketing’
Dtable
Etable
Database System Concepts - 7th Edition 3.85 ©Silberschatz, Korth and Sudarshan
Find the teacher name who teach the course with
course id ‘CS-319’
SELECT name
FROM instructor , teaches
WHERE [Link] = [Link] and course_id = ‘CS-319’
instructor
teaches
Database System Concepts - 7th Edition 3.86 ©Silberschatz, Korth and Sudarshan
The Rename Operation
Database System Concepts - 7th Edition 3.87 ©Silberschatz, Korth and Sudarshan
The Rename Operation
Database System Concepts - 7th Edition 3.88 ©Silberschatz, Korth and Sudarshan
The Rename Operation
s me
l ia na
e a n
b l tio
t a ela
r r
co
Database System Concepts - 7th Edition 3.89 ©Silberschatz, Korth and Sudarshan
Find the teacher name who teach the course with
course id ‘CS-319’ [rename the tables names]
instructor
teaches
Database System Concepts - 7th Edition 3.90 ©Silberschatz, Korth and Sudarshan
Find the teacher name who teach the course with
course id ‘CS-319’ [rename the tables names]
SELECT [Link]
FROM instructor ins , teaches tch
WHERE [Link] = [Link] and tch.course_id = ‘CS-319’
instructor
teaches
Database System Concepts - 7th Edition 3.91 ©Silberschatz, Korth and Sudarshan
Self Join Example
c es
Relation emp-super ren
fe
re
i ch
wh ute
ute trib
b t
tt ri er a
a th
an no
s a
ha wn
ble o
ta i ts
a
h en
w
Find the supervisor of “Bob”
Find the supervisor of the supervisor of “Bob”
Can you find ALL the supervisors (direct and indirect) of
“Bob”?
Database System Concepts - 7th Edition 3.92 ©Silberschatz, Korth and Sudarshan
Find the supervisor of “Bob”
SLECT supervisor
FROM emp-super emp, emp-super sup
WHERE [Link] = ‘Bob’
SELECT [Link]
FROM emp-super emp, emp-super sup
WHERE [Link] = ‘Bob’ and [Link]= [Link]
Database System Concepts - 7th Edition 3.93 ©Silberschatz, Korth and Sudarshan
String Operations
Database System Concepts - 7th Edition 3.94 ©Silberschatz, Korth and Sudarshan
String Operations
percent ( % ).
The % character matches any substring.
• '%dar%'
• 'Intro%' beginning with “Intro”.
• ‘%d’ Ending with “d”
Database System Concepts - 7th Edition 3.95 ©Silberschatz, Korth and Sudarshan
String Operations
Database System Concepts - 7th Edition 3.96 ©Silberschatz, Korth and Sudarshan
Find the names of all instructors whose name ending with the
substring “an”.
SELECT [Link]
FROM instructor I
WHERE [Link] like '%an'
instructor
Database System Concepts - 7th Edition 3.97 ©Silberschatz, Korth and Sudarshan
Find the course id and the names of all instructors whose teach courses
that have id ending with the substring “101”.
instructor
teaches
Database System Concepts - 7th Edition 3.98 ©Silberschatz, Korth and Sudarshan
Find the course id and the names of all instructors whose teach courses
that have id ending with the substring “101”.
SELECT [Link], T.course_id
FROM instructor I, teaches T
WHERE [Link] = [Link] and course_id like '%101'
instructor
teaches
Database System Concepts - 7th Edition 3.99 ©Silberschatz, Korth and Sudarshan
Find the course id and the names of all instructors whose name beginning
with the char ‘E’.
instructor
teaches
Database System Concepts - 7th Edition 3.100 ©Silberschatz, Korth and Sudarshan
Find the course id and the names of all instructors whose name beginning
with the char ‘E’.
SELECT [Link], T.course_id
FROM instructor I, teaches T
WHERE [Link] = [Link] and [Link] like ‘E%'
instructor
teaches
Database System Concepts - 7th Edition 3.101 ©Silberschatz, Korth and Sudarshan
String Operations
Database System Concepts - 7th Edition 3.102 ©Silberschatz, Korth and Sudarshan
String Operations (Cont.)
e r(s)
upp er(s)
low (s)
trim
Database System Concepts - 7th Edition 3.103 ©Silberschatz, Korth and Sudarshan