Databases
Learning Objectives: By the end of this topic you should be able to: describe the terms used in a relational database identify primary keys and foreign keys define relationships between entities using an entity relationship diagram for a given scenario Define
What is a database? What information would you record about a customer of a supermarket? What information would you record about a book in a library?
What is a database? a collection of data organised into a structured format.
What is a database? Surname: James First name: Samantha Date of Birth: 20.12.89 Address: 16 Poppy Close Town: Southam County: Warwickshire a collection of data organised into a structured format.
What is a database? Surname: James First name: Samantha Date of Birth: 20.12.89 Address: 16 Poppy Close Town: Southam County: Warwickshire Surname: Brown First name: David Date of Birth: 20.12.89 Address: 16 Poppy Close Town: Southam County: Warwickshire a collection of data organised into a structured format.
What is a database? Surname: James First name: Samantha Date of Birth: 20.12.89 Address: 16 Poppy Close Town: Southam County: Warwickshire Surname: Brown First name: David Date of Birth: 20.12.89 Address: 16 Poppy Close Town: Southam County: Warwickshire Surname: Lastler First name: David Date of Birth: 20.12.89 Address: 16 Poppy Close Town: Southam County: Warwickshire a collection of data organised into a structured format.
What is a database? Surname: James First name: Samantha Date of Birth: 20.12.89 Address: 16 Poppy Close Town: Southam County: Warwickshire Surname: Brown First name: David Date of Birth: 20.12.89 Address: 16 Poppy Close Town: Southam County: Warwickshire Surname: Lastler First name: David Date of Birth: 20.12.89 Address: 16 Poppy Close Town: Southam County: Warwickshire Surname: Whitefield First name: Nina Date of Birth: 21.02.76 Address: 16 Poppy Close Town: Southam County: Warwickshire a collection of data organised into a structured format.
Tables
Tables aka: an entity a collection of data about a certain ‘thing’ a collection of records in a relational database tables contain: rows (each row contains a record) columns (each column is a field) a relational database is made up of a number of tables a library database will have tables for Books, Members, …. the name of a table is usually capitalised: CUSTOMER(CustomerID, Forename, Surname,……, …..) Relationships
Records a record is a row in a database table, contains all the data about a single item or event a customer file record will store the data about a customer records are made up of a number of fields each record contains individual items of data (in fields)
Fields aka: an attribute an individual data item within a record a column in a database record, each record will be made up of a series of fields e.g. name, address etc. BOOK entity of a library will usually contain the attributes: ISBN, Title, AuthorForename, AuthorSurname, Publisher, …..
Primary Key every record in a table needs to be able to be identified primary key is the field that identifies a record uniquely e.g. account number, part number, membership number hint: often has ID, Number, No, or Code at the end of the field name often underlined e.g. MEMBER(MemberID, Forename, Surname, TelNo, ….) used to create a relationship between tables used to prevent data duplication Foreign Key
Standard Notation e.g. for a library database: MEMBER(MemberID, Forename, Surname, ….) BOOK(BookID, Title, Author ….) LOANS(LoanNo, MemberID, BookID, DateOut, …) Entities are written in capital letters Attributes are written with an initial capital Primary keys are underlined or labelled PK Foreign keys are overlined for OCR board or labelled FK
Databases A database consists of a number of t*****.
Databases A database consists of a number of tables.
Databases A database consists of a number of tables. A table consists of a number of r******.
Databases A database consists of a number of tables. A table consists of a number of records.
Databases A database consists of a number of tables. A table consists of a number of records. A record consists of a number of f*****.
Databases A database consists of a number of tables. A table consists of a number of records. A record consists of a number of fields.
Databases A database consists of a number of tables A table consists of a number of records A record consists of a number of fields The most important field is the p****** k**.
Databases A database consists of a number of tables A table consists of a number of records A record consists of a number of fields The most important field is the primary key.
Databases A database consists of a number of tables A table consists of a number of records A record consists of a number of fields The most important field is the primary key A primary key contains data u***** to a record.
Databases A database consists of a number of tables A table consists of a number of records A record consists of a number of fields The most important field is the primary key A primary key contains data unique to a record. Foreign Key
Library Database Members of a library are loaned books
Library Database Members of a library are loaned books Entities:
Library Database Members of a library are loaned books Entities:
Library Database Members of a library are loaned books Entities:
Library Database Members of a library are loaned books Entities: LOANS Relationships
Library Database Members of a library are loaned books Entities: MEMBERS(MembersID, Forename, Surname, …...) BOOKS LOANS Relationships
Library Database Members of a library are loaned books Entities: MEMBERS(MembersID, Forename, Surname, …...) BOOKS(BookID, ISBN, Title, Author, …..) LOANS Relationships
Library Database Members of a library are loaned books Entities: MEMBERS(MembersID, Forename, Surname, …...) BOOKS(BookID, ISBN, Title, Author, …..) LOANS(LoanID, Relationships
Library Database Members of a library are loaned books Entities: MEMBERS(MembersID, Forename, Surname, …...) BOOKS(BookID, ISBN, Title, Author, …..) LOANS(LoanID, MembersID, Relationships
Library Database Members of a library are loaned books Entities: MEMBERS(MembersID, Forename, Surname, …...) BOOKS(BookID, ISBN, Title, Author, …..) LOANS(LoanID, MembersID, BookID, Relationships
Library Database Members of a library are loaned books Entities: MEMBERS(MembersID, Forename, Surname, …...) BOOKS(BookID, ISBN, Title, Author, …..) LOANS(LoanID, MembersID, BookID, LoanDate, …) Relationships
Foreign Key used to link the tables in a relational database. a field in one table that is also the primary key in another often overlined (or labelled FK): MEMBERS(MembersID, Forename, Surname, …...) BOOK(BookID, ISBN, Title, Author, …..) LOANS(LoanID, MembersID, BookID, LoanDate, ….)
Entity Relationship Diagrams New slideshow: ERD ERD
Driving School Database
Driving School Database Customers
Driving School Database Customers book
Driving School Database Customers book lessons
Driving School Database Customers book lessons in a
Driving School Database Customers book lessons in a car
Driving School Database Customers book lessons in a car with an
Driving School Database Customers book lessons in a car with an instructor.
Driving School Database Customers book lessons in a car with an instructor. Entities:
Driving School Database Customers book lessons in a car with an instructor. Entities: CUSTOMERS
Driving School Database Customers book lessons in a car with an instructor. Entities: CUSTOMERS LESSONS
Driving School Database Customers book lessons in a car with an instructor. Entities: CUSTOMERS LESSONS CAR
Driving School Database Customers book lessons in a car with an instructor. Entities: CUSTOMERS LESSONS CAR INSTRUCTOR
Restaurant Database Customers make reservations. Orders are made of menu items and are given to the waiter. Entities: CUSTOMERS RESERVATION ORDER MENU_ITEMS WAITER
Foreign Key used to link the tables in a relational database. a field in one table that is also the primary key in another often overlined: MEMBER(MemberID, Forename, Surname, …...) BOOK(BookID, ISBN, Title, …..) LOANS(LoanID, MemberID, BookID, LoanDate, ….)
Relationships Members of a library are loaned books Entities: MEMBERS LOANS
Relationships MEMBERS
Relationships MEMBERS LOANS
Relationships MEMBERS LOANS
Relationships MEMBERS LOANS BOOKS
Relationships MEMBERS LOANS BOOKS
Relationships MEMBERS MemberID LOANS LoanID BOOKS BookID
Relationships MEMBERS MemberID LOANS LoanID MemberID BOOKS BookID
Relationships MEMBERS LOANS BOOKS MemberID LoanID MemberID BookID
Relationships MEMBERS LOANS BOOKS MemberID Forename Surname PostCode LoanID MemberID BookID DateOut BOOKS BookID Title Author ISBN
Relationships MEMBERS LOANS BOOKS MemberID Forename Surname PostCode LOANS LoanID MemberID BookID DateOut BOOKS BookID Title Author ISBN MEMBERS(MemberID, Forename, Surname, PostCode) BOOKS(BookID, Title, Author, ISBN) LOANS(LoanID, MemberID, BookID, DateOut) Relationships Primary Key
Foreign Key used to link the tables in a relational database. a field in one table that is also the primary key in another often overlined: MEMBER(MemberID, Forename, Surname, …...) BOOK(BookID, ISBN, Title, …..) LOANS(LoanID, MemberID, BookID, LoanDate, ….)
Relationships (SBD 2012) Outlets have visits for different types of inspection Entities: OUTLET INSPECTIONTYPES INSPECTIONVISITS
Exam question:
Exam question:
Exam question:
Exam question:
Relationships
Relationships A primary key is a ***** that contains a ****** item of **** that identifies a particular ******.
Relationships A primary key is a ***** that contains a ****** item of **** that identifies a particular ******. A foreign key is the ******* *** from another e***** and is used to l*** the t****s together forming a ************.
Relationships the links between entities in a relational database:
Relationships the links between entities in a relational database:
Relationships the links between entities in a relational database: there are three degrees of relationship between entities:
Relationships the links between entities in a relational database: there are three degrees of relationship between entities: one-to-one,
Relationships the links between entities in a relational database: there are three degrees of relationship between entities: one-to-one, one-to-many,
Relationships the links between entities in a relational database: there are three degrees of relationship between entities: one-to-one, one-to-many, many-to-many.
Relationships
One-to-One Relationship
One-to-Many Relationship
Many-to-Many Relationship
Many-to-Many Relationship usually broken down into two 1:many relationships using a link table
Many-to-Many Relationship
Exam question: A company stores details of customers and bookings in a database. Two of the entities in the database are CUSTOMER and BOOKING. What is the relationship between CUSTOMER and BOOKING? [1]
Exam question:
Exam question:
Exam Question: A company manufactures electronic tills for use in shops. The company has decided to write a database to store information about its sales. The entities that will be used in the database are: CUSTOMER: A list of the customers that have purchased tills from them TILL: A list of tills that are produced by the company SALESPERSON: The member of the company who was responsible for the sale ORDER: The details of the orders placed by the company ORDER_LINES: The details of the individual tills that make up each order Draw the Entity Relationship Diagram (ERD) making sure that the degree of each relationship is identified. [6]
Relationships to link records in different tables making sure data is not duplicated by ensuring referential integrity reduces storage space required since data is not duplicated
Entity-Relationship diagrams E-R diagram (ERD),
Entity-Relationship diagrams E-R diagram (ERD), a graphical representation of the entities and the relationships between them,
Entity-Relationship diagrams E-R diagram (ERD), a graphical representation of the entities and the relationships between them, from Access
Entity-Relationship diagrams E-R diagram (ERD), a graphical representation of the entities and the relationships between them, from Access manually drawn CUSTOMER BOOKINGS STAFF
Entity Relationship diagram Members of a marching band who are loaned instruments and attend events:
Standard Notation Entities are written in capital letters Attributes are written with an initial capital Primary keys are underlined or labelled PK Foreign keys are overlined for OCR board or labelled FK
Standard Notation Entities are written in capital letters Attributes are written with an initial capital Primary keys are underlined or labelled PK Foreign keys are overlined for OCR board or labelled FK e.g. for a library database: MEMBER(MemberID, Forename, Surname, ….) BOOK(BookID, Title, Author ….) LOANS(LoanNo, MemberID, BookID, DateOut, …)
Referential Integrity every foreign key must have a related primary key value e.g. for a customer ID in a hire table there must be the same customer (ID) in the customer table you can’t hire something to a non-existent customer! if a customer record is deleted then all ‘hires’ by the deleted cusomer are removed. ensures no redundant data is stored
Duplicate Data more than 1 copy of a data record e.g. customer’s data entered & stored twice data taking up more storage space than necessary possible data inconsistency 2 different copies of the data
Entity Relationship Diagram (SBD 2012)