Data Modeling using XML Schemas [PDF]

the primary key. ○ Eg: PRIMARY KEY (Student) = . ○ Primary key attributes cannot take null values. Mu

17 downloads 15 Views 202KB Size

Recommend Stories


XML Conceptual Modeling using UML
I want to sing like the birds sing, not worrying about who hears or what they think. Rumi

Modeling data using directional distributions
The wound is the place where the Light enters you. Rumi

Using the XML Interface
Never let your sense of morals prevent you from doing what is right. Isaac Asimov

Using Python And XML for Flood Plain Delineation Modeling
Everything in the universe is within you. Ask all from yourself. Rumi

Peugeot 504 - Schemas electriques PDF
Don't be satisfied with stories, how things have gone with others. Unfold your own myth. Rumi

Agenda XML XML XML XML
Never wish them pain. That's not who you are. If they caused you pain, they must have pain inside. Wish

XML and LINQ to XML - CIS151 [PDF]
Jul 24, 2013 - Sections 24.7–24.11 demonstrate techniques for working with XML documents in C# applications. Visual C# provides ..... urn:deitel:textInfo. Compare this to lines 9–11 of Fig. 24.5, where we had to prefix the file and description el

XML Data Mediation and Collaboration
The only limits you see are the ones you impose on yourself. Dr. Wayne Dyer

Knowledge representation of large medical data using XML
You have survived, EVERY SINGLE bad day so far. Anonymous

Using AutoMed for XML Data Transformation and Integration
You miss 100% of the shots you don’t take. Wayne Gretzky

Idea Transcript


The Relational Model

Murali Mani

Why Relational Model? z

Currently the most widely used z

z

Older models still used z

z

IBM’s IMS (hierarchical model)

Recent competitions z

z

Vendors: Oracle, Microsoft, IBM

Object Oriented Model: ObjectStore

Implementation standard for relational Model z z

SQL (Structured Query Language) SQL 3: includes object-relational extensions Murali Mani

1

Relational Model z

Structures z z

Relations (also called Tables) Attributes (also called Columns or Fields)

Note: Every attribute is simple (not composite or multi-valued) Constraints z

z

z

z

Key and Foreign Key constraints (More constraints later)

Eg: Student Relation (The following 2 relations are equivalent) Student Student

sNumber

sName

sNumber

sName

1

Dave

2

Greg

2

Greg

1

Dave

Cardinality = 2 Arity/Degree = 2

Murali Mani

Relational Model z

Schema for a relation z z

z

Schema for a database z

z

Schemas for all relations in the database

Tuples (Rows) z

z

Eg: Student (sNumber, sName) PRIMARY KEY (Student) =

The set of rows in a relation are the tuples of that relation

Note: Attribute values may be null Murali Mani

2

Primary Key Constraints z

A set of attributes is a key for a relation if: z z

z z z z

No two distinct tuples can have the same values in all key fields A proper subset of the key attributes is not a key.

Superkey: A proper subset of a superkey may be a superkey If multiple keys, one of them is chosen to be the primary key. Eg: PRIMARY KEY (Student) = Primary key attributes cannot take null values Murali Mani

Candidate Keys (SQL: Unique) z

z z z

Keys that are not primary keys are candidate keys. Specified in SQL using UNIQUE Attribute of unique key may have null values ! Eg: Student (sNumber, sName) PRIMARY KEY (Student) = CANDIDATE KEY (Student) = Murali Mani

3

Violation of key constraints z

A relation violates a primary key constraint if: z

z

z

There is a row with null values for any attribute of primary key. (or) There are 2 rows with same values for all attributes of primary key

Consider R (a, b) where a is unique. R violates the unique constraint if all of the following are true z

2 rows in R have the same non-null values for a Murali Mani

Keys: Example Student sNumber

sName

address

1

Dave

144FL

2

Greg

320FL

Primary Key: Candidate key: Some superkeys: {, , , }

Murali Mani

4

Foreign Key Constraints z

z

To specify an attribute (or multiple attributes) S1 of a relation R1 refers to the attribute (or attributes) S2 of another relation R2 Eg: Professor (pName, pOffice) Student (sNumber, sName, advisor) PRIMARY KEY (Professor) = FOREIGN KEY Student (advisor) REFERENCES Professor (pName) Murali Mani

Foreign Key Constraints z z z

z z

FOREIGN KEY R1 (S1) REFERENCES R2 (S2) Like a logical pointer The values of S1 for any row of R1 must be values of S2 for some row in R2 (null values are allowed) S2 must be a key for R2 R2 can be the same as R1 (i.e., a relation can have a foreign key referring to itself). Murali Mani

5

Foreign Keys: Examples Dept (dNumber, dName) Person (pNumber, pName, dept) Persons working for Depts

PRIMARY KEY (Dept) = PRIMARY KEY (Person) = FOREIGN KEY Person (dept) REFERENCES Dept (dNumber)

Person (pNumber, pName, father) PRIMARY KEY (Person) = FOREIGN KEY Person (father) REFERENCES Person (pNumber)

Person and his/her father

Murali Mani

Violation of Foreign Key constraints z

z

Suppose we have: FOREIGN KEY R1 (S1) REFERENCES R2 (S2) This constraint is violated if z

z

Consider a row in R1 with non-null values for all attributes of S1 If there is no row in R2 which have these values for S2, then the FK constraint is violated.

Murali Mani

6

Relational Model: Summary z

Structures z z

z

Relations (Tables) Attributes (Columns, Fields)

Constraints z

Key z

z z

Primary key, candidate key (unique)

Super Key Foreign Key Murali Mani

ER schema → Relational schema Simple Algorithm z

z

Entity type E → Relation E’ z Attribute of E → Attribute as E’ z Key for E → Primary Key for E’ For relationship type R between E1, E2, …, En z Create separate relation R’ z Attributes of R’ are primary keys of E1, E2, …, En and attributes of R z Primary Key for R’ is defined as: ƒ ƒ

z

If the maximum cardinality of any Ei is 1, primary key for R’ = primary key for Ei Else, primary key for R’ = primary keys for E1, E2, …, En

Define “appropriate” foreign keys from R’ to E1, E2, …, En Murali Mani

7

Simple algorithm: Example 1 pNumber

dNumber Person

(1, *)

Works For

(0, *)

Dept

pName

dName years

Person (pNumber, pName) Dept (dNumber, dName) WorksFor (pNumber, dNumber, years) PRIMARY KEY (Person) = PRIMARY KEY (Dept) = PRIMARY KEY (WorksFor) = FOREIGN KEY WorksFor (pNumber) REFERENCES Person (pNumber) FOREIGN KEY WorksFor (dNumber) REFERENCES Dept (dNumber) Murali Mani

Simple Algorithm: Example 2 Supplier (sName, sLoc) Consumer (cName, cLoc) Product (pName, pNumber) Supply (supplier, consumer, product, price, qty)

pNumber

pName

Product (0, *) sName

cName Supplier

(1, *)

Supply

(0, *)

Consumer

sLoc

cLoc price

qty

PRIMARY Key (Supplier) = PRIMARY Key (Consumer) = PRIMARY Key (Product) = PRIMARY Key (Supply) = FOREIGN KEY Supply (supplier) REFERENCES Supplier (sName) FOREIGN KEY Supply (consumer) REFERENCES Consumer (cName) FOREIGN KEY Supply (product) REFERENCES Product (pName) Murali Mani

8

Simple Algorithm: Example 3 pNumber

pName

Part

superPart (0, *)

subPart (0, 1)

Part (pName, pNumber) Contains (superPart, subPart, quantity)

Contains

quantity

PRIMARY KEY (Part) = PRIMARY KEY (Contains) = FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber) FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber) Murali Mani

Decreasing the number of Relations Technique 1 z

If the relationship type R contains an entity type, say E, whose maximum cardinality is 1, then R may be represented as attributes of E. z

z

If the cardinality of E is (1, 1), then no “new nulls” are introduced If the cardinality of E is (0, 1) then “new nulls” may be introduced.

Murali Mani

9

Example 1 sNumber

pNumber Student

(1,1)

Has Advisor

(0, *)

Professor

sName

pName years

Student (sNumber, sName, advisor, years) Professor (pNumber, pName) PRIMARY KEY (Student) = PRIMARY KEY (Professor) = FOREIGN KEY Student (advisor) REFERENCES Professor (pNumber) Note: advisor will never be null for a student Murali Mani

Example 2 pNumber

dNumber Person

(0,1)

Works For

(0, *)

Dept

pName

dName years

Person (pNumber, pName, dept, years) Dept (dNumber, dName) PRIMARY KEY (Person) = PRIMARY KEY (Dept) = FOREIGN KEY Person (dept) REFERENCES Dept (dNumber) Dept and years may be null for a person Murali Mani

10

Example 3 pNumber

pName

Part

superPart (0, *)

subPart (0, 1) Contains

quantity

Part (pNumber, pname, superPart, quantity) PRIMARY KEY (Part) = FOREIGN KEY Part (superPart) REFERENCES Part (pNumber) Note: superPart gives the superpart of a part, and it may be null Murali Mani

Decreasing the number of Relations Technique 2 (not recommended) z

z

If the relationship type R between E1 and E2 is 1:1, and the cardinality of E1 or E2 is (1, 1), then we can combine everything into 1 relation. Let us assume the cardinality of E1 is (1, 1). We have one relation for E2, and move all attributes of E1 and for R to be attributes of E2. z z

If the cardinality of E2 is (1, 1), no “new nulls” are introduced If the cardinality of E2 is (0, 1) then “new nulls” may be introduced. Murali Mani

11

Example 1 sNumber

pNumber Student

(0,1)

Has Advisor

(1,1)

Professor

sName

pName years

Student (sNumber, sName, pNumber, pName, years) PRIMARY KEY (Student) = CANDIDATE KEY (Student) = Note: pNumber, pName, and years can be null for students with no advisor

Murali Mani

Example 2 sNumber

pNumber Student

(1,1)

Has Advisor

(1,1)

Professor

sName

pName years

Student (sNumber, sName, pNumber, pName, years) PRIMARY KEY (Student) = CANDIDATE KEY (Student) = Note: pNumber cannot be null for any student. Murali Mani

12

Other details z

Composite attribute in ER z

z

Include an attribute for every component of the composite attribute.

Multi-valued attribute in ER z

z

We need a separate relation for any multi-valued attribute. Identify appropriate attributes, keys and foreign key constraints.

Murali Mani

Composite and Multi-valued attributes in ER sNumber

sName

major

Student

sAge address

street

city

state

Student (sNumber, sName, sAge, street, city, state) StudentMajor (sNumber, major) PRIMARY KEY (Student) = PRIMARY KEY (StudentMajor) = FOREIGN KEY StudentMajor (sNumber) REFERENCES Student (sNumber) Murali Mani

13

Weak entity types z

Consider weak entity type E z z

z

z

A relation for E, say E’ Attributes of E’ = attributes of E in ER + keys for all indentifying entity types. Key for E’ = the key for E in ER + keys for all the identifying entity types. Identify appropriate FKs from E’ to the identifying entity types.

Murali Mani

Weak entity types: Example

Dept (dNumber, dName) Course (cNumber, dNumber, cName) PRIMARY KEY (Dept) = PRIMARY KEY (Course) = FOREIGN KEY Course (dNumber) REFERENCES Dept (dNumber)

Murali Mani

14

ISA Relationship types: Method 1 sNumber

sName

Student (sNumber, sName) UGStudent (sNumber, year) GradStudent (sNumber, program)

Student

year

ISA

UGStudent

ISA

PRIMARY KEY (Student) = PRIMARY KEY (UGStudent) = PRIMARY KEY (GradStudent) =

program

GradStudent

An UGStudent will be represented in both Student relation as well as UGStudent relation (similarly GradStudent)

FOREIGN KEY UGStudent (sNumber) REFERENCES Student (sNumber) FOREIGN KEY UGStudent (sNumber) REFERENCES Student (sNumber)

Murali Mani

ISA Relationship types: Method 2 sNumber

sName

Student

year

ISA

ISA

UGStudent

program

GradStudent

Student (sNumber, sName, year, program) PRIMARY KEY (Student) = Note: There will be null values in the relation. Murali Mani

15

ISA Relationship types: Method 3 Student (sNumber, sName) UGStudent (sNumber, sName, year) GradStudent (sNumber, sName, program) UGGradStudent (sNumber, sName, year, program) sNumber

sName

PRIMARY KEY (Student) = PRIMARY KEY (UGStudent) = PRIMARY KEY (GradStudent) = PRIMARY KEY (UGGradStudent) =

Student

year

ISA

UGStudent

ISA

program

Any student will be represented in only one of the relations as appropriate.

GradStudent

Murali Mani

16

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.