SECTION D
2)Display the item number and total cost for each order line (total cost = no of items X item cost). Name the calculated column TOTAL COST.
Answer:
SELECT item_number, no_of_items * item_cost “TOTAL COST”
FROM ORDER_LINE
4)Display the order number and client number from the ORDER table. Output the result in the format. Client <clientno> ordered <orderno>
Answer:
SELECT ‘Client ‘+ clientno+’ordered ‘+ orderno AS result
FROM ORDER
6)Display the client name and order date for all orders using the traditional method.
Answer:
SELECT name, order_date
FROM
CLIENT c INNER JOIN ORDER o
ON (c.clientno = o.clientno);
7)Repeat query (7) but also display all clients who have never ordered anything.
Answer:
SELECT name, order_date
FROM
CLIENT c LEFT OUTER JOIN ORDER o
ON (c.clientno = o.clientno);
8) Display the client name and order date for all orders using the natural join keywords.
SELECT name, order_date
FROM
CLIENT NATURAL JOIN ORDER;
9) Display the client name and order date for all orders using the JOIN . . . USING method.
SELECT name, order_date
FROM CLIENT c JOIN ORDER o
USING (clientno);
10) Display the client number, order date and shipping date for all orders where the shipping date is between three and six months after the order date.
SELECT clientno, order_date, shipping_date
FROM
CLIENT c,
ORDER o,
ORDER_LINE ol
WHERE c.clientno = o.clientno
AND o.orderno = ol.orderno
AND shipping_date BETWEEN ADD_MONTHS(shipping_date,3) AND ADD_MONTHS(shipping_date,6);
16) Display the order number, order line number and the shipping date. If the shipping date is null, display the string <not shipped yet>.
SELECT orderno, order_line_number, NVL(shipping_date,’<not shipped yet>’)
FROM ORDER_LINE
18)Display the clientno and total value for all orders placed by that client. Output the result in the following format: Client <clientno> has placed orders to the value of <total value>
SELECT ‘Client ‘+clientno+’ has placed order to the value of ‘+ SUM(no_of_items*item_cost)
FROM ORDER_LINE
GROUP BY clientno
19) Display all clients whose name begins with the letter J or contains the letter M anywhere or contains E as the third letter.
SELECT *
FROM CLIENT
WHERE UPPER(name) LIKE ‘J%’
OR upper(name) LIKE ‘%M%’
OR
Upper(name) LIKE ‘??E%’
20)Using a set operator, display the client number of all clients who have never placed an order.
Answer:
SELECT clientno
FROM CLIENT
MINUS
SELECT clientno
FROM ORDER
21)Using a set operator, display the client number of all clients who have ever placed an order and whose name does not contain the string Sm.
SELECT clientno
FROM CLIENT
WHERE INSTR(name,’Sm’) = 0
INTERSECT
SELECT clientno
FROM
ORDER
23)Display the client name for all clients who have placed an order where any order line has more than 3 items. Do not use a table join anywhere in your query.
SELECT name
FROM CLIENT c,
ORDER o,
ORDER_LINE ol
WHERE c.clientno = o.clientno
AND o.orderno = ol.orderno
AND ol.no_of_items> 3
26)Display the earli.
Measures of Dispersion and Variability: Range, QD, AD and SD
SECTION D2)Display the item number and total cost for each order l.docx
1. SECTION D
2)Display the item number and total cost for each order line
(total cost = no of items X item cost). Name the calculated
column TOTAL COST.
Answer:
SELECT item_number, no_of_items * item_cost “TOTAL
COST”
FROM ORDER_LINE
4)Display the order number and client number from the ORDER
table. Output the result in the format. Client <clientno> ordered
<orderno>
Answer:
SELECT ‘Client ‘+ clientno+’ordered ‘+ orderno AS result
FROM ORDER
6)Display the client name and order date for all orders using the
traditional method.
Answer:
SELECT name, order_date
FROM
CLIENT c INNER JOIN ORDER o
ON (c.clientno = o.clientno);
7)Repeat query (7) but also display all clients who have never
ordered anything.
Answer:
SELECT name, order_date
FROM
CLIENT c LEFT OUTER JOIN ORDER o
ON (c.clientno = o.clientno);
8) Display the client name and order date for all orders using
the natural join keywords.
SELECT name, order_date
2. FROM
CLIENT NATURAL JOIN ORDER;
9) Display the client name and order date for all orders using
the JOIN . . . USING method.
SELECT name, order_date
FROM CLIENT c JOIN ORDER o
USING (clientno);
10) Display the client number, order date and shipping date for
all orders where the shipping date is between three and six
months after the order date.
SELECT clientno, order_date, shipping_date
FROM
CLIENT c,
ORDER o,
ORDER_LINE ol
WHERE c.clientno = o.clientno
AND o.orderno = ol.orderno
AND shipping_date BETWEEN
ADD_MONTHS(shipping_date,3) AND
ADD_MONTHS(shipping_date,6);
16) Display the order number, order line number and the
shipping date. If the shipping date is null, display the string
<not shipped yet>.
SELECT orderno, order_line_number, NVL(shipping_date,’<not
shipped yet>’)
FROM ORDER_LINE
18)Display the clientno and total value for all orders placed by
that client. Output the result in the following format: Client
<clientno> has placed orders to the value of <total value>
SELECT ‘Client ‘+clientno+’ has placed order to the value of
‘+ SUM(no_of_items*item_cost)
3. FROM ORDER_LINE
GROUP BY clientno
19) Display all clients whose name begins with the letter J or
contains the letter M anywhere or contains E as the third letter.
SELECT *
FROM CLIENT
WHERE UPPER(name) LIKE ‘J%’
OR upper(name) LIKE ‘%M%’
OR
Upper(name) LIKE ‘??E%’
20)Using a set operator, display the client number of all clients
who have never placed an order.
Answer:
SELECT clientno
FROM CLIENT
MINUS
SELECT clientno
FROM ORDER
21)Using a set operator, display the client number of all clients
who have ever placed an order and whose name does not contain
the string Sm.
SELECT clientno
FROM CLIENT
WHERE INSTR(name,’Sm’) = 0
INTERSECT
SELECT clientno
FROM
ORDER
23)Display the client name for all clients who have placed an
4. order where any order line has more than 3 items. Do not use a
table join anywhere in your query.
SELECT name
FROM CLIENT c,
ORDER o,
ORDER_LINE ol
WHERE c.clientno = o.clientno
AND o.orderno = ol.orderno
AND ol.no_of_items> 3
26)Display the earliest shipping date in the format:
DD/MON/YYYY
SELECT to_date(MIN(shipping_date),’DD/MON/YYYY’)
FROM ORDER_LINE
27)Display the order number and the number of months since
the order was shipped for all orders that have been shipped in
the last year (365 days). (Hint: Unshipped orders will have a
null value).
SELECT o.orderno,
MONTHS_BETWEEN(shipping_date,order_date)
FROM ORDER o,
ORDER_LINE ol
WHERE o.orderno = ol.orderno
AND shipping_date BETWEEN sysdate AND sysdate-365
29)Display the surname for all employees who earn less the
average salary of all those employees in the department with the
lowest average salary.
Answer:
SELECT lastname
FROM EMPLOYEE e
WHERE salary < (SELECT MIN(avg_salary) FROM (SELECT
AVG(salary) avg_salary, department
FROM EMPLOYEE
GROUP BY department))
5. 30)Display the client number and the total number of orders the
client has placed for all clients who have placed at least one
order in 2011. (Hint: Use exists).
U did not done this query and my Professor needs it.
Distance-Vector Routing (graded)
When should distance-vector routing be used, and how do
distance-vector routing protocols work? Can you provide some
examples? How does distance vector complete its convergence
process?
Dynamic Routing (graded)
Discuss how dynamic routing occurs as compared to static
routing. What is a stub network, and when might these be used?
Lecture_07_S12014.ppt
Welcome to
CIS 2002: DATABASE DESIGN AND IMPLEMENTATION
Semester 1, 2014 – LECTURE 7
*
6. *
*
3.wmf
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been copied and communicated to you
by or on behalf of The University of Southern
Queensland pursuant to Part VA of the
Copyright Act
1968 (
the Act
).
The material in thi
s communication may be subject to
copyright under the Act. Any further copying or
communication of this material by you may be the subject
of copyright protection under the Act.
Do not remove this notice.
7. USAGE OF SLIDES
Use of these lecture slides is restricted to teaching staff and
students enrolled in this course. All students who use these
slides should have acquired the prescribed texts.
These slides are for the personal use of students on the Study
Desk only.
Students should not copy slides, allow third parties access to
the slides or distribute the slides.
Copyright of these slides vests in the university and, where
applicable, Cengage Education or Pearson (publishers of the
textbooks)
*
*
READINGS – WEEK 9Study Book, Module 7
*
*
TERNARY RELATIONSHIPSWe have concentrated on
analysing the relationship between two entities so
farSometimes, three (or more) entities can be involved in a
relationship which require careful analysisTernary relationships
involve three entities.
*
*
8. TERNARY RELATIONSHIPSConsider the business rules:An
employee may have one or more skills and a skill may belong to
many employeesAn employee may work on one or more projects
and a project may require many employeesA project requires
one or many skills to complete and a skill may be required in
several projects.We need to store information about which skills
each employee uses on each project
*
*
TERNARY RELATIONSHIPS
*
*
EMPLOYEE
SKILL
PROJECT
EMPLOYEE(empid#,name,date started)
SKILL(skillid#,skill name)
PROJECT(projectid#,name,loc,budget)
TERNARY RELATIONSHIPSThe ring structure is called a
closed relationship which may indicate redundancy.
When this happens, we resolve the three M:M relationships with
a single intersecting entity in the middle tying in all the other
entities.
*
9. *
TERNARY RELATIONSHIPS
*
*
EMPLOYEE
SKILL
PROJECT
SKILL USAGE
EMPLOYEE(empid#,name, date started)
SKILL(skillid#,skill name)
PROJECT(projectid#,name, loc, budget)
SKILL USAGE(empid#, skillid#, projectid#)
SKILL USAGE stores information about each SKILL an
EMPLOYEE uses in each PROJECT.
QUATERNARY RELATIONSHIPS?Similar to ternary
relationship but four entities are linked via a single intersection
entity – rare indeed!
*
*
NORMALISATIONThe process of organising relational
database in order to minimise redundancy and dependencies.
Involves splitting larger relations (or tables) into smaller and
less redundant tables and defining relationships between
10. them.First Normal Form (1NF)Second Normal Form (2NF)Third
Normal Form (3NF)Boyce-Codd Normal Form (BCNF)Fourth
Normal Form (4NF)Fifth Normal Form (5NF), …..
A relational database is considered “normalised” when it is in
3NF!
*
*
REDUNDANCYRefers to unnecessary data storage or
duplication of storage
Best done before relations are derived
*
*
REDUNDANT ENTITIESSometimes system is poorly
understood, especially during early phases of analysisCases of
redundant entities:Different users may have different names for
the same entityTwo entities have so much in common that they
are really one
*
*
REDUNDANT ATTRIBUTESWhen unwanted attributes are
present in a relationOccurs most often when a sub-type is
required but not implemented
*
*
11. DERIVED VALUESOccurs when the value in a column can be
calculated using values from other columnsExample:“Age”
column will have derived values if there is already a column for
“date of birth”“total” column will have derived values if it is
multiplication of the values from “rate” column and “quantity”
column.We should avoid storing derived values However, we
store them in certain cases to solve performance problems or
when we wish to query the column frequently
*
*
REDUNDANT RELATIONSHIPSWhen unwanted relationships
are created between relationsMost common in closed
loopsExample: If we resolved a ternary relationship using three
intersection entities rather than one
*
*
The DATABASE DESIGN PROCESS
AN EMPLOYEE WORKS FOR ONE COMPANY AND IS
ASSIGNED TO ONE DEPARTMENT. A DEPARTMENT MAY
HAVE MANY EMPLOYEES.
For EMPLOYEE, we store EMPID, NAME,
SKILLTYPE,SKILLNAME and MULTIPLE ADRESSES, (e.g.
home, mailing).
For DEPARTMENT, we store DEPTID and NAME.
For COMPANY, we store COMPANYNAME.
*
*
12. STEP 1: DRAW DATA MODEL (ER DIAGRAM)
( I will not do this in detail here – YOU ARE ALL EXPERTS)
*
*
The DATABASE DESIGN PROCESS
STEP 2: DERIVE RELATIONS / ENTITY LIST
COMPANY(companyname#)
DEPARTMENT(deptid#,name)
EMPLOYEE(empid#,name,skilltype,skillname,
((address)),companyname#,deptid#)
*
*
The DATABASE DESIGN PROCESS
Step 3: Normalise relations.
This is our task to learn this week!
After the top-down modelling process, we perform a bottom-up
check on our work using Normalisation. Often, we find that
there are potential problems.
The most dangerous problems are called redundancy and
anomalies.
*
*
The DATABASE DESIGN PROCESS
13. Anomalies
Anomalies are irregularities in the database design.
Consider the following relation:
ACTIVITY(studno#, activity, fee)
100 Skiing 200
150 Swimming 50
175 Squash 50
200 Swimming
50Consider:delete stud# 100
*
*
*
3
*
*
Anomalies
ACTIVITY(studno#, activity, fee)
100 Skiing 200
150 Swimming 50
175 Squash 50
200 Swimming
50Consider:delete stud# 100 (loss of information about
“skiing”)
14. *
*
*
Anomalies
ACTIVITY(studno#, activity, fee)
100 Skiing 200
150 Swimming 50
175 Squash 50
200 Swimming 50
Scuba Diving 150Consider:delete stud# 100 (loss of
information about skiing)insert “Scuba Diving” activity and its
corresponding fee as $150
CAN THIS BE DONE?
*
*
*
Anomalies
ACTIVITY(studno#, activity, fee)
100 Skiing 200
150 Swimming 50
175 Squash 50
200 Swimming
50Consider:delete stud# 100 (loss of information about
15. skiing)insert “Scuba Diving” activity with $150 fee (can’t insert
until at least a student takes up scuba diving activity)
*
*
*
Anomalies
ACTIVITY(studno#, activity, fee)
100 Skiing 200
150 Swimming 50
175 Squash 50
200 Swimming
60Consider:delete stud# 100 (loss of information about
skiing)insert “Scuba Diving” activity with $150 fee (can’t
insert until at least a student takes up scuba diving
activity)update swimming cost from $50 to $60
*
*
*
Anomalies
ACTIVITY(studno#, activity, fee)
100 Skiing 200
150 Swimming 50
16. 175 Squash 50
200 Swimming
60Consider:delete stud# 100 (loss of information about
skiing)insert “Scuba Diving” activity with $150 fee (can’t
insert until at least a student takes up scuba diving
activity)update swimming cost from $50 to $60 – if it is only
updated for studno# 200 (inconsistent results since its not
updated for studno# 150)
*
Solution
The problem is that the ACTIVITY relation has information
about the activity and student together