What is a database A database is any collection of information (data). This collection represents part of the real world. The data is organized in some manner so that the information contained within the database can be easily retrieved.
Examples of database Phone books Library School Search engines
Basic Database Concepts Field: a single piece of information Record: a collection of related fields Table: a collection of related records Database: a collection of tables
Difference between a database and arelational database The difference is in the way the tables are constructed.
DatabaseEMPLOYEE tableName Employee Number Hire DateBill White 1122 1/1/2001Ron Smith 2387 27/9/2001Greg King 4456 24/8/1999CUSTOMER tableName Address PhoneLyons 123 A Street 916-444-55Dennys 435 Elm Street 916-478-23IHOP 654 17th Av. 916-458-77ORDERS tableCustomer Name Order Number CostLyons 101 23.78 €Lyons 145 100.25 €IHOP 152 54.89 €
Relational databaseEMPLOYEE tableEmployeeID Name Hire Date1122 Bill White 1/1/20012387 Ron Smith 27/9/20014456 Greg King 24/8/1999CUSTOMER tableCustomerID Name Address Phone100 Lyons 123 A Street 916-444-55101 Dennys 435 Elm Street 916-478-23102 IHOP 654 17th Av. 916-458-77ORDERS tableOrder Number Customer Number SalesID Cost101 100 1122 23.78 €145 100 2387 100.25 €152 102 2387 54.89 €
Database Management System (DBMS) Software that allows users to create, maintain, and query your data in the related tables Examples: MS Access, Oracle, FoxPro, MySql
Primary and foreign keys Primary Key, a value that is unique to each record Foreign Key, a primary key of one table included in another table. Link tables to each other. Find the PK and FK on the last relational database example.
Referential Integrity DEPARTMENT Referential integrity is a DID DName database concept that 13 Marketing ensures that relationships 14 Accounts between tables remain consistent. 15 Personnel What happens if: EMPLOYEE Marketing’s DID is changed EID EName DID to 16 in DEPARTMENT? The entry for Accounts is 1515 John Smith 13 deleted from 1600 Mary Brown 14 DEPARTMENT? 1717 Mark Jones 13 1800 Jane Smith 13
Types of relationshipsExample:In a College database we might have Students, Subjects, and Teachers, who are tutors at the same time. Students might have attributes such as their ID, Name, and could have relationships with Subjects and Tutors.
Types of relationships One to one: each teacher has a unique office. One to many: a teacher may tutor many students, but each student has just one tutor. Many to many: each student takes several subjects, and each subject is taken by several students.Exercise:1. In the database Employee-Department, what type of relationship do you find?More examples:1. In our database we are going to add Projects (new table). A project can have many employees working on it. And, an employee can work on many projects.2. A manager manages one department; a department has only one manager.
Types of relationships In a database we store information about books, the authors, the category of the books, and the publishers. A book can have more than one author. A book can be of more than one category. A publisher publishes many books. A book is published only for one publisher. What types of relationships do we have here?
Designing a database structure As you are gathering information in the requirements phase, you should be thinking about these items: Identify all of the fields you are going to need and ways to organize the related fields into tables Determine or build a primary key for each table if needed
Designing a database structure Compare the two tables below. You will see situations that you want to avoid:1) Avoid data redundancy.2) If you can avoid data redundancy, you can avoid data mistakes. Look at the customer name for customer #635 in both tables. Two different spellings. Which one is correct?3) Notice that there are two different customers in the Orders table that have the same customer number (#104). If your billing system uses the customer number to drive it, who’s going to get the bill?4) Notice that some of the customers in the Orders table aren’t even listed in the Customer table. Will these customers ever get a bill?
CUSTOMER tableCustID Name Address Phone City State104 Meadows Restaurant Pond Hill Rd 313-792 Monroe Mi128 Grand River Restaurant 37 Queue Av. 313-729 Lacota MI163 Bentham’s Restaurant 1366 36th St 517-792 Monroe MI635 Oaks Restaurant 3300 West Russell St 419-336 Maumee OH741 Prime Cut Steakhouse 2819 East 10th St 219-336 Mishawalka IN779 Gateway Lounge 3408 Gateway B1 419-361 Sylvania OHORDERS tableOrdID CustID Name Billing Date Invoice Amount202 104 Meadows Restaurant 15/1/12 1280.50226 635 Oakes Restaurant 15/1/12 1939.00231 779 Gateway Lounge 15/1/12 1392.50309 741 Prime Cut Steakhouse 15/2/12 1928.00313 104 Stokes Inn 15/2/12 1545.00377 128 Grand River Restaurant 15/3/12 562.00359 635 Raks Restaurant 15/3/12 1939.00373 779 Gateway Lounge 15/3/12 1178.00
Designing a database structure A database is to be made to store information about a catalogue of CDs. Information to be stored about each CD includes their titles, prices, and genre. This catalogue will take into account only the “Rock”, “Pop”, and “Rap” genre. Each CD will also have an artist, and each artist may produce several CDs. Artists have names associated with them.
Designing a database structure Identify tables, fields, and relationships from the problem description. How do I find tables and fields? Tables and fields are often represented by nouns in the description, so possible tables from the CD problem description include database, catalogue, cd, title (of CD), price, genre, names. Some of these might not be needed, and there may be others
Designing a database structure How do I tell the difference between a table and a field? Field values are atomic, so fields cant have smaller parts or be groups of things. Fields cant participate in relationships. Fields have a single value (a string, a number, etc), whereas tables have complex values (groups of fields generally).
Designing a database structure How do I find potential relationships between tables? Relationships are often verbs or phrases that link two tables in the problem description. For example, if we have identified the tables CD and artist, then a relationship between them is described by the sentence "Each CD will also have an artist, and each artist may produce several CDs."
Designing a database structure How do I determine the type of relationship? Sometimes this is apparent from the description. For example the sentence "Each CD will also have an artist, and each artist may produce several CDs" indicates that there is a one-to-many relationship between artists and CDs - each artist is related to many CDs, but each CD is related to just one artist. Sometimes it is less clear, and you need to think about what situations are possible (not just likely). This often involves detailed knowledge of the problem, which means that making the right decision helps to make the database represent this knowledge.
Designing a database structureSo which are our tables?Which fields should be primary key?What relationships will be needed?What data type should you use for each field?ARTIST table CD tableartID cdIDartName artID cdTitle cdPrice cdGenre
Adding information to the database Suppose we want to add the following CDs to the database: Artist Title Genre Price Stellar Mix Rock 9.99 Stellar Magic Lines Rock 11.99 Spiritualized Amazing Grace Pop 9.99 The Feelers Supersystem Rock 11.99 Minuet The 88 Electronica 12.99 The Feelers Communicate Rock 9.99 Gang Starr Daily Operation Rap 10.99
Adding information to the database Each CD is going to require two entries - one in the CD table, and one in the Artist. However, we dont want to put each artist in more than once, and we need to know the artists ID before we can insert the CD. This means that there are three stages to inserting a CD: Check to see if there is an entry for the artist involved If there is, get their artID, if not make a new entry with a new artID Make an entry in the CD table with the artID from step 2
Validation rules Range check Look-up check Format check Length check: passwords Type check
Data verification There are two main methods of verification: Double entry - entering the data twice and comparing the two copies. Proofreading data - this method involves someone checking the data entered against the original document.