Вы находитесь на странице: 1из 65

Designing a Relational Database

Conceptual Schema Design Procedure (CSDP)


This PowerPoint file can be found in
K:\Student Share Files\Year 10\ICT Information Communication & Technology\CSDP

Relational databases
O Relational databases are the most

common form of databases. O Relational information systems are designed to avoid common issues arising in Flat-file databases:
O Redundancy O Inefficiency O Update anomalies O Data loss

Relational tables
O A relational database is made up of a

group of named tables. O Each data entry in a cell is called an . O Each column of data is referred to as a . O Each row of data in the table is a or .

3 Relational Table Rules


1. Each cell must contain and value (or be NULL) 2. Each record (tuple) must be . That is: there are not to be any duplicate tuples. 3. There is no inherent order to a relational table.

This is the KEY!


O In order to gain some form of uniqueness to

each row, we can set one (or more) of the fields as . O A key field(s) cannot have duplicate values. E.g. idNo could be a key field as long as there are no two numbers the same. O Multiple keys can be used if the value of the combined fields is what makes it unique. O E.g. student/subject/semester achieves a grade.

So is there an easy way to create a relational IS?


O Yes the Conceptual Schema Design

Procedure (CSDP), used in conjunction with the Optimal Normal Form (ONF) will always create relational tables.

Some basic terminology:


= Object Role Modelling (CS) = Shows the relationships between data = Universe of Discourse what will and wont be included in the CSD = The formal declaration of what and wont be included

More terminology
= basic objects in UoD (can be either physical or abstract) = the type of object eg. Code, name etc = What each entity does. Eg. Sam studies IPT studies is the role = the number of entities involved in a fact type = predicate(entity) eg: Olivia is_male = predicate(entity,entity) eg: Olivia has_gender Male = predicate(entity,entity,entity) eg: Olivia obtained VHA in IPT

Conceptual Schema Design: 7 Simple Steps


The CSDP follows 7 steps: 1. Create elementary facts (from Elementary Sentences) 2. Draw the base Conceptual Schema 3. Check for entities which can be combined and for derivative facts 4. Add uniqueness constraints (check arity) 5. Add mandatory constraints (check derivatives) 6. Add value and subtype constraints 7. Add other constraints and do final check

Create elementary facts


O An elementary facts state the

relationships between entities in the most atomic way possible. This means that they cannot be reduced further without loss of the relationship. e.g. O Bob studies IPT is an elementary fact . O Bob studies loses some of the relationship between the student and the subject.

Create Elementary Facts (2)


O More examples on pages: 166 & 167
O Complete questions 1 4, Act 8.2

Elementary Sentences
O An elementary sentence is a formal

statement of an elementary fact. O It breaks the fact into its component parts: O Entity(ies) the object in the fact O Role what the object does

Elementary Sentences (2)


O The entity is the object stated in its
O

O
O O

Primary state In the example, Bob studies IPT, there are 2 entities: Bob and IPT. Bob is a Student. Bob represents the Student Name IPT is a Subject. IPT represents the Subject Name.

Elementary Sentences (3)


O Each entity has three (3) parts:

1. The Object type (Student) 2. The Reference (Name) 3. Example (Bob) O Eg. Bob now becomes: Student(name) Bob and IPT: Subject(name) IPT

Elementary Sentences (4)


O A role must be played both ways:
O Eg. O Bob studies IPT, and IPT is studied by

Bob

Elementary sentences (5)


O

Put it all together and Bob studies IPT becomes: Student(name) Bob studies/is studied by Subject(name) IPT

Entity

Role

Entity

Another format:
Student (name) Bob studies/is studied by Subject (name) IPT
Entity Role Entity

This format allows for an easier identification of the entities and roles, but takes more space. My preference is for one line, however, you may use either.

Working with Ternary facts


O Bob studied IPT in Semester 1
O 3 Entities: Bob, IPT and Semester O Student (name) Bob O Subject (name) IPT O Semester (#) 1

Student (name) Bob studies/is studied by Subject(name) IPT in/for Semester(#) 1

Just Remember!
O Write your facts out first
O Ensure that they are elementary O Identify entities (underline the objects) O Write each fact as an Elementary

Sentence

Step 2: Create the CSD!


An example CSD:

A bigger example (still not enterprise!)


O TV Guide:

Converting Elementary Sentences to CSD


O A Conceptual Schema is really composed of:
O Entities the objects O Roles what they do O Constraints how many times they can be in

the DB, what types etc.


O All put together with

relationships!

O After all, a CSD simply displays the

relationships between entities.

Breaking it down Entities


O An entity is simply displayed as a circle

with the Type and (reference mode) O Eg. Or Bob =

Breaking it down Roles


O Roles (what the entities do) are displayed

as blocks. O Eg. or Studies/is studied by =

Breaking it down Constraints


O There are too many constraints to

introduce here, but some include uniqueness constraints, mandatory constraints, frequency constraints and so on. O We will look at these in greater depth later in the CSDP.

Start with fragments


O Once you have your elementary sentence,

it simply becomes a case of replacing the entities with ovals and the roles with boxes. O Student(name) Bob studies/is studied by Subject(name) IPT
Student (name) Subject (name)

studies

is_studied_by

But first, Some rules:


O There are 3 main rules for drawing a CSD:

1. Each entity must play at least 1 role. 2. Each role must be played by at least 1 entity 3. Each role must be played by at most 1 entity O Finally, there should only be 1 diagram with all areas connected through others.

Examples of the rules


O Are the following legal? If not, why not?
a
b c

e
Object1 Object3

A larger example Its all school


StudentId 1202 1202 1202 1203 1203 1202 1202 Student Name B. Jones B. Jones B. Jones J. Seinfeld J. Seinfeld B. Jones B. Jones PC 11 PCA 11 PCA 11 PCA 11 PCC 11 PCC 11 PCA 11 PCA Gender M M M F F M M SubjectId 11IPTA 11ENGC 11CHEMA 11IPTA 11ENGB 11IPTA 11ENGC Subject Name IPT English Chemistry IPT English IPT English

1203

J. Seinfeld

11 PCC

11IPTA

IPT

Its all school Elementary Facts


O Student 1202 is named B. Jones
O Student 1202 is in PC 11 PCA O Student 1202 has a gender of Male O Student 1202 studies Subject 11IPTA O Subject 11IPTA is called IPT

Its all school Convert to elementary sentences B. Jones ** O Student(id) 1202 has/is for Name
O Student(id) 1202 is in/has PC (code) 11 PCA O Student(id) 1202 has/is for Gender(code) M O Student(id) 1202 studies/is studied by

Subject(code) 11 IPTA O Subject(code) 11IPTA is called/is for Name IPT

Its all school Convert to CS fragments


Student (id) is called is for Name
Student (id) is in has PC (code)

Student (id)

has

is for

Gender (code)

Student (id)

studies

is studied by

Subject (id)

Subject (id)

is called

is for

Name

Its all school Identify like entities


Student (id) is called is for Name
Student (id) is in has PC (code)

Student (id)

has

is for

Gender (code)

Student (id)

studies

is studied by

Subject (id)

Subject (id)

is called

is for

Name

Its all school Combine into one CS


PC (code) has is in studies is studied by Subject (id)

Like entities have been combined into 1. All the entity/role relationships also come off the one entity
Gender (code) is for has

is called Student (id)

is for

has

is for

Name

A sample
O Pg 171

Question 8: Go from start (as is) through to Conceptual Schema create elementary facts, elementary sentences, conceptual schema fragments, whole conceptual schema.

The answer to the sample


Elementary facts 1. BorrowId has a Fullname 2. BorrowId has a Title Mr 3. BorrowId has a DOB 4. BorrowId has a street address 5. BorrowId has a Suburb 6. BorrowId has a Phone number 7. BorrowId has an Issue Date 8. The suburb has a post code

The answer to the sample


Elementary Sentences: 1. Borrower(Id) has/is for Person(name) 2. Borrower(Id) has/is for Title(code) 3. Borrower(Id) has/is for DOB(date) 4. Borrower(Id) has/is for Address(street) 5. Borrower(Id) has/is for Suburb(name) 6. Borrower(Id) has/is for Phone(#) 7. Borrower(Id) has/is for IssueDate(date) 8. Suburb(name) has/is for PCode(#)

The answer to the sample


O CS Fragments

The answer to the sample


O Put it all together:

Like entities have been combined into 1. All the entity/role relationships also come off the one entity

Combined and Derived facts


ItemID 0101 0112 0113 0114 ItemName Backpack Cap Socks(wool) Socks(cotton ) Cost($) 15.95 1.15 1.95 0.52 RRP($) 39.95 24.95 15.95 12.95 Profit($) 24.00 23.80 14.00 12.43

Item(id) 1010 is called/is for Item(name) Backpack Item(id) 1010 has/is for Cost($) 15.95 Item(id) 1010 has/is for RRP($) 39.95 Item(id) 1010 has/is for Profit($) 24.00

The CSD

Profit can worked out by RRP Cost This means it can be derived

Uniqueness Constraints
O Uniqueness constraints simply state how

O
O O

O
O

many times an entity can play a role. They are displayed by drawing a line/arrow above the role which is unique. There are four types of uniqueness constraints: 1-many many-1 (reversal of 1:many) 1-1 many-many

Table for examples


O All the examples will come from this table:
StudentId 1202 1202 1202 1203 1203 1202 1202 1203 Student Name B. Jones B. Jones B. Jones J. Seinfeld J. Seinfeld B. Jones B. Jones J. Seinfeld PC 11 PCA 11 PCA 11 PCA 11 PCC 11 PCC 11 PCA 11 PCA 11 PCC Gender M M M F F M M F SubjectId 11IPTA 11ENGC 11CHEMA 11IPTA 11ENGB 11IPTA 11ENGC 11IPTA Subject Name IPT English Chemistry IPT English IPT English IPT

How do I know which is which?


O To decide which constraint to use, simply

ask the question: O How many times can this entity play this role? O How many times can the other entity play the role?

A 1-many constraint
O A 1 to many constraint indicates that an

entity play a role only 1 time but that the reverse role can be played many times. e.g: O Student(id)1203 has/is_for Gender(code)m O How many genders can a student have? 1 O How many students can a gender be for? many

Constraint is 1:many

1:many what does it look like?


Draw arrow/line above the unique role. Student has gender can only be played once, Therefore arrow above has

Student (id)

has

is for

Gender (code)

1 to 1 Constraint
O Student(id) 1203 has Student(name) B.

Jones O How many names can a Student Id have?1


O How many ids can a student name have?

Constraint is 1:1

1:1 what does it look like?


Each role is played once, therefore unique. Each role has its own arrow over it.

Student (id)

has

has

Student (name)

many - many
O Student(id) 1203 studies Subject(code)

11ENGA
O How many subjects can a student study? O How many students are in a subject?

man y many

Constraint is many:many

many:many what does it look like?


The roles can both be played many times. It is the combination of their values which makes it unique. The arrow therefore must go over both of the roles.

Student (id)

studies

is studied by

Subject (code)

Add constraints exercises


O Add uniqueness constraints to the

examples we have done so far.

Ternary Relationships: Does this work?


O Student(name) Claudia studies/is studied

by Subject(code) 11 ENG O Student(name) Claudia achieves/is achieved by Grade(LOA) HA Or O Subject(code) 11 ENG scores/is score for Grade(LOA) HA
O No because we now dont know what the grade

was for. Claudia (for example) achieves a HA for English and a VHA for IPT which is which?

So, what now?


O Ternary relationship:
O Student(name) Claudia studying/studied

by Subject(code) 11ENG scores/is score for Grade(LOA) HA


O What makes this unique is the Student

studying the Subject.

Ternary CSD:

Sometimes, a ternary CSD is appropriate. BUT

Nested Relationships
O A nested relationship represents

O O O

the relationship between the combination of the Student and the Subject with the Grade. So, a Grade is dependant upon the Student-Subject combination. Elementary sentences: StudentSubject = [Student(name) studies/is studied by Subject(code)] StudentSubject scores/score for Grade(LOA)

Questions
O Pages 181 182
O Questions 1 and 4 O FINISH FOR HOMEWORK!!!!

Bunyip Town Library


O Q8 pg 171

Mandatory Constraints
O Mandatory constraints are used to show

when an entity must play a role. O It is shown on the CSD with a dot ( ) on the entity where the line linking it to the role meets it.

Entity Constraints
O An entity constraint is used to restrict the

type of data which is allowed to be stored for that entity. O E.g. Gender(code) would only need M or F to be stored. O These are shown by using { } next to the entity being restricted. O It can include simple values or a range.

Example Entity Contraint


Gender will only be able to be stored as either an M or an F.

Levels of Achievement (LOA) can be in the range of A to E, thus A..E

Frequency Constraints
O Frequency constraints are used to show

how many times a role can be played. O Obviously, these can only be used on many roles as the uniqueness constraint states it can only be played once. O These are usually placed above or below the role with a line indicating which it applies to.

Frequency Constraint Example


Using these Frequency constraints: Each student MUST study 6 or less subjects Each Subject must have 24 or less students Obviously, frequency constraints can also state that a role must be played exactly a number of times (=x) or more than a number of times (>=x)

Optimal Normal Form


O ONF is used to turn a fully formed conceptual

schema diagram into properly relational tables ready for creation in a database. O It has three steps: 1. Loop all the 1-m relationships based around a central entity 2. Loop all the m-m (and nested) relationships as individual tables 3. Create named tables and underline the unique entities.

With ONF:

Вам также может понравиться