Database: - A Database Is A Collection of Related Information

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 49

DATABASE

• A database is a collection of related


information.
– You want to store data on the disk in a way that
is most efficient in disk space usage.
– You want to present data to the user with the
faster response.
Relational database
• The three key component used in relational
database are :
– Entities ( Tables )
– Attributes ( Columns )
– Relationships ( Primary / Foreign Key )
Entities
• An entities defines any person,place,thing
or concept for which data will be collected.
– Person: student, teacher
– Place : classroom,building
– thing : computer
– concept : course , student attendance
Noun Phrases ( Entities )
• We have ten departments on campus that
offer about 400 different courses -
everything from astronomy101 to Zoology
410. Of course, we don’t have enough
professors to tech every course every
semester , but we do manage to offer about
150 different course each semester.
Entities
• A good practice to list all of the entities in
your database with one-sentence description
of what that entity represents.
Entities
• Teacher: A person employed by college who is
responsible for instructing a class of students.
• Student: A person who is enrolled in class at the college
and attends class sessions.
• Course: A course defines the subject material taught in a
class.
• Class: A scheduled instance of a course that is taught by
one teacher and that meets in particular room during
specific date .
• Class Session: an instance of a class that occurs at
particular date and time.
Entity and data model
• In data model diagram ( often referred to as
an entity-relationship (ER) diagram or
database schema ), entities are most often
drawn as boxes.
Attributes
• Attributes are additional characteristics or
information defined for an entity.
• An entity’s attributes don’t define an entity ,
but they provide additional information
about an entity.
Attributes
• Teacher: Name, Social Security Number , address ,
salary.
• Student: Name , Social Security Number , Address ,
Grade Point Average.
• Course: Course Number , Course Name ,
Prerequisites.
• Class:Course Number , Assigned teacher, Maximum
Enrolment , Student Enrolled.
• Class Session: Course number , date and time of
session , student attending.
Relationships
• A relationship is a logical linkage between
two entities that describes how the entities
are associated with each other.
• Relationships often have a verb phrase
associated with them in the data model.
Relationships
• A teacher instructs classes.
• A course define subject material for
classes.
• Students are enrolled in classes.
• Students attend class sessions.
• A class occurs as class sessions.
Types of Relationships
• The classification of a relationship is called
cardinality.
• There are three types of cardinality.
– One-to-One.
– One-to-Many.
– Many-to-Many.
One-to-One
• A single instance of one entity is associated
with a single instance of another entity.
One-to-Many
• An instance of an entity ( called the parent)
is associated with zero or several instance
of another entity ( called the child ).
Many-to-Many
• Many instances of an entity are associated
with many instances of another entity.
• SQL-Server does not support a direct
implementation of this type of relationship;
however , it is possible to model this type of
cardinality using two one-to-many
relationships.
Entities & Attributes In SQL
• Entities and attributes are implemented in
SQL server as tables and columns.
• In SQL server , tables are used to store
information about each instance of an entity
as a row in the table, with each attribute
stored in a column.
• Rows and Columns are sometimes referred to
a records and fields.
SQL Table & column
Restriction
• Table and column names cannot be longer
than 30 characters.
• Table name must be unique within the
database.
• Column name must be unique within a
table.
Relational Model
• For Relational Model to work properly ,
each row must be unique . This concept is
known as entity integrity or row integrity.
• In SQL row uniqueness is enforced by
using a special column in the table called
the primary key.
Defining the Primary Key
• The primary key is a special column or group of
columns in the table that can be used to identify
any one row and must be unique in the table.
• SQL server provides a several ways to enforce the
uniqueness of the primary key:
– Using primary key constraint when table is defined.
– Using the sp_primarykey stored procedure to define the
primary key.
– Create a unique index on the primary key column(s).
Surrogate Key
• The easiest way to create a primary key is
to create a special column called an identity
column, also known as an auto-number or
sequence column.
• This type of the primary key is known as a
surrogate key.
Intelligent key
• If there is values of column or group of
columns guaranteed to be unique then you
have found a candidate key.
• You will ultimately define a primary key by
choosing one of these candidate keys
( intelligent key ).
Primary Key (PK) &
Alternative Key(AK)
• SQL server only allows one primary key to
be defined per table , even thought there
may be several valid candidate
keys(alternative keys).
• SQL server then enforce uniqueness on the
alternative keys as well as the primary keys.
Column Constraints
• You may specify additional constraints on
columns when designing your database to build
basic business requirements into your data
model.
• There are 3 fundamental types of column
constraints used in database design:
– Not Null (NN)
– No duplicate (ND)
– No Change (NC)
Column Constraints
• These constraints may be combined , as in case of
a primary key , which is usually marked as NN
and ND.
• The not null concept allows the database designer
to require the entry of value in particular column.
SQL server implements this option by NULL or
NOT NULL keywords.
• The no duplicates concept specifies that the values
in a column must be unique.SQL implements this
concept in a number of ways , including a primary
key constraint , a unique index.
Column Constraints
• The no change concept allows the database
designer to prohibit changes to the values in a
column.
• This concept is used manly for primary key
because primary key values are used to create
relationships between tables , and changes to
primary key could result in referential integrity
violation.SQL server implements this concept
using reference constraints or triggers.
Column Constraints
• You also may assign security permissions to
columns so that only certain users of database may
make changes to values in those columns.
• SQL server also enables you to build custom
constraints on columns to enforce more complex
rules.
Non-Decomposable Columns
• One Important consideration when implementing
attributes in table involves decomposing an
attributes into several columns.
• The address attribute should be decomposed into
columns for address , city , state.
• Storing values in columns that cannot be
decomposed further has several advantages.
Advantage Non-Decomposable
Columns
• Columns are easier to update.
• Columns are easier to query.
• Data integrity is more efficient because
each column may have its own data type
and constrains.
Implementing Relationships
• In SQL Server Relationships between tables
they are not represented by any particular
object in database.
• Instead , relationships are created by
logically linking the primary key column(s)
of the parent table with the foreign key
Column(s) in child table.
Primary Keys & Foreign keys
• Understanding how primary keys and foreign keys
relate to each other is vital to understanding
relationships in SQL server.
• When a relationship is created between tables the
parent table in the relationship contributes its
primary key to the child table , where it is known as
a foreign key.
• SQL Server uses primary key and foreign key
values to join tables when querying information
from more than one table.
One-to-One & One-to-many

• If the foreign key is one of the columns in the


child table’s primary key , then the relation is
known as identifying relationship.
• In this case , the child entity is known as
dependent entity. And foreign keys in
identifying relationships cannot contain null
value.
One-to-One & One-to-many

• If the foreign key is not part of the child


table’s primary key , then the relation is known
as non-identifying relationship.
• In this case , the child entity is known as
independent entity. And foreign keys in non-
identifying relationships may be marked as not
null(nn) value.
Many-to-Many
• In data modeling , the entity used in building
a many-to-many relationship is called
associative entity, but many database
designers simply refer to it as join table.
• To implement a many-to-many relationship ,
define the associative entity in the data
model and create one-to-many relationship
from each of the original entities to
associative entity.
Recursive Relationships
• Is implemented just like normal one-to-one
or one-to-many relation, except that the
same table acts as both the parent and the
child in the relationship.
• In this case the foreign key is renamed , this
is known as rolenaming.
Referential integrity
• Referential integrity is a way of ensuring
that the primary key and the foreign key
values used to create a relationship never
get out of sync, And the child entity never
to be orphaned.
Insert Problem
• If a row is inserted into child table that has a
foreign key values that does not match any
primary key in the parent table, then the
child row will be an orphan. Typically , the
insert is restricted.
Delete Problem
• If a row is deleted from the parent table ,
then any row in the child table that refer to
the parent row are orphaned.Typically , the
delete cascades or is restricted.
Update Problem
• If the primary key value of the parent table
is changed , then any row in the child table
the refer to the parent are orphaned.
Typically the update may be cascaded to the
child table, the update may be restricted , or
the child’s foreign key may be set to null.
Implementing Referential
integrity
• RI is maintained by using by using triggers
or declarative referential integrity (DRI)
constraints.
• DRI constraints can only restrict insert,
delete, update.
• Triggers are more flexible and implement a
restriction , a cascade , or set null .
Normalization
• Normalization is a technique of spreading data
across multiple tables so that relevant data
information is kept together based on certain
guidelines.
• Normalization results in controlled redundancy
of data and therefore provide a good balance
between disk space usage and performance.
• There are 5 rules on normalization called
normal forms , but the first 3 are the most
widely used.
Normalization Advantages

• Dependencies between data are identified.


• Redundant data ( and all of the problem
associated with it ) is minimized.
• The data model is flexible and easier to
maintain.
First Normal Form (1NF)
• Eliminate repeating groups of attributes in
an entity.
Second Normal Form (2NF)
• Eliminate Partial key dependencies ( each
attribute in an entity must depend on
whole key , not just part of it ).
Third Normal Form (3NF)
• Eliminate redundant information by
eliminating interdependencies between
non-key attributes.
Case Study
Employee working on various
projects
• Employee_project Table
– Emp_id , emp_name , emp_sex , emp_salary ,
emp_bonus , emp_gros_salary, proj_id ,
proj_name , proj_start , proj_end ,
emp_proj_start
First Normal Form (1NF)
• Employee table:
– Emp_id , emp_name , emp_sex , emp_salary ,
emp_bonus , emp_gros_salary
• Employee_project Table:
– Emp_id , proj_id , proj_name , proj_start ,
proj_end , emp_proj_start
Second Normal Form (2NF)
• Employee table:
– Emp_id , emp_name , emp_sex , emp_salary ,
emp_bonus , emp_gros_salary
• Employee_project Table:
– Emp_id , proj_id , emp_proj_start
• Project Table:
– proj_id ,proj_name , proj_start , proj_end
Third Normal Form (3NF)
• Employee table:
– Emp_id , emp_name , emp_sex , emp_salary ,
emp_bonus
• Employee_project Table:
– Emp_id , proj_id , emp_proj_start
• Project Table:
– proj_id ,proj_name , proj_start , proj_end
Denormalizing a Database
• Joins are required across tables to retrieve
information that resides in various tables.
• Various method can be employed to
denormalize a database table.
– Horizontal partitioning ( selected records )
– vertical partitioning ( selected fields )
– Adding redundant and computed columns
( calculated field ).

You might also like