Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Databases Relational Model Primary Keys Relation or Relationship Foreign Keys Relationships between entities Integrity Constraints Power of.

Similar presentations


Presentation on theme: "Relational Databases Relational Model Primary Keys Relation or Relationship Foreign Keys Relationships between entities Integrity Constraints Power of."— Presentation transcript:

1 Relational Databases Relational Model Primary Keys Relation or Relationship Foreign Keys Relationships between entities Integrity Constraints Power of Relational Model More on relational keys

2 The relational model represents information in relations (called tables) –Each relation represents an entity (class) - table –Each column of a table represents the attributes of the entity - field –Each row of a table represents an instance of the entity – tuple or record –Each row/column intersection represents a single data value A database schema is a collection of table definitions (relation schemas) A relational database is a collection of tables –Each table stores objects for a single relation schema

3 The rows of a relational table are unique –No 2 rows have the same values for all of the attributes A primary key is an attribute or collection of attributes which uniquely identifies each row –No 2 rows have the same values for all attributes in the primary key Every table must have a primary key - Why? A primary key declaration is a constraint –A table is not allowed to have 2 different rows that have the same value for the primary key –Database systems enforce primary key constraints By blocking any attempt to modify a table that will result in a violation of the key constraint

4 Relation is not Relationship Be careful of these two words –Relation –Relationship A relation is a table that contains rows (instances of entity) A relationship is a connection between two entities Why is the relational model so named? –Not because of the relationships between entities –Because it is a collection of relations

5 Foreign Keys Controlled redundancy is how the relational model works. Relations share a common attribute and are thus linked. When an attribute of a relation is also the primary key of another relation that attribute is called a foreign key. Consider our video/movie problem again. Looking at an instance of a video (which is now a relation) we can use the MOVIE_ID to locate the info about the movie in the movie relation. Yes, the MOVIE_ID is redundant data, but it is controlled redundancy and this is the power of the relational model.

6 Movie movieIdtitlegenrelengthrating 101The Thirty-Nine Steps mystery101R 123Annie Hallromantic comedy 110R 145Lady and the Tramp animated comedy 93PG 189Animal Housecomedy87PG-13 450Elizabethcostume drama123PG-13 553Stagecoachwestern130R 987Duck Soupcomedy99PG-13

7 Video videoIddateAcquiredmovieIdstoreId 1011/25/19981013 1112/5/19971233 11212/31/19951235 1134/5/19981235 1144/5/19981895 1233/25/19861233 1455/12/19951455 775644/29/19911893 909873/25/19994503 9978710/10/19979875

8 Relationships between entities The relational model can only handle two types of relationships between entities: –One-to-one (OK) Sale and item??? –One-to-many (most useful in the relational model) A movie is copied on many videos (DVDs if not in Oakridge) –Many-to-many (can’t do this in the relational model) We have to create a new entity or relation called a bridge entity or bridge or junction table

9 One-to-one Relationship Consider the relationship between video and CurrentRental again Each Video can only be currently rented once and each CurrentRental can only involve a single video. Both have the same primary key (videoId). Usually when you have a one-to-one relationship you should seriously consider whether there should only be one entity instead of two.

10 One-to-many Relationship Consider the relationship between movie and video We link these relations by taking the primary key of the one and putting it into the many relation. It is considered a foreign key in the second relation. If the relationship is identifying then the foreign key will be part of the primary key If the relationship is non-identifying then the foreign key will not be part of the primary key

11 Many-to-Many Relationship Types Many-to-Many relationships between 2 entities cannot be represented as simple attributes in either related table Consider the relationship between truck drivers and trucks at a trucking company. Each driver drives many trucks and each truck is driven by many drivers. How do we represent that in the relational model? We create a new bridge entity or table. Perhaps we call it a trip. We take the primary keys from each of the entities and they become the primary key for the new entity. (draw driver and truck)

12 Integrity constraints The relational model provides two types of integrity constraints to ensure the integrity of the data: Entity integrity – the primary key column of a table cannot contain any null values and each value must be unique Referential integrity – the foreign key column of a relation must contain either nulls or if a value is not null the value must exist as a primary key in the related table. The RDBMS enforces these constraints for us

13 Power of Relational Model Abstraction –The table is a logical structure –Much easier to understand and work with –No physical structures Manipulation/Extraction –Relational Algebra –SQL standard (sort of) with all RDBMS

14 More on relational keys Superkey An attribute (or combination of attributes) that uniquely identifies each instance in a table Candidate key A minimal superkey. A superkey that does not contain a subset of attributes that is itself a superkey Primary key A candidate key selected to uniquely identify all other attribute values in any given row. Cannot contain null entries. Alternate key A candidate key that is not the primary key (might be used as secondary access via an index) Foreign key An attribute or combination of attributes of a table that are also the primary key of another table. Composite key A key that is composed of more than one attribute.


Download ppt "Relational Databases Relational Model Primary Keys Relation or Relationship Foreign Keys Relationships between entities Integrity Constraints Power of."

Similar presentations


Ads by Google