Copyright © 2004 AdventNet Data Modeling Basic Course
Oct 14, 2014
Copyright © 2004 AdventNet
Data Modeling
Basic Course
Copyright © 2004 AdventNet
Some Definitions
Database: Storage mechanism for the information that we require
Relational database: A database consisting of more than one table and which are related in some way.
Data Model: Collection of description of tables together with the operations or functions that manipulate them in order to intelligently organize data.
Copyright © 2004 AdventNet
Remember
Tables are also relations Tables are constructed and associated
to each other through shared fields Fields are also “columns” or
“attributes” Records are also “rows” or “tuples” Tables are related through common
fields designated as primary and foreign keys
Copyright © 2004 AdventNet
Example
Create a table with following columns Employee name Date of Birth Address
Create table Employee(name varchar(20) not null, bdate date, address varchar(30));
Populate the table
Copyright © 2004 AdventNet
SQL Queries
Select name from Employee where address like’%Velachery%’;
Insert into Employee values (‘vidhya’,1972-01-29’,’5A,Porur Street,East Tambaram,Chennai’);
Delete from Employee where name=‘vidhya’;
Update Employee set bdate=‘1999-02-9’ where name=‘vidhya’;
Copyright © 2004 AdventNet
SQL Operations
Three basic update operations on relations are Insert- is used to add records Delete- removes record Update – can change an existing
record
Copyright © 2004 AdventNet
Data Integrity Constraints
A limitation that you place on the data that users can enter into a row or column or groups of columns or table.
Constraints include: PK, UNIQUE, FK/REFERENCES, CHECK & DEFAULT Domain Constraints Entity Constraints Referential User Defined
Copyright © 2004 AdventNet
Domain Integrity
Domain Constraints apply at column/ attribute level.
Ensures that set of data values fall within a specified range(domain) in order to be valid.
Examples: correct data type; values that fall within the range supported by the system; null status; permitted size values
Copyright © 2004 AdventNet
Entity Integrity
Entity Integrity Constraint applies at the row level
Ensures that each row in a table is uniquely identified. For a car the license number is a key
as no two cars will have the same license number
Copyright © 2004 AdventNet
Referential Integrity
Referential Integrity applies at table level
Referential integrity is typically enforced with a Primary Key (PK) and Foreign Key (FK) combination
Example- When an order is entered, the entered customer id must exist in customers.
Copyright © 2004 AdventNet
Example
Let us add some more attributes to the Employee table- Date of Joining Team Name Manager Name Email address Phone number
Populate Data
Copyright © 2004 AdventNet
Exercise
What are the constraints we would place for each attribute of the Employee table— Data type,size Nullable Unique PK/FK
Do this exercise again at the end of the course— Notice the changes..
Copyright © 2004 AdventNet
Primary Key
The Primary Key is an Attribute or set of Attributes that uniquely identifies a specific instance of an Entity.
Primary Key field cannot contain a NULL value
Each Table should have a primary key A primary key that is made up of more
than one attribute is known as Composite Key
Copyright © 2004 AdventNet
Primary key - Example
What is the primary key in our Employee table??
What if another employee with the same name joins the company?
We introduce <EmployeeID> as the primary key.
Copyright © 2004 AdventNet
Foreign Key
A key used in one table to represent the value of a primary key in a related table.
While primary keys must contain unique values, foreign keys may have duplicates
Copyright © 2004 AdventNet
Surrogate Key
A surrogate key is a key that has no meaning other then uniquely identifying a row in the table -it contains no meaningful business information.
Use Surrogate Keys to all “important” or core entities.
EmployeeID is a surrogate key.
Copyright © 2004 AdventNet
NULL Avoidance
Databases allow NULLs, but they cause numerous problems, such as complicating the code – conditions like if (someAttribute != NULL)
AdventNet Rule: No NULLs allowed Use default values such as “No
Comment Entered” or “No Description Available” or “No Subject Provided”
Copyright © 2004 AdventNet
Example
If an Employee does not have a phone number, what would we enter in the Employee table??
A “null” could mean that the phone number is not known to us or that the employee does not possess a phone.
Copyright © 2004 AdventNet
Data Modelling – Why?
So that you can query and find what you want easily
To prevent bugs arising from inconsistent data
Well organized data greatly reduces and simplifies code needed for an application, and therefore improves quality
Database, XML are data storage mechanisms that should be backed up with well defined structure ie; data model
Copyright © 2004 AdventNet
Elements of a Data Model
Entity - An Entity is a thing or object of importance about which data must be collected
Attribute- An attribute describes information about an entity that must be captured
Relationships- Represents an association between two or more entities
Copyright © 2004 AdventNet
Entity
We have captured information (Name, Date of Birth, Address,Manager Name,Team,Email Address, Phone number) about each Employee and we cannot describe an employee without these items.
Hence, Employee is an Entity about which we want to collect data
Copyright © 2004 AdventNet
Attribute
Name, Date of Birth, Address, Email Address, Team name, Manager, Phone number describe information about the employee
Hence, these are Attributes of the Employee table
Copyright © 2004 AdventNet
Exercise
Make a list of the possible Entities, Attributes for the music CD’s that you have at home CD Name Movie Name Songs
Continue……
Copyright © 2004 AdventNet
Entity Attribute Relationship
Name
Address
Owns/leasesMakeModelColour
Copyright © 2004 AdventNet
Relationship
A relationship relates two or more distinct entities with a specific meaning.
Relationships are created between tables using the primary key field and a foreign key field.
Copyright © 2004 AdventNet
Example
What is the relationship here- Employee ,PC Department, Employee Project, Employee
Copyright © 2004 AdventNet
Example
An employee is assigned a PC, each PC is owned by an employee
An employee works in a department , a department has many employees
A project can have multiple employees working on it and each employee can work on multiple projects at the same time
Copyright © 2004 AdventNet
Exercise
What is the relationship for the following- Student,class Customer,Order Sales representative, customers Movie,song
Copyright © 2004 AdventNet
Solution
A student belongs to a class, a class has many students
A customer places an order or many orders, an order belongs to a customer
A sales representative meets many customers, a customer meets many sales representatives.
A movie contains many songs, a song belongs to a movie
Copyright © 2004 AdventNet
ER Diagram
The model visually represents the Entity-Relationship concepts by the Entity-Relationship diagram.
The basic constructs of the ER model are entities, relationships, and attributes.
Copyright © 2004 AdventNet
E1 E2
A1 A1 A2 A2
R
E-Entity
A-Attribute
R -Relationship
ER DiagramWe can express the overall structure of data
graphically with an ER Diagram
Copyright © 2004 AdventNet
ER Diagram Example
One Room can hold many classes, but each class is taught in only one room
class room
No. building
seats
No.
Copyright © 2004 AdventNet
Issues – In the Employee table
What if one employee belongs to multiple teams – such as during a transition to a new team?
What if an employee does not have a manager?
Copyright © 2004 AdventNet
Example - Solution
Employee Table: EmployeeID, Name, Date Of Birth,
Date Of Joining, Email Address, Phone Number
Employee Team: EmployeeID, TeamID
Employee Manager: EmployeeID,ManagerID
Copyright © 2004 AdventNet
Points to Remember
Uniquely identify records Eliminate Recurring Fields Eliminate Data Entry Anomalies Eliminate NULL entries
Copyright © 2004 AdventNet
Example-Student-Activity
We’ll design a database to keep track of students’ sports activities. We’ll track each activity a student takes and the cost to do that activity.
Copyright © 2004 AdventNet
Example- Student-Activity
Student name Activity Cost
John Smith Tennis $36
Jane Bloggs Badminton $40
John Smith Swimming $30
John Smith Tennis $35
Mark Antony Golf $60
Mark Antony Swimming $30
Copyright © 2004 AdventNet
Student-Activity-Think
Redundant data entry If the tennis fees goes up to $39?
Redundant information If 50 students take swimming, we have to type in
both the activity and its cost each time Inconsistent data
Notice that there are conflicting prices for tennis Insertion anomalies
What if our school introduces a new activity, such as sailing, at $50.
Deletion anomalies If Mark Antony transfers to another school?
Copyright © 2004 AdventNet
Solution to Student-Activity
Student(<StudentID>,FirstName,LastName)
Participant(<Student ID,Activity>) Activities(<Activity>,Cost)
Copyright © 2004 AdventNet
Example-
Model the following- An academic department of a college
has the attributes Department name, Dept phone no., List of Faculty.
We want to store the following - City, street,house number, house colour, city population
Copyright © 2004 AdventNet
Solution
1) Relation is 1:N <DepartmentID>, Dept Name, Dept
Phone no. <FacultyID>,DepartmentID
2) <CityID>, City Name, City population <AddressID>,house no., house colour,
streetID, CityID <streetID>,street name
Copyright © 2004 AdventNet
See links
Bookstore
Airline
Copyright © 2004 AdventNet
Quiz
A primary key is An attribute that can be used to identify a
single row in a table A key used in one table to relate to key in
another table An index for a table
A well defined data model allows us to Have a record of data Store data in a sequence Reduce amount of code needed for an
application
Copyright © 2004 AdventNet
Quiz
Grades of a student should be A or B or C. You enter Z. What happens? The keyboard Z key gets locked An error message – constraint violated
appears on the screen Nothing
The three kinds of anomalies are insertion,selection,deletion insertion,update,deletion update, selection,deletion
Copyright © 2004 AdventNet
How do I find
•Division 1 has Pat, Chris, and Jamie
•Division 1 operates branches 101 and 102
•Who works at branch 101?
Copyright © 2004 AdventNet
Exercise
Model the following An Employee has the following details-
Name,Date of Birth, department name, skill set
The following details about a student is required- Student name, subject, marks
Restaurant order information needed-Customer Name, Address, Order Date, Item Name, Item Quantity
Online store customer information- First name, Last Name, address, city, state,Pin code
Copyright © 2004 AdventNet
Guidelines
Every table should have a primary key NULL values not allowed Data should be present one and only
once Multiple values for an attribute not
allowed
Copyright © 2004 AdventNet
Questions???