Basic SQL
Basic SQL
Dr. Byunggu Yu 1
Creating a domain type (rename a domain type)
e.g., create domain fullname char(30)
A1, A2, ..., An are attribute names and D1, D2, ..., Dn are domain types.
Note, there are three types of constraints: primary key (Ai, Aj, ..., Al),
foreign key (Ai, Aj, ..., Al) references relation-name <on delete cascade>
<on update cascade>, and check (predicate)
Dr. Byunggu Yu 2
e.g. create table employee (
e-num integer not-null,
e-name fullname,
e-age integer,
dept-num integer,
primary key (e-num),
foreign key (dept-num) references department
on update cascade
on delete cascade,
check (e-age <= 100)
)
Note, the available predicates in "check" is discussed in Section 3 (the
"where" clause of SQL DML).
Modify Relation Schema (Add or Delete an attribute)
alter table relation-name add A D
alter table relation-name drop A
Note, "A" is an attribute and "D" is a domain type.
Note, when an attribute is added to a relation, all tuples in the relation are
assigned "null" as the value for the new attribute.
We cannot give any constraint (or not-null) on the new attribute.
e.g. alter table employee add e-office char(30)
alter table employee drop e-office
Dr. Byunggu Yu 3
The query expression will be discussed in Section 3 (select ~ from ~ where
~). Note, (attribute names) is optional.
drop view view-name
e.g. create view dept-emp (d-num, d-name, emp-num, emp-name) as
select department.dept-num, dept-name, e-num, e-name
from department, employee
where department.dept-num = employee.dept-num
Dr. Byunggu Yu 4
2. Instance Modification: DML (Data Manipulation
Language) Features of SQL (1 of 2)
Inserting Tuples into a Relation
Type 1: insert into relation-name <(attribute names)> values (tuple value)
Type 2: insert into relation-name <(attribute names)> query-expression
Note, "(attribute names)" is optional. This option enables us to change the
order of domains.
Type1 e.g. insert into employee
values (001, "Sam Murlas", 35, 1101)
e.g. insert into employee (e-name, e-num, dept-num, e-age)
values ("Sam Murlas", 001, 1101, 35)
Type 2 uses SQL query-expression. SQL query-expression (i.e., select ~
from ~ where ~ ) is discussed in Section 3 in detail. So, this section gives
only one simple example.
Type2 e.g. insert into account (branch-name, account-number, balance)
select branch-name, loan-number, 200
from loan
where branch-name = "Laramie"
Note, the result schema of the subquery must be the same as (attribute
names) in "into" clause.
Dr. Byunggu Yu 5
Deleting Tuples from a Relation
delete from relation-name <where P>
Note, P is a predicate. See the "where" clause in Section 3.
Updating Tuples
update relation-name set assignment <where P>
Note, P is a predicate. See the "where" clause in Section 3.
Dr. Byunggu Yu 6
3. Extraction of Information from the underlying
database: DML (Data Manipulation Language)
Features of SQL (2 of 2)
Structure of query-expression
select * or L
from relation-name1 (or query-expression) <as temporary-name1>, ...,
relation-nameN (or query-expression) <as temporary-nameN>
<where P1>
<group by attribute-names <having P2> >
<order by attr-name asc (or desc), attr-name asc (or desc), ...>
"select" clause
• * means there is no projection
• L is a list. L can include constant, A, f(A), and arithmetic expression
(e.g., 0.3*A) involving A, where A is an attribute of the cartesian
product of the relations in "from" clause and function f is an
aggregation function. Aggregation functions: avg(), min(), max(),
sum(), count().
"from" clause
If a query-expression is used in "from" clause, "as temporary-name" is
mandatory since we need the name of the result relation of the sub-query.
This clause produces a cartesian product of every relation in the clause. This
single big relation is used by the other clauses as the base relation.
"where" clause
The predicate P1 can involve so many things including +, -, *, /, >, <, >=,
<=, <>, =, and, or, not, like, between~and~, is null, in (query-expression),
Dr. Byunggu Yu 7
some (query-expression), all (query-expression), exists (query-expression),
unique (query-expression), and every attribute of the "from" clause.
"group by" clause
Group the tuples having the same values on the given attributes. "having"
clause is to filter the groups. P2 is applied after the grouping. Thus, rather
than raw attributes, aggregation functions are used.
"order by" clause
Ascending order (i.e., asc) is default. Represents the order in which the
result tuples are placed in the result relation of the query. Sorting is very
expensive.
Simple Selection
select * from employee
select * from employee where dept-num = 1101
Dr. Byunggu Yu 8
Union, Intersect, and Set-Difference (unlike select, duplicates are
eliminated)
Assumptions: checking schema = {custID, acc-num, balance}
saving schema = {custID, acc-num, balance}
customer schema = {custID, name, phone, street, city, zip}
Note, "union all", "intersect all", "except all" can be used to allow
duplicates.
Dr. Byunggu Yu 9
Note#1 Duplicates
"select ~" query does not eliminate duplicates in the result set!
You can use "select distinct ~" to eliminate duplicates. Note, "select all ~"
is allowed but "all" is default.
However, set operations, such as union, intersect, and except, on two
"select~" queries eliminate duplicates from the result relation automatically.
Use "all" to allow duplicates.
Aggregation functions ignore null. But, count(*) function counts that since
there is a tuple.
e.g.,
relation r
A B C
1 4 null
1 3 null
2 2 1
Dr. Byunggu Yu 10
Natural Join and Theta Join (inner, left outer, right outer, full outer
joins)
Assumptions:
relation r relation s
A B C B C D
1 2 2 2 3 4
1 2 3 3 3 1
2 3 3 1 1 1
Examples:
r inner join s on r.B = s.B
(Note, inner join is, in fact, theta join)
A r.B r.C s.B s.C D
1 2 2 2 3 4
1 2 3 2 3 4
2 3 3 3 3 1
Same values
Dr. Byunggu Yu 11
r natural full outer join s
A B C D
1 2 2 null
1 2 3 4
2 3 3 1
null 1 1 1
Join Types: inner join, left outer join, right outer join, full outer join
Join Conditions: natural, on P, and using (attribute names), where P is a
predicate.
You choose one of join types and one of join conditions
Dr. Byunggu Yu 12
Natural Join: r natural inner join s = r inner join s using (every common
attribute)
Theta Join: r inner join s on P (Note, P is the theta)
Left Outer Join: r natural left outer join s
Right Outer Join: r natural right outer join s
Full Outer Join: r natural full outer join s
Note, the condition "natural" is the same as "using (every common attribute)"
More Examples
Assumptions: checking schema = {custID, acc-num, balance, branchID}
saving schema = {custID, acc-num, balance, branchID}
customer schema = {custID, name, phone, street, city, zip}
branchs schema = {branchID, branchName, city, zip, assets}
Dr. Byunggu Yu 13
select branchName, city
from branches
where assets < some (select assets from branches where city="Laramie")
Note, "some" is "at least one".
select branchName, city
from branches
where assets > all (select assets from branches where city="Laramie")
select name
from customer
where exists (select * from checking where
checking.custID=customer.custID)
select name
from customer
where not exists (select * from checking where
checking.custID=customer.custID)
select name
from customer
where unique (select custID from checking where
checking.custID=customer.custID)
select name
from customer
Dr. Byunggu Yu 14
where not unique (select custID from checking where
checking.custID=customer.custID)
select r, p
from (select name, phone, balance
from customer C, checking K
where C.custID=K.custID) as r(n, p, b)
where b >= 2000
Dr. Byunggu Yu 15