Upload
sarah-peggs
View
216
Download
0
Embed Size (px)
Citation preview
By Helen Spiropoulos
This database models the UTS Library Website: www.lib.uts.edu.au
It allows users to search for resources (book, journal, dvd, cd etc) by subject, author, title, category and publisher.
Staff can check if students have loans overdue and also when student cards expire. They can also access the sales contact of publishers if an item needs replacing.
The attributes in each table allow for an extensive range of queries.
LIB_Category
catIDcatDesc
LIB_Resource
ISBNTitleEditionPubID*YearPubMediaCopies
LIB_Subject
SubNoSubNameFalculty
LIB_ResSubject
SubNo*ISBN*
LIB_Author
AuIDAuNameBirthDeathGender
LIB_ResCategory
catID*ISBN*
LIB_ResAuthor
AuID*ISBN*AuNo
LIB_Publisher
PubIDPubNameSalesContact
LIB_Student
StuIDStuNameEmailExpirary
LIB_Copy
ISBN*CopyNo
LIB_Loan
(ISBN*CopyNo*)StuID*DateLoanDateDue
One to Many Relationship
pubID PubName SalesContact
1 Wiley
2 Prentice Hall
3 Pearson Education
4 McGraw-Hill
5 East Sussex
6 Everbest [email protected]
ISBN Title Edition
PubID Year Media
Copies
0131451413
Database concepts 2 3 2005 book 1
1256748844 Managing a Corporate Enterprise 3 3 2005 dvd 1
• One publisher can publish many resources
Primary Key
Foreign Key
SubNoSubNameFaculty
LIB_Subject
SubNo*ISBN*
LIB_ResSubject
ISBNTitleEdPubIDYearPubMediaCopies
LIB_Resource
Many to Many Relationship
SubNo ISBN
31061 0471347116
31061 0131451413
31061 0072880678
21121 0471180327
31474 0072880678
subNo
subName Falculty
31061 Database Principles Information Technology
21121 Managing electronic business processes Information Technology
ISBN Title Ed pubID
Year
Media
Copies
0131451413
Database concepts 2 3 2005
book 1
0471347116
Data management : databases and organizations 4 1 2003
book 2
0072880678
Database design, application development, and administration
2 4 2004
book 4
One resource can be for many subjectsOne subject can have many resources
Query on a single entity/table
select * from LIB_author where augender = 'M';
auID auName auBirth
auDeath
augender
1 Kroenke, David M. M
2 Watson, Richard T M
3 Mannino, Micheal V. M
4 McKeown, Patrick G. 1943 M
5 Hartas, Leo M
6 Prentice, Steve M
select isbn, media from LIB_resource where media <> 'book';
ISBN Media
1256748844 dvd
1246450099M magazine
Select all resources other than books
Select all male authors
Query using Natural Join
select subname, title, copies from lib_resource
natural join lib_ressubject natural join lib_subject
Where falculty = ‘Information Technology’;
subName Title Copies
Database Principles Database concepts 1
Database Principles Data management : databases and organizations 2
Database Fundamentals Database design, application development, and administration 4
Database Principles Database design, application development, and administration 4
Managing Electronic Business Processes
Metamorphosis : a guide to the World Wide Web & electronic commerce
1
List subject, resource and copies of all resources currently usedby the faculty of Information Technology
select subname, title, copies from lib_resource, lib_subject, lib_ressubject
where lib_resource.isbn = lib_ressubject.isbn and
lib_subject.subno = lib_ressubject.subno
and faculty = 'Information Technology';
subName Title Copies
Database Principles Database concepts 1
Database Principles Data management : databases and organizations 2
Database Fundamentals Database design, application development, and administration 4
Database Principles Database design, application development, and administration 4
Managing Electronic Business Processes
Metamorphosis : a guide to the World Wide Web & electronic commerce
1
Query using Cross Product
Group By using Having
show the authors who have authored more than 1 resource
select auname, count(*) from lib_author, lib_resauthor, lib_resource
where lib_resource.isbn = lib_resauthor.isbn
and lib_resauthor.auID = lib_author.auID
group by auname having count(*) > 1;
auName Count
Jones, Katee K. 2
Watson, Richard T. 2
Query using a Sub Query
select auname, aubirth from lib_author where aubirth <= all
(select aubirth from lib_author
where audeath is null and aubirth is not null);
auName auBirth
McKeown, Patrick G. 1943
show the ‘known’ oldest author who is still alive
Self Join
Select all authors who authored two books (assuming no author hasAuthored more than two books) and the ISBN of each book.
select au.auname, resau1.isbn as book1, resau2.isbn as book2
from lib_resauthor resau1, lib_resauthor resau2, lib_author au
where resau1.auid = au.au and resau2.auid = au.auid
and resau1.isbn < resau2.isbn;
auName Book1 Book2
Watson, Richard T. 0471180327 0471347116
Jones, Katee K. 1246450099M 1256748844
Data Integrity – CHECK Constraints
Create table LIB_Author (
values go here….
auBirth CHAR(4),
auDeath CHAR(4),
auGender TEXT,
CONSTRAINT LIB_Author_AuBirth CHECK ((AuBirth < AuDeath) AND
(AuBirth >= 0000) AND (AuBirth <= 9999)),
CONSTRAINT LIB_Author_AuDeath CHEck (AuDeath > AuBirth),
CONSTRAINT LIB_Author_AuGender CHECK (AuGender IN ('M','F'))
);
SQL Syntax for Actions
Create table LIB_ResCategory(catID INTEGER,ISBN TEXT,
CONSTRAINT LIB_ResCategoryPk PRIMARY KEY (CatID,ISBN),
CONSTRAINT LIB_ResCategoryFk_INVALID_ISBN FOREIGN KEY (ISBN) REFERENCES LIB_Resource (ISBN)
ON DELETE CASCADEON UPDATE CASCADE,
CONSTRAINT LIB_ResCategoryFk_INVALID_CatID FOREIGN KEY (CatID) REFERENCES LIB_Category (CatID)
ON DELETE RESTRICTON UPDATE CASCADE
);
This will delete/update theentry of the isbn inLib_rescategory if theresource is deleted/updated from Lib_resource
This will restrict the deletion of a category ifthere are still resources in the category. Itwill also update any changes in catID fromLIB_category to the matching catID in LIB_resCategory.
Creating a View
create view booksonloan (isbn, title, copyno, daysout, due) as select
lib_loan.isbn, title, lib_loan.copyno, '07 June, 2007' - dateloan, datedue
from lib_resource, lib_copy, lib_loan where
lib_resource.isbn = lib_copy.isbn and lib_copy.copyno = lib_loan.copyno
and lib_copy.isbn = lib_loan.isbn;
ISBN Title copyNo DaysOut Due
0471347116 Data management : databases and organizations 1 13 2007-06-25
0471347116 Data management : databases and organizations 2 10 2007-06-28
0131451413 Database concepts 1 33 2007-06-05
1246450099M
Greece and why it rocks 1 13 2007-06-25