SQL Case Studies
SQL Case Studies
DATABASE DESIGN
Master Thesis Weiguang Zhang McMaster University- Computing and Software
Master Thesis Weiguang Zhang McMaster University- Computing and Software
Abstract
Typical relational database design examples in textbooks and undergraduate courses are
small and do not provide any real opportunity to practice the design, they simply illustrate
and illuminate the principles. On the other end of the spectrum are typical industrial
databases whose designs are complex and extensive, and so not suitable as a project for a
one term database course. The objective of this thesis is to design and develop a
collection of ten projects that would be usable as term projects in relational database
system design for a typical undergraduate database course. To this end a suite of ten case
studies are presented. Each project is taken from its informal specification to a relational
schema using entity-relationship modeling and its translation to relational model, to
database schema, to implementation of the database, to interactive SQL querying of the
installed database and finished with a simple application programmed in C using the
installed database and accessing it via embedded SQL.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
iv
Acknowledgments
I would like to express my gratitude to all of those who made it possible to complete this
thesis, in particular to my supervisors Dr. Antoine Deza and Dr. Frantisek Franek.
I appreciate the great aid and support from all the members of the Advanced Optimization
Laboratory.
I would also like to thank my family for their understanding and continuous support.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
v
Abbreviations
IBM: International Business Machines
ACM: Association of Computing Machinery
DBMS: Database Management System
SQL: Structured Query Language
ODBC: Open Database Connectivity
J DBC: J ava Database Connectivity
ER: Entity Relationship
PHP: Personal Home Page
API: Application Programming Interface
CLI: Call Level Interface:
ESQL: Embedded Structured Query Language
IE: Information Engineering
IDEF1X: Integrated Definition for Information Modeling
Master Thesis Weiguang Zhang McMaster University- Computing and Software
vi
List of Figures
Figure 1: Summary of aspects of the Logical and Physical Models ................................. 7
Figure 2: The University System Logical Model ........................................................... 10
Figure 3: The University System Physical Model .......................................................... 11
Figure 4: The Airline Reservation Logical Model .......................................................... 27
Figure 5: The Airline Reservation Physical Model ........................................................ 28
Figure 6: The Movie Rental Logical Model ................................................................... 35
Figure 7: The Movie Rental Physical Model .................................................................. 36
Figure 8: The Car Rental Logic Model ........................................................................... 44
Figure 9: The Car Rental Physical Model....................................................................... 45
Figure 10: The Course Registration Logical Model ......................................................... 52
Figure 11: The Course Registration Physical Model ........................................................ 53
Figure 12: The Emergency Room Logical Model ............................................................ 61
Figure 13: The Emergency Room Physical Model ........................................................... 62
Figure 14: The Property Rental Logical Model ................................................................ 70
Figure 15: The Property Rental Physical Model............................................................... 71
Figure 16: The Software Project Logical Model .............................................................. 81
Figure 17: The Software Project Physical Model ............................................................. 82
Figure 18: The Tour Operator System Logical Model ..................................................... 89
Figure 19: The Tour Operator System Physical Model .................................................... 90
Figure 20: The Warehouse System Logical Model .......................................................... 99
Figure 21: The Warehouse System Physical Model ....................................................... 100
Master Thesis Weiguang Zhang McMaster University- Computing and Software
viii
Contents
Abstract ............................................................................................................................ iiii
Acknowledgments ........................................................................................................... ivv
Abbreviations .................................................................................................................. viv
Terminologies and Symbols of ERwin IE Format ........................................................ iv
List of Figures ................................................................................................................ viiii
Chapter 1: Introduction .................................................................................................... 1
Chapter 2: Objectives, Decisions, and Methods .............................................................. 4
Chapter 3: University System ........................................................................................... 8
3.1 UNIVERSITY SYSTEM INFORMAL DESCRIPTION....................................................................................8
3.2 UNIVERSITY SYSTEM LOGICAL MODEL..............................................................................................10
3.3 UNIVERSITY SYSTEM DB2 PHYSICAL MODEL....................................................................................11
3.4 UNIVERSITY SYSTEM DB2 SCHEMA...................................................................................................12
3.5 UNIVERSITY SYSTEM INTERACTIVE QUERIES.....................................................................................14
3.6 UNIVERSITY SYSTEM APPLICATION PROGRAM..................................................................................16
Chapter 4: Airline Reservation....................................................................................... 25
4.1 AIRLINE RESERVATION INFORMAL DESCRIPTION................................................................................25
4.2 AIRLINE RESERVATION LOGICAL MODEL..........................................................................................27
4.3 AIRLINE RESERVATION DB2 PHYSICAL MODEL................................................................................28
4.4 AIRLINE RESERVATION DB2 SCHEMA................................................................................................29
4.5 AIRLINE RESERVATION INTERACTIVE QUERIES..................................................................................31
Chapter 1: Introduction
Database Management Systems are really ubiquitous in this age of Internet commerce.
Although the development of relational database system theory and practice can be traced
to the 1970 seminal paper A Relational Model of Data for Large Shared Data Banks by
E.F. Codd [1], the explosive growth of the use of relational database management systems
came with the advent of Internet. Databases not only represent significant infrastructure
for computer applications, but they also process the transactions and exchanges that drive
most of the world economy. A significant and growing segment of the software industry,
known as the database industry includes IBM Corporation, Oracle Corporation, Informix
Corporation, Sybase Incorporated, Teradata Corporation and Microsoft Corporation.
E. F. Codd found the database technologies of the late 1960s taking the old-fashioned
view that the burden of finding information should be on users ... unsatisfactory. He
proposed what he called a relational model based on two fundamental premises: What
Codd called the "relational model" rested on two key points:
1. It provided means of describing data with its natural structure onlythat is,
without any formatting aspects, i.e. superimposing any additional structure for
machine representation purposes.
2. Between application programs on the one hand and the database system on the
other. Accordingly, it provided a natural and consistent basis for a high level
query language which facilitated maximal independence
These aspects are utilized every second all over the world as most of the Internet
traffic and most of the Internet commerce rely on database access of some form. The
relational databases allow various applications in various programming languages to
access and modify a databases.
There are many drawbacks to the relational model and in many ways it is quite
obsolete, see [2] or in particular the ACM blog by M. Stonebraker, one of the pioneers of
the relational database management systems, [3]. In general , the disadvantages of the
relational approach can be summarized by:
1. They are slow and cumbersome; in the early days they were slow - relational
DBMS (database management systems) have to employ many tables to conform
Master Thesis Weiguang Zhang McMaster University- Computing and Software
2
to the various normalization rules. This can make them slow and resource
inefficient. However most contemporary relational DBMS do not have
performance problems now.
2. Restricted attribute sizes. Attribute lengths are usually capped by a maximum
size. This can lead to occasional practical problems e.g. a company with a 400
character name - not frequent, but it can happen.
3. SQL does not provide an efficient way to browse alphabetically through an index.
Thus some systems cannot provide a simple title A-Z browse.
4. To fine-tune a SQL query is not a simple task and its performance may often
depend on the SQL query engine having up-to-date statistics of the database.
5. They do not storing of objects, in essence, objects must be disassembled
before storing them in a relational database, and assembled when they are
fetched.
Yet, despite these deficiencies an obsoleteness, the use of relational databases
proliferates. The major advantages could be summarized as follows:
1. Overwhelmingly, the most popular type of DBMS in use and as a result technical
development effort ensures that advances appear quickly and reliably.
2. A multitude of third party tools that are designed to work with the popular
relational DBMS via standards such as Open Database Connectivity (ODBC) or
J ava Database Connectivity (J DBC).
3. A multitude of third party design and modeling tools for the relational model, such
as ERwin
software
and presented in the form of what is called in Erwins terminology the Logical
and the Physical models.
3. The DB2 database schema is produced based on the ER model.
4. Several SQL queries that can be used to query of the installed database are
presented.
5. A simple application program in C is presented that utilize embedded SQL to
work with the installed database -- usually performing the same queries used in
the part 4.
Since the size of the thesis is already large enough and so only for the first project the
source code of the C application programs is presented, for all other projects the source
codes are listed in the Appendix.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
4
.
A decision concerning application programming affected the selection of the DBMS
for our projects. Any application program must access a database, usually using a
software tool or an interface provided by the vendor of the DBMS. Among languages
popular for database application programming, Python and PHP stand out. Python
accesses the database using API -- application programming interface. If Python was just
chosen as the language of application programming, the students would have to know or
learn Python and learn the appropriate API -- a too steep requirement to fit into a single
term, database course that should be focused on the DBMS rather than Python or API of
the DBMS. PHP access the database using Microsoft ODBC. Similarly as for Python, the
students would have to know or learn PHP and learn the language of ODBC, moreover
the ODBC is a software that must be purchased independently from the DBMS. Another
popular language for database application is J ava that accesses a database using J DBC.
Even though this is a less stringent requirement than PHP and ODBC, it still is too
stringent.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
5
Using embedded SQL in C programs is the least stringent requirement as the students
do not have to learn any new language as by the time they take the database course,
practically all students know enough of C, they cover SQL in the database course, so the
expenditure for learning both static embedded SQL and dynamic embedded SQL is
significantly less than learning ODBC, or J DBC, or API etc. Embedded SQL, though not
limited to C, works best with C as the CLI -- call level interface -- to which embedded
SQL are translated by the pre-processor is well defined in C as they are all actually
programmed in C. For these reasons, we opted for application programming based on
embedded SQL and C.
Another important decision concerned which of the concrete database management
systems (DBMS for short) should be used for thesis. The original idea to include several
major ones -- Oracle, DB2, and MySQL -- was rejected as the resulting thesis would be
just too large. For each system, we looked at several criteria:
How available it is to students, and McMaster students in particular?
How commercially successful and practical the system is?
Does ERwin provide modeling support for it?
Does the system provide a native support -- i.e. set of tools -- for embedded
SQL programming in C?
On availability to students, all three DBMS considered scored equally as they all offer
free versions: DB2 has a free student version, Oracle provided free Oracle Database
Express Edition, and MySQL that started as an Open Software is in public domain. On
particular availability to McMaster students DB2 scored the highest as it is the DBMS of
choice for the undergraduate database course and its graduate variant.
In commercial success and practicality, Oracle is a bit ahead of DB2 that is a bit ahead
of MySQL, however the differences were not significant enough for our purposes.
Contemporary version of ERwin provides modeling support for all three DBMS
considered equally.
For embedded SQL (ESQL for short), both DB2 and Oracle provide a very good
support, while MySQL lacks any native support for ESQL, third-party providers have
been promising ESQL tools (pre-processor) for some time, but none is still available.
Based on the above criteria and the fact that due to the size, for this thesis we had to
consider a single DBMS, DB2 was chosen over Oracle due to better availability in
particular to McMaster students. MySQL was rejected for the lack of ESQL support.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
6
For the data modeling part of our projects, we decided to use ERwin modeling tool. In
real world, most data models are the form of ER diagrams that visually represent entities,
attributes, and relationships. As mentioned in the introduction, the original Chens format
is not very suitable as it is too bulky. Most of available software modeling tools thus
provide ER models that are closer to the relational model than to the pure ER model.
ERwin is no exception, and supports both the Information Engineering format (IE, most
popular format) and the IDEF1X format. Moreover ERwin is user friendly and easy to
learn, provides free Community Edition of Erwin Data Modeler, and for most of the
DBMS we considered generates database schemas automatically.
ERwin models have two forms, so-called Logical Model and Physical Model. The
Logical Model is a representation of an organizations data, organized in terms of entities
and relationships and independent of any particular data management technology. It
includes all entities and relationships among them, all primary keys for all entities are
specified, i.e. weak entities are resolved, all attributes for each entity are specified, all
foreign keys are specified. Normalization occurs at this level, if needed.
The basic steps in producing the Logical Model are:
Specify all entities.
Find attributes for each entity.
Specify primary keys for all entities.
Find the relationships between different entities.
Resolve many-to-many relationships.
Perform normalization.
The Physical Model represents how the model will be built/stored in the database. A
physical database model shows all table structures, including column names, column data
types, column constraints, primary keys, foreign keys, and relationships between tables .
The features of the Physical Model include the specification all tables and columns,
foreign keys are used to identify relationships between tables, intentional de-
normalization may occur at this level based on user requirements. The physical
considerations may cause the Physical Model to be quite different from the Logical
Model, thus the Physical Model will be different for different DBMS. For example, data
type for a column maybe different between DB2 and SQL Server.
The basic steps in producing the Physical Model are:
Convert entities into tables.
Convert relationships into foreign keys.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
7
ABC University is a large institution with several campuses. Each campus has a different
name, address, distance to the city center and the only bus running to the campus. Each
campus has one club. The name of the club, the building in which the club is located, the
phone number of the club and the multiple sports which club offers, should all be
recorded.
The University consists of a number of faculties, such as the Art Faculty, the
Science Faculty, and so on. Each faculty has a name, dean and building. A faculty may be
divided into a number of schools, for example, the Science Faculty has a School of
Physics and a School of Chemistry. Each school belongs to one faculty only and is
located on just one campus, but one campus maybe the location of many schools.
Every school has name and an building assigned to. Each school offers different
programmes and each programme can be offered by only one school. Each programme
has a unique code, title, level and duration. Each programme comprises several courses,
different programmes have different courses. Each course has a unique code and course
title. Some courses may have one or more prerequisite courses and one course can be the
prerequisite course of some other courses.
Each of the students is enrolled in a single programme of study which involves a
fixed core of courses specific to that programme as well as a number of electives taken
from other programmes. Students work on courses and are awarded a grade in any course
if he/she passes the course. Otherwise the student has to re-take the failed course. The
system needs to record the year and term in which the course was taken and the grade
awarded to the student. Every student has a unique ID. The system also keeps the student
name, birthday and the year he/she enrolled in the course.
The school employs lecturers to teach the students. A lecturer is allowed to work
for one school only. Each lecturer is assigned an ID which is unique across the whole
university. The system keeps the lecturers name, title and the office room. A supervisor
maybe in charge of several lecturers, but a lecturer, however reports to only one
supervisor. A lecturer can teach many different courses. A course may also have been
taught by many different lecturers.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
9
Query 1: List all the schools are located in 'Toronto Campus', and sort them by school
name.
SELECT SCHLNM AS SCHOOL_NAME
FROM SCHOOL
WHERE CMPSNM = ' Tor ont o Campus'
ORDER BY SCHLNM
Query 2: List all the programmes provided by 'science faculty'.
SELECT P. PROGCD AS PROGRAMME_CODE
FROM PROGRAMME P
I NNER J OI N SCHOOL S ON P. SCHLNM = S. SCHLNM
I NNER J OI N FACULTY F ON S. FACNM = F. FACNM
WHERE F. FACNM = ' sci ence f acul t y'
Query 3: Give all the names of the lecturers who are the members of the committee and
sort by their name.
SELECT DI STI NCT L. LECTNM AS LECTURER_NAME
FROM COMMI TTEE_LECTURER CL
J OI N LECTURER L ON CL. STFI D = L. STFI D
ORDER BY L. LECTNM
Query 4: List all supervisor's name and the name of the lecturer they manage. Please sort
by supervisor name and lecturer name.
SELECT SUP. LECTNM AS SUPERVI SOR_NAME, L. LECTNM AS LECTURER_NAME
FROM LECTURER SUP
I NNER J OI N LECTURER L ON SUP. STFI D = L. SUPI D
ORDER BY SUP. LECTNM, L. LECTNM
Query 5: Give all the lecturers who are not the member of the committee.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
15
voi d depad( char * s) {
i nt i ;
i =st r l en( s) - 1;
whi l e( s[ i ] ==' ' ) s[ i - - ] =' \ 0' ;
}
char buf f er [ 300] ;
i nt mai n( ) {
i nt i , j ;
connect ( ) ;
whi l e( 1) {
A: pr i nt f ( " ent er number of quer y you want t o execut e "
" ( 1- 10) , \ n 0 t o qui t , anyt hi ng el se al l quer i es\ n" ) ;
f get s( buf f er , 300, st di n) ;
i f ( buf f er [ 0] ==' \ n' ) {
pr i nt f ( " not hi ng ent er ed\ n" ) ;
cont i nue;
}
i =j =0;
whi l e( buf f er [ j ] ! =' \ n' ) {
i f ( buf f er [ j ] >=' 0' && buf f er [ j ] <=' 9' )
i = 10*i +buf f er [ j ] - ' 0' ;
el se{
pr i nt f ( " i ncor r ect ent r y\ n" ) ;
got o A;
}
j ++;
}
i f ( i == 0) br eak;
el se i f ( i == 1) quer y1( ) ;
el se i f ( i == 2) quer y2( ) ;
el se i f ( i == 3) quer y3( ) ;
el se i f ( i == 4) quer y4( ) ;
el se i f ( i == 5) quer y5( ) ;
el se i f ( i == 6) quer y6( ) ;
el se i f ( i == 7) quer y7( ) ;
el se i f ( i == 8) quer y8( ) ;
el se i f ( i == 9) quer y9( ) ;
el se i f ( i == 10) quer y10( ) ;
el se {
do_al l ( ) ;
br eak;
}
}
di sconnect ( ) ;
r et ur n 0;
}
i nt count , l en, l en1;
char buf 1[ 80] ;
EXEC SQL BEGI N DECLARE SECTI ON;
Master Thesis Weiguang Zhang McMaster University- Computing and Software
18
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 2 I NTO : pr ogr amme_code;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " PROGRAMME_CODE\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - \ n" ) ;
}
depad( pr ogr amme_code) ;
pr i nt f ( " %s\ n" , pr ogr amme_code) ;
count ++;
}
EXEC SQL CLOSE cur 2;
sql er r ( " CLOSE cur 2" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y3( ) {
pr i nt f ( " di spl ayi ng r esul t of quer y 3\ n" ) ;
EXEC SQL DECLARE cur 3 CURSOR FOR
SELECT DI STI NCT L. LECTNM AS LECTURER_NAME
FROM COMMI TTEE_LECTURER CL
J OI N LECTURER L ON CL. STFI D = L. STFI D
ORDER BY L. LECTNM;
sql er r ( " DECLARE cur 3" ) ;
EXEC SQL OPEN cur 3;
sql er r ( " OPEN cur 3" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 3 I NTO : l ect ur er _name;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " LECTURER_NAME\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - \ n" ) ;
}
pr i nt f ( " %s\ n" , l ect ur er _nam) ;
count ++;
}
EXEC SQL CLOSE cur 3;
sql er r ( " CLOSE cur 3" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y4( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 4\ n" ) ;
EXEC SQL DECLARE cur 4 CURSOR FOR
Master Thesis Weiguang Zhang McMaster University- Computing and Software
20
}
EXEC SQL CLOSE cur 5;
sql er r ( " CLOSE cur 5" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y6( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 6\ n" ) ;
EXEC SQL DECLARE cur 6 CURSOR FOR
SELECT PROGCD AS PROGRAMME_CODE, COUNT( CRSECD) AS NUMBER_OF_COURSE
FROM COURSE
GROUP BY PROGCD;
sql er r ( " DECLARE cur 6" ) ;
EXEC SQL OPEN cur 6;
sql er r ( " OPEN cur 6" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 6 I NTO : pr ogr amme_code,
: number _of _cour se;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " PROGRAMME_CODE NUMBER_OF_COURSE\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( pr ogr amme_code) ;
depad( number _of _cour se) ;
pr i nt f ( " %s" , pr ogr amme_code) ;
f or ( i = st r l en( pr ogr amme_code) ; i <11; put char ( ' ' ) , i ++) ;
pr i nt f ( " %s" , number _of _cour se) ;
count ++;
}
EXEC SQL CLOSE cur 6;
sql er r ( " CLOSE cur 6" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y7( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 7\ n" ) ;
EXEC SQL DECLARE cur 7 CURSOR FOR
SELECT L. LECTNM AS LECTURER_NAME, C. CRSETI TL AS COURSE_TI TLE
FROM LECTURER_COURSE LC
I NNER J OI N LECTURER L ON LC. STFI D = L. STFI D
I NNER J OI N COURSE C ON LC. CRSECD = C. CRSECD
ORDER BY L. LECTNM;
sql er r ( " DECLARE cur 7" ) ;
Master Thesis Weiguang Zhang McMaster University- Computing and Software
22
count ++;
}
EXEC SQL CLOSE cur 8;
sql er r ( " CLOSE cur 8" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y9( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 9\ n" ) ;
EXEC SQL DECLARE cur 9 CURSOR FOR
SELECT P. PROGCD AS PROGRAMME_CODE, COUNT( CS. STUI D) AS
NUMBER_OF_STUDENTS
FROM COURSE_STUDENT CS
LEFT J OI N COURSE C ON CS. CRSECD = C. CRSECD
LEFT J OI N PROGRAMME P ON P. PROGCD = C. PROGCD
GROUP BY P. PROGCD
ORDER BY NUMBER_OF_STUDENTS DESC
FETCH FI RST 5 ROWS ONLY;
sql er r ( " DECLARE cur 9" ) ;
EXEC SQL OPEN cur 9;
sql er r ( " OPEN cur 9" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 9 I NTO : pr ogr amme_code,
: number _of _st udent s;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " PROGRAMME_CODE NUMBER_OF_STUDENTS\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( pr ogr amme_code) ;
depad( number _of _st udent s) ;
pr i nt f ( " %s" , pr ogr amme_code) ;
f or ( i = st r l en( pr ogr amme_code) ; i <12; put char ( ' ' ) , i ++) ;
pr i nt f ( " %s\ n" , number _of _st udent s) ;
count ++;
}
EXEC SQL CLOSE cur 9;
sql er r ( " CLOSE cur 9" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y10( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 10\ n" ) ;
Master Thesis Weiguang Zhang McMaster University- Computing and Software
24
There are 6 different airlines in 6 different countries: Canada AirCan, USA - USAir,
UK - BritAir, France - AirFrance, Germany - LuftAir, Italy - ItalAir. Their flights involve
the following 12 cities: Toronto and Montreal in Canada, New York and Chicago in US,
London and Edinburgh in UK, Paris and Nice in France, Bonn and Berlin in Germany,
Rome and Naples in Italy. In each of the 12 cities, there is a (single) booking office. You
are going to design a central air-reservation database to be used by all booking offices.
The flight has a unique flight number, air line code, business class indicator,
smoking allowed indicator. Flight availability has flight number, date +time of departure,
number of total seats available in business class, number of booked seats in business
class, number of total seats available in economy class, and number of booked seats in
economy class.
The customers may come from any country, not just the 6 above, and from any
province/state, and from any city. Customer has first & last name, mailing address, zero
or more phone numbers, zero or more fax numbers, and zero or more email addresses.
Mailing address has street, city, province or state, postal code and country. Phone/fax
number has country code, area code and local number. Email address has only one string,
and no structure is assumed. A customer can book one or more flights. Two or more
customers may have same mailing address and/or same phone number(s) and/or same fax
number(s). But the email address is unique for each customer. First and last names do not
have to be unique.
Booking has an unique booking number, booking city, booking date, flight
number, date +time of departure (in local time, and time is always in hours and minutes),
date +time of arrival (in local time), class indicator, total price (airport tax in origin +
airport tax in destination +flight price in local currency. The flight price for business
class is 1.5 times of the listed flight price), status indicator (three types: booked. Canceled
the customer canceled the booking, scratched the customer had not paid in full 30
days prior to the departure), customer who is responsible for payment, amount-paid-so far
(in local currency), outstanding balance (in local currency), the first & last names to be
printed on the ticket. The airport taxes must be stored in local currencies (i.e. Canadian
Master Thesis Weiguang Zhang McMaster University- Computing and Software
26
dollars, US dollars, British Pounds, French francs, German marks, and Italian Liras).
Since the exchange rates change daily, they also must be stored for calculations of all
prices involved.
Though France, Germany, and Italy have had a common currency for a while, we
used the names of their original currencies to involve in this exercise currency exchange
rates and their changes.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
27
The movies are rented out in stores and there are several stores. Each store has a unique
distributor that supplies the store with tapes. A distributor may supply more than one
store. Each distributor has a name, an address, and a phone number. Each store has a
name, an address, and a phone number. For each employee we must keep the following
information: working store, a name, a supervisor, an address , a phone number, SIN
(social insurance number) and the date when the employee was hired. For each customer
we have to keep the following information: a name, an address, and a phone number (if
any).
For each rental, we must keep track of which employee served the customer,
which movie and which copy (i.e. type) the customer rented, information about payment,
the date and the time of the rental, the status (rented, returned_in_time, returned_late), the
rate (i.e. the price), and if applicable, due date and overdue charges. About the payment
we have to keep which of the employees accepted the payment (does not have to be the
same employee who rented the tape), the type of payment (i.e. cash, check, credit card,
direct debit for each type you must provide for relevant information to be kept, e.g.
credit card number if credit card is used), the amount of the payment, date +time of the
payment, payment status (completed if cash or the money have been received, approved if
debit or credit card go through, pending if the check has not cleared yet). About each tape
we have to keep information in what condition the tape is and what movie is on the tape.
About each movie we have to keep its title, directors name, simple description, the name
of a (single) major star, the movies rating (use numbers 1-5).
Query 1: Give all the customers who lives in Hamilton. Display customer_id and
customer_name.
SELECT CUSTI D AS CUSTOMER_I D, CUSTNM AS CUSTOMER_NAME
FROM CUSTOMER
WHERE CUSTCI TY=' Hami l t on'
Master Thesis Weiguang Zhang McMaster University- Computing and Software
40
Query 2: Display the total payment are received by each employee, and sort by empsin.
SELECT EMPSI N AS EMPLOYEE_SI N, SUM( AMT) AS TOTAL_AMT
FROM PAYMENT
GROUP BY EMPSI N
ORDER BY EMPSI N
Query 3: Display the total movies are rented out by each store, and sort by storeid.
SELECT STOREI D, COUNT( TAPEI D) AS MOVI E_RENTED
FROM MOVI E_RENTAL
GROUP BY STOREI D
ORDER BY STOREI D
Query 4: Display all the tapes are never rented out in every store, and sort by movieid
& tapeid.
SELECT DI STI NCT T. MOVI EI D, T. TAPEI D
FROM TAPE T
LEFT J OI N MOVI E_RENTAL R ON T. MOVI EI D=R. MOVI EI D AND T. TAPEI D=R. TAPEI D
WHERE R. MOVI EI D I S NULL
ORDER BY T. MOVI EI D, T. TAPEI D
Query 5: Display all customers who did not rent any movie so far and sort by custid.
SELECT CUSTI D, CUSTNM
FROM CUSTOMER
WHERE CUSTI D NOT I N ( SELECT DI STI NCT CUSTI D FROM MOVI E_RENTAL)
ORDER BY CUSTI D
Query 6: Display the total amount received by different payment type, and sort by
ptdesc.
SELECT TP. PTDESC AS PAYMENT_TYPE, SUM( AMT) AS TOTAL_AMT
FROM PAYMENT P
I NNER J OI N PAYMENT_TYPE TP ON P. PTI D = TP. PTI D
GROUP BY TP. PTDESC
ORDER BY TP. PTDESC
Query 7. Display the number of movies rented out based on the movie rating, and sort by
rating.
SELECT M. RATI NG, COUNT( MR. MOVI EI D) AS NO_OF_MOVI ES
FROM MOVI E_RENTAL MR
I NNER J OI N MOVI E M ON MR. MOVI EI D = M. MOVI EI D
GROUP BY M. RATI NG
Query 8: Display top 5 customers based on their total payment, and sort their total
payment decreased.
SELECT CUSTI D, SUM( AMT) AS TOTAL_AMT
Master Thesis Weiguang Zhang McMaster University- Computing and Software
41
FROM PAYMENT
GROUP BY CUSTI D
ORDER BY TOTAL_AMT DESC
FETCH FI RST 5 ROWS ONLY
Query 9: List all the movies customer rented. Please display the columns: movie_title,
rental_status, rental_rate, rental_employee, the employ accept the
payment, payment_type and payment_status.
SELECT M. MOVI ETI TL AS MOVI E_TI TLE,
RS. RDESC AS RENTAL_STATUS,
MR. RRATE AS RENTAL_RATE,
E1. EMPNM AS RENTAL_EMPLOYEE,
E2. EMPNM AS CASHI ER_EMPLOYEE,
PT. PTDESC AS PAYMENT_TYPE,
PS. PDESC AS PAYMENT_STATUS
FROM MOVI E_RENTAL MR
I NNER J OI N CUSTOMER C ON MR. CUSTI D = C. CUSTI D
I NNER J OI N MOVI E M ON MR. MOVI EI D = M. MOVI EI D
I NNER J OI N RENTAL_STATUS RS ON MR. RSTATUSI D = RS. RSTATUSI D
I NNER J OI N EMPLOYEE E1 ON MR. EMPSI N = E1. EMPSI N
LEFT J OI N PAYMENT P ON MR. PAYI D = P. PAYI D
LEFT J OI N EMPLOYEE E2 ON P. EMPSI N = E2. EMPSI N
LEFT J OI N PAYMENT_STATUS PS ON P. PSTATUSI D = PS. PSTATUSI D
LEFT J OI N PAYMENT_TYPE PT ON P. PTI D = PT. PTI D
WHERE C. CUSTNM = ' cust omer 1'
Query 10: List all the manager's name and the name of employee they manage.
Please sort by manager sin & employee sin.
SELECT MGR. EMPNM AS MANAGER_NAME, E. EMPNM AS EMPLOYEE_NAME
FROM EMPLOYEE E
I NNER J OI N EMPLOYEE MGR ON E. MGRSI N = MGR. EMPSI N
ORDER BY MGR. EMPSI N, E. EMPSI N
Master Thesis Weiguang Zhang McMaster University- Computing and Software
42
For certain weeks we have promotional rentals that are usually 60% of the regular
price, but may be also of different percentage. They always affect only a single class of
cars i.e. we may have a promotion for subcompacts, but during that week we do not
have any promotions for compacts, sedans or luxury cars. During some years we can have
many promotions, in some we have none. The promotions cannot be applied to the
employees.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
44
In our college, students need to register for courses before new semester start. All of
programs in our college take four years to finish.
Each course has a unique designation, title, description, year (in which year of
study the course is to be taken, for instance 2
nd
year course), and classroom. A course can
have no or many tutorial sections, and no or many lab sections. Each course is taught by
exactly one instructor. Each instructor has a unique id, name, departmental affiliation,
office room, phone extension, and a unique email address. Each student has a unique id,
name, and the year of his/her study. A student cannot be an instructor. A course can have
zero or many tutorial sections unique to the course (i.e. tutorial sections are not shared by
different courses). Each tutorial section has exactly one TA assigned. A TA can tutor
more than one tutorial section for the same course, and any number of tutorials for
different courses. A TA cannot be an instructor, however a student can work as a TA (in
that case his/her student id is used as TA id). A course can have zero or many lab sections
unique to the course (i.e. lab sections are not shared by different courses). Each lab
section has exactly one LA assigned. An LA can oversee more than one lab section for
the same course, and any number of labs for different courses. An LA cannot be an
instructor, however a student can work as a LA (in which case his/her student id is used
as the LA id). In fact, a student can work as a TA and an LA simultaneously. Thus, a TA
may or may not be a student, an LA may or may not be a student. A person can work as
both, a TA and a LA. TA has the same attributes as instructor, the same goes for LA.
Each course has zero to many courses designated as its prerequisites and zero to
many courses designated as its anti-requisites. Prerequisite courses are of the same or
lower year, anti-requisite courses are of the same year. In the system we keep information
of what courses a student has taken and what courses the student is registering. All
courses are either Pass or Fail. A student can register a course only if he/she has passed
all the prerequisites and has not passed any or is not registered in any of the anti-
requisites. A student can only register a course of the appropriate year, i.e. a student in
year X of study can only register and take course of year X.
In our Emergency Room (ER), we have three distinct types of workers: receptionists,
nurses, and doctors. Any of the workers can in fact be a patient. Each person in the
proposed system, be it a patient or a worker has a last, a first, possibly a middle name, and
one or more addresses. An address consists of a country, province, city, street and street
number. Each person can have none or more email addresses, none or more telephone
numbers.
The workers work in ER in shifts. A shift consists of start and end time. The shifts
do not overlap, but they are consecutive, i.e. there is a shift on at any given time and day.
We are assuming that the model we are creating (and eventually the database we will
design) covers some extended period of time. Each worker will thus be assigned to many
shifts in that period. Exactly two receptionists are assigned to each shift, a group of two or
more nurses is assigned to each shift, a group of two or more doctors is assigned to each
shift, one of the doctors assigned to a shift is the shifts triage doctor.
When a patient comes to ER, it happens during a particular shift. The patient is
admitted by a particular receptionist, is seen by the triage doctor of the shift. The patient
may be send home, prescribed some medication by the triage doctor and send home, or is
staying in ER in which case the patient is assigned a bed and case doctors (one of the
doctors on each shift best qualified for the particular problem of the patient). Each bed is
supervised by a single nurse during a shift, but a nurse may supervise many beds, or none
at all. The case doctor(s) may prescribe a medication that is administered to the patient by
a single nurse in each shift for the duration of the patient taking the medicine. Each
medication has a name, and for each patient there may be a different dosage and different
number of times a day to take it.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
61
)
CREATE TABLE BED(
NUMBER CHAR( 3) NOT NULL,
PRI MARY KEY ( NUMBER)
)
CREATE TABLE SHI FT(
SHI FTI D I NTEGER NOT NULL,
FROM TI MESTAMP NOT NULL,
TO TI MESTAMP NOT NULL,
PRI MARY KEY ( SHI FTI D) ,
UNI QUE ( FROM, TO) ,
CHECK ( FROM < TO)
)
CREATE TABLE HASE(
I D I NTEGER NOT NULL,
EADDRESS CHAR( 20) NOT NULL,
PRI MARY KEY ( I D, EADDRESS) ,
FOREI GN KEY ( I D) REFERENCES PERSON ( I D) ,
FOREI GN KEY ( EADDRESS) REFERENCES EMAI L ( EADDRESS)
)
CREATE TABLE HASP(
I D I NTEGER NOT NULL,
AREACODE CHAR( 3) NOT NULL,
NUMBER CHAR( 7) NOT NULL,
PRI MARY KEY ( I D, AREACODE, NUMBER) ,
FOREI GN KEY ( I D) REFERENCES PERSON ( I D) ,
FOREI GN KEY ( AREACODE, NUMBER) REFERENCES PHONENO( AREACODE, NUMBER)
)
CREATE TABLE HASA(
I D I NTEGER NOT NULL,
PROVI NCE CHAR( 2) NOT NULL,
CI TY CHAR( 10) NOT NULL,
STREET CHAR( 10) NOT NULL,
STREETNO CHAR( 6) NOT NULL,
PRI MARY KEY ( I D, PROVI NCE, CI TY, STREET, STREETNO) ,
FOREI GN KEY ( I D) REFERENCES PERSON ( I D) ,
FOREI GN KEY ( PROVI NCE, CI TY, STREET, STREETNO) REFERENCES ADDRESS
( PROVI NCE, CI TY, STREET, STREETNO)
)
CREATE TABLE RONS(
RI D I NTEGER NOT NULL,
SHI FTI D I NTEGER NOT NULL,
PRI MARY KEY ( RI D, SHI FTI D) ,
FOREI GN KEY ( RI D) REFERENCES RECEPTI ONI ST ( RI D) ,
FOREI GN KEY ( SHI FTI D) REFERENCES SHI FT ( SHI FTI D)
)
CREATE TABLE NONS(
NI D I NTEGER NOT NULL,
SHI FTI D I NTEGER NOT NULL,
PRI MARY KEY ( NI D, SHI FTI D) ,
FOREI GN KEY ( NI D) REFERENCES NURSE ( NI D) ,
FOREI GN KEY ( SHI FTI D) REFERENCES SHI FT ( SHI FTI D)
)
Master Thesis Weiguang Zhang McMaster University- Computing and Software
65
)
CREATE TABLE ADM(
PI D I NTEGER NOT NULL,
RI D I NTEGER NOT NULL,
SHI FTI D I NTEGER NOT NULL,
ADMI SSI ON TI MESTAMP,
PRI MARY KEY ( PI D) ,
FOREI GN KEY ( PI D) REFERENCES PATI ENT ( PI D) ,
FOREI GN KEY ( RI D, SHI FTI D) REFERENCES RONS ( RI D, SHI FTI D)
)
CREATE TABLE TRI AGEBY(
PI D I NTEGER NOT NULL,
DI D I NTEGER NOT NULL,
PRI MARY KEY ( PI D) ,
FOREI GN KEY ( PI D) REFERENCES PATI ENT ( PI D) ,
FOREI GN KEY ( DI D) REFERENCES DOCTOR ( DI D)
)
8.5 Emergency Room Interactive Queries
Query 1: The query returns an empty set if con1 is satisfied.
( ( SELECT RI D FROM RECEPTI ONI ST) I NTERSECT ( SELECT NI D FROM NURSE) )
UNI ON
( ( SELECT RI D FROM RECEPTI ONI ST) I NTERSECT ( SELECT DI D FROM DOCTOR) )
UNI ON
( ( SELECT NI D FROM NURSE) I NTERSECT ( SELECT DI D FROM DOCTOR)
Query 2: The query returns an empty set if con2 is satisfied.
SELECT EADDRESS FROM EMAI L
EXCEPT
SELECT EADDRESS FROM HASE
Query 3: The query returns an empty set if con3 is satisfied.
SELECT AREACODE, NUMBER FROM PHONENO
EXCEPT
SELECT AREACODE, NUMBER FROM HASP
Query 4: The query returns an empty set if con4 is satisfied.
SELECT PROVI NCE, CI TY, STREET, STRETNO FROM ADDRESS
EXCEPT
SELECT PROVI NCE, CI TY, STREET, STREETNO FROM HASA
Query 5: The query returns an empty set if con5 is satisfied.
SELECT I D FROM PERSON
EXCEPT
Master Thesis Weiguang Zhang McMaster University- Computing and Software
67
Our company arranges rentals of properties owned by both private and business owners.
We assign every property owner a unique owner number for identification, we record its
address (consisting of a street, street number, town or city, and province), the owners
name (consisting of first, middle, and last name for a person or name of a business), and
the owners email addresses and the owner phone numbers. For a business owner, we
record the type (description) of its business. Each property is identified by a unique
property number, we record its address and its type. Each property may be placed in
several advertisements. Each such advertisement may be displayed in many newspapers
on several dates. The newspapers are identified by unique names.
The term renter refers to a private person or a business who signed a rental
agreement for a property. Each such rental agreement is identified in our database by a
unique rental number. We record the date of the singing of the rental agreement, the
starting and ending date of the rental agreement. A renter can rent many properties. A
renter, prior to accepting the rental agreement may view the property repeatedly and we
record the date of viewing. For each renter, we record its address, its name, its email
address and phone numbers. Each renter has a unique renter number in our database.
Our agency is organized into branches and every staff member is allocated to
exactly one branch. Each branch has one manager who is a member of the staff. In our
database, we identify the staff by a unique staff number. For each staff member we record
address, name, email address, phone numbers, sex, position, and salary. Each property is
in care of one of our branches. Each renter refers to the branch that is in care of the
property it rents. Each property is overseen by a unique staff member. Each branch has an
address, phone number, and a unique branch number.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
70
)
CREATE TABLE PROPERTY (
PROPERTY_NO CHAR( 4) NOT NULL,
STREET_NO CHAR( 4) NOT NULL,
STREET CHAR( 10) NOT NULL,
CI TY CHAR( 10) NOT NULL,
PROVI NCE CHAR( 2) NOT NULL,
POSTAL_CODE CHAR( 6) NOT NULL,
OVERSEEN_BY CHAR( 4) NOT NULL,
OWNED_BY CHAR( 4) NOT NULL,
TYPE CHAR( 2) NOT NULL,
PRI MARY KEY ( PROPERTY_NO) ,
FOREI GN KEY ( OVERSEEN_BY) REFERENCES STAFF,
FOREI GN KEY ( OWNED_BY) REFERENCES OWNER,
CHECK ( PROVI NCE I N ( ' AL' , ' BC' , ' MA' , ' NB' , ' NF' , ' NT' , ' NS' , ' NU' , ' ON' ,
' PE' , ' QB' , ' SA' , ' YU' ) ) ,
CHECK ( ( ' A' <=SUBSTR( POSTAL_CODE, 1, 1) AND SUBSTR( POSTAL_CODE, 1, 1) <=' Z' )
AND ( ' 0' <=SUBSTR( POSTAL_CODE, 2, 1) AND SUBSTR( POSTAL_CODE, 2, 1) <=' 9' )
AND ( ' A' <=SUBSTR( POSTAL_CODE, 3, 1) AND SUBSTR( POSTAL_CODE, 3, 1) <=' Z' )
AND ( ' 0' <=SUBSTR( POSTAL_CODE, 4, 1) AND SUBSTR( POSTAL_CODE, 4, 1) <=' 9' )
AND ( ' A' <=SUBSTR( POSTAL_CODE, 5, 1) AND SUBSTR( POSTAL_CODE, 5, 1) <=' Z' )
AND ( ' 0' <=SUBSTR( POSTAL_CODE, 6, 1) AND SUBSTR( POSTAL_CODE, 6, 1) <=' 9' ) )
)
CREATE TABLE RENTAL_AGREEMENT (
PROPERTY_NO CHAR( 4) NOT NULL,
RENTAL_NO CHAR( 4) NOT NULL,
SI GNI NG_DATE DATE NOT NULL,
STARTI NG_DATE DATE NOT NULL,
ENDI NG_DATE DATE NOT NULL,
RENTER_NO CHAR( 4) NOT NULL,
PRI MARY KEY ( PROPERTY_NO, RENTAL_NO) ,
FOREI GN KEY ( PROPERTY_NO) REFERENCES PROPERTY,
FOREI GN KEY ( RENTER_NO) REFERENCES RENTER,
CHECK ( SI GNI NG_DATE <= STARTI NG_DATE) ,
CHECK ( STARTI NG_DATE <= ENDI NG_DATE)
)
CREATE TABLE RENTER_EMAI L (
EMAI L_ADDR CHAR( 20) NOT NULL,
RENTER_NO CHAR( 4) NOT NULL,
PRI MARY KEY ( EMAI L_ADDR, RENTER_NO) ,
FOREI GN KEY ( RENTER_NO) REFERENCES RENTER
)
CREATE TABLE STAFF_EMAI L (
EMAI L_ADDR CHAR( 20) NOT NULL,
STAFF_NO CHAR( 4) NOT NULL,
PRI MARY KEY ( EMAI L_ADDR, STAFF_NO) ,
FOREI GN KEY ( STAFF_NO) REFERENCES STAFF
)
CREATE TABLE OWNER_EMAI L (
EMAI L_ADDR CHAR( 20) NOT NULL,
OWNER_NO CHAR( 4) NOT NULL,
PRI MARY KEY ( EMAI L_ADDR, OWNER_NO) ,
FOREI GN KEY ( OWNER_NO) REFERENCES OWNER
)
Master Thesis Weiguang Zhang McMaster University- Computing and Software
75
AND ( ' 0' <=SUBSTR( PHONE_NO, 3, 1) AND SUBSTR( PHONE_NO, 3, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 4, 1) AND SUBSTR( PHONE_NO, 4, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 5, 1) AND SUBSTR( PHONE_NO, 5, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 6, 1) AND SUBSTR( PHONE_NO, 6, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 7, 1) AND SUBSTR( PHONE_NO, 7, 1) <=' 9' ) )
)
CREATE TABLE BRANCH_PHONE (
AREA_CODE CHAR( 3) NOT NULL,
PHONE_NO CHAR( 7) NOT NULL,
EXTENSI ON VARCHAR( 5) ,
BRANCH_NO CHAR( 4) NOT NULL,
PRI MARY KEY ( AREA_CODE, PHONE_NO, BRANCH_NO) ,
FOREI GN KEY ( BRANCH_NO) REFERENCES BRANCH,
CHECK( ( ' 0' <=SUBSTR( AREA_CODE, 1, 1) AND SUBSTR( AREA_CODE, 1, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( AREA_CODE, 2, 1) AND SUBSTR( AREA_CODE, 2, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( AREA_CODE, 3, 1) AND SUBSTR( AREA_CODE, 3, 1) <=' 9' ) ) ,
CHECK( ( ' 0' <=SUBSTR( PHONE_NO, 1, 1) AND SUBSTR( PHONE_NO, 1, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 2, 1) AND SUBSTR( PHONE_NO, 2, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 3, 1) AND SUBSTR( PHONE_NO, 3, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 4, 1) AND SUBSTR( PHONE_NO, 4, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 5, 1) AND SUBSTR( PHONE_NO, 5, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 6, 1) AND SUBSTR( PHONE_NO, 6, 1) <=' 9' )
AND ( ' 0' <=SUBSTR( PHONE_NO, 7, 1) AND SUBSTR( PHONE_NO, 7, 1) <=' 9' ) )
)
CREATE TABLE VI EWI NG (
PROPERTY_NO CHAR( 4) NOT NULL,
RENTER_NO CHAR( 4) NOT NULL,
VI EWI NG_DATE DATE NOT NULL,
PRI MARY KEY ( PROPERTY_NO, VI EWI NG_DATE, RENTER_NO) ,
FOREI GN KEY ( PROPERTY_NO) REFERENCES PROPERTY,
FOREI GN KEY ( RENTER_NO) REFERENCES RENTER
)
CREATE TABLE NEWSPAPER (
PAPER_NAME CHAR( 20) NOT NULL,
PRI MARY KEY ( PAPER_NAME)
)
CREATE TABLE ADVERTI SEMENT (
PAPER_NAME CHAR( 20) NOT NULL,
AD_NO CHAR( 4) NOT NULL,
AD_DATE DATE NOT NULL,
PROPERTY_NO CHAR( 4) NOT NULL,
PRI MARY KEY ( PAPER_NAME, AD_NO) ,
FOREI GN KEY ( PAPER_NAME) REFERENCES NEWSPAPER,
FOREI GN KEY ( PROPERTY_NO) REFERENCES PROPERTY
)
ALTER TABLE BRANCH
ADD CONSTRAI NT MANAGER_CNST FOREI GN KEY ( MANAGER) REFERENCES
STAFF( STAFF_NO)
Master Thesis Weiguang Zhang McMaster University- Computing and Software
77
Query 1: Give the staff number of each of the staff members whose salary is greater than
5000. Please, sort them by the staff number.
SELECT STAFF_NO FROM STAFF
WHERE SALARY > 5000
ORDER BY STAFF_NO
Query 2: Give the renter number of each of the renters who has a viewing record. Please
avoid duplications.
SELECT DI STI NCT RENTER_NO FROM VI EWI NG
Query 3: Give the dates of all the advertisements posted in THE GLOBE AND MAIL in
2005. Please, avoid duplications.
SELECT DI STI NCT AD_DATE FROM ADVERTI SEMENT
WHERE PAPER_NAME = ' THE GLOBE AND MAI L' AND AD_DATE>=' 2005- 01- 01'
AND AD_DATE<=' 2005- 12- 31'
ORDER BY AD_DATE
Query 4: Give the email addresses and the renter number for all the private renters.
Please, sort them by the renter number.
SELECT EMAI L_ADDR, RENTER. RENTER_NO
FROM RENTER_EMAI L, RENTER
WHERE RENTER_EMAI L. RENTER_NO = RENTER. RENTER_NO
AND TYPE_OF_BUSI NESS I S NULL
ORDER BY RENTER. RENTER_NO
Query 5: Find the properties that are already advertised but not yet rented. Please, avoid
duplications.
SELECT DI STI NCT PROPERTY_NO FROM ADVERTI SEMENT
WHERE PROPERTY_NO NOT I N ( SELECT DI STI NCT PROPERTY_NO
FROM RENTAL_AGREEMENT)
Query 6: Give the names and the branch numbers of all the staff members working in the
branch which is located in Hamilton. The names should be listed in an
alphabetic order (by last, then by first, then by middle names).
SELECT FI RST_NAME, MI DDLE_NAME, LAST_NAME, BRANCH_NO
FROM STAFF, BRANCH
WHERE STAFF. ALLOCATED_TO=BRANCH. BRANCH_NO
AND BRANCH. CI TY=' HAMI LTON'
ORDER BY LAST_NAME, FI RST_NAME, MI DDLE_NAME
Master Thesis Weiguang Zhang McMaster University- Computing and Software
78
Query 7: Give the staff numbers and the names of all the workers who live on the same
street, city, and province as their manager. The names should be listed in an
alphabetic order (by last, then by first, then by middle names).
SELECT STAFF. STAFF_NO, FI RST_NAME, MI DDLE_NAME, LAST_NAME
FROM STAFF, ( SELECT STAFF_NO, BRANCH_NO, STAFF. STREET, STAFF. CI TY,
STAFF. PROVI NCE FROM STAFF, BRANCH
WHERE STAFF. STAFF_NO = BRANCH. MANAGER) AS T
WHERE STAFF. ALLOCATED_TO = T. BRANCH_NO AND
STAFF. STAFF_NO ! = T. STAFF_NO AND
STAFF. STREET = T. STREET AND
STAFF. CI TY = T. CI TY AND
STAFF. PROVI NCE = T. PROVI NCE
ORDER BY LAST_NAME, FI RST_NAME, MI DDLE_NAME
Query 8: Find the branch number and the average salary of the branch that has the highest
average salary. Please, call the branch number as branch_no and the
average salary as avg_salary.
( SELECT ALLOCATED_TO AS BRANCH_NO, AVG( SALARY) AS AVG_SALARY
FROM STAFF GROUP BY ALLOCATED_TO)
EXCEPT
( SELECT T1. ALLOCATED_TO AS BRANCH_NO, T1. AVG_SALARY
FROM ( SELECT ALLOCATED_TO, AVG( SALARY) AS AVG_SALARY
FROM STAFF GROUP BY ALLOCATED_TO) AS T1,
( SELECT ALLOCATED_TO, AVG( SALARY) AS AVG_SALARY
FROM STAFF GROUP BY ALLOCATED_TO) AS T2
WHERE T1. AVG_SALARY < T2. AVG_SALARY)
Query 9: Find the owners and renters who have 2 or more phone numbers. Call the
owner/renter number as customer_no, set the value of
type_of_customer to 'owner' if the customer is an owner, and to
'renter' if he/she is a renter. Please, only list the customer_no and
type_of_customer.
( SELECT OWNER_NO AS CUSTOMER_NO, ' OWNER' AS TYPE_OF_CUSTOMER
FROM OWNER
WHERE OWNER_NO I N ( SELECT OWNER_NO FROM OWNER_PHONE
GROUP BY OWNER_NO HAVI NG COUNT( *) >= 2) )
UNI ON
( SELECT RENTER_NO AS CUSTOMER_NO, ' RENTER' AS TYPE_OF_CUSTOMER
FROM RENTER
WHERE RENTER_NO I N ( SELECT RENTER_NO FROM RENTER_PHONE
GROUP BY RENTER_NO HAVI NG COUNT( *) >= 2) )
Query 10: Assuming that each advertisement costs 100 dollars, give the branch
number and the amount spent on the advertisements for each branch. Name
the branch number as branch_no, and the amount as ad_cost.
SELECT T2. ALLOCATED_TO AS BRANCH_NO, SUM( C) *100 AS AD_COST
Master Thesis Weiguang Zhang McMaster University- Computing and Software
79
SELECT *
FROM DEPENDS
Query 7: Give project id of all projects that do not depend on any other project.
( SELECT di st i nct PROJ ECT_I D FROM PROJ ECT)
EXCEPT
( SELECT PROJ ECT_I D FROM DEPENDS)
Query 8: Give the project id of the projects that depend on the most other projects.
SELECT T. PROJ ECT_I D
FROM ( SELECT PROJ ECT_I D, COUNT( *) AS DEPEND_COUNT
FROM DEPENDS GROUP BY PROJ ECT_I D) AS T,
( SELECT MAX( DC) AS MAXDC
FROM ( SELECT COUNT( *) AS DC
FROM DEPENDS GROUP BY PROJ ECT_I D) )
WHERE T. DEPEND_COUNT = MAXDC
Query 9: Give description, bug id, and project id of all bug reports for all projects that
have most bug reports.
SELECT BUG_I D, BUG. PROJ ECT_I D, DESCRI PTI ON
FROM BUG, ( SELECT T. PROJ ECT_I D, T. BC
FROM ( SELECT PROJ ECT_I D, COUNT( *) AS BC
FROM BUG GROUP BY PROJ ECT_I D) AS T,
( SELECT MAX( BC1) AS MAXBC
FROM ( SELECT COUNT( *) AS BC1
FROM BUG GROUP BY PROJ ECT_I D) )
WHERE T. BC=MAXBC) AS S
WHERE BUG. PROJ ECT_I D=S. PROJ ECT_I D
Query 10: Give user id of a developer with the least amount of bug reports.
SELECT USER_I D
FROM ( SELECT USER_I D, COUNT( *) AS BPD
FROM ( SELECT BUG_I D, BUG. PROJ ECT_I D, USER_I D
FROM BUG, PROJ ECT, DEVELOPER
WHERE BUG. PROJ ECT_I D=PROJ ECT. PROJ ECT_I D
AND OWNED_BY=USER_I D)
GROUP BY USER_I D)
WHERE BPD I N ( SELECT MI N( BPD) AS MI NBPD
FROM ( SELECT USER_I D, COUNT( *) AS BPD
FROM ( SELECT BUG_I D, BUG. PROJ ECT_I D, USER_I D
FROM BUG, PROJ ECT, DEVELOPER
WHERE BUG. PROJ ECT_I D=PROJ ECT. PROJ ECT_I D
Master Thesis Weiguang Zhang McMaster University- Computing and Software
87
uses the accommodation. All prices are assumed to be in Canadian dollars, not conversion
is involved, regardless where the place is. Each place is identified by a single address.
Each provider of accommodation or meal has a unique designation.
Master Thesis Weiguang Zhang McMaster University- Computing and Software
89
Figure 19: The Tour Operator System Physical Model
Master Thesis Weiguang Zhang McMaster University- Computing and Software
91
SELECT DI STI NCT PARTI CI P. PI D, NAME FROM PARTI CI P, I TI NERARI ES, PERSONS
WHERE PARTI CI P. DESI G=I TI NERARI ES. DESI G
AND I TI NERARI ES. DATE >= ' 12/ 18/ 2003' AND PARTI CI P. PI D=PERSONS. PI D
AND NOT EXI STS ( SELECT I TI NERARI ES. DATE FROM I TI NERARI ES
WHERE I TI NERARI ES. DATE < ' 12/ 18/ 2003'
AND I TI NERARI ES. DESI G=PARTI CI P. DESI G)
Master Thesis Weiguang Zhang McMaster University- Computing and Software
97
Query 1: give all employee_no for all the workers that work under manager with the first
name Tony7 and the last name Tona7 with no middle name.
SELECT WORKER. EMPLOYEENO
FROM WORKER,
( SELECT MANAGER. EMPLOYEENO
Master Thesis Weiguang Zhang McMaster University- Computing and Software
104
Bibliography
[1] E.F. Codd, A Relational Model of Data for Large Shared Data Banks,
Communications of ACM, Volume 13, Number 6, 1970, 377--387
[2] Pioneer calls relational database technology obsolete,
http://www.computerworlduk.com/news/applications/5059/pioneer-calls-relational-
database-technology-obsolete/
[3] Michael Stonebraker, The End of a DBMS Era (Might be Upon Us)
http://cacm.acm.org/blogs/blog-cacm/32212-the-end-of-a-dbms-era-might-be-upon-
us/fulltext
[4] Peter Pin-shan Chen, The Entity-Relationship Model: Toward a Unified View of
Data, ACM Transactions on Database Systems, Volume 1, 1976, 9--36
Appendix to
A SUITE OF CASE STUDIES IN
RELATIONAL DATABASE DESIGN
M.Sc. thesis by WEIGUANG ZHANG
McMaster University, J anuary 2012
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
Contents
Appendix 1: Airline Reservation Application Program ................................................. 2
Appendix 2: Movie Rental Application Program ......................................................... 14
Appendix 3: Car Rental Application program ............................................................. 24
Appendix 4: Course Registration Application program .............................................. 34
Appendix 5: Emergency Room Application program .................................................. 39
Appendix 6: Property Rental Application Program ..................................................... 47
Appendix 7: Software Project Application Program ................................................... 57
Appendix 8: Tour Operator System Application Program ......................................... 67
Appendix 9: Warehouse System Application Program ................................................ 84
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
i nt i ;
i =st r l en( s) - 1;
whi l e( s[ i ] ==' ' ) s[ i - - ] =' \ 0' ;
}
char buf f er [ 300] ;
i nt mai n( ) {
i nt i , j ;
connect ( ) ;
whi l e( 1) {
A: pr i nt f ( " ent er number of quer y you want t o execut e "
" ( 1- 10) , \ n 0 t o qui t , anyt hi ng el se al l quer i es\ n" ) ;
f get s( buf f er , 300, st di n) ;
i f ( buf f er [ 0] ==' \ n' ) {
pr i nt f ( " not hi ng ent er ed\ n" ) ;
cont i nue;
}
i =j =0;
whi l e( buf f er [ j ] ! =' \ n' ) {
i f ( buf f er [ j ] >=' 0' && buf f er [ j ] <=' 9' )
i = 10*i +buf f er [ j ] - ' 0' ;
el se{
pr i nt f ( " i ncor r ect ent r y\ n" ) ;
got o A;
}
j ++;
}
i f ( i == 0) br eak;
el se i f ( i == 1) quer y1( ) ;
el se i f ( i == 2) quer y2( ) ;
el se i f ( i == 3) quer y3( ) ;
el se i f ( i == 4) quer y4( ) ;
el se i f ( i == 5) quer y5( ) ;
el se i f ( i == 6) quer y6( ) ;
el se i f ( i == 7) quer y7( ) ;
el se i f ( i == 8) quer y8( ) ;
el se i f ( i == 9) quer y9( ) ;
el se i f ( i == 10) quer y10( ) ;
el se {
do_al l ( ) ;
br eak;
}
}
di sconnect ( ) ;
r et ur n 0;
}
i nt count , l en, l en1;
char buf 1[ 80] ;
EXEC SQL BEGI N DECLARE SECTI ON;
sql i nt 32 cust omer _i d;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
ORDER BY C. CUSTI D;
sql er r ( " DECLARE cur 6" ) ;
EXEC SQL OPEN cur 6;
sql er r ( " OPEN cur 6" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 6 I NTO : cust omer _i d,
: f i r st _name,
: l ast _name,
: phone_no,
: emai l ;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " CUSTOMER_I D FI RST_NAME LAST_NAME PHONE_NO EMAI L\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( cust omer _i d) ;
spr i nt f ( buf f er , " %s" , cust omer _i d) ;
f or ( l en=st r l en( buf f er ) ; l en < 12; l en++) st r cat ( buf f er , " " ) ;
depad( f i r st _name) ;
spr i nt f ( buf f er +l en, " %s" , f i r st _name) ;
f or ( l en=st r l en( buf f er ) ; l en < 22; l en++) st r cat ( buf f er , " " ) ;
depad( l ast _name) ;
spr i nt f ( buf f er +l en, " %s" , l ast _name) ;
f or ( l en=st r l en( buf f er ) ; l en < 32; l en++) st r cat ( buf f er , " " ) ;
depad( phone_no) ;
spr i nt f ( buf f er +l en, " %s" , phone_no) ;
f or ( l en=st r l en( buf f er ) ; l en < 42; l en++) st r cat ( buf f er , " " ) ;
depad( emai l ) ;
spr i nt f ( buf f er +l en, " %s" , emai l ) ;
pr i nt f ( " %s\ n" , buf f er ) ;
count ++;
}
EXEC SQL CLOSE cur 6;
sql er r ( " CLOSE cur 6" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y7( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 7\ n" ) ;
EXEC SQL DECLARE cur 7 CURSOR FOR
SELECT BKGNO AS BOOKI NG_NO, CUSTI D AS CUSTOMER_I D, FNO AS FLI GHT_NO,
ORI G AS ORI GI N, DEST AS DESTI NATI ON, C. CLASS AS CLASS,
S. STATUS AS STATUS, CI TY. CI TYNM AS BOOKI NG_CI TY
FROM BOOKI NG B
I NNER J OI N STATUS S ON B. STATUSI D = S. STATUSI D
I NNER J OI N CLASS C ON B. CLASSI D = C. CLASSI D
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
10
}
voi d quer y8( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 8\ n" ) ;
EXEC SQL DECLARE cur 8 CURSOR FOR
SELECT C. CI TYNM AS CI TY, SUM( TOTPRI CE) AS TOTAL_PRI CE,
SUM( PAI DAMT) AS TOTAL_PAYMENT, SUM( BAL) AS TOTAL_BALANCE
FROM BOOKI NG B
I NNER J OI N CI TY C ON B. BKGCI TY = C. CI TYI D
WHERE STATUSI D <> 2
GROUP BY C. CI TYNM
ORDER BY C. CI TYNM;
sql er r ( " DECLARE cur 8" ) ;
EXEC SQL OPEN cur 8;
sql er r ( " OPEN cur 8" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 8 I NTO : ci t y,
: t ot al _pr i ce,
: t ot al _payment ,
: t ot al _bal ance;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " CI TY TOTAL_PRI CE TOTAL_PAYMENT TOTAL_BALANCE\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( ci t y) ;
pr i nt f ( " %s" , ci t y) ;
f or ( l en=st r l en( buf f er ) ; l en < 15; l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf 1, " %d" , t ot al _pr i ce) ;
f or ( l en1=st r l en( buf 1) ; l en1 10; l en1++, l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf 2, " %d" , t ot al _payment ) ;
f or ( l en2=st r l en( buf 2) ; l en2 10; l en2++, l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf 3, " %d" , t ot al _bal ance) ;
f or ( l en3=st r l en( buf 3) ; l en3 10; l en3++, l en++) st r cat ( buf f er , " " ) ;
pr i nt f ( " %s\ n" , buf f er ) ;
count ++;
}
EXEC SQL CLOSE cur 8;
sql er r ( " CLOSE cur 8" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y9( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 9\ n" ) ;
EXEC SQL DECLARE cur 9 CURSOR FOR
SELECT BKGNO AS BOOKI NG_NO, ORI G AS ORI GI N, DEST AS DESTI NATI ON,
FPRI CE AS FLI GHT_PRI CE,
TOTPRI CE AS PREVI OUS_TOTAL_PRI CE,
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
11
12
13
quer y6( ) ;
quer y7( ) ;
quer y8( ) ;
quer y9( ) ;
quer y10( ) ;
}
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
14
15
i =st r l en( s) - 1;
whi l e( s[ i ] ==' ' ) s[ i - - ] =' \ 0' ;
}
char buf f er [ 300] ;
i nt mai n( ) {
i nt i , j ;
connect ( ) ;
whi l e( 1) {
A: pr i nt f ( " ent er number of quer y you want t o execut e "
" ( 1- 10) , \ n0 t o qui t , anyt hi ng el se al l quer i es\ n" ) ;
f get s( buf f er , 300, st di n) ;
i f ( buf f er [ 0] ==' \ n' ) {
pr i nt f ( " not hi ng ent er ed\ n" ) ;
cont i nue;
}
i =j =0;
whi l e( buf f er [ j ] ! =' \ n' ) {
i f ( buf f er [ j ] >=' 0' && buf f er [ j ] <=' 9' )
i = 10*i +buf f er [ j ] - ' 0' ;
el se{
pr i nt f ( " i ncor r ect ent r y\ n" ) ;
got o A;
}
j ++;
}
i f ( i == 0) br eak;
el se i f ( i == 1) quer y1( ) ;
el se i f ( i == 2) quer y2( ) ;
el se i f ( i == 3) quer y3( ) ;
el se i f ( i == 4) quer y4( ) ;
el se i f ( i == 5) quer y5( ) ;
el se i f ( i == 6) quer y6( ) ;
el se i f ( i == 7) quer y7( ) ;
el se i f ( i == 8) quer y8( ) ;
el se i f ( i == 9) quer y9( ) ;
el se i f ( i == 10) quer y10( ) ;
el se {
do_al l ( ) ;
br eak;
}
}
di sconnect ( ) ;
r et ur n 0;
}
i nt count , l en, l en1;
char buf 1[ 80] ;
EXEC SQL BEGI N DECLARE SECTI ON;
sql i nt 32 cust omer _i d;
char cust omer _name[ 20] ;
char empl oyee_SI N[ 9] ;
doubl e t ot al _amount ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
16
sql i nt 32 st or e_i d;
sql i nt 32 movi e_r ent ed;
sql i nt 32 movi e_i d;
sql i nt 32 t ape_i d;
char payment _t ype[ 10] ;
doubl e t ot al _amount ;
sql i nt 16 r at i ng;
sql i nt 16 number _of _movi es;
char movi e_t i t l e[ 20] ;
char r ent al _st at us[ 20] ;
doubl e r ent al _r at e;
char r ent al _empl oyee[ 20] ;
char cashi er _empl oyee[ 20] ;
char payment _st at us[ 20] ;
char manager _name[ 20] ;
char empl oyee_name[ 20] ;
EXEC SQL END DECLARE SECTI ON;
voi d quer y1( ) {
pr i nt f ( " di spl ayi ng r esul t of quer y 1\ n" ) ;
EXEC SQL DECLARE cur 1 CURSOR FOR
SELECT CUSTI D AS CUSTOMER_I D, CUSTNM AS CUSTOMER_NAME
FROM CUSTOMER
WHERE CUSTCI TY=' Hami l t on' ;
sql er r ( " OPEN cur 1" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 1 I NTO : cust omer _i d,
: cust omer _name;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " CUSTOMER_I D CUSTOMER_NAME\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( cust omer _name) ;
spr i nt f ( buf f er , " %d" , cust omer _i d) ;
whi l e( st r l en( buf f er ) <15) st r cat ( buf f er , " " ) ;
st r cat ( buf f er , cust omer _name) ;
count ++;
}
EXEC SQL CLOSE cur 1;
sql er r ( " CLOSE cur 1" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y2( ) {
pr i nt f ( " di spl ayi ng r esul t of quer y 2\ n" ) ;
EXEC SQL DECLARE cur 2 CURSOR FOR
SELECT EMPSI N AS EMPLOYEE_SI N, SUM( AMT) AS TOTAL_AMT
FROM PAYMENT
GROUP BY EMPSI N
ORDER BY EMPSI N;
EXEC SQL OPEN cur 2;
sql er r ( " OPEN cur 2" ) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
17
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 2 I NTO : empl oyee_SI N,
: t ot al _amount ;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " EMPLOYEE_SI N TOTAL_AMT\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( empl oyee_SI N) ;
spr i nt f ( buf f er , " %s" , empl oyee_SI N) ;
whi l e( st r l en( buf f er ) <15) st r cat ( buf f er , " " ) ;
pr i nt f ( " %d\ n" , t ot al _amount ) ;
count ++;
}
EXEC SQL CLOSE cur 2;
sql er r ( " CLOSE cur 2" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y3( ) {
pr i nt f ( " di spl ayi ng r esul t of quer y 3\ n" ) ;
EXEC SQL DECLARE cur 3 CURSOR FOR
SELECT STOREI D, COUNT( TAPEI D) AS MOVI E_RENTED
FROM MOVI E_RENTAL
GROUP BY STOREI D
ORDER BY STOREI D;
sql er r ( " DECLARE cur 3" ) ;
EXEC SQL OPEN cur 3;
sql er r ( " OPEN cur 3" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 3 I NTO : st or e_i d,
: movi e_r ent ed;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " STORE_I D MOVI E_RENTED\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
spr i nt f ( buf f er , " %d" , st or e_i d) ;
whi l e( st r l en( buf f er ) <11) st r cat ( buf f er , " " ) ;
pr i nt f ( " %d\ n" , movi e_r ent ed) ;
count ++;
}
EXEC SQL CLOSE cur 3;
sql er r ( " CLOSE cur 3" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y4( ) {
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
18
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 4\ n" ) ;
EXEC SQL DECLARE cur 4 CURSOR FOR
SELECT DI STI NCT T. MOVI EI D, T. TAPEI D
FROM TAPE T
LEFT J OI N MOVI E_RENTAL R
ON T. MOVI EI D=R. MOVI EI D
AND T. TAPEI D=R. TAPEI D
WHERE R. MOVI EI D I S NULL
ORDER BY T. MOVI EI D, T. TAPEI D;
sql er r ( " DECLARE cur 4" ) ;
EXEC SQL OPEN cur 4;
sql er r ( " OPEN cur 4" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 4 I NTO : movi e_i d,
: t ape_i d;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " MOVI E_I D TAPE_I D\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
spr i nt f ( buf f er , " %d" , movi e_i d) ;
whi l e( st r l en( buf f er ) <11) st r cat ( buf f er , " " ) ;
pr i nt f ( " %d\ n" , t ape_i d) ;
count ++;
}
EXEC SQL CLOSE cur 4;
sql er r ( " CLOSE cur 4" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y5( ) {
pr i nt f ( " di spl ayi ng r esul t of quer y 5\ n" ) ;
EXEC SQL DECLARE cur 5 CURSOR FOR
SELECT CUSTI D, CUSTNM
FROM CUSTOMER
WHERE CUSTI D NOT I N ( SELECT DI STI NCT CUSTI D FROM MOVI E_RENTAL)
ORDER BY CUSTI D;
sql er r ( " DECLARE cur 5" ) ;
EXEC SQL OPEN cur 5;
sql er r ( " OPEN cur 5" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 5 I NTO : cust omer _i d,
: cust omer _name;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " CUSTOMER_I D CUSTOMER_NAME\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
19
20
21
EXEC SQL CLOSE cur 8;
sql er r ( " CLOSE cur 8" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y9( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 9\ n" ) ;
EXEC SQL DECLARE cur 9 CURSOR FOR
SELECT M. MOVI ETI TL AS MOVI E_TI TLE,
RS. RDESC AS RENTAL_STATUS,
MR. RRATE AS RENTAL_RATE,
E1. EMPNM AS RENTAL_EMPLOYEE,
E2. EMPNM AS CASHI ER_EMPLOYEE,
PT. PTDESC AS PAYMENT_TYPE,
PS. PDESC AS PAYMENT_STATUS
FROM MOVI E_RENTAL MR
I NNER J OI N CUSTOMER C ON MR. CUSTI D = C. CUSTI D
I NNER J OI N MOVI E M ON MR. MOVI EI D = M. MOVI EI D
I NNER J OI N RENTAL_STATUS RS ON MR. RSTATUSI D = RS. RSTATUSI D
I NNER J OI N EMPLOYEE E1 ON MR. EMPSI N = E1. EMPSI N
LEFT J OI N PAYMENT P ON MR. PAYI D = P. PAYI D
LEFT J OI N EMPLOYEE E2 ON P. EMPSI N = E2. EMPSI N
LEFT J OI N PAYMENT_STATUS PS ON P. PSTATUSI D = PS. PSTATUSI D
LEFT J OI N PAYMENT_TYPE PT ON P. PTI D = PT. PTI D
WHERE C. CUSTNM = ' cust omer 1' ;
sql er r ( " DECLARE cur 9" ) ;
EXEC SQL OPEN cur 9;
sql er r ( " OPEN cur 9" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 9 I NTO : movi e_t i t l e,
: r ent al _st at us,
: r ent al _r at e,
: r ent al _empl oyee,
: cashi er _empl oyee,
: payment _t ype,
: payment _st at us;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " MOVI E_TI TLE RENTAL_STATUS RENTAL_RATE RENTAL_EMP CASHI ER_EMP
PAYMENT_TYPE pAYMENT_STATUS \ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( movi e_t i t l e) ;
depad( r ent al _st at us) ;
depad( r ent al _empl oyee) ;
depad( cashi er _empl oyee) ;
depad( payment _t ype) ;
depad( payment _st at us) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
22
pr i nt f ( " %s" , movi e_t i t l e) ;
f or ( i = st r l en( movi e_t i t l e) ; i <12; put char ( ' ' ) , i ++) ;
pr i nt f ( " %s\ n" , r ent al _st at us) ;
f or ( i = st r l en( r ent al _st at us) ; i <26; put char ( ' ' ) , i ++) ;
spr i nt f ( buf f er , " %d" , r ent al _r at e) ;
whi l e( st r l en( buf f er ) <38) st r cat ( buf f er , " " ) ;
pr i nt f ( " %s\ n" , r ent al _empl oyee) ;
f or ( i = st r l en( r ent al _empl oyee) ; i <48; put char ( ' ' ) , i ++) ;
pr i nt f ( " %s\ n" , cashi er _empl oyee) ;
f or ( i = st r l en( cashi er _empl oyee) ; i <60; put char ( ' ' ) , i ++) ;
pr i nt f ( " %s\ n" , payment _t ype) ;
f or ( i = st r l en( payment _t ype) ; i <72; put char ( ' ' ) , i ++) ;
pr i nt f ( " %s\ n" , payment _st at us) ;
count ++;
}
EXEC SQL CLOSE cur 9;
sql er r ( " CLOSE cur 9" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y10( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 10\ n" ) ;
EXEC SQL DECLARE cur 10 CURSOR FOR
SELECT MGR. EMPNM AS MANAGER_NAME, E. EMPNM AS EMPLOYEE_NAME
FROM EMPLOYEE E
I NNER J OI N EMPLOYEE MGR ON E. MGRSI N = MGR. EMPSI N
ORDER BY MGR. EMPSI N, E. EMPSI N;
sql er r ( " DECLARE cur 10" ) ;
EXEC SQL OPEN cur 10;
sql er r ( " OPEN cur 10" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 10 I NTO : manager _name,
: empl oyee_name;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " MANAGER_NAME EMPLOYEE_NAME\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( manager _name) ;
depad( empl oyee_name) ;
pr i nt f ( " %s" , manager _name) ;
f or ( i = st r l en( manager _name) ; i <15; put char ( ' ' ) , i ++) ;
pr i nt f ( " %S\ n" , empl oyee_name) ;
count ++;
}
EXEC SQL CLOSE cur 10;
sql er r ( " CLOSE cur 10" ) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
23
24
25
21. Executes a query to insert into table RENTAL the new address, the to_location is to be the same
as the from_location (obtained in 17), from_date is CURRENT DATE, to_date is NULL, and
return_odo is NULL.
22. Displays a message that the rental was added to the database.
23. Executes a query to select all records from RENTAL.
24. Using a cursor, displays all rows of RENTAL.
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
#i ncl ude <st r i ng. h>
EXEC SQL I NCLUDE SQLCA;
i nt connect ed=0;
voi d sql er r ( char * x) {
i f ( SQLCODE! =0) {
pr i nt f ( " er r or %s ( %d) \ n" , x, SQLCODE) ;
i f ( connect ed) {
EXEC SQL CONNECT RESET;
pr i nt f ( " di sconnect ed f r omOURDB\ n" ) ;
}
exi t ( 1) ;
}
}
voi d connect ( ) {
EXEC SQL CONNECT TO OURDB;
sql er r ( " CONNECT TO OURDB" ) ;
pr i nt f ( " Connect ed t o OURDB\ n" ) ;
connect ed = 1;
}
voi d di sconnect ( ) {
EXEC SQL CONNECT RESET;
sql er r ( " CONNECT RESET" ) ;
pr i nt f ( " Di sconnect ed f r omOURDB\ n" ) ;
connect ed = 0;
}
voi d depad( char * s) {
i nt i ;
i =st r l en( s) - 1;
whi l e( s[ i ] ==' ' ) s[ i - - ] =' \ 0' ;
}
#def i ne s_dl i cense 4
#def i ne s_car i d 9
#def i ne s_f r om_l ocat i on 15
#def i ne s_dr opof f _l ocat i on 24
#def i ne s_f r om_dat e 33
#def i ne s_t o_dat e 45
#def i ne s_t ank 56
#def i ne s_i ni t _odo 62
#def i ne s_r et ur n_odo 71
i nt mai n( ) {
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
26
27
}
whi l e( 1) {
pr i nt f ( " ent er ci t y: " ) ;
f f l ush( st dout ) ;
f get s( buf f , 400, st di n) ;
buf f [ st r l en( buf f ) - 1] =' \ 0' ;
i f ( st r l en( buf f ) >20) {
pr i nt f ( " ent r y t oo l ong\ n" ) ;
cont i nue;
}
st r cpy( ci t y, buf f ) ;
br eak;
}
whi l e( 1) {
pr i nt f ( " ent er pr ovi nce: " ) ;
f f l ush( st dout ) ;
f get s( buf f , 400, st di n) ;
buf f [ st r l en( buf f ) - 1] =' \ 0' ;
i f ( st r l en( buf f ) >2) {
pr i nt f ( " ent r y t oo l ong\ n" ) ;
cont i nue;
}
st r cpy( pr ovi nce, buf f ) ;
br eak;
}
whi l e( 1) {
pr i nt f ( " ent er post al code: " ) ;
f f l ush( st dout ) ;
f get s( buf f , 400, st di n) ;
buf f [ st r l en( buf f ) - 1] =' \ 0' ;
i f ( st r l en( buf f ) >6) {
pr i nt f ( " ent r y t oo l ong\ n" ) ;
cont i nue;
}
st r cpy( pcode, buf f ) ;
br eak;
}
/ * Let ' s f i nd out i f t he addr ess i s al r eady i n t he syst em*/
EXEC SQL DECLARE cur 0 CURSOR FOR
SELECT ADDRESSI D, SNUMBER, STREET, CI TY, PROVI NCE, PCODE
FROM ADDRESS
WHERE SNUMBER=: snumber
AND STREET=: st r eet
AND CI TY=: ci t y
AND PROVI NCE=: pr ovi nce
AND PCODE=: pcode;
sql er r ( " DECLARE cur 0" ) ;
EXEC SQL OPEN cur 0;
sql er r ( " OPEN cur 0" ) ;
EXEC SQL FETCH cur 0 I NTO : addr essi d;
i f ( SQLCODE==0) {
EXEC SQL CLOSE cur 0;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
28
29
30
pr i nt f ( " per son ( %s, %s) al r eady a cust omer \ n" , f name, l name) ;
el se{
EXEC SQL I NSERT I NTO CUSTOMER ( DLI CENSE) VALUES
( : dl i cense) ;
sql er r ( " I NSERT I NTO CUSTOMER" ) ;
pr i nt f ( " cust omer ( %s, %s) added\ n" , f name, l name) ;
}
EXEC SQL CLOSE cur 3;
sql er r ( " CLOSE cur 3" ) ;
whi l e( 1) {
pr i nt f ( " ent er car di d: " ) ;
f f l ush( st dout ) ;
f get s( buf f , 400, st di n) ;
buf f [ st r l en( buf f ) - 1] =' \ 0' ;
car i d=at oi ( buf f ) ;
EXEC SQL DECLARE cur 4 CURSOR FOR
SELECT CARI D FROM CAR WHERE CARI D=: car i d;
sql er r ( " DECLARE cur 4" ) ;
EXEC SQL OPEN cur 4;
sql er r ( " OPEN cur 4" ) ;
EXEC SQL FETCH cur 4 I NTO : _car i d;
i f ( SQLCODE! =0) {
pr i nt f ( " car wi t h car i d %d not i n t he syst em\ n" , _car i d) ;
EXEC SQL CLOSE cur 4;
sql er r ( " CLOSE cur 4" ) ;
cont i nue;
}
EXEC SQL CLOSE cur 4;
sql er r ( " CLOSE cur 4" ) ;
EXEC SQL DECLARE cur 5 CURSOR FOR
SELECT CARI D
FROM RENTAL
WHERE CARI D=: car i d
AND TO_DATE I S NULL;
sql er r ( " DECLARE cur 5" ) ;
EXEC SQL OPEN cur 5;
sql er r ( " OPEN cur 5" ) ;
EXEC SQL FETCH cur 5 I NTO : _car i d;
i f ( SQLCODE==0) {
pr i nt f ( " car wi t h car i d %d r ent ed out \ n" , _car i d) ;
EXEC SQL CLOSE cur 5;
sql er r ( " CLOSE cur 5" ) ;
cont i nue;
}
EXEC SQL CLOSE cur 5;
sql er r ( " CLOSE cur 5" ) ;
br eak;
}
whi l e( 1) {
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
31
32
br eak;
}
pr i nt f ( " ent er odomet er r eadi ng: " ) ;
f f l ush( st dout ) ;
f get s( buf f , 400, st di n) ;
buf f [ st r l en( buf f ) - 1] =' \ 0' ;
i ni t _odo = at oi ( buf f ) ;
/ * we have t o r ead t abl e RENTAL and f i nd maxi mumRENTALI D so
we can det er mi ne RENTALI D f or t he new r ent al */
EXEC SQL DECLARE cur 8 CURSOR FOR
SELECT MAX( RENTALI D) FROM RENTAL;
sql er r ( " DECLARE cur 8" ) ;
EXEC SQL OPEN cur 8;
sql er r ( " OPEN cur 8" ) ;
EXEC SQL FETCH cur 8 I NTO : r ent al i d;
i f ( SQLCODE==0)
r ent al i d++;
el se
r ent al i d=1;
/ * so now we have al l i nf or mat i on t o ent er t he r ent al */
EXEC SQL I NSERT I NTO RENTAL
( RENTALI D, DLI CENSE, CARI D, FROM_LOCATI ON, DROPOFF_LOCATI ON, FROM_DATE, TO_DATE,
TANK, I NI T_ODO, RETURN_ODO)
VALUES
( : r ent al i d, : dl i cense, : car i d, : f r om_l ocat i on, : dr opof f _l ocat i on,
CURRENT DATE, NULL, ' F' , : i ni t _odo, NULL) ;
sql er r ( " I NSERT I NTO RENTAL" ) ;
pr i nt f ( " r ent al %d successf ul l y i nser t ed\ n" , r ent al i d) ;
pr i nt f ( " di spl ay t he cont ent s of t abl e RENTAL\ n" ) ;
EXEC SQL DECLARE cur 9 CURSOR FOR
SELECT RENTALI D, DLI CENSE, CARI D, FROM_LOCATI ON, DROPOFF_LOCATI ON, FROM_DATE,
TO_DATE, TANK, I NI T_ODO, RETURN_ODO
FROM RENTAL;
sql er r ( " DECLARE cur 9" ) ;
EXEC SQL OPEN cur 9;
sql er r ( " OPEN cur 9" ) ;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 9 I NTO : r ent al i d,
: dl i cense,
: car i d,
: f r om_l ocat i on,
: dr opof f _l ocat i on: dr opof f _l ocat i on_i nd,
: f r om_dat e,
: t o_dat e: t o_dat e_i nd,
: t ank,
: i ni t _odo,
: r et ur n_odo: r et ur n_odo_i nd;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
33
depad( dl i cense) ;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f ( " RENTALI D\ n" ) ;
pr i nt f ( " DLI C CARI D FROM_LOC DROP_LOC FROM_DATE TO_DATE TANK I NI T_ODO
RET_ODO\ n" ) ;
f i r st =0;
}
spr i nt f ( buf f , " %d" , r ent al i d) ;
whi l e( st r l en( buf f ) <s_dl i cense) st r cat ( buf f , " " ) ;
spr i nt f ( &buf f [ st r l en( buf f ) ] , " %s" , dl i cense) ;
whi l e( st r l en( buf f ) <s_car i d) st r cat ( buf f , " " ) ;
spr i nt f ( &buf f [ st r l en( buf f ) ] , " %d" , car i d) ;
whi l e( st r l en( buf f ) <s_f r om_l ocat i on) st r cat ( buf f , " " ) ;
spr i nt f ( &buf f [ st r l en( buf f ) ] , " %d" , f r om_l ocat i on) ;
whi l e( st r l en( buf f ) <s_dr opof f _l ocat i on) st r cat ( buf f , " " ) ;
spr i nt f ( &buf f [ st r l en( buf f ) ] , " %d" , dr opof f _l ocat i on) ;
whi l e( st r l en( buf f ) <s_f r om_dat e) st r cat ( buf f , " " ) ;
st r cat ( buf f , f r om_dat e) ;
whi l e( st r l en( buf f ) <s_t o_dat e) st r cat ( buf f , " " ) ;
i f ( t o_dat e_i nd==0)
st r cat ( buf f , t o_dat e) ;
el se
st r cat ( buf f , " - - " ) ;
whi l e( st r l en( buf f ) <s_t ank) st r cat ( buf f , " " ) ;
st r cat ( buf f , t ank) ;
whi l e( st r l en( buf f ) <s_i ni t _odo) st r cat ( buf f , " " ) ;
spr i nt f ( &buf f [ st r l en( buf f ) ] , " %d" , i ni t _odo) ;
whi l e( st r l en( buf f ) <s_r et ur n_odo) st r cat ( buf f , " " ) ;
i f ( r et ur n_odo_i nd==0)
spr i nt f ( &buf f [ st r l en( buf f ) ] , " %d" , r et ur n_odo) ;
el se
st r cat ( buf f , " - - " ) ;
pr i nt f ( " %s\ n" , buf f ) ;
}
EXEC SQL CLOSE cur 9;
sql er r ( " CLOSE cur 9" ) ;
i f ( f i r st )
pr i nt f ( " t abl e RENTAL i s empt y\ n" ) ;
di sconnect ( ) ;
r et ur n 0;
}
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
34
35
quer y6( ) ;
quer y7( ) ;
EXEC SQL CONNECT RESET;
sql er r ( " CONNECT RESET" ) ;
pr i nt f ( " Di sconnect ed f r omOURDB\ n" ) ;
r et ur n 0;
}
voi d quer y1( ) {
EXEC SQL DECLARE cur 1 CURSOR FOR
SELECT NAME, DESI G, STATUS
FROM STUDENT, PERSON, L1
WHERE STUDENT. I D=L1. I D
AND ( STUDENT. I D=' 0000041' OR STUDENT. I D=' 0000042' )
AND STUDENT. I D=PERSON. I D
uni on
SELECT NAME, DESI G, STATUS
FROM STUDENT, PERSON, L2
WHERE STUDENT. I D=L2. I D
AND ( STUDENT. I D=' 0000041' OR STUDENT. I D=' 0000042' )
AND STUDENT. I D=PERSON. I D;
EXEC SQL OPEN cur 1;
sql er r ( " OPEN cur 1" ) ;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 1 I NTO : NAME,
: DESI G,
: STATUS;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f ( " \ nQUERY 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
pr i nt f ( " NAME DESI G STATUS\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %s" , NAME) ;
whi l e( st r l en( l i ne) < 25) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , DESI G) ;
whi l e( st r l en( l i ne) < 35) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , STATUS) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 1;
sql er r ( " CLOSE cur 1" ) ;
}
voi d quer y2( ) {
EXEC SQL DECLARE cur 2 CURSOR FOR
SELECT PERSON. NAME
FROM PERSON, STUDENT
WHERE PERSON. I D=STUDENT. I D
AND STUDENT. YEAR=1;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
36
EXEC SQL OPEN cur 2;
sql er r ( " OPEN cur 2" ) ;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 2 I NTO : NAME;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f ( " \ nQUERY 2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
pr i nt f ( " NAME\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %s" , NAME) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 2;
sql er r ( " CLOSE cur 2" ) ;
}
voi d quer y3( ) {
EXEC SQL DECLARE cur 3 CURSOR FOR
SELECT PERSON. NAME
FROM PERSON, I NSTRUCTOR, HASI , Y1COURSE
WHERE PERSON. I D=I NSTRUCTOR. I D
AND HASI . I D=I NSTRUCTOR. I D
AND HASI . DESI G=Y1COURSE. DESI G;
EXEC SQL OPEN cur 3;
sql er r ( " OPEN cur 3" ) ;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 3 I NTO : NAME;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f ( " \ nQUERY 3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
pr i nt f ( " NAME\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %s" , NAME) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 3;
sql er r ( " CLOSE cur 3" ) ;
}
voi d quer y4( ) {
EXEC SQL DECLARE cur 4 CURSOR FOR
SELECT DI STI NCT DESI G
FROM TUTORI AL;
EXEC SQL OPEN cur 4;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
37
38
EXEC SQL OPEN cur 6;
sql er r ( " OPEN cur 6" ) ;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 6 I NTO : NAME;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f ( " \ nQUERY 6 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
pr i nt f ( " DESI G\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %s" , NAME) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 6;
sql er r ( " CLOSE cur 6" ) ;
}
voi d quer y7( ) {
EXEC SQL DECLARE cur 7 CURSOR FOR
SELECT DI STI NCT NAME
FROM PERSON, I NSTRUCTOR
WHERE ( PERSON. I D=I NSTRUCTOR. I D)
AND NAME NOT I N ( SELECT DI STI NCT NAME
FROM PERSON, I NSTRUCTOR, HASI
WHERE ( I NSTRUCTOR. I D=PERSON. I D)
AND ( HASI . I D=PERSON. I D)
AND HASI . DESI G I N ( SELECT COURSE. DESI G
FROM COURSE, LAB
WHERE COURSE. DESI G=LAB. DESI G
AND SECTI ON=2)
) ;
EXEC SQL OPEN cur 7;
sql er r ( " OPEN cur 7" ) ;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 7 I NTO : NAME;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f ( " \ nQUERY 7 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
pr i nt f ( " DESI G\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %s" , NAME) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 7;
sql er r ( " CLOSE cur 7" ) ;
}
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
39
40
i f ( connect ed) {
EXEC SQL CONNECT RESET;
pr i nt f ( " di sconnect ed f r omOURDB\ n" ) ;
}
exi t ( 1) ;
}
}
i nt mai n( ) {
EXEC SQL CONNECT TO OURDB;
sql er r ( " CONNECT TO OURDB" ) ;
pr i nt f ( " Connect ed t o OURDB\ n" ) ;
connect ed = 1;
quer y1( ) ;
quer y2( ) ;
quer y3( ) ;
quer y4( ) ;
quer y5( ) ;
quer y6( ) ;
quer y7( ) ;
quer y8( ) ;
quer y9( ) ;
quer y10( ) ;
quer y11( ) ;
quer y12( ) ;
quer y13( ) ;
quer y14( ) ;
quer y15( ) ;
quer y16( ) ;
EXEC SQL CONNECT RESET;
sql er r ( " CONNECT RESET" ) ;
pr i nt f ( " Di sconnect ed f r omOURDB\ n" ) ;
r et ur n 0;
}
voi d quer y1( ) {
/ / The quer y r et ur ns an empt y set i f CON1 i s sat i sf i ed
EXEC SQL DECLARE cur 1 CURSOR FOR
( ( SELECT RI D FROM RECEPTI ONI ST) I NTERSECT ( SELECT NI D FROM NURSE) )
UNI ON
( ( SELECT RI D FROM RECEPTI ONI ST) I NTERSECT ( SELECT DI D FROM DOCTOR) )
UNI ON
( ( SELECT NI D FROM NURSE) I NTERSECT ( SELECT DI D FROM DOCTOR) ) ;
EXEC SQL OPEN cur 1;
sql er r ( " OPEN cur 1" ) ;
EXEC SQL FETCH cur 1 I NTO : i d;
i f ( SQLCODE==0)
pr i nt f ( " CON1 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON1 sat i sf i ed\ n" ) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
41
42
43
i f ( SQLCODE==0)
pr i nt f ( " CON6 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON6 sat i sf i ed\ n" ) ;
EXEC SQL CLOSE cur 6;
sql er r ( " CLOSE cur 6" ) ;
}
voi d quer y7( ) {
/ / The quer y r et ur ns an empt y set i f CON7 i s sat i sf i ed
EXEC SQL DECLARE cur 7 CURSOR FOR
( ( SELECT SHI FTI D FROM SHI FT) EXCEPT ( SELECT SHI FTI D FROM RONS) )
UNI ON
( ( SELECT RI D FROM RECEPTI ONI ST) EXCEPT ( SELECT RI D FROM RONS) ) ;
EXEC SQL OPEN cur 7;
sql er r ( " OPEN cur 7" ) ;
EXEC SQL FETCH cur 7 I NTO : i d;
i f ( SQLCODE==0)
pr i nt f ( " CON7 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON7 sat i sf i ed\ n" ) ;
EXEC SQL CLOSE cur 7;
sql er r ( " CLOSE cur 7" ) ;
}
voi d quer y8( ) {
/ / The quer y r et ur ns an empt y set i f CON8 i s sat i sf i ed
EXEC SQL DECLARE cur 8 CURSOR FOR
( SELECT SHI FTI D FROM SHI FT)
EXCEPT
( SELECT T. SHI FTI D
FROM NONS AS T, NONS AS R
WHERE T. NI D <> R. NI D AND T. SHI FTI D = R. SHI FTI D) ;
EXEC SQL OPEN cur 8;
sql er r ( " OPEN cur 8" ) ;
EXEC SQL FETCH cur 8 I NTO : i d;
i f ( SQLCODE==0)
pr i nt f ( " CON8 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON8 sat i sf i ed\ n" ) ;
EXEC SQL CLOSE cur 8;
sql er r ( " CLOSE cur 8" ) ;
}
voi d quer y9( ) {
/ / The quer y r et ur ns an empt y set i f CON9 i s sat i sf i ed
EXEC SQL DECLARE cur 9 CURSOR FOR
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
44
( ( SELECT SHI FTI D FROM SHI FT) EXCEPT ( SELECT SHI FTI D FROM NONS) )
UNI ON
( ( SELECT NI D FROM NURSE) EXCEPT ( SELECT NI D FROM NONS) ) ;
EXEC SQL OPEN cur 9;
sql er r ( " OPEN cur 9" ) ;
EXEC SQL FETCH cur 9 I NTO : i d;
i f ( SQLCODE==0)
pr i nt f ( " CON9 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON9 sat i sf i ed\ n" ) ;
EXEC SQL CLOSE cur 9;
sql er r ( " CLOSE cur 9" ) ;
}
voi d quer y10( ) {
/ / The quer y r et ur ns an empt y set i f CON10 i s sat i sf i ed
EXEC SQL DECLARE cur 10 CURSOR FOR
( ( SELECT SHI FTI D FROM SHI FT)
EXCEPT
( SELECT T. SHI FTI D
FROM DONS AS T, DONS AS R
WHERE T. DI D <> R. DI D AND T. SHI FTI D = R. SHI FTI D) ) ;
EXEC SQL OPEN cur 10;
sql er r ( " OPEN cur 10" ) ;
EXEC SQL FETCH cur 10 I NTO : i d;
i f ( SQLCODE==0)
pr i nt f ( " CON10 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON10 sat i sf i ed\ n" ) ;
EXEC SQL CLOSE cur 10;
sql er r ( " CLOSE cur 10" ) ;
}
voi d quer y11( ) {
/ / The quer y r et ur ns an empt y set i f CON11 i s sat i sf i ed
EXEC SQL DECLARE cur 11 CURSOR FOR
( ( SELECT SHI FTI D FROM SHI FT) EXCEPT ( SELECT SHI FTI D FROM DONS) )
UNI ON
( ( SELECT DI D FROM DOCTOR) EXCEPT ( SELECT DI D FROM DONS) ) ;
EXEC SQL OPEN cur 11;
sql er r ( " OPEN cur 11" ) ;
EXEC SQL FETCH cur 11 I NTO : i d;
i f ( SQLCODE==0)
pr i nt f ( " CON11 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON11 sat i sf i ed\ n" ) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
45
46
: i d;
i f ( SQLCODE==0)
pr i nt f ( " CON14 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON14 sat i sf i ed\ n" ) ;
EXEC SQL CLOSE cur 14;
sql er r ( " CLOSE cur 14" ) ;
}
voi d quer y15( ) {
/ / The quer y r et ur ns an empt y set i f CON15 i s sat i sf i ed
EXEC SQL DECLARE cur 15 CURSOR FOR
( SELECT FROM, TO, ADMI SSI ON
FROM SHI FT, ADM
WHERE SHI FT. SHI FTI D=ADM. SHI FTI D
AND ( ADMI SSI ON < FROM OR TO < ADMI SSI ON) ) ;
EXEC SQL OPEN cur 15;
sql er r ( " OPEN cur 15" ) ;
EXEC SQL FETCH cur 15 I NTO : f r om,
: t o,
: admi ssi on;
i f ( SQLCODE==0)
pr i nt f ( " CON15 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON15 sat i sf i ed\ n" ) ;
EXEC SQL CLOSE cur 15;
sql er r ( " CLOSE cur 15" ) ;
}
voi d quer y16( ) {
/ / The quer y r et ur ns an empt y set i f CON16 i s sat i sf i ed
EXEC SQL DECLARE cur 16 CURSOR FOR
( ( SELECT PI D FROM TRI AGEBY)
EXCEPT
( SELECT TRI AGEBY. PI D
FROM DONS, ADM, TRI AGEBY
WHERE TRI AGEBY. PI D=ADM. PI D
AND ADM. SHI FTI D=DONS. SHI FTI D
AND DONS. DI D=TRI AGEBY. DI D) ) ;
EXEC SQL OPEN cur 16;
sql er r ( " OPEN cur 16" ) ;
EXEC SQL FETCH cur 16 I NTO : i d;
i f ( SQLCODE==0)
pr i nt f ( " CON16 not sat i sf i ed\ n" ) ;
el se
pr i nt f ( " CON16 sat i sf i ed\ n" ) ;
EXEC SQL CLOSE cur 16;
sql er r ( " CLOSE cur 16" ) ;
}
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
47
48
49
}
}
di sconnect ( ) ;
r et ur n 0;
}
i nt count , l en, l en1;
char buf 1[ 80] ;
EXEC SQL BEGI N DECLARE SECTI ON;
char st af f _no[ 5] ;
char r ent er _no[ 5] ;
char ad_dat e[ 11] ;
char emai l _addr [ 21] ;
char pr oper t y_no[ 5] ;
char br anch_no[ 5] ;
char f i r st _name[ 11] ;
char mi ddl e_name[ 11] ;
sql i nt 16 mi ddl e_name_i nd;
char l ast _name[ 21] ;
doubl e avg_sal ar y;
char cust omer _no[ 5] ;
char t ype_of _cust omer [ 10] ;
sql i nt 32 ad_cost ;
EXEC SQL END DECLARE SECTI ON;
voi d quer y1( ) {
pr i nt f ( " di spl ayi ng r esul t of quer y 1\ n" ) ;
EXEC SQL DECLARE cur 1 CURSOR FOR
SELECT STAFF_NO
FROM STAFF
WHERE SALARY > 5000
ORDER BY STAFF_NO;
EXEC SQL OPEN cur 1;
sql er r ( " OPEN cur 1" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 1 I NTO : st af f _no;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " STAFF_NO\ n" ) ;
pr i nt f ( " - - - - - - - - \ n" ) ;
}
depad( st af f _no) ;
pr i nt f ( " %s\ n" , st af f _no) ;
count ++;
}
EXEC SQL CLOSE cur 1;
sql er r ( " CLOSE cur 1" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y2( ) {
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
50
51
52
53
54
55
UNI ON
( SELECT RENTER_NO AS CUSTOMER_NO, ' RENTER' AS TYPE_OF_CUSTOMER
FROM RENTER
WHERE RENTER_NO I N ( SELECT RENTER_NO
FROM RENTER_PHONE
GROUP BY RENTER_NO
HAVI NG COUNT( *) >= 2) ) ;
sql er r ( " DECLARE cur 9" ) ;
EXEC SQL OPEN cur 9;
sql er r ( " OPEN cur 9" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 9 I NTO : cust omer _no,
: t ype_of _cust omer ;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " CUSTOMER_NO TYPE_OF_CUSTOMER\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( cust omer _no) ;
depad( t ype_of _cust omer ) ;
pr i nt f ( " %s" , cust omer _no) ;
f or ( i = st r l en( cust omer _no) ; i <12; put char ( ' ' ) , i ++) ;
pr i nt f ( " %s\ n" , t ype_of _cust omer ) ;
count ++;
}
EXEC SQL CLOSE cur 9;
sql er r ( " CLOSE cur 9" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y10( ) {
i nt i ;
pr i nt f ( " di spl ayi ng r esul t of quer y 10\ n" ) ;
EXEC SQL DECLARE cur 10 CURSOR FOR
SELECT T2. ALLOCATED_TO AS BRANCH_NO, SUM( C) *100 AS AD_COST
FROM ( SELECT PROPERTY_NO, COUNT( *) AS C
FROM ADVERTI SEMENT
GROUP BY PROPERTY_NO) AS T1,
( SELECT PROPERTY_NO, ALLOCATED_TO
FROM PROPERTY, STAFF
WHERE PROPERTY. OVERSEEN_BY = STAFF. STAFF_NO) AS T2
WHERE T1. PROPERTY_NO = T2. PROPERTY_NO
GROUP BY T2. ALLOCATED_TO;
sql er r ( " DECLARE cur 10" ) ;
EXEC SQL OPEN cur 10;
sql er r ( " OPEN cur 10" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 10 I NTO : br anch_no,
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
56
: ad_cost ;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " BRANCH_NO AD_COST\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( br anch_no) ;
pr i nt f ( " %s" , br anch_no) ;
f or ( i = st r l en( br anch_no) ; i <10; put char ( ' ' ) , i ++) ;
pr i nt f ( " %d\ n" , ad_cost ) ;
count ++;
}
EXEC SQL CLOSE cur 10;
sql er r ( " CLOSE cur 10" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d do_al l ( ) {
quer y1( ) ;
quer y2( ) ;
quer y3( ) ;
quer y4( ) ;
quer y5( ) ;
quer y6( ) ;
quer y7( ) ;
quer y8( ) ;
quer y9( ) ;
quer y10( ) ;
}
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
57
58
59
60
61
62
get char ( ) ;
}
voi d quer y5( ) {
pr i nt f ( " di spl ayi ng quer y 5\ n" ) ;
get char ( ) ;
EXEC SQL DECLARE cur 5 CURSOR FOR
SELECT PROJ ECT_I D
FROM ( SELECT PROJ ECT_I D, COUNT( *) AS PATCH_COUNT
FROM PATCH
GROUP BY PROJ ECT_I D) ,
( SELECT MAX( PC) AS MAXPC
FROM ( SELECT COUNT( *) AS PC
FROM PATCH
GROUP BY PROJ ECT_I D) )
WHERE PATCH_COUNT=MAXPC;
sql er r ( " DECLARE cur 5" ) ;
EXEC SQL OPEN cur 5;
sql er r ( " OPEN cur 5" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 5 I NTO : pr oj ect i d;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " PROJ ECT_I D\ n" ) ;
pr i nt f ( " - - - - - - - - \ n" ) ;
}
depad( pr oj ect i d) ;
pr i nt f ( " %s\ n" , pr oj ect i d) ;
count ++;
}
EXEC SQL CLOSE cur 5;
sql er r ( " CLOSE cur 5" ) ;
pr i nt f ( " %d r ecor d( s) sel ect ed\ n" , count ) ;
get char ( ) ;
}
voi d quer y6( ) {
pr i nt f ( " di spl ayi ng quer y 6\ n" ) ;
get char ( ) ;
EXEC SQL DECLARE cur 6 CURSOR FOR
SELECT * FROM DEPENDS;
sql er r ( " DECLARE cur 6" ) ;
EXEC SQL OPEN cur 6;
sql er r ( " OPEN cur 6" ) ;
count =0;
whi l e( 1) {
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
63
64
voi d quer y8( ) {
pr i nt f ( " di spl ayi ng quer y 8\ n" ) ;
get char ( ) ;
EXEC SQL DECLARE cur 8 CURSOR FOR
SELECT T. PROJ ECT_I D
FROM ( SELECT PROJ ECT_I D, COUNT( *) AS DEPEND_COUNT
FROM DEPENDS
GROUP BY PROJ ECT_I D) AS T,
( SELECT MAX( DC) AS MAXDC
FROM ( SELECT COUNT( *) AS DC
FROM DEPENDS
GROUP BY PROJ ECT_I D) )
WHERE T. DEPEND_COUNT = MAXDC;
sql er r ( " DECLARE cur 8" ) ;
EXEC SQL OPEN cur 8;
sql er r ( " OPEN cur 8" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 8 I NTO : pr oj ect i d;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " PROJ ECT_I D\ n" ) ;
pr i nt f ( " - - - - - - - - \ n" ) ;
}
depad( pr oj ect i d) ;
pr i nt f ( " %s\ n" , pr oj ect i d) ;
count ++;
}
EXEC SQL CLOSE cur 8;
sql er r ( " CLOSE cur 8" ) ;
pr i nt f ( " %d r ecor d( s) sel ect ed\ n" , count ) ;
get char ( ) ;
}
voi d quer y9( ) {
pr i nt f ( " di spl ayi ng quer y 9\ n" ) ;
get char ( ) ;
EXEC SQL DECLARE cur 9 CURSOR FOR
SELECT BUG_I D, BUG. PROJ ECT_I D, DESCRI PTI ON
FROM BUG, ( SELECT T. PROJ ECT_I D, T. BC
FROM ( SELECT PROJ ECT_I D, COUNT( *) AS BC
FROM BUG
GROUP BY PROJ ECT_I D) AS T,
( SELECT MAX( BC1) AS MAXBC
FROM ( SELECT COUNT( *) AS BC1
FROM BUG
GROUP BY PROJ ECT_I D) )
WHERE T. BC=MAXBC) AS S
WHERE BUG. PROJ ECT_I D=S. PROJ ECT_I D;
sql er r ( " DECLARE cur 9" ) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
65
EXEC SQL OPEN cur 9;
sql er r ( " OPEN cur 9" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 9 I NTO : bugi d,
: pr oj ect i d,
: descr i pt i on;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " BUG_I D PROJ ECT_I D DESCRI PTI ON\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( pr oj ect i d) ;
depad( descr i pt i on) ;
descr i pt i on[ 16] =' \ 0' ;
spr i nt f ( buf f er , " %d" , bugi d) ;
whi l e( st r l en( buf f er ) <10) st r cat ( buf f er , " " ) ;
st r cat ( buf f er , pr oj ect i d) ;
whi l e( st r l en( buf f er ) <20) st r cat ( buf f er , " " ) ;
st r cat ( buf f er , descr i pt i on) ;
pr i nt f ( " %s\ n" , buf f er ) ;
count ++;
}
EXEC SQL CLOSE cur 9;
sql er r ( " CLOSE cur 9" ) ;
pr i nt f ( " %d r ecor d( s) sel ect ed\ n" , count ) ;
get char ( ) ;
}
voi d quer y10( ) {
pr i nt f ( " di spl ayi ng quer y 10\ n" ) ;
get char ( ) ;
EXEC SQL DECLARE cur 10 CURSOR FOR
SELECT USER_I D
FROM ( SELECT USER_I D, COUNT( *) AS BPD
FROM ( SELECT BUG_I D, BUG. PROJ ECT_I D, USER_I D
FROM BUG, PROJ ECT, DEVELOPER
WHERE BUG. PROJ ECT_I D=PROJ ECT. PROJ ECT_I D
AND OWNED_BY=USER_I D)
GROUP BY USER_I D)
WHERE BPD I N ( SELECT MI N( BPD) AS MI NBPD
FROM ( SELECT USER_I D, COUNT( *) AS BPD
FROM ( SELECT BUG_I D, BUG. PROJ ECT_I D, USER_I D
FROM BUG, PROJ ECT, DEVELOPER
WHERE BUG. PROJ ECT_I D=PROJ ECT. PROJ ECT_I D
AND OWNED_BY=USER_I D)
GROUP BY USER_I D) ) ;
sql er r ( " DECLARE cur 10" ) ;
EXEC SQL OPEN cur 10;
sql er r ( " OPEN cur 10" ) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
66
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 10 I NTO : user i d;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " USER_I D\ n" ) ;
pr i nt f ( " - - - - - - - - \ n" ) ;
}
depad( user i d) ;
pr i nt f ( " %s\ n" , user i d) ;
count ++;
}
EXEC SQL CLOSE cur 10;
sql er r ( " CLOSE cur 10" ) ;
pr i nt f ( " %d r ecor d( s) sel ect ed\ n" , count ) ;
get char ( ) ;
}
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
67
68
}
i nt mai n( ) {
i nt i , c;
EXEC SQL CONNECT TO OURDB;
sql er r ( " CONNECT TO OURDB" ) ;
pr i nt f ( " Connect ed t o OURDB\ n" ) ;
connect ed = 1;
agai n:
pr i nt f ( " ent er t he number of t he quer y : " ) ; f f l ush( st dout ) ;
i = 0;
whi l e( ( c = get char ( ) ) ! = ' \ n' ) {
i f ( c < ' 0' | | c > ' 9' ) {
pr i nt f ( " i ncor r ect ent r y, r edo\ n" ) ;
got o agai n;
}
i = 10*i +c- ' 0' ;
i f ( i > 15) {
pr i nt f ( " i ncor r ect ent r y, r edo\ n" ) ;
got o agai n;
}
}/ / endwhi l e
i f ( i < 1 | | i > 15) {
pr i nt f ( " i ncor r ect ent r y, r edo\ n" ) ;
got o agai n;
}
pr i nt f ( " goi ng t o per f or mquer y %d\ n" , i ) ;
i f ( i == 1)
quer y1( ) ;
el se i f ( i == 2)
quer y2( ) ;
el se i f ( i == 3)
quer y3( ) ;
el se i f ( i == 4)
quer y4( ) ;
el se i f ( i == 5)
quer y5( ) ;
el se i f ( i == 6)
quer y6( ) ;
el se i f ( i == 7)
quer y7( ) ;
el se i f ( i == 8)
quer y8( ) ;
el se i f ( i == 9)
quer y9( ) ;
el se i f ( i == 10)
quer y10( ) ;
el se i f ( i == 11)
quer y11( ) ;
el se i f ( i == 12)
quer y12( ) ;
el se i f ( i == 13)
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
69
quer y13( ) ;
el se i f ( i == 14)
quer y14( ) ;
el se
quer y15( ) ;
EXEC SQL CONNECT RESET;
sql er r ( " CONNECT RESET" ) ;
pr i nt f ( " Di sconnect ed f r omOURDB\ n" ) ;
r et ur n 0;
}
voi d quer y1( ) {
EXEC SQL DECLARE cur 1 CURSOR FOR
SELECT CUSTOMERS. PI D, NAME
FROM CUSTOMERS, PERSONS
WHERE CUSTOMERS. PI D=PERSONS. PI D;
EXEC SQL OPEN cur 1;
sql er r ( " OPEN cur 1" ) ;
t ab1=7;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 1 I NTO : PI D,
: NAME;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f ( " PI D Name\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %d" , PI D) ;
whi l e( st r l en( l i ne) < t ab1) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , NAME) ;
pr i nt f ( " %s\ n" , l i ne) ;
}
EXEC SQL CLOSE cur 1;
sql er r ( " CLOSE cur 1" ) ;
}
voi d quer y2( ) {
EXEC SQL DECLARE cur 2 CURSOR FOR
SELECT CUSTOMERS. PI D, NAME, COUNTRY, CI TY, STREET, STRNO, POBox
FROM CUSTOMERS, PERSONS, ADDRESSES, HASA
WHERE CUSTOMERS. PI D=HASA. PI D
AND HASA. adI D=ADDRESSES. adI D
AND CUSTOMERS. PI D=PERSONS. PI D;
EXEC SQL OPEN cur 2;
sql er r ( " OPEN cur 2" ) ;
t ab1=7;
t ab2=18;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
70
t ab3=28;
t ab4=41;
t ab5=56;
t ab6=66;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 2 I NTO : PI D,
: NAME,
: COUNTRY,
: CI TY,
: STREET: STREET_I ND,
: STRNO: STRNO_I ND,
: POBox: POBox_I ND;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f (
" PI D NAME COUNTRY CI TY STREET STRNO POBox\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %d" , PI D) ;
whi l e( st r l en( l i ne) < t ab1) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , NAME) ;
whi l e( st r l en( l i ne) < t ab2) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , COUNTRY) ;
whi l e( st r l en( l i ne) < t ab3) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , CI TY) ;
whi l e( st r l en( l i ne) < t ab4) st r cat ( l i ne, " " ) ;
i f ( STREET_I ND==0)
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , STREET) ;
el se
spr i nt f ( l i ne+st r l en( l i ne) , " - - " ) ;
whi l e( st r l en( l i ne) < t ab5) st r cat ( l i ne, " " ) ;
i f ( STRNO_I ND==0)
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , STRNO) ;
el se
spr i nt f ( l i ne+st r l en( l i ne) , " - - " ) ;
whi l e( st r l en( l i ne) < t ab6) st r cat ( l i ne, " " ) ;
i f ( POBox_I ND==0)
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , POBox) ;
el se
spr i nt f ( l i ne+st r l en( l i ne) , " - - " ) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 2;
sql er r ( " CLOSE cur 2" ) ;
}
voi d quer y3( ) {
EXEC SQL DECLARE cur 3 CURSOR FOR
SELECT HASCO. PI D, NAME, DESI G, AMOUNT
FROM HASCO, PERSONS
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
71
72
i f ( f i r st ) {
pr i nt f (
" PI D NAME\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %d" , PI D) ;
whi l e( st r l en( l i ne) < t ab1) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , NAME) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 4;
sql er r ( " CLOSE cur 4" ) ;
}
voi d quer y5( ) {
EXEC SQL DECLARE cur 5 CURSOR FOR
SELECT DI STI NCT HASCO. PI D, NAME
FROM HASCO, I SLP, PERSONS
WHERE HASCO. DESI G=I SLP. DESI G
AND I SLP. adI D=100
AND HASCO. PI D=PERSONS. PI D;
EXEC SQL OPEN cur 5;
sql er r ( " OPEN cur 5" ) ;
t ab1=7;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 5 I NTO : PI D,
: NAME;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f (
" PI D NAME\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %d" , PI D) ;
whi l e( st r l en( l i ne) < t ab1) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , NAME) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 5;
sql er r ( " CLOSE cur 5" ) ;
}
voi d quer y6( ) {
EXEC SQL DECLARE cur 6 CURSOR FOR
SELECT DI STI NCT AMOUNT,
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
73
VALI D_DATE,
PRI CI NG,
FROMD,
TOD,
MI NP,
MAXP,
PENALTY,
DESI G
FROM I SDP
WHERE adI D=103;
EXEC SQL OPEN cur 6;
sql er r ( " OPEN cur 6" ) ;
t ab1=8;
t ab2=20;
t ab3=29;
t ab4=38;
t ab5=45;
t ab6=51;
t ab7=57;
t ab8=66;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 6 I NTO : AMOUNT,
: VALI D_DATE,
: PRI CI NG,
: FROMD,
: TOD,
: MI NP,
: MAXP,
: PENALTY,
: DESI G;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f (
" AMOUNT VALI D_DATE PRI CI NG FROMD TOD MI NP MAXP PENALTY DESI G\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %f " , AMOUNT) ;
whi l e( st r l en( l i ne) < t ab1) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , VALI D_DATE) ;
whi l e( st r l en( l i ne) < t ab2) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , PRI CI NG) ;
whi l e( st r l en( l i ne) < t ab3) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , FROMD) ;
whi l e( st r l en( l i ne) < t ab4) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , TOD) ;
whi l e( st r l en( l i ne) < t ab5) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %u" , MI NP) ;
whi l e( st r l en( l i ne) < t ab6) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %u" , MAXP) ;
whi l e( st r l en( l i ne) < t ab7) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %f " , PENALTY) ;
whi l e( st r l en( l i ne) < t ab8) st r cat ( l i ne, " " ) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
74
75
f i r st =0;
}
spr i nt f ( l i ne, " %s" , DESI G) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 8;
sql er r ( " CLOSE cur 8" ) ;
}
voi d quer y9( ) {
EXEC SQL DECLARE cur 9 CURSOR FOR
SELECT DI STI NCT EMPLOYEES. PI D, NAME
FROM EMPLOYEES, GUI DES, CUSTOMERS, PERSONS
WHERE ( EMPLOYEES. PI D=GUI DES. PI D
OR EMPLOYEES. PI D=CUSTOMERS. PI D)
AND EMPLOYEES. PI D=PERSONS. PI D;
EXEC SQL OPEN cur 9;
sql er r ( " OPEN cur 9" ) ;
t ab1=7;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 9 I NTO : PI D,
: NAME;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f (
" PI D NAME\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %d" , PI D) ;
whi l e( st r l en( l i ne) < t ab1) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , NAME) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 9;
sql er r ( " CLOSE cur 9" ) ;
}
voi d quer y10( ) {
EXEC SQL DECLARE cur 10 CURSOR FOR
SELECT DI STI NCT PARTI CI P. PI D, NAME
FROM PARTI CI P, I TI NERARI ES, PERSONS
WHERE PARTI CI P. DESI G=I TI NERARI ES. DESI G
AND I TI NERARI ES. DATE >= ' 12/ 18/ 2003'
AND PARTI CI P. PI D=PERSONS. PI D
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
76
77
78
spr i nt f ( l i ne, " %f " , AMOUNT) ;
whi l e( st r l en( l i ne) < t ab1) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , VALI D_DATE) ;
whi l e( st r l en( l i ne) < t ab2) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , PRI CI NG) ;
whi l e( st r l en( l i ne) < t ab3) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , FROMD) ;
whi l e( st r l en( l i ne) < t ab4) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , TOD) ;
whi l e( st r l en( l i ne) < t ab5) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %u" , MI NP) ;
whi l e( st r l en( l i ne) < t ab6) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %u" , MAXP) ;
whi l e( st r l en( l i ne) < t ab7) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %f " , PENALTY) ;
whi l e( st r l en( l i ne) < t ab8) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , DESI G) ;
pr i nt f ( " %s\ n" , l i ne) ;
}/ * endwhi l e */
EXEC SQL CLOSE cur 11;
sql er r ( " CLOSE cur 11" ) ;
}
voi d quer y12( ) {
EXEC SQL DECLARE cur 12 CURSOR FOR
SELECT DI STI NCT AMOUNT,
VALI D_DATE,
PRI CI NG,
FROMD,
TOD,
MI NP,
MAXP,
PENALTY,
DESI G
FROM I SBP, PLACES
WHERE I SBP. adI D=PLACES. adI D
AND PLACES. DESI GN=' pr ov1'
UNI ON
SELECT DI STI NCT AMOUNT,
VALI D_DATE,
PRI CI NG,
FROMD,
TOD,
MI NP,
MAXP,
PENALTY,
DESI G
FROM I SLP, PLACES
WHERE I SLP. adI D=PLACES. adI D
AND PLACES. DESI GN=' pr ov1'
UNI ON
SELECT DI STI NCT AMOUNT,
VALI D_DATE,
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
79
PRI CI NG,
FROMD,
TOD,
MI NP,
MAXP,
PENALTY,
DESI G
FROM I SDP, PLACES
WHERE I SDP. adI D=PLACES. adI D
AND PLACES. DESI GN=' pr ov1'
UNI ON
SELECT DI STI NCT AMOUNT,
VALI D_DATE,
PRI CI NG,
FROMD,
TOD,
MI NP,
MAXP,
PENALTY,
DESI G
FROM I SSP, PLACES
WHERE I SSP. adI D=PLACES. adI D
AND PLACES. DESI GN=' pr ov1' ;
EXEC SQL OPEN cur 12;
sql er r ( " OPEN cur 12" ) ;
t ab1=8;
t ab2=20;
t ab3=29;
t ab4=38;
t ab5=45;
t ab6=51;
t ab7=57;
t ab8=66;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 12 I NTO : AMOUNT,
: VALI D_DATE,
: PRI CI NG,
: FROMD,
: TOD,
: MI NP,
: MAXP,
: PENALTY,
: DESI G;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f (
" AMOUNT VALI D_DATE PRI CI NG FROMD TOD MI NP MAXP PENALTY DESI G\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %f " , AMOUNT) ;
whi l e( st r l en( l i ne) < t ab1) st r cat ( l i ne, " " ) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
80
81
MAXP,
PENALTY,
DESI G
FROM I SDP
WHERE I SDP. DESI G=' t our 1'
UNI ON
SELECT DI STI NCT AMOUNT,
VALI D_DATE,
PRI CI NG,
FROMD,
TOD,
MI NP,
MAXP,
PENALTY,
DESI G
FROM I SSP
WHERE I SSP. DESI G=' t our 1' ;
EXEC SQL OPEN cur 13;
sql er r ( " OPEN cur 13" ) ;
t ab1=8;
t ab2=20;
t ab3=29;
t ab4=38;
t ab5=45;
t ab6=51;
t ab7=57;
t ab8=66;
f i r st =1;
whi l e( 1) {
EXEC SQL FETCH cur 13 I NTO : AMOUNT,
: VALI D_DATE,
: PRI CI NG,
: FROMD,
: TOD,
: MI NP,
: MAXP,
: PENALTY,
: DESI G;
i f ( SQLCODE! =0) br eak;
i f ( f i r st ) {
pr i nt f (
" AMOUNT VALI D_DATE PRI CI NG FROMD TOD MI NP MAXP PENALTY DESI G\ n" ) ;
f i r st =0;
}
spr i nt f ( l i ne, " %f " , AMOUNT) ;
whi l e( st r l en( l i ne) < t ab1) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , VALI D_DATE) ;
whi l e( st r l en( l i ne) < t ab2) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , PRI CI NG) ;
whi l e( st r l en( l i ne) < t ab3) st r cat ( l i ne, " " ) ;
spr i nt f ( l i ne+st r l en( l i ne) , " %s" , FROMD) ;
whi l e( st r l en( l i ne) < t ab4) st r cat ( l i ne, " " ) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
82
83
84
85
voi d di sconnect ( ) {
EXEC SQL CONNECT RESET;
sql er r ( " CONNECT RESET" ) ;
pr i nt f ( " Di sconnect ed f r omOURDB\ n" ) ;
connect ed = 0;
}
voi d depad( char * s) {
i nt i ;
i =st r l en( s) - 1;
whi l e( s[ i ] ==' ' ) s[ i - - ] =' \ 0' ;
}
char buf f er [ 300] ;
i nt mai n( ) {
i nt i , j ;
connect ( ) ;
whi l e( 1) {
A: pr i nt f ( " ent er number of quer y you want t o execut e ( 1- 8) , 0 t o qui t \ n" ) ;
f get s( buf f er , 300, st di n) ;
i f ( buf f er [ 0] ==' \ n' ) {
pr i nt f ( " not hi ng ent er ed\ n" ) ;
cont i nue;
}
i =j =0;
whi l e( buf f er [ j ] ! =' \ n' ) {
i f ( buf f er [ i ] >=' 0' && buf f er [ i ] <=' 9' )
i = 10*i +buf f er [ i ] - ' 0' ;
el se{
pr i nt f ( " i ncor r ect ent r y\ n" ) ;
got o A;
}
j ++;
}
i f ( i > 8) {
pr i nt f ( " i ncor r ect ent r y\ n" ) ;
got o A;
}
i f ( i == 0) br eak;
el se i f ( i == 1) quer y1( ) ;
el se i f ( i == 2) quer y2( ) ;
el se i f ( i == 3) quer y3( ) ;
el se i f ( i == 4) quer y4( ) ;
el se i f ( i == 5) quer y5( ) ;
el se i f ( i == 6) quer y6( ) ;
el se i f ( i == 7) quer y7( ) ;
el se i f ( i == 8) quer y8( ) ;
el se pr i nt f ( " i ncor r ect ent r y\ n" ) ;
}
di sconnect ( ) ;
r et ur n 0;
}
i nt count , l en, l en1;
char buf 1[ 80] ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
86
EXEC SQL BEGI N DECLARE SECTI ON;
char empl oyeeno[ 7] ;
char l ast [ 21] ;
char mi ddl e[ 11] ;
char f i r st [ 11] ;
shor t mi ddl e_i nd;
char ar ea_code[ 4] ;
char number [ 7] ;
char par t no[ 6] ;
sql i nt 32 or i g_quant i t y;
sql i nt 32 r emai ni ng_quant i t y;
char bo_dat e[ 11] ;
char backor der ed_by[ 7] ;
char f d[ 11] ;
char war ehousei d[ 5] ;
sql i nt 32 bi nno;
sql i nt 32 r emai ni ng_capaci t y;
char manager no[ 7] ;
sql i nt 32 number _managed;
EXEC SQL END DECLARE SECTI ON;
voi d quer y1( ) {
pr i nt f ( " di spl ayi ng quer y 1\ n" ) ;
EXEC SQL DECLARE cur 1 CURSOR FOR
SELECT WORKER. EMPLOYEENO
FROM WORKER,
( SELECT MANAGER. EMPLOYEENO
FROM MANAGER, HASNAME
WHERE MANAGER. EMPLOYEENO=HASNAME. EMPLOYEENO
AND HASNAME. FI RST=' TONY7'
AND HASNAME. MI DDLE I S NULL
AND HASNAME. LAST=' TONA7' ) AS T
WHERE WORKER. MANAGERNO=T. EMPLOYEENO;
sql er r ( " DECLARE cur 1" ) ;
EXEC SQL OPEN cur 1;
sql er r ( " OPEN cur 1" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 1 I NTO : empl oyeeno;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " EMPLOYEENO\ n" ) ;
pr i nt f ( " - - - - - - - - - - \ n" ) ;
}
depad( empl oyeeno) ;
pr i nt f ( " %s\ n" , empl oyeeno) ;
count ++;
}
EXEC SQL CLOSE cur 1;
sql er r ( " CLOSE cur 1" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
87
88
EXEC SQL OPEN cur 3;
sql er r ( " OPEN cur 3" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 3 I NTO : empl oyeeno,
: ar ea_code,
: number ;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " EMPLOYEENO AREA_CODE NUMBER\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( empl oyeeno) ;
spr i nt f ( buf f er , " %s" , empl oyeeno) ;
f or ( l en=st r l en( buf f er ) ; l en < 12; l en++) st r cat ( buf f er , " " ) ;
depad( ar ea_code) ;
spr i nt f ( buf f er +l en, " %s" , ar ea_code) ;
f or ( l en=st r l en( buf f er ) ; l en < 22; l en++) st r cat ( buf f er , " " ) ;
depad( number ) ;
spr i nt f ( buf f er +l en, " %s" , number ) ;
pr i nt f ( " %s\ n" , buf f er ) ;
count ++;
}
EXEC SQL CLOSE cur 3;
sql er r ( " CLOSE cur 3" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y4( ) {
pr i nt f ( " di spl ayi ng quer y 4\ n" ) ;
EXEC SQL DECLARE cur 4 CURSOR FOR
SELECT DI STI NCT ASSEMBLYNO
FROM SUBPART
ORDER BY ASSEMBLYNO;
sql er r ( " DECLARE cur 4" ) ;
EXEC SQL OPEN cur 4;
sql er r ( " OPEN cur 4" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 4 I NTO : par t no;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " PARTNO\ n" ) ;
pr i nt f ( " - - - - - - \ n" ) ;
}
depad( par t no) ;
pr i nt f ( " %s\ n" , par t no) ;
count ++;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
89
}
EXEC SQL CLOSE cur 4;
sql er r ( " CLOSE cur 4" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}
voi d quer y5( ) {
pr i nt f ( " di spl ayi ng quer y 5\ n" ) ;
EXEC SQL DECLARE cur 5 CURSOR FOR
SELECT MANAGER. EMPLOYEENO, PARTNO, ORI G_QUANTI TY, REMAI NI NG_QUANTI TY,
BO_DATE, BACKORDERED_BY
FROM MANAGER, CURRENT_BACKORDER
WHERE MANAGER. EMPLOYEENO=CURRENT_BACKORDER. BACKORDERED_BY;
sql er r ( " DECLARE cur 5" ) ;
EXEC SQL OPEN cur 5;
sql er r ( " OPEN cur 5" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 5 I NTO : empl oyeeno,
: par t no,
: or i g_quant i t y,
: r emai ni ng_quant i t y,
: bo_dat e,
: backor der ed_by;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " EMPLOYEENO PARTNO ORI G_QUANTI TY REMAI NI NG_QUANTI TY BO_DATE
BACKORDERED_BY\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - \ n" ) ;
}
depad( empl oyeeno) ;
spr i nt f ( buf f er , " %s" , empl oyeeno) ;
f or ( l en=st r l en( buf f er ) ; l en < 11; l en++) st r cat ( buf f er , " " ) ;
depad( par t no) ;
spr i nt f ( buf f er +l en, " %s" , par t no) ;
f or ( l en=st r l en( buf f er ) ; l en < 18; l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf 1, " %d" , or i g_quant i t y) ;
f or ( l en1=st r l en( buf 1) ; l en1 < 13; l en1++, l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf f er +l en, " %s" , buf 1) ;
f or ( l en=st r l en( buf f er ) ; l en < 32; l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf 1, " %d" , r emai ni ng_quant i t y) ;
f or ( l en1=st r l en( buf 1) ; l en1 < 18; l en1++, l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf f er +l en, " %s" , buf 1) ;
f or ( l en=st r l en( buf f er ) ; l en < 51; l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf f er +l en, " %s" , bo_dat e) ;
f or ( l en=st r l en( buf f er ) ; l en < 62; l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf f er +l en, " %s" , backor der ed_by) ;
Appendix to Master Thesis Weiguang Zhang McMaster University- Computing and Software
90
91
92
FROM WORKER
GROUP BY MANAGERNO) AS T1
EXCEPT
SELECT T1. MANAGERNO, T1. NUMBER_MANAGED
FROM ( SELECT MANAGERNO, COUNT( *) AS NUMBER_MANAGED
FROM WORKER
GROUP BY MANAGERNO) AS T1,
( SELECT MANAGERNO, COUNT( *) AS NUMBER_MANAGED
FROM WORKER
GROUP BY MANAGERNO) AS T2
WHERE T1. NUMBER_MANAGED > T2. NUMBER_MANAGED;
sql er r ( " DECLARE cur 8" ) ;
EXEC SQL OPEN cur 8;
sql er r ( " OPEN cur 8" ) ;
count =0;
whi l e( 1) {
EXEC SQL FETCH cur 8 I NTO : manager no,
: number _managed;
i f ( SQLCODE! =0) br eak;
i f ( count ==0) {
pr i nt f ( " MANAGERNO NUMBER_MANAGED\ n" ) ;
pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ;
}
depad( manager no) ;
spr i nt f ( buf f er , " %s" , manager no) ;
f or ( l en=st r l en( buf f er ) ; l en < 10; l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf 1, " %d" , number _managed) ;
f or ( l en1=st r l en( buf 1) ; l en1 < 14; l en1++, l en++) st r cat ( buf f er , " " ) ;
spr i nt f ( buf f er +l en, " %s" , buf 1) ;
pr i nt f ( " %s\ n" , buf f er ) ;
count ++;
}
EXEC SQL CLOSE cur 8;
sql er r ( " CLOSE cur 8" ) ;
pr i nt f ( " \ n %d r ecor d( s) sel ect ed. \ n" , count ) ;
}