Relational Databases HSC IPT Introduction There are two types of database: Flat file and Relational. The database you created is flat because all the data is lumped together in one table and some duplication does occur. Relational databases use a series of related tables which helps reduce redundant data. New Terms ATTRIBUTE – the defined property of an entity. Attributes are the same as fields in a flat file database. FOREIGN KEY – an attribute which is the primary key of another table. DATA REDUNDANCY – where the same data is stored in two or more records or files. This is undesirables duplication of data that takes up valuable space. RELATIONSHIP – the way in which entities are related to each other. The Key Field is used to link entities. Relational Databases Every record has a PRIMARY KEY FIELD or a unique combination of fields forming a COMPOUND KEY. Data redundancy is reduced as the same data is not scattered around numerous files. All data is stored in tables. Types of Relationships ONE TO ONE - each record in the first entity (ie table) is related to exactly one record in the second entity. For example each primary teacher is assigned to one class in the primary school. ONE TO MANY - each record in the first entity relates to many records in the second entity. For example, each secondary teacher is assigned to many classes in the high school. MANY TO MANY – each record in the first entity relates to many records in the second entity, and each record in the second entity is related to many records in the first entity. For example, each student studies many subjects and each subject is studied by many students. Schema SCHEMA is an organised plan of the entire database. It shows where the data is found and the logical relationships. CUSTOMER CustomerID FirstName LastName Address State Postcode SALESPERSONS SalespersonId FirstName LastName WorkPhone HireDate ORDERS OrderID CustomerID OrderDate SalespersonID OrderPaid ORDER DETAILS OrderID ProductID Quantity PRODUCTS ProductID ProductName UnitsInStock UnitsOnOrder UnitPrice Normalisation of Data Normalisation of data is the process of analysing data to create the most efficient database structure. The most important procedure in normalisation is to eliminate data redundancy by removing the repeating fields and regrouping them as a new record. It involves dividing a database into two or more tables and defining relationships between the tables. There are three main normal forms. First Normal Form 1NF: Each field in a table contains different information. For example, in an employee list, each table would contain only one date of birth field. Second Normal Form 2NF: No field values can be derived from another field. For example, if a table already included a date of birth field, it could not also include a birth year field, as this information would be redundant. Third Normal Form 3NF: No duplicate information is permitted. For example, if two tables both require a date of birth field, the date of birth information would be placed into a separate table, and the two other tables would then access the information via an index field in the date of birth table. Questions What’s the difference between a flat file database and a relational database? What is data redundancy? How does a relational database eliminate data redundancy? What are the three types of relationships? What is a schema? What is meant by normalisation of data? Homework What 3 tables would you need? What fields would you need in these tables? Start planning/thinking about this over the weekend. Have a go at drawing a schema!!