Professional Documents
Culture Documents
Purpose Development
process
Data Definition Define the Database Database
Language (DDL) CREATE Tables, indexes, Views Physical Design
Establish Foreign Keys Database
DROP or Truncate tables Maintenance
Data Manipulation Maintain the data: Database
Language (DML) INSERT Data Implementation
1
Structured Query Language (SQL) fall 2006
2
Structured Query Language (SQL) fall 2006
Examples:
create table customer
(customer-ID number(6.0) NOT NULL,
customer-name char(20) NOT NULL ,
customer-street char(30),
customer-city char(30),
constraint primary key (customer-ID))
create table branch
(branch-name char(15) NOT NULL,
branch-city char(30),
assets integer,
constraint primary key (branch-name))
create table account
(account-number integer (10) NOT NULL,
branch-name char(15) check (branch-name IN (‘A’, ‘B’, ‘C’)),
date-opened date default sysdate,
balance decimal(8.2),
constraint primary key (account-number),
constraint foreign key (branch-name) references branch)
create table depositor
(customer-name char(20) NOT NULL,
account-number char(10),
constraint primary key (customer-name, account-number),
constraint foreign key (account-number) references account,
3
Structured Query Language (SQL) fall 2006
4
Structured Query Language (SQL) fall 2006
to match.
ON UPDATE SET NULL When a customerID is changed in the
CUSTOMER table, any customerID in the
ORDER table that matches the old customerID
is set to NULL
ON UPDATE SET When a customerID is changed in the
DEFAULT CUSTOMER table, any customerID in the
ORDER table that matches the old customerID
is set to predefined default value
c) ASSERTIONS:
An assertion is a general condition that we wish the database always to
satisfy.
The syntax for creating assertion is:
CREATE ASSERTION assertionName
CHECK (searchCondition)
Domain constraints and referential integrity constraints are special cases
of assertions.
Example of assertions: the sum of all customer invoices must be less than
or equal to the credit limits assigned to that customer.
When an assertion is made, the system tests it for validity, and tests it
again on every update that may violate the assertion.
Assertion example 1: The sum of all loan amounts for each branch must be
less than the sum of all account balances at the branch.
create assertion sum-constraint check
(not exists (select * from branch
where (select sum(amount) from loan
where loan.branch-name = branch.branch-name)
>= (select sum(amount) from account
where loan.branch-name = branch.branch-name)))
Assertion example 2:
Every loan has at least one borrower who maintains an account with a
minimum balance or $1000.00
create assertion balance-constraint check
(not exists (select * from loan
where not exists ( select *
from borrower, depositor, account
where loan.loan-number = borrower.loan-number
5
Structured Query Language (SQL) fall 2006
b)Inserting a record that has some null attributes requires identifying the
fields that actually get data:
INSERT INTO PRODUCT (PRODUCT_ID, PRODUCT_DESCRIPTION,
PRODUCT_FINISH, STANDARD_PRICE, PRODUCT_ON_HAND)
VALUES (1, ‘End Table’, ‘Cherry’, 175, 8);
6
Structured Query Language (SQL) fall 2006
7
Structured Query Language (SQL) fall 2006
Syntax:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
Clauses of the SELECT statement:
Clause Descriptions
SELECT List the columns (and expressions) that should be returned
from the query
8
Structured Query Language (SQL) fall 2006
9
Structured Query Language (SQL) fall 2006
FROM CUSTOMER
GROUP BY CUSTOMER_STATE
HAVING COUNT(CUSTOMER_STATE) > 1;
10
Structured Query Language (SQL) fall 2006
2. Complex Queries:
We can use the sets operations to combine the results of two or
more queries into a single result query (complex Query).
2.1 Relational sets operations:
2.1.1 UNION operation:
The UNION of two query results, A and B, is a table containing all rows
that are in the first query result A or the second query result B or
both
Combines rows from two or more queries without including duplicate
rows.
The output (union of multiple queries) together into a single result
table
11
Structured Query Language (SQL) fall 2006
Example:
Combined Query Result
(SELECT city (SELECT city
FROM branch FROM branch
WHERE city IS NOT NULL) WHERE city IS NOT NULL)
UNION UNION CORRESPONDING BY city
(SELECT city (SELECT city
FROM propertyForRent FROM propertyForRent
WHERE city IS NOT NULL) WHERE city IS NOT NULL)
If ALL is specified, the result can include duplicate rows.
12
Structured Query Language (SQL) fall 2006
Branch
BranchNo Street City ZipCode
01 Deer Red 22 Amman 14270
02 Main 12 Alssalt 11310
03 Shardy 8 Aqaba 09146
04 Palace 31 Amman 22183
05 Gardens 27 irbid 21054
PropertyForRent
PropertyNO street city type rent ownerID EmpNo BranchNO
P110 A 12 Amman House 1000 O11 E06 O1
P114 A 26 Amman Flat 400 O12 E08 03
P115 B 05 Alssalt Flat 350 O15 E011 05
P210 C 08 Irbid Flat 700 O28 E06 02
P211 D 36 Aqaba House 1200 048 E06 03
P215 D 12 Alzarka Flat 540 044 E01 01
P217 F 12 Jarash House 860 011 E02 04
P218 G 05 Irbid Flat 320 032 E03 01
Employee
EmpNo Ename position sex DOB Salary BranchNo
E01 Samir Manager M 22-06-78 650 01
E03 Sawsan Assistant F 02-09-68 430 02
E05 Ali Supervisor M 03-06-88 490 05
E06 Hany Assistant M 07-03-81 310 03
E08 Layla Assistant F 28-12-84 330 04
E011 Rauf Manager M 30-11-76 570 02
E012 Amy supervisor f 20-10-75 425 01
13
Structured Query Language (SQL) fall 2006
ON clause performs the equality check for common columns of the two
tables
Multiple Table Join Example:
Assemble all information necessary to create an invoice for order number
1006:
SELECT CUSTOMER.CUSTOMER_ID, CUSTOMER_NAME,
CUSTOMER_ADDRESS, CITY, SATE, POSTAL_CODE, ORDER.ORDER_ID,
ORDER_DATE, QUANTITY, PRODUCT_DESCRIPTION,
STANDARD_PRICE, (QUANTITY * UNIT_PRICE)
FROM CUSTOMER, ORDER, ORDER_LINE, PRODUCT
2.2 SubQueries:
14
Structured Query Language (SQL) fall 2006
15
Structured Query Language (SQL) fall 2006
WHERE EXISTS
(SELECT * FROM PRODUCT
WHERE PRODUCT_ID = ORDER_LINE.PRODUCT_ID
AND PRODUCT_FINISH = ‘Natural ash’);
The EXISTS operator will return a TRUE value if the subquery
resulted in a non-empty set, otherwise it returns a FALSE
The subquery is testing for a value that comes from the outer query
Another example of Correlated Subquery: The following is a
correlated subquery. It contains the same tables in both levels of the
query
SELECT W1.Title, W1.Copy
FROM WORK W1
WHERE W1.Title IN
( SELECT W2.Title
FROM WORK W2
WHERE W1.Title = W2.Title
AND
W1.WorkID <> W2.WorkID);
Subquery Examples:
16
Structured Query Language (SQL) fall 2006
Trigger example: Suppose that the bank deals with balance overdrafts by:
Setting the account balance to zero
Creating a loan in the amount of the overdraft
Giving this loan a loan number identical to the account number of the
overdrawn account
The condition for executing the trigger is an update to the account
relation that results in a negative balance value. This trigger example in
SQL will be as follows:
create trigger overdraft-trigger after update on account
referencing new row as nrow
17
Structured Query Language (SQL) fall 2006
18
Structured Query Language (SQL) fall 2006
19
Structured Query Language (SQL) fall 2006
20
Structured Query Language (SQL) fall 2006
21
Structured Query Language (SQL) fall 2006
22