1 / 24

The Relational Model

The Relational Model. Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa. Important Points. Relational Model Definitions Relational Keys Relational Integrity. Tables. Relational database is a collection of tables

toby
Télécharger la présentation

The Relational Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Relational Model Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa

  2. Important Points • Relational Model Definitions • Relational Keys • Relational Integrity

  3. Tables • Relational database is a collection of tables • Heading: table name and column names • Body: rows, occurrences of data Student

  4. CREATE TABLE Statement CREATE TABLE Student ( StdSSN CHAR(11), StdFirstName VARCHAR(50), StdLastName VARCHAR(50), StdCity VARCHAR(50), StdState CHAR(2), StdZip CHAR(10), StdMajor CHAR(6), StdClass CHAR(6), StdGPA DECIMAL(3,2) )

  5. Common Data Types • CHAR(L) • VARCHAR(L) • INTEGER • FLOAT(P) • Date/Time: DATE, TIME, TIMESTAMP • DECIMAL(W, R) • BOOLEAN

  6. Relationships

  7. Alternative Terminology

  8. Relational Model Definitions • A relation is a table with columns and rows. • An attribute is a named column of a relation. • A tuple is a row of a relation. • A domain is a set of allowable values for one or more • attributes. • The degree of a relation is the number of attributes it contains. • The cardinality of a relation is the number of tuples it contains. • A relational database is a collection of normalized relations with distinct relation names. • The intension of a relation is the structure of the relation including its domains. • The extension of a relation is the set of tuples currently in the relation.

  9. Example

  10. Try It Student • 1) What is the name of the relation? • 2) What is the cardinality of the relation? • 3) What is the degree of the relation? • 4) What is the domain of StdGPA? What is the domain of StdSSN?

  11. Database • A database consists of multiple relations • Information about an enterprise is broken up into parts, with each relation storing one part of the information account : stores information about accountsdepositor : stores information about which customer owns which account customer : stores information about customers • Storing all information as a single relation such as bank(account_number, balance, customer_name, ..)results in • repetition of information (e.g., two customers own an account) • the need for null values (e.g., represent a customer without an account)

  12. Formal Definition • The relational model may be visualized as tables and fields, but it is formally defined in terms of sets and set operations. • A relation schema R with attributes A =<A1, A2, …, An> is denoted R(A1, A2, …, An) where each Ai is an attribute name that ranges over a domain Di denoted dom(Ai). • Example: Product(id, name, supplierId, categoryId, price) • R = Product (relation name) • Set A = {id, name, supplierId, categoryId, price} • dom(price) is set of all possible positive currency values • dom(name) is set of all possible strings that represent people's names

  13. Relation Schemas and Instances • A relation schema is a definition of a single relation. • The relation schema is the intension of the relation. • A relational database schema is a set of relation schemas. • A relation instance denoted r(R) over a relation schema R(A1, A2, …, An) is a set of n-tuples <d1, d2, ..., dn> where each di is an element of dom(Ai) or is null. • The relation instance is the extension of the relation. • A value of null represents a missing or unknown value.

  14. Cartesian Product (review) • The Cartesian product written as D1 x D2 is a set operation that takes two sets D1 and D2 and returns the set of all ordered pairs such that the first element is a member of D1 and the second element is a member of D2. • Example: • D1 = {1,2,3} • D2 = {A,B} • D1 x D2 = {(1,A), (2,A), (3,A), (1,B), (2,B), (3,B)} • Practice Questions: • 1) Compute D2 x D1. • 2) Compute D2 x D2. • 3) If |D| denotes the number of elements in set D, how many elements are there in D1 x D2 in general. • What is the cardinality of D1 x D2 x D1 x D1?

  15. Relation Instance • A relation instance r(R) can also be defined as a subset of the Cartesian product of the domains of all attributes in the relation schema. That is, r(R) dom(A1) x dom(A2) x … x dom(An) • Example: • R = Person(id, firstName, lastName) • dom(id) = {1,2}, dom(firstName) = {Joe, Steve} • dom(lastName) = {Jones, Perry} • dom(id) x dom(firstName) x dom(lastName) = { (1,Joe,Jones), (1,Joe,Perry), (1,Steve,Jones), (1,Steve,Perry), (2,Joe,Jones), (2,Joe,Perry), (2,Steve,Jones), (2,Steve,Perry)}

  16. Properties of Relations A relation has several properties: • Each relation name is unique. • No two relations have the same name. • Each cell of the relation (value of a domain) contains exactly one atomic (single) value. • Each attribute of a relation has a distinct name. • The values of an attribute are all from the same domain. • Each tuple is distinct. There are no duplicate tuples. • This is because relations are sets. In SQL, relations are bags. • The order of attributes is not important. • The order of tuples has no significance.

  17. Relational Keys • Keys are used to uniquely identify a tuple in a relation. • Note that keys apply to the relational schema not to the relational instance. That is, looking at the current instance cannot tell you for sure if the set of attributes is a key. • A superkey is a set of attributes that uniquely identifies a tuple in a relation. • A key is a minimal set of attributes that uniquely identifies a • tuple in a relation. • A candidate key is one of the possible keys of a relation. • A primary key is the candidate key designated as the distinguishing key of a relation. • A foreign key is a set of attributes in one relation referring to a candidate key of another relation. • Many DDLs require that a foreign reference a primary key.

  18. Example Relations • Employee-Project Database: • Employees have a unique number, name, title, and salary. • Projects have a unique number, name, and budget. • An employee may work on multiple projects and a project may have multiple employees. An employee on a project has a particular responsibility and duration on the project. • Relations: EMP(ENO, ENAME, TITLE, SAL) PROJ (PNO, PNAME, BUDGET) WORKSON(ENO, PNO, RESP, DUR) • Underlined attributes denote keys.

  19. Example Relation Instances Questions: 1) Is ename a key for emp? 2) Is eno a key for WorksOn? 3) List all the superkeys for WorksOn.

  20. Practice Questions Questions: 1) List the candidate keys for the relation. 2) Pick a primary key for the relation. 3) Is name a candidate key for Person? 4) List all the superkeys for Person. Assumptions: 1) A person has only one driver’s license. 2) A driver’s license uniquely identifies a person.

  21. Relational Integrity • Integrity rules are used to insure the data is accurate. • Constraints are rules or restrictions that apply to the database and limit the data values it may store. • Types of constraints: • Domain constraint - Every value for an attribute must be an element of the attribute's domain or be null. • null represents a value that is currently unknown or not applicable. • null is not the same as zero or an empty string. • Entity integrity constraint - In a base relation, no attribute of a primary key can be null and each value must be unique. • Referential integrity constraint - If a foreign key exists in a relation, then the foreign key value must match a primary key value of a tuple in the referenced relation or be null.

  22. Foreign Keys Example

  23. Integrity Questions Question: 1) Find all violations of integrity constraints in these three relations.

  24. General Constraints • There are more general constraints that some DBMSs can enforce. These constraints are often called enterprise constraints or semantic integrity constraints. • Examples: • An employee cannot work on more than 2 projects. • An employee cannot make more money than their manager. • An employee must be assigned to at least one project. • Ensuring the database follows these constraints is usually achieved using triggers and assertions.

More Related