Download presentation
Presentation is loading. Please wait.
Published byKarin Rogers Modified over 7 years ago
1
CompSci 280 S2 2107 Introduction to Software Development
The Relational Model
2
Today’s Agenda Topics: Reading: Introduction The Relational Model
Database Relationships One to One One to Many Many to Many Constraints Reading: Lecture13
3
Modeling an App with a 3-Tier Architecture
Written docs UI models (e.g. screen diagrams) Flow or sequence charts State machines Data models (e.g. class diagrams) Lecture13
4
1.Introduction What is a Database Management System DBMS? Definition
Software that manages and stores the data. The applications interact with the system, which retrieves and manipulates the data. Advantages of Database systems over file systems Data Integrity; Controlled Redundancy; Standards can be proposed since the data definitions are also shared; The ability to implement decisions on access control; The ability to balance conflicting needs by tuning and optimising Security of data. Lecture13
5
2.The Relational Model Was introduced by Codd in 1970 (journal paper);
It is based on a simple and uniform data structure - the relation - and it has a solid theoretical foundation; The Relational Model represents the Database as a collection of Relations, which are sets from a mathematical point of view; One Entity type is a Relation; One Relationship Type is either a Relation or one or more attributes; The retrieval of information involves manipulating the Relations using set operations and also operators defined in the relational algebra; Examples of relational database systems: Oracle, Microsoft SQL Server, Sybase SQL Server, Lecture13
6
2.The Relational Model Relational Database
Data from several tables is tied together (related) using a field that the tables have in common A Simple Relational Database Example Lecture13
7
2.The Relational Model Some Rules …
Every table has a unique name. Attributes in tables have unique names. Every row/tuple is unique. Lecture13
8
2.The Relational Model Concept
Relational modeling uses primary keys and foreign keys to maintain relationships Primary Key (cannot have null value) - attribute(s) that uniquely identify a tuple in a table. Foreign Key - an attribute that is a primary key in another table. Composite keys are primary keys that are made of more than one attribute Weak entities Associative entities Lecture13
9
2.The Relational Model Customer Table
Mapping UML class diagrams to Relational Data Base Tables CREATE TABLE Customer ( customerID INT NOT NULL primary key, name VARCHAR(50) NOT NULL, tempAmount decimal ); Lecture13
10
3.Database Relationships
There are several types of database relationships. One to One Relationships One to Many and Many to One Relationships Many to Many Relationships Lecture13
11
3.Database Relationships One to One Relationships
There are two tables: Customers table: Addresses table: We have a relationship between the Customers table and the Addresses table. If each address can belong to only one customer, this relationship is "One to One". Keep in mind that this kind of relationship is not very common. Normally one table is enough: Lecture13
12
3.Database Relationships One to Many Relationships
This is the most commonly used type of relationship. Consider an e-commerce website, with the following: Customers can make many orders. Orders can contain many items. Items can have descriptions in many languages. Example: Each customer may have zero, one or multiple orders. But an order can belong to only one customer. Lecture13
13
3.Database Relationships Many to Many Relationships
In some cases, you may need multiple instances on both sides of the relationship. For example, each order can contain multiple items. And each item can also be in multiple orders. We need to create an extra table: Lecture13
14
4.Constraints Primary & Foreign Keys
Tables can be related through primary/foreign key relationships (e.g. A customer may order one or more orders) Primary key Guarantees the uniqueness of a row Can be composed of one or more columns Foreign key Establishes logical relationship between tables One or more columns of a table that match the primary key of another table CustomerID FirstName LastName ALFKI Maria Anders Alfreds Futterkiste ANATR Ana Trujillo Ana Trujillo ... Foreign Key 1 ∞ Relation OrderID CustomerID OrderDate 10248 WILMK 04-Jul-1996 10249 TRADH 05-Jul-1996 Primary Key Lecture13
15
4.Constraints A CONSTRAINT is a rule that limits (or restricts) what data can be placed in a column of a table. Constraints are used to ensure that the data is kept consistent and meaningful. UNIQUE Constraints Ensures that every row contains a different value NOT NULL Constraints Ensures that there are no nulls in that column PRIMARY KEY Constraints Combination of Unique and Not Null constraint Referential Integrity Constraints Lecture13
16
4.Constraints Referential Integrity Constraints
Main type of data validation within relational database Ensures relationships are maintained between the data in one table with data in another table Performed when data is being changed Performed during Insert, Update and Delete Statements will fail if Referential Integrity is not maintained For examples: Case 1: add a new row to the child table Case 2: update/delete an existing row Lecture13
17
4.Constraints Case 1 Parent table: customer, child table: order
In the Data Table(child), a value can always be Deleted or set to null, but any new value must pass validation For example: Insert a new order for a new customer: 20001, AXETH (Axen Thomas), Solution: Add “Axen Thomas” into the Customer table FIRST Then, add the new order to the Order table CustomerID FirstName LastName ALFKI Maria Anders Alfreds Futterkiste ANATR Ana Trujillo Ana Trujillo ... Foreign Key OrderID CustomerID OrderDate 10248 WILMK 04-Jul-1996 10249 TRADH 05-Jul-1996 Primary Key Lecture13
18
There is an order made by WILMK in the order table
4.Constraints Case 2 In the List of Valid Values in the parent table, a new valid value can always be added, but a value can only be deleted if it is not used at all in the Data Table. For example: Remove WILMK from the customer table Solution: Use the RI options to “Cascade” All the ROWS of the Data Table are DELETED if they were using the value that is being deleted There is an order made by WILMK in the order table CustomerID FirstName LastName ALFKI Maria Anders Alfreds Futterkiste ANATR Ana Trujillo Ana Trujillo ... Foreign Key OrderID CustomerID OrderDate 10248 WILMK 04-Jul-1996 10249 TRADH 05-Jul-1996 Primary Key Lecture13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.