Database Management Concepts A2 Computing 3.9 Databases F453
Key Points Flat & Relational Entity Relationships Normalisation Primary, Secondary, Foreign Keys Data Consistency Data Integrity Data Redundancy DBMS Data Dictionary DDL DML SQL
Database Terminology A database is… A collection of data/tables/records Table names are also known as ENTITIES A record is… A collection of completed fields about a particular entity. A row in a database. TUPLE A field is…. Part of a record containing one specific data item. Eg. Name, Address, DOB, Genre ATTRIBUTE
Primary and Foreign Key A Primary Key is a Unique Identifier within a table. Foreign key is a field in one table which is also the primary key of another table. Foreign keys are used to establish a relationship between the main table and other subsidiary tables.
Secondary Key A field that is unique to that record that isn’t the primary key, but it could be. Eg… EmployeeID, Name, NINumber, PhoneNo Primary Key Secondary Key
Flat File Databases FirstName Surname ContactNo DateOfBirth Cost InstructorName John Hainsworth 01474 678456 25/05/1989 25 20 Hansworth 15 Tony Hodson 01474 212394 07/09/1984 Mike Setvenson 01474 665498 15/11/1990 Denton Sharon Hart 22/04/1985 Richard Ellis 01474 151884 12/08/1988 Muller Lucie Harris 01474 908 123 14/12/1990 Beccy Lock 01474 505783 03/06/1984 Omar Ibqual 01474 689236 05/07/1991 Gareth Jones 01474 889322 26/06/1985 Seb 01474 255873 21/06/1986 10 Harries
Problems with Flatfile Databases Duplicate records Spelling mistakes could be made If John changed his telephone number, it would have to be changed 3 times
Redundancy and Integrity Data Redundancy Unnecessary duplication of data Data Integrity The consistency of data How correct it is
Relationships One to one An instructor teaches a course One to many An instructor teaches more than one course Many to many Activity Instructor Activity Instructor Activity Instructor
Normalisation Breaks things down to smallest possible units 1st Normal Form 2nd Normal Form 3rd Normal Form
Problems with Data Redundancy and Integrity Un-normalised StudentID Forename Surname ActivityID Activity Instructor 100012 David Jackson 1 4 3 Riding Windsurfing Climbing Susie Holman Jim Jackson David Smith 100011 Susie Thomas 100014 Jerry Kilmer 2 Windsurding 4x4 Driving Tom Wilson 100016 Lisa Terry Data is un-normalised. Problems with Data Redundancy and Integrity
1st Normal Form No repeating attributes Still has Data Redundancy Primary Key StudentID Forename Surname ActivityID 100012 David Jackson 1 4 3 100011 Susie Thomas 100014 Jerry Kilmer 2 100016 Lisa Terry Foreign Key ActivityID Activity Instructor 1 Riding Susie Holman 2 4x4 Driving Tom Wilson 3 Climbing David Smith 4 Windsurfing Jim Jackson No repeating attributes Still has Data Redundancy Repeating Student Names Primary Key
2nd Normal Form Use Link Table StudentID Forename Surname 100012 David Jackson 100011 Susie Thomas 100014 Jerry Kilmer 100016 Lisa Terry ActivityID Activity Instructor 1 Riding Susie Holman 2 4x4 Driving Tom Wilson 3 Climbing David Smith 4 Windsurfing Jim Jackson StudentID ActivityID 100012 1 3 100014 2 100016 Use Link Table
3rd Normal Form StudentID Forename Surname 100012 David Jackson 100011 Susie Thomas 100014 Jerry Kilmer 100016 Lisa Terry ActivityID Activity InstructorID 1 Riding 2 4x4 Driving 3 Climbing 4 Windsurfing Booking ID StudentID ActivityID 1 100012 2 3 100014 4 100016 InstructorID Instructor 1 Susie Holman 2 Tom Wilson 3 David Smith 4 Jim Jackson
Entity Relationship Diagram 3NF 3NF – Never have a Many to Many Relationship No repeating data fields (aside from primary and foreign keys) Student Booking Activity Instructor
Database Management System DBMS Separates the physical data and the user interface that accesses the data Many different employees accessing the same data via a server Allows program-data independence
Problems with Sharing Data Many users access the same data file Concurrent Access What if 2 users are trying to edit the same data item simultaneously? Database Management System Record Locking
DBMS example DBMS is a ‘layer’ of software that operates between the data and the applications that access the data
Role of DBMS Software that acts as an interface between user and database Allows the definition / creation / maintenance / manipulation of the database Creation and maintenance of the data dictionary Backup and recovery Security
Software used to control access to data DBMS Database Management System provides an interface between the data and the user Software used to control access to data
Three levels of DBMS External Conceptual Internal The users view of the data. The complexities of the data are hidden from the user so this view is the front-end. Conceptual The entities, structure, attributes and relationships. Internal This describes how the data is stored physically and how it will be accessed.
Advantages & Disadvantages of DBMS Security Data Independence The data is independent from the programs and applications that interact with it Data is easily shared and distributed Disadvantages Expensive Closely monitor security System failure
Items in Data Dictionary A file containing descriptions of data in database used by database managers when altering database structure names of tables characteristics of data (e.g. length, data type) relationships between data identifies primary keys identifies foreign keys defines access rights
Data definition language (DDL) DDL is the language used to control the structure of the database (Schema) It is used to create new entities, attributes, keys and relationships. Create and manage Data Dictionary Access Rights Create Database Create Table PrimaryKey = PupilID
DDL Example
Common DDL commands
Data Manipulation Language - DML Allows control and modification and manipulation of Data Insert Delete Amend Update Retrieve SQL - Query
DDL controls the data structure and DML controls the actual data DDL & DML DDL controls the data structure and DML controls the actual data
Structured Query Language (SQL) SQL is a 4th generation programming language that is used to create databases and insert, update and extract data Syntax gives a command word that tells system what operation to carry out instructions to tell system what parts of database to carry operation out on SQL is a DDL and a DML because it allows applications to query a database as well as inserting & updating data
Structured Query Language Query a database using the following SQL commands: SELECT FROM WHERE ORDER BY
SQL Example Consider the table FORM To delete 7C from the table: DELETE FROM Form WHERE Formname = ‘7C’ To insert a new row: INSERT INTO Form VALUES (‘7E’, ‘Mr Post’, 205) 205 doesn’t need quotes because it is numeric Formname Formteacher Formroom 7A Mr Brown 102 7B Mr Collins 301 7C Miss Jones 107 7D Mrs Smith 201
To create SQL reports Formname Formteacher Formroom 7A Mr Brown 102 7B Mr Collins 301 7C Miss Jones 107 7D Mrs Smith 201 7E Mr Post 205 SELECT * FROM Form WHERE Formteacher = “Mrs Smith” This gives: 7D Mrs Smith 201 SELECT Formroom from Form WHERE Formteacher = “Mrs Smith” This gives: 201 SELECT Formname FROM Form Where Formroom < 200 What does this do?