Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Fundamentals Lecture 4 Useful website for MySQL download language.com/workshops/Default.asp ?workshop=21.

Similar presentations


Presentation on theme: "Database Fundamentals Lecture 4 Useful website for MySQL download language.com/workshops/Default.asp ?workshop=21."— Presentation transcript:

1 Database Fundamentals Lecture 4 Useful website for MySQL download http://www.net- language.com/workshops/Default.asp ?workshop=21

2 The Design Process Good decisions require good information derived from raw facts known as data.

3 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.

4 NoteNoPackerCompComp Add ItemNoQtyPartNoDescrip 300JWBloggsYork12001234Nuts 300JWBloggsYork22002234Bolts 300JWBloggsYork32003334Nails 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

5 Architect Plan Builder House Database Designer Database Design Database Builder Database System

6 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.

7 Entities Things it may be required to keep information about. Can be: People (e.g. Customers) Objects (e.g. Products) happening (e.g.ad hits)

8 address 14 Grand Ave 59 Cucumber Dr 928 Shingles Rd 2572 Family Ave phone 01484-752934 01274-842472 01924-824173 01246-474738 Company_num 13 14 17 23 Company_name Big Deal Ltd Pickles Inc Real Roofing Co Gigafred & Son company Company_num 14 23 17 13 23 13 23 14 13 17 ad_num 48 49 52 55 62 63 64 77 99 101 102 119 Hit_fee 0.01 0.02 0.01 0.03 0.02 0.01 0.02 0.03 0.01 0.02 product date July 13 July 14 July 15 ad_num 49 55 48 63 101 62 119 102 52 48 64 119 48 101 63 48 77 99 ad hits

9 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)

10 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.

11 Relationships customer invoice product

12 Three types of Relationhip one-to-one (1:1) one-to-many (1:M) many-to-many (M:N)

13 cardriver cardriver cardriver cardriver 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.

14 A typical company Managing Director Company Sales Staff Customer employs manages take orders Relationship name

15 Sales Staff Customer take orders Sales StaffCustomer Order acceptsplaces 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.

16 Sales Staff Staffid Name DOB Employ start date Order Orderid Staffid* Customerid* Order details Customer Customerid Name Address Contact name Telephone Foreign keys

17 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)

18 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 to describe this.

19 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.

20 Week 4 practical In week 4 you will be working with the 3 tables: book author stock And you will make queries such as:

21 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) );

22 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 );

23 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 ;

24 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 ;


Download ppt "Database Fundamentals Lecture 4 Useful website for MySQL download language.com/workshops/Default.asp ?workshop=21."

Similar presentations


Ads by Google