Database FundamentalsLecture 4
Useful website for MySQL download
http://www.net-language.com/workshops/Default.asp?workshop=21
The Design Process
Good decisions require good information derived from raw
facts known as data.
Importance of good database design
• Well designed database facilitates data management and becomes a good information generator.
• It will grow well.• It is easy to use.
NoteNo
Packer Comp Comp Add
ItemNo
Qty PartNo Descrip
300 JW Bloggs York 1 200 1234 Nuts
300 JW Bloggs York 2 200 2234 Bolts
300 JW Bloggs York 3 200 3334 Nails
What is wrong with this organisation’s database?
Info about Packer, Comp and CompAdd has been repeated in more than one row
If Bloggs was the only company to have purchased Nuts and then decided they did not want them so were deleted from packing note, contents of ParNo and Descrip fields would be lost
PartNo and Descrip fields cannot be established until a packing note has been raised.
Update anomaly
Delete anomaly
Insert anomaly
Architect
Plan
Builder
House
Database Designer
Database Design
Database Builder
Database System
The Database Design Process
1 Define the current process.
2 Define the components of the organisation.
3 Define rules (how organisation is run).
4 Model the database.
5 Define the relationships.
6 Review.
7 Create the database.
4, 5, 6 & 7 are pertinent to this module.
Entities
Things it may be required to keepinformation about.Can be: People (e.g. Customers)
Objects (e.g. Products)happening (e.g.ad hits)
address14 Grand Ave59 Cucumber Dr928 Shingles Rd2572 Family Ave
phone01484-75293401274-84247201924-82417301246-474738
Company_num13141723
Company_nameBig Deal LtdPickles IncReal Roofing CoGigafred & Son
company
Company_num
142317132323231323141317
ad_num
484952556263647799
101102119
Hit_fee
0.010.020.010.030.020.010.020.030.030.010.010.02
product
dateJuly 13July 13July 14July 14July 14July 14July 14July 14July 14July 14July 14July 14July 14July 14July 15July 15July 15July 15
ad_num49554863
10162
119102524864
11948
10163487799
ad hits
Is it an entity?
Rules:1. An entity must be important to the
organisation.2. An entity must have at least one
attribute.3. An entity must occur more than once
(there must be more than one customer)4. Each entity occurrence (record) must be
uniquely identifiable (customer id)
Entities are represented like this and they are always singular.
customer
Bits of data associated with the entity are attributes:
Customer number
Customer name
Customer address
Contact name
Telephone
Way of identifying occurrence (record) - the primary key.
Relationships
customer invoice
product
Three types of Relationhip
• one-to-one (1:1)• one-to-many (1:M)• many-to-many (M:N)
car driver
car driver
car driver
car driver
1:1 A car can only have one driver; a driver can have only one car.
1:M A car can have more than one driver; a driver can only one car.
1:M A car can have only one driver; a driver can have more than one car.
M:N A car can have more than one driver; a driver can have more than one car.
A typical company
Managing
DirectorCompan
y
Sales Staff Customer
employs
manages
take orders
Relationship name
Sales Staff Customertake
orders
Sales Staff Customer
Order
accepts places
This many-to-many relationship does not enable mapping between a particular order and a particular member of the sales staff
The entity Order uniquely defines an instance involving both Sales Staff and Customer. From a single many-to-many relationship two one-to-many relationships have been created.
Sales Staff
Staffid
Name
DOB
Employ start date
Order
Orderid
Staffid*
Customerid*
Order details
Customer
Customerid
Name
Address
Contact name
Telephone
Foreign keys
A model is:
• Simplified abstractions of real world events or conditions.
• A database model is a collection of logical constructs used to represent the data structure and the data relationships within the database
• 2 categories of database models:– Conceptual models (what is presented)– Implementation models (how it is
represented)
Entity (E-R) Relationship Model
(top down approach)• A conceptual model and like other
conceptual models it uses three types of relationship:
• one-to-many (1:N)• one-to-one (1:1)• many-to-many (M:N) Multiplicity is a term which can be used todescribe this.
• ENTITY - is a person place or thing for which data are to be collected or stored e.g.student, book, author, stock
• ATTRIBUTE - characteristics of an entity e.g. student entity may include the attributes student number, date of entry, pathway, dob, home address, phone number etc.
• RELATIONSHIP - an association between entities e.g.
Week 4 practical
In week 4 you will be working with the 3 tables:
• book• author• stock
And you will make queries such as:
USE ;DROP TABLE IF EXISTS book;CREATE TABLE book(
bookid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL,author VARCHAR(20) NULL,topic VARCHAR(20) NOT NULL,pages INT UNSIGNED NULL,firstPubDate YEAR NULL,publisher VARCHAR(40) NOT NULL,price FLOAT(4,2)
);
DROP TABLE IF EXISTS stock;CREATE TABLE stock(
bookid INT UNSIGNED NOT NULL,authorid INT UNSIGNED NOT NULL,reorder INT NOT NULL,instock INT NOT NULL
); DROP TABLE IF EXISTS author;CREATE TABLE author(
authorid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
lastname VARCHAR(20) NOT NULL,firstname VARCHAR(20) NOT NULL
);
SELECT book.title, author.lastname, author.firstname, stock.instock
FROM book, author, stock WHERE stock.instock <5
AND stock.bookid=book.bookid AND stock.authorid=author.authorid ;
1) SELECT title FROM book \c ; 2) SELECT book.title, author.authorid FROM book, author \c ; 3) SELECT book.title, author.authorid, stock.instock FROM book, author, stock WHERE author.lastname='GLANCEY' AND author.authorid=stock.authorid \c ; 4) SELECT book.title, author.authorid, stock.instock FROM book, author, stock WHERE author.lastname='GLANCEY' AND author.authorid=stock.authorid AND stock.bookid=book.bookid \c ; 5) SELECT book.title, author.lastname, author.firstname, stock.instock FROM book, author, stock WHERE stock.instock <5 AND stock.bookid=book.bookid AND stock.authorid=author.authorid \c ;
Top Related