Relation schema
Download
Report
Transcript Relation schema
Data Models
Example: A relation is a table.
Attributes
Attributes
(column
(column
headers)
headers)
Tuples
Tuples
(rows)
(rows)
name
Winterbrew
Bud Lite
Relation
Relation
name
name
manf
Pete’s
Anheuser-Busch
Beers
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Pearson Education, Inc.
Slide 2-1
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.
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Pearson Education, Inc.
Slide 2-2
Example
Suppose we have the following information in our database:
Student
Database instance
Section
Grade
Report
Name
StudentNumber
Class
Major
Smith
17
1
CS
Brown
8
2
CS
SectionIdentifier
CourseNumber
Semester
Year
Instructor
85
MATH2410
Fall
98
King
92
CS1310
Fall
98
Anderson
102
CS3320
Spring
99
Knuth
112
MATH2410
Fall
99
Chang
119
CS1310
Fall
99
Anderson
135
CS3380
Fall
99
Stone
StudentNumber
SectionIdentifier
Grade
17
112
B
17
119
C
8
85
A
8
92
A
8
102
B
8
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
135
Copyright © 2004 Pearson Education, Inc.
A
Slide 1-3
2-3
Example
Suppose we have the following information in our database:
Student
Name
Section
SectionIdentifier
Grade
Report
StudentNumber
StudentNumber
Schema constructs
CourseNumber
Class
Semester
SectionIdentifier
Major
Year
Instructor
Grade
Schema diagram
Slide 1-4
2-4
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Pearson Education, Inc.
Example of a Relation
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 5
Formal Definitions - Schema
The Schema (or description) of a Relation:
Example:
CUSTOMER (Cust-id, Cust-name, Address, Phone#)
Denoted by R(A1, A2, .....An)
R is the name of the relation
The attributes of the relation are A1, A2, ..., An
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 6
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:
<632895, "John Smith", "101 Main St. Atlanta, GA 30332",
"(404) 894-2000">
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)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 7
Key Constraints (continued)
If a relation has several candidate keys, one is chosen
arbitrarily to be the primary key.
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
The primary key attributes are underlined.
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
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 8
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 9
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
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 10
COMPANY Database Schema
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 11
Referential Integrity Constraints for COMPANY database
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 12
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 (
DNAME
VARCHAR(10)
NOT NULL,
DNUMBER
INTEGER
NOT NULL,
MGRSSN
CHAR(9),
MGRSTARTDATE
CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP );
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 8- 13
Assignment
• 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
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 14
Assignment
Important points
• Due Date
• For Wednesday Class March 9th
• For Thursday Class March 10th
• For Friday Class March 11th
• 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
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Slide 5- 15