Database Handbook
Database Handbook
Database Handbook
Semester B - 2013
Semester B - 2013
Keep in touch!
There are plenty of ways to keep in touch with whats happening at UEL.
You can:
The QR code below will take you to the Events page on the UEL website,
or you can go direct to uel.ac.uk/events
Semester B - 2013
ontents
Semester B - 2013
1. Module Specification
Module Title:
Module Leader:
Database Systems
Level: 2
Juliette Alfred-Lewars
Email: [email protected]
Credit: 20
Pre-requisite:
None
Co-Leader
Naveed Hamid
Email: [email protected]
Pre-cursor None
Co-requisite:
None
ECTS credit:
Skills module: No
University-wide option: No
Semester B - 2013
50%
Learning
Outcomes
demonstrated
1, 5-8
50%
1-4
Peter Rob, Carlos Coronel and Steven Morris, 2013, Database Principles:
Fundamentals of Design, Implementation, and Management, 10th Int. Ed., Cengage
Course Technology Learning EMEA.
ISBN: 978-1-133-31197-3 or ISBN: 1-133-31197-0
Bijoy Bordoloi and Douglas Bock, Oracle SQL, 2004, Pearsons Education
LTD, Prentice Hall. ISBN: 0-13-101138-3
MATA-TOLEDO, R. A. AND CUSHMAN, P. K., 2000, Fundamentals of SQL
Programming. Schaums Outlines
Recommended
Semester B: 2013
Semester B: 2013
Indicative
Activity
Teaching and
Learning Time
(10 hrs per credit):
Student/Tutor
Activity: (e.g. lectures/seminars/tutorials/workshops/studio work etc)
Contact Time:
24 hours
Lectures
(11*2 hours + 2 hr revision)
24 hours
Tutorials/practicals
10 hours
Workshops and student presentations assessment
Student Learning Activity: (e.g. seminar reading and preparation/assignment
Time:
preparation/ background reading/ group work/portfolio/diary etc )
142 hours
Private study
Total hours:
200 hours
Semester B: 2013
2. Introduction
This is a 2nd level Module within the School of Architecture, Computing and
Engineering subject area. It is a mandatory requirement for all undergraduate
Computing and Business Information Systems programmes.
This module complements the level 2 modules in the Modular Scheme and has
links with the Systems Analysis module. Where the Systems Analysis module
gives a broad sweep of systems analysis ideas and methodologies, the Database
Systems module focuses on the design, concepts, processing and implementation
issues of Database Management Systems.
3. Aims
To promote a knowledge and understanding of
Data Modelling
Methods of organising files such that data can be stored and retrieved
efficiently
Semester B: 2013
5. Assessment
The assessment for this module is in two parts.
Part 1: Groups of threes Practical Task (Coursework) = 50%
You will be required to form groups of THREEs only, to develop the case study
by implementing an agreed design in Oracle. Your group will be expected to
identify the management needs of the organisation, design and implement a
realistic database system and develop a range of SQL queries based on the case
study to be given using only ORACLE.
Part 2: 1 hour written examination = 50%
There will be a One hour 30 minutes written examination at the end of the
module. The questions will be based on lecture topics.
6. Lecture Plans
6.1.
SUMMARY
Week
Starting
Lecture
Week 1 Introduction to DBMS:
What is a database and
database management
system
Tutorial
Tutorial Questions
Practical
Accessing Oracle Creating tables
Week 2
Tutorial Questions
and sample case
studies
Week 3
Design: Relational
Model
Tutorial Questions
and sample case
studies
Week 4
Design: Relational
Model (continued)
Tutorial Questions
and sample case
studies
Week 5
Normalisation
Tutorial Questions
Continue from
previous week
Semester B: 2013
10
Week
Starting
Lecture
Week 6 Structured Query
Language (SQL)
Tutorial
Tutorial Questions
Practical
10 and 30 SQL
questions
Week 7
Tutorial Questions
30 SQL questions
Structured Query
Language (SQL)
Advanced
(Assignment Part 1
submission during
tutorial for feedback)
Week 8
Query Optimisation
Tutorial Questions
30 SQL questions
Week 9
Transaction,
Concurrency and
Recovery
Tutorial Questions
30 SQL questions
and working on
assignment
Week 10
Integrity, Security,
Legal and ethical
Requirements
Tutorial Questions
30 SQL questions
and working on
assignment
Week 11
Network and
Distributed Database
Systems
Tutorial Questions
30 SQL questions
and working on
assignment
Week 12
Revision Week
Week 13
Exams
(Date to be advised)
Semester B: 2013
11
INTRODUCTION
Aims
Learning Objectives
Essential/Required
Reading
Other Reading
Date Chapters 1, 2, 3
Connolly and Begg Chapters 1, 9-11
Ritchie chapter 1
Semester B: 2013
12
DATA MODEL
Aims
Learning Objectives
Tutorial
Essential/Required
Reading
Other reading
Semester B: 2013
13
RELATIONAL MODELLING
Aims
Learning Objectives
understand optionality
Tutorial
Essential/Required
Reading
Other reading
Semester B: 2013
14
Topic 5
Subject
NORMALISATION
Aims
Learning Objectives
Tutorial
Exercises on Normalisation
Essential/Required
Reading
Other reading
Ritchie, Chapter 5.
Ramakrishnan and Gehrke, Chapter 19
Connolly & Begg, Chapters 13
Date, Chapter 11
Semester B: 2013
15
Aims
Learning Objectives
SQL Exercises
Colin Ritchie, Chapter 13
R.Sunderraman, Chapter 2
Ramakrishnan and Gehrke, Chapter 5
http://sqlcourse.com/intro.html
Semester B: 2013
16
QUERY OPTIMISATION
Aims
Learning Objectives
Tutorial
Essential/Required
Reading
Other reading
Semester B: 2013
17
Aims
Learning Objectives
Tutorial
Essential Reading
Other reading
Semester B: 2013
18
Aims
Learning Objectives
Tutorial/Practical
Essential/Required
Reading
Other reading
Semester B: 2013
19
Topic 11
Subject
Aims
Learning Objectives
By the end of this major topic, the student should be able to:
*
Client-Server systems
Distributed databases
ODBC
JDBC
Tutorial
Practical
Essential/Required
Reading
Semester B: 2013
20
Semester B: 2013
21
7. Tutorials
Week 1 Tutorial: Introduction
Semester B: 2013
22
5. Create a UML ERD for the following scenario. (Note: The word many
merely means more than one in the database modelling environment.)
A customer can rent many videotapes from the UEL Video store.
Each of the videotapes can be rented to many customers during
that period of time.
6. What is a Relation with regards to Relational database and what are its
basic constructs?
7. What is the Relational Oath and why is it important?
8. Discuss the different types of database architecture.
9. Evaluate and explain Codds twelve rules for determining how relational
database management system (DBMS) is.
Semester B: 2013
23
From the case study below, draw a UML Entity Relationship Model, identify all
entities, relationships, multiplicity, primary and foreign keys and list some
attributes for the following case study.
Present your ER solution to the class.
SCENARIO 1
FORD Cars
Ford is an organization that makes many models of cars, where a model
is characterised by a name and a suffix (such as GL or XL which
indicates the degree of luxury) and an engine size.
Semester B: 2013
24
Each model is made up of many parts and each part type may be used in
the manufacture of more than one model. Each part has a description
and an ID code. Each model of car is produced at just one of the firms
factories,
which
are
located
in
London,
Birmingham,
Bristol,
Semester B: 2013
25
Working in groups of 4s (based on size of tutorial class could be more) from the
case study below, draw a UML Entity Relationship Model, identify all entities,
relationships, multiplicity, primary and foreign keys and list some attributes for
the following case study.
SCENARIO 2
UNIVERSITY OF DAGENHAM
The University of Dagenham consists of several branches. Within each
branch there are several departments.
Semester B: 2013
26
Week 5: NORMALISATION
Objectives of Normalization
To act as a bottom-up alternative to E-R modelling
To act as a check on the results obtained by E-R modelling.
To act as a more precise method of resolving data modelling problems.
To depend less on intuition than E-R modelling.
To be useful when it is required to redesign existing problem files.
To eliminate or control data redundancy.
To eliminate insertion, deletion and update anomalies.
In a normalized database or file collection, every table or file contains data facts
about just one entity type; every record or row contains data facts about one
entity.
1. Describe with examples, the steps in reducing a data set to third normal
form.
2. The table below show some occurrences for the entity PATIENT. Normalise
the data showing clearly each step involved.
a. When normalising keep the following assumption in mind:
A given consultant is only available on certain dates.
You can also state any other assumptions, indicate the key fields
and functional dependencies.
Semester B: 2013
27
Patient
number
Patient
name
Patient
age
G.P.
number
G.P.
name
P1
D Adams
20
G1
T Wolfe
P2
G Green
37
G2
P3
P4
P5
P Orton
V William
N Dean
29
83
102
G3
G2
G3
Semester B: 2013
Date of
Hospital
Treatment
03.06.06
05.06.06
10.10.06
Const
ID
Consultant
name
C3
C3
C1
L Peach
03.06.06
10.06.06
V Green
L Peach
V Green
09.10.06
09.06.06
10.06.90
28
Sample
required
A Willis
A Willis
T Brown
Consultant
Room
number
1053
1053
1070
C3
C10
A Willis
T Brown
1053
1070
Sputum
Blood
C2
C2
C10
J Keen
J Keen
T Brown
1067
1067
1070
Blood
Urine
Sputum
Blood
None
Urine
ENAME
ALLEN
WARD
MARTIN
TURNER
ADAMS
MILLER
DEPTNO
30
30
30
30
20
10
SAL
1600
1250
1250
1500
1100
1300
4. Display all employees who were recruited during 1983, giving their name,
department and hire-date.
Semester B: 2013
29
7.
List the department numbers, name and the location in department name
order.
9.
10.b The hard bit. As above but list their managers' names as well as their
numbers.
10.c List the employee's name and salary and manager's name and salary for
all employees who earn more than their managers.
Semester B: 2013
30
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Semester B: 2013
31
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
Semester B: 2013
32
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
Semester B: 2013
33
Semester B: 2013
34
Semester B: 2013
35
Semester B: 2013
36
2. List and discuss some of the factors that influenced the evolution of the
DDBMS.
Semester B: 2013
37
Semester B: 2013
38
8. Practicals
Weeks 1 3 PRACTICALS: Introduction to Oracle
For the next three weeks you will be practicing using Oracle to develop a simple
database system. This will include learning the syntax for changing some of the
defaults of the page settings, creating tables, inserting rows and simple SQL
statements.
It is advised that you first try to type directly in Oracle, so that you will recognise
the difficulty in correcting mistakes, etc. Once, you have completed that
exercise, then, use Notepad to type the syntax, then copy and paste at the
SQL> prompt in Oracle. Remember to save your notepad document on your
home drive (H) for ease of reference and review.
Follow the steps below to log into Oracle.
1. Log into UEL network account.
2. To start Oracle:
Click on Start
Click on Programs
Click on Local Programming
Click on Oracle Client - Oracle81
Click on Application Development
Click on SQL Plus
3. Log into Oracle:
In the Log On Box, type your Oracle user name (this will be your
student number), eg, U1234567 (remember to include the U)
Once you have logged in you will see the SQL> prompt.
Semester B: 2013
39
4. If your password does not work you can try and reset it:
Click on:
Password
Start/Departmental
Menu/Computing/Change
Oracle
Please note, if this is the first time you are using Oracle at the
University, none of the tables below would be on your Oracle server.
Therefore, when you type Drop table Emp; Oracle will display table
does not exist. Do not worry. Start typing from CREATE, to create the
tables.
DROP
DROP
DROP
DROP
DROP
TABLE
TABLE
TABLE
TABLE
TABLE
EMP;
DEPT;
BONUS;
SALGRADE;
DUMMY;
Semester B: 2013
40
Once you have finished typing the above syntax to create the EMP table, you can
display a description of the table, eg, the column headings and data type, by typing
DESC EMP; then press the enter key..
To add rows in the tables, use the keyword/command IINSERT INTO. Notice, when
entering a number, you do not use quotation marks. Do so, only for text. Also, the
values must be typed in the same order as the column headings.
Once you have inserted the rows for the Emp table, you can view the table, by typing
SELECT * FROM EMP; You will notice that the columns are wrapping over type SET
NUM 7; this will reduce the numeric columns and make the display more readable.
Now, either type the slash key / and press the ENTER key, to repeat the last statement,
or type SELECT * FROM EMP; again to see the difference.
To select specific attributes from the EMP table, eg, if you want to know the names of
the employee and their salary. Type the following statement:
SELECT ENAME, SAL
FROM EMP;
Semester B: 2013
41
Continue creating the tables for the database and try the sample SQL statements.
INTO
INTO
INTO
INTO
INTO
SALGRADE
SALGRADE
SALGRADE
SALGRADE
SALGRADE
VALUES
VALUES
VALUES
VALUES
VALUES
(1,700,1200);
(2,1201,1400);
(3,1401,2000);
(4,2001,3000);
(5,3001,9999);
Once you have completed typing the sample database, try the following
simple SQL statement:
1. Select * from TAB: This will display all available tables in (your)
database.
Semester B: 2013
42
CREATING TABLES
To create tables, you need to know:
1.
2.
3.
4.
Table name
The attributes
Attribute/data types eg, number, date, char or varchar(2)
Column size
P_ID
D_ID
Patient
TS_ID P_ID
D_ID C_ID
T_Session
C_ID
Consultant
D_ID
Doctor
Semester B: 2013
43
Semester B: 2013
44
Semester B: 2013
45
Semester B: 2013
46
ADDITIONAL SYNTAX
EXAMPLES
To view all attributes and data types
in a table
DESC PATIENT;
DESC tablename;
To view whole table:
SELECT * FROM tablename; SELECT * FROM PATIENT;
To drop a table
DROP TABLE tablename;
To delete the values in a table but
retain the attributes and data types:
DELETE FROM tablename;
To delete a specify row in a table:
DELETE FROM tablename
WHERE searchCondition;
To alter a table:
(ie, adding, and changing columns
not the values in a table only the table
structure)
ALTER TABLE tablename
ADD column-name
dataType;
Semester B: 2013
47
UPDATE PATIENT
SET P_ADD = EASTCOTE, P_NAME =
JULIAN
WHERE P_ID = 002;
Not specifying a WHERE clause will automatically change
every value to the columns specified.
Repeat complete statement for each row you want
to update.
To create a view:
(ie, creating a temporary table from
an existing table in the database to
store specific information)
CREATE VIEW
NewTableName
(NewColumn, NewColumn,
etc)
AS SELECT ColumnName,
ColumnName, ColumnName
from tablename;
Semester B: 2013
48
Introduction to SQL
http://w3.one.net/~jhoffman/sqltut.htm
SQL Interperter Tutorial with live practice database.
http://sqlcourse.com/
SQL Commands.
Note brackets ( ) are part of the command, { } identify optional repeating
items, [ ] optional clauses.
Creating a new version of an existing table.
command
The
Semester B: 2013
49
create
table...as...
Global Insert
'LONDON')
update tablename
update emp set sal = 6000,
set columname=expression {,columname=expression} hiredate = sysdate
[where search_condition]
where job = 'PRESIDENT'
Deleting Rows
Creating Views
Semester B: 2013
50
Select Commands
Retrieve all rows
select ...
from ...
order by
columnname
[asc|desc]{
,columnname
[asc|desc]}
Grouping data
select ...
from ...
where search_condition
select salary
from emp
where name = 'FRED'
Comparison operators
= > < >= <=
<> != (means not equal)
Logical Operators
and
or
not
select name
from emp
where jobs = 'CLERK'
and salary > 1000
Semester B: 2013
51
select name
from emp
where ( job = 'CLERK or job = 'ANALYST')
and salary < 1500
Range searching
select name
from emp
where salary between 3000 and 5000
Pattern matching
select name
from emp
where name like 'A%'
select comm
from emp
where comm is not null (or the opposite, is null )
Joining Tables
To retrieve data from more than one table we must specify how the table are to be
joined. We are actually specifying what the relationship between one table and
another is, so referring to your ER diagram will help you identify what fields should
be joined between two tables. With the relationship between dept and emp being;
each department can have many employees and each employee works in one
department, the join is between the key field deptno in dept and the foreign key
deptno in emp.
Therefore to retrieve the dept name
and the employee in that dept the
two tables must be joined.
the join )
Semester B: 2013
52
specify
When two or more table are being join, if the fieldname has been used in both tables
the name must be prefixed by the name of the table, ie dept.deptno. It is usual
practise to use an alias in the from clause for simplicity. ie the
from clause becomes
from dept d, emp e so d and e are aliases
then the where clause can use these as
where d.deptno = e.deptno
in the select portion as there is no ambiguity you need not use the alias but it is good
practice to do so.
So our query becomes
DEPTNO
30
30
30
30
20
10
SAL
1600
1250
1250
1500
1100
1300
Semester B: 2013
53
Display all employees who were recruited during 1983, giving their name,
department and hire-date.
List the department numbers, name and the location in department name
order.
Semester B: 2013
54
2.
3.
4.
5.
6.
7.
8.
9.
10.
Semester B: 2013
55
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
Semester B: 2013
56
24.
25.
26.
27.
28.
29.
30.
Semester B: 2013
57
FROM CUS
SELECT *
FROM INVOICE
SELECT *
FROM CUS
SELECT *
FROM CUS
LIKE Clause
SELECT & FROM CUS
Semester B: 2013
58
FROM CUS
FROM CUS
FROM CUS
IN Clause
SELECT * FROM INVOICE
WHERE INV_DATE BETWEEN
1-dec-90 AND 13-jan-91;
SELECT * FROM CUS
WHERE SNAME
BETWEEN
E AND M;
SELECT * FROM CUS
WHERE SNAME
BETWEEN
E AND Lzz;
SELECT * FROM DELIVERY
WHERE PROD_NO
IN (1,3,5);
SELECT DISTINCT C_NO
FROM DELIVERY
WHERE PROD_NO
NOT IN (2,4,5);
SELECT * FROM CUS
WHERE TITLE IN (Miss, Ms, Mrs);
AND, OR and NOT can be used in combination to realize more complex conditions. Take care with
your logic.
Semester B: 2013
59
AGGREGATE FUNCTIONS
SELECT SUM (BALANCE)
FROM CUS;
FROM CUS
FROM CUS;
HAVING is used to perform a filtering effect similar to WHERE, after the GROUP BY. Non-output
agg. functions can be performed by a HAVING clause.
GROUP BY Clause
SELECT DEPARTMENT, AVG(AGE)
FROM EMPLOYEE
GROUP BY DEPARTMENT;
SELECT EMP_NO, AVG(AGE)
FROM EMPLOYEE
GROUP BY DEPARTMENT;
SELECT MAX(SUM(BALANCE))
FROM CUS
GROUP BY CITY;
The ORDER BY clause is used to determine the order that the output appears in. DESC indicates
descending order. You can order on more than 1 col.
HAVING Clause
SELECT DEPARTMENT, AVG(AGE)
Semester B: 2013
60
FROM EMPLOYEE
GROUP BY DEPARTMENT
HAVING AVG(AGE) < 27;
SELECT TERM, GROUP_NO
FROM GROUPS
GROUP BY TERM, GROUP_NO
HAVING COUNT(*) > 2;
SELECT DEPARTMENT, COUNT(*), AVG(AGE)
FROM EMPLOYEE
GROUP BY DEPARTMENT
HAVING COUNT(*) > 3
AND AVG(AGE) > 27;
SELECT DEPARTMENT, AVG(AGE)
FROM EMPLOYEE
GROUP BY DEPARTMENT
HAVING COUNT(*) > 1;
A join may involve one (self-join) or more tables. The join criteria often but not always involve
foreign keys. The OUTER JOIN is implemented using a (+) near one col. name.
Note that alternative syntax is used in SQL92
ORDER BY Clause
SELECT &
FROM CUS
ORDER BY SNAME;
SELECT *
FROM EMPLOYEE
WHERE DEPT_NO = 3
ORDER BY SALARY DESC;
SELECT *
FROM EMPLOYEE
ORDER BY JOB, SALARY, DESC;
SELECT SNAME, COMM/SAL
FROM EMPLOYEE
ORDER BY COMM/SAL;
A join may involve one (self-join) or more tables. The join criteria often but not always involve
foreign keys. The OUTER JOIN is implemented using a (+) near one col. name.
Note that alternative syntax is used in SQL92
Semester B: 2013
61
The subquery delivers one or more values which can be used in the WHERE clause of the outer
query. Note that values returned by the subquery cannot be output.
Subqueries (1)
SELECT NAME
FROM EMPLOYEE
WHERE DEPARTMENT =
(SELECT DEPARTMENT
FROM EMPLOYEE
WHERE NAME = Karen);
SELECT *
FROM PRODUCT
WHERE PROD_NO IN
(SELECT PROD_NO
FROM DELIVERY);
SELECT *
FROM PRODUCT
WHERE PROD_NO NOT IN
(SELECT PROD_NO
FROM DELIVERY);
Take great care when using ANY and ALL. < ANY means less that at least one. != ANY means
not equal to at least one. This might not be what you would expect.
Subqueries (2)
SELECT *
FROM EMPLOYEE
WHERE SALARY < ANY
(SELECT SALARY
FROM EMPLOYEE);
Semester B: 2013
62
SELECT *
FROM EMPLOYEE
WHERE SALARY != ANY
(SELECT SALARY
FROM EMPLOYEE);
SELECT *
FROM EMPLOYEE
WHERE SALARY >= ALL
(SELECT SALARY
FROM EMPLOYEE);
SELECT *
FROM EMPLOYEE
WHERE SALARY =
(SELECT MAX(SALARY)
FROM EMPLOYEE);
The first query contains a correlated subquery, which means that for every row in the outer query,
(every employee), all rows in the subquery have to be inspected. Correlated subqueries may be
identified as such by the presence in the subquery of a tables alias (A.) from the outer query.
Subqueries (3)
SELECT *
FROM EMPLOYEE A
WHERE SALARY > ALL
(SELECT SALARY
FROM EMPLOYEE B
WHERE B.EMP_NO != A.EMP_NO);
SELECT CITY, SUM(BALANCE)
FROM CUS
GROUP BY CITY
HAVING SUM(BALANCE) =
(SELECT MAX(SUM(BALANCE))
FROM CUS
GROUP BY CITY);
The set operations UNION, INTERSECT and MINUS are usually applied to separate tables
although they can also be applied to the results of subquerires. UNION has the effect of appending,
INTERSECT delivers rows which are in both sets, and MINUS rows in one but not the other.
Semester B: 2013
63
INTERSECT
SELECT STUD_NO, NAME, AGE
FROM CELL0);
SELECT * FROM VIOLINISTS
MINUS
SELECT * FROM PIANISTS
Unlike IN, EXISTS does not require a value to be passed from the outer query to subqueries. The
third query reads Who has bought all products?
Semester B: 2013
64
VIEW (oracle)
GRANT SELECT
ON EMP2
Granting
privileges on a
view
Here, a view is being used to restrict the columns of the EMPLOYEE tables that public can see. They can
select (not update, insert etc.)
Views (2)
CREATE VIEW EMP3 AS
SELECT *
FROM EMPLOYEE
WHERE DEPT_NO =
(SELECT DEPTNO
FROM EMPLOYEE
WHERE ENAME = USER
AND JOB = MANAGER;
Semester B: 2013
65
Views (3)
VIEWs can:
restrict rows
restrict columns
restrict rows and columns
calculate summary data
group by
join
subquery
The idea of a view is to give the user a restricted subset of data involving one or more base tables for
security reasons or because the user wants to take a particular view of the data.
Views (4)
CREATE VIEW LONCUS AS
SELECT * FROM CUS
WHERE CITY = London;
Restrict
columns
SELECT *
FROM VIEWS
WHERE VIEWNAME LIKE LON%;
Views can be used to restrict the rows and/or columns that the view user will be able to access. One view
can be built on another. It is possible to have a view of a join but not to join views. The DD contains
useful data on views that exist.
Semester B: 2013
66
Views (5)
CREATE VIEW SUMMARY1 AS
SELECT CITY, SUM(CRED_LIM)
MAX_RISK, SUM(BALANCE) DEBT
FROM CUS
GROUP BY CITY;
SELECT * FROM SUMMARY;
gives:
city
Dover
Leeds
London
max_risk
600
500
2500
debt
561
510
490
This view gives a useful summary by city of the total of credit limits and the total of current debts. When a
base table is updated, all the views based on it are also updated.
Views (6)
Updating Views (1)
UPDATE CUS
SET BALANCE = BALANCE + 100
WHERE C_NO = 5;
SELECT C_NO, BALANCE
FROM LONCUS
WHERE C_NO = 5;
Here, the base table has been updated.
The value of the balance field in one row of CUS has been added to. Since LONCUS is based on CUS and
customer no.5 is a London customer, the SELECT will show the new value of balance. The new value
shows through into the view.
Views (7)
Updating Views (2)
UPDATE LONCUS
SET BALANCE = BALANCE + 100
WHERE C_NO = 5;
SELECT C_NO, BALANCE
FROM CUS
WHERE C_NO = 5;
Here, the view LONCUS has been updated. The new value shows through into the base table CUS. There
are some rather severe restrictions on updates via views, as the next slide shows.
Semester B: 2013
67
Views (8)
Updating Views (3)
Restrictions on View Updates
DELETE
view is based on one table only
view must contain no GROUP BY
view must contain no DISTINCT
UPDATE
as above
view must contain no column expressions
INSERT
as above
all the non null columns from the base table are in the view
There are severe restrictions on updating via views. Note that only the SQL SELECT can access views
based on more than one table inserts, updates and deletes cannot.
Views (9)
Views of more than one table
CREATE VIEW STATEMENT
(NAME, ADDRESS, DATE, INVOICE, AMOUNT, PAYMENT) AS
SELECT TITLE, SNAME, STREET, CITY, POSTC,
INV_DATE,B.INV_NO,B.AMOUNT, C.AMOUNT
FROM CUS A,INVOICE B, PAYMENT C
WHERE A.C_NO = B.C_NO
AND B.INV_NO = C.INV_NO(+));
This view is used for printing statements at the end of each month. It will always reflect the latest state of
the base tables.
Note that this view cannot be updated since there is more than one base table.
GRANT statement
Database security in SQL is vested in the GRANT command
This command is used mostly by the DBA (DataBase Administrator)
There are two main forms of the GRANT command:
First form: priveleges w.r.t. users
Second form: privileges w.r.t. tabels and views.
The first form of GRANT is used to register new users and the second form is to give users access to tables
and views.
Semester B: 2013
68
DBA-only commands
User names and passwords are held in a catalog table on the database.
Semester B: 2013
69
All forms of GRANT may be used only by those with DBA privileges, except GRANT CONNECT TO to
change your own password.
ALTER
change table structure
DELETE
delete records
INDEX
create and drop indexes
INSERT
insert records
SELECT
permission to select
UPDATE
update records
ALL all the granters privileges
In the second form of the GRANT command, users are granted privileges with respect to individual tables
or views. With a VIEW, ALTER, INDEX and ALL are not allowed.
Syntax:
GRANT {privilege, ...|ALL} ON
<table-or-view>
TO {user|PUBLIC}
[WITH GRANT OPTION];
Authorizes grantee to grant all or
part of his privileges in turn to others
The privileges can be any combination of SELECT, INSERT, DELETE, UPDATE, ALTER and INDEX
for a table and just the first four for a view ALL grants all of the privileges possessed by the granter.
UPDATE may be followed by a list of columns, limiting the grantee to updating just those columns in the
table or view.
Semester B: 2013
70
Table Definitions
CUS
C_NO
TITLE
SNAME
INITS
STREET
CITY
POSTC
CRED_LIM
1
2
3
4
5
6
Mr
Miss
Mr
Mr
Ms
Mrs
Sallaway
Lauri
Jackson
Dziduch
Woods
Williams
GR
P
R
M
SQ
C
12 Fax Rd
5 Dux St
2 Lux Ave
31 Low St
17 Nax Rd
41 Cax St
London
London
Leeds
Dover
London
Dover
WC1
N1
LE1 2AB
DO2 9CD
E18 4WW
DO2 8WD
1000
500
500
100
1000
INVOICE
BALANCE
42.56
200
510
149.23
250.1
412.21
PAYMENT
INV_NO
C_NO
INV_DATE
AMOUNT
940
1002
1003
1004
1005
1006
1017
1
4
1
2
3
5
6
5-DEC-90
12-JAN-91
12-JAN-91
14-JAN-91
20-JAN-91
21-JAN-91
22-JAN-91
26.2
149.23
16.26
200
510
250.1
412.21
INV_NO
PMT_NO
PMT_DATE
AMOUNT
940
1005
1017
940
2
1
1
3
12-DEC-90
14-JAN-91
30-JAN-91
19-JAN-91
13
510
100
10
PRODUCT
DELIVERY
PROD_NO
DESCR
QIS
MINQ
REORDQ
PRICE
C_NO
PROD_NO
QTY
DEL_DATE
1
2
3
4
5
Bat
Ball
Hoop
Net
Rope
10
5
3
2
1
5
5
5
5
10
10
20
10
10
10
12
2
3
20
6
3
3
1
5
3
2
1
4
3
3
2
3
6
4
1
3-NOV-90
3-NOV-90
7-NOV-90
12-NOV-90
12-NOV-90
VIOLIN
PIANO
CELLO
STU_NO
NAME
AGE
STUD_NO
NAME
AGE
STUD_NO
NAME
AGE
1
2
4
Fred
Sally
David
10
11
10
2
4
5
Jane
David
Zena
12
10
11
4
6
David
Josey
10
11
STU_NO
NAME
AGE
Ashfak
12
FLUTE
Semester B: 2013
71
CUST
CNO
1
2
3
PURCHASE
NAME
Alan
Bill
Charles
CNO
1
1
2
PROD
PRNO
a
b
a
PRNO
a
b
GROUPS
TERM
GROUP_NO
MEMBER
1
1
1
1
1
1
2
2
2
2
1
1
1
2
2
2
10
10
1
1
1
2
3
4
5
6
3
2
5
7
CANDIDATE
CAND_NO
NAME
CONS_NO
PARTY
NO_OF_VOTES
1
2
3
4
5
6
9
7
8
10
21
29
41
50
52
Fred
Jim
Peter
John
Mike
Jane
Sue
Mary
Ulrike
U Li
Rosa
Patty
Astrid
Gordon
Ben
1
1
1
2
2
2
1
2
1
3
3
3
3
3
1
Labour
Cons
Liberal
Labour
SLD
Cons
SDP
Green
Indep
Red Guards
Simbianese
Simbianese
Liberal
Labour
Green
100
120
50
150
50
100
160
150
150
150
30
12
3
160
70
Semester B: 2013
72
DESCR
Apple
Ball
Week
No.
17-Sep-12
INDUCTION
24-Sep-12
TEACHING
01-Oct-12
TEACHING
08-Oct-12
TEACHING
15-Oct-12
TEACHING
22-Oct-12
TEACHING
29-Oct-12
TEACHING
05-Nov-12
TEACHING
12-Nov-12
TEACHING
19-Nov-12
10
TEACHING
26- Nov-12
11
TEACHING
03-Dec-12
12
TEACHING
10-Dec-12
13
TEACHING
17-Dec-12
STUD VAC
24-Dec-12
STUD VAC
31-Dec-12
STUD VAC
07-Jan-13
14
EXAMS
14-Jan-13
15
EXAMS/MARKING
21-Jan-13
16
MARKING
28-Jan-13
17
INDUCTION/FB
04-Feb-13
18
TEACHING/AB
11-Feb-13
19
TEACHING
18-Feb-13
20
TEACHING
25-Feb-13
21
TEACHING
04-Mar-13
22
TEACHING
11-Mar-13
23
TEACHING
Semester B: 2013
73
18-Mar-13
24
TEACHING
25-Mar-13
STUD VAC
01-Apr-13
STUD VAC
08-Apr-13
25
TEACHING
15-Apr-13
26
TEACHING
22-Apr-13
27
29-Apr-13
28
TEACHING
06-May-13
29
TEACHING
13-May-13
30
REVISION
20-May-13
31
EXAMS
27-May-13
32
EXAMS/MARKING
03-Jun-13
33
MARKING
10-Jun-13
34
FB
17-Jun-13
35
AB/COUNSELLING
TEACHING
24-Jun-13
STUD VAC
01-Jul-13
STUD VAC
08-Jul-13
STUD VAC
15-Jul-13
STUD VAC
22-Jul-13
STUD VAC
29-Jul-13
STUD VAC
05-Aug-13
STUD VAC
12-Aug-13
STUD VAC
19-Aug-13
36
STUD VAC/EXAMS
26-Aug-13
37
STUD VAC/MARKING
02-Sep-13
38
STUD VAC/FB/AB
09-Sep-13
16-Sep-13
STUD VAC
1
Semester B: 2013
INDUCTION
74