Presentation is loading. Please wait.

Presentation is loading. Please wait.

Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Pearson Education, Inc. Slide 2-1 Data Models Example: A relation.

Similar presentations


Presentation on theme: "Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Pearson Education, Inc. Slide 2-1 Data Models Example: A relation."— Presentation transcript:

1 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Pearson Education, Inc. Slide 2-1 Data Models Example: A relation is a table. Attributes (column headers) Tuples (rows) Relation name name manf WinterbrewPete’s Bud LiteAnheuser-Busch Beers Attributes (column headers) Tuples (rows) Relation name

2 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Pearson Education, Inc. Slide 2-2 Schemas versus Instances Database Schema: The description of a database. Includes descriptions of the database structure and the constraints that should hold on the database. Relation schema = relation name and attribute list. Optionally: types of attributes. Example: Beers(name, manf) or Beers(name: string, manf: string) Database = collection of relations. Database schema = set of all relation schemas in the database.

3 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Pearson Education, Inc. Slide 2-3 StudentNumberSectionIdentifierGrade 17112B 17119C 885A 892A 8102B 8135A Suppose we have the following information in our database: Slide 1-3 NameStudentNumberClassMajor Smith171CS Brown82CS Student SectionIdentifierCourseNumberSemesterYearInstructor 85MATH2410Fall98King 92CS1310Fall98Anderson 102CS3320Spring99Knuth 112MATH2410Fall99Chang 119CS1310Fall99Anderson 135CS3380Fall99Stone Section Grade Report Example Database instance

4 Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Pearson Education, Inc. Slide 2-4 StudentNumberSectionIdentifierGrade Suppose we have the following information in our database: Slide 1-4 NameStudentNumberClassMajor Student SectionIdentifierCourseNumberSemesterYearInstructor Section Grade Report Example Schema constructs Schema diagram

5 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 5 Example of a Relation

6 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 6 Formal Definitions - Schema The Schema (or description) of a Relation: Denoted by R(A1, A2,.....An) R is the name of the relation The attributes of the relation are A1, A2,..., An Example: CUSTOMER (Cust-id, Cust-name, Address, Phone#) CUSTOMER is the relation name Defined over the four attributes: Cust-id, Cust-name, Address, Phone# Each attribute has a domain or a set of valid values. For example, the domain of Cust-id is 6 digit numbers.

7 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 7 Formal Definitions - Tuple A tuple is an ordered set of values (enclosed in angled brackets ‘ ’) Each value is derived from an appropriate domain. A row in the CUSTOMER relation is a 4-tuple and would consist of four values, for example: This is called a 4-tuple as it has 4 values A tuple (row) in the CUSTOMER relation. A relation is a set of such tuples (rows)

8 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 8 Key Constraints (continued) If a relation has several candidate keys, one is chosen arbitrarily to be the primary key. The primary key attributes are underlined. Example: Consider the CAR relation schema: CAR(State, Reg#, SerialNo, Make, Model, Year) We chose SerialNo as the primary key The primary key value is used to uniquely identify each tuple in a relation Provides the tuple identity Also used to reference the tuple from another tuple General rule: Choose as primary key the smallest of the candidate keys (in terms of size) Not always applicable – choice is sometimes subjective

9 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 9 Referential Integrity Tuples in the referencing relation R1 have attributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R2. A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK]. A referential integrity constraint can be displayed in a relational database schema as a directed arc from R1.FK to R2.

10 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 10 Relational Database Schema Relational Database Schema: A set S of relation schemas that belong to the same database. S is the name of the whole database schema S = {R1, R2,..., Rn} R1, R2, …, Rn are the names of the individual relation schemas within the database S Following slide shows a COMPANY database schema with 6 relation schemas

11 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 11 COMPANY Database Schema

12 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 12 Referential Integrity Constraints for COMPANY database

13 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 13 CREATE TABLE In SQL2, you can use the CREATE TABLE command for specifying the primary key attributes, secondary keys, and referential integrity constraints (foreign keys). Key attributes can be specified via the PRIMARY KEY and UNIQUE phrases CREATE TABLE DEPT ( DNAMEVARCHAR(10)NOT NULL, DNUMBERINTEGERNOT NULL, MGRSSNCHAR(9), MGRSTARTDATECHAR(9), PRIMARY KEY (DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGRSSN) REFERENCES EMP );

14 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 14 Define at least five tables with formal schema definition(as given in slide 6 or slide 5-6 At least four of these tables should be connected to other tables with foreign key relation Show these tables with referential integrity constraints(like in slide 14) Create each tables SQL queries with CREATE table statement Assignment

15 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 15 Important points Due Date For Wednesday Class March 9 th For Thursday Class March 10 th For Friday Class March 11 th Each homework will be delivered in lab lectures Late delivery will not be tolerated Homeworks will be delivered on paper, email submission will not be accepted Keep a copy of your assignment, we will continue to use our tables in next assignments Assignment


Download ppt "Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Pearson Education, Inc. Slide 2-1 Data Models Example: A relation."

Similar presentations


Ads by Google