Relational database
-
Upload
michele-brady -
Category
Documents
-
view
20 -
download
0
description
Transcript of Relational database
Introduction to Relational Databases
Informal look to Relational Databases
The relational model is having the three aspects:
� Structural aspect:
� The data in the database is perceived by the user as
tables
� Integrity aspect:� Integrity aspect:
� Those tables satisfy certain integrity constraints
� Manipulative aspect
� The operators are available to the user for manipulating those tables
A formal definition� Relational databases store data in the form of tables
� The rows of a table are called as tuples� The columns of a table are known as attributes� Every attribute has a data type associated with it� Every attribute has a domain which provides the set of possible
values� Tables are called as relations� The table names are called as relational variables (relvar)
A Formal Definition
Domains
� A relation may be regarded as a set of tuples
� A domain has a logical definitions: eg., “phone_number” are the set of 10 digit numbers
� A domain defines posible values of an attribute, may have a data-type or a format defined for itdata-type or a format defined for it
� An attribute is the name of a role played by some domain. Eg.,domain Date may be used to define attributes “Invoice-date” and “Payment-date”
Relation schema
� A relation schema is used to describe a relation� A relation schema R, denoted by R(A1,A2,...,An):
� where R is a relation name and� A1,A2,...,An - a list of attributes
� The degree of a relation is the number of attributes� Instance r of R : a specific “value” or population of R� Instance r of R : a specific “value” or population of R
Characteristics� Ordering of tuples in a relation:
� The tuples are not considered to be ordered, even though they appear to be in the tabular form
� Ordering of attributes:� We will consider the attributes in R(A1,A2,...,An) and the� We will consider the attributes in R(A1,A2,...,An) and the
values in t = <v1,v2,...,vn> to be ordered.
� Values in a tuple:� All values are considered atomic (indivisible).
� A special null is used to represent unknown or inapplicable values
Characteristics
Relational Integrity Constraints
� Constraints are conditions that must hold on all valid relation instances.
� Key Constraints
� Super Key
� Key� Key
� Candidate key
� Entity integrity Constraints
� Primary key
� Referential integrity Constraints
� Foreign key
� Another implicit constraint is the domain constraint Every value in a tuple must be from the domain of its attribute (or it could be null, if allowed for that attribute)
SuperKey
� Superkey of R:
� Is a set of attributes SK of R with the following condition:
� No two tuples in any valid relation state r(R) will have the same value for SK
� That is, for any distinct tuples t1 and t2 in r(R), t1[SK] ≠ � That is, for any distinct tuples t1 and t2 in r(R), t1[SK] ≠ t2[SK]
� This condition must hold in any valid state r(R).
� Every relation has at least one default superkey—the set of all its attributes.
� {Ssn, Name, Age}—is a superkey
Key
� Key: A set of attributes K, whose values uniquely identify a tuple in any instance. And none of the proper subsets of K has this property.
� {ssn} is a key for student relation.
� Can be called as minimal superkey, that is, a superkey from � Can be called as minimal superkey, that is, a superkey from which we cannot remove any attributes and still have the uniqueness constraint can hold.
� The superkey {Ssn, Name, Age}is not a key of STUDENT because removing Name or Age or both from the set still leaves us with a superkey.
� Any superkey formed from a single attribute is called as key.
Candidate Keys� A relation can have more than one key.� Each of the keys is called a candidate key
� Consider a car relation where has two candidate keys: License_number and Engine_serial_number.
Primary Key
� A given relation may have two or more candidate keys
� Exactly one of those keys be chosen as the primary key, the others are then called alternate keys
� The CAR relation with two candidate keys: LicenseNumber and EngineSerialNumberEngineSerialNumber
Primary Key
� We could choose either {ID} or
{First, Last} as the Primary Key.
� ID is more convenient as it is a
single column and we know it will
always be unique (what happens
if another John Smith is added?)
Entity Integrity
� The primary key attributes PK of each relation schema R cannot have null values in any tuple of r(R)
� This is because primary key values are used to identify the individual tuples
� Other attributes of R may be constrained to disallow null, even � Other attributes of R may be constrained to disallow null, even though they are not members of the PK
Foreign keys
� Foreign key is a set of attributes of some relvar R2 whose values are required to match values of some candidate key of some relvar R1.
� Can be specified using two terms
� the referencing relation � the referencing relation
� the referenced relation
� A foreign key is a set of attributes, say FK, of referencing relation whose values are required to match values of candidate key CK of referenced relation.
� The FK and CK are the same data type.
� The FK must appear as a value of CK.
� FK is simple or composite according as CK .
Referential Integrity
� Referential integrity – the database must not contain any unmatched foreign key values.
� The value in the foreign key column FK of the referencing relation can be either:
� a value of an existing primary key PK in the referenced � a value of an existing primary key PK in the referenced relation, or
� a null
� In other words, the constraint says simply:
� If B references A, then A must exist
Referential Integrity
{DID} is a Candidate Key for {DID} is a Foreign Key in Employee
Department Employee
{DID} is a Candidate Key for Department – Each entry has a unique value for DID
{DID} is a Foreign Key in Employee- each Employee’s DID value iseither NULL, or matches an entry inthe Department relation.
FOREIGN KEY <item commalist> REFERENCES <relvar name>
Referential Integrity
{ID} is a Candidate Key for Employee,
and {Manager} is a Foreign Key, which
refers to the same relation – every
tuple’s Manager value is either NULL or
matches an ID value
FOREIGN KEY Manager REFERENCES Employee(ID)
Referential Integrity
� When relations are updated, referential integrity can be violated� When a referenced tuple is updated / deleted, an exception will
be raised� To compensate this, the options are:
� Cascade: Actions cascaded to the matching tuples� Cascade: Actions cascaded to the matching tuples� Restrict: Restricted to the case where there are no matching
tuples� Nullify: Make values NULL� Triggers: Actions defined by the user
Referential Integrity
� What happens if
Marketing’s DID is
changed to 16 in
Department?
�� The entry for Accounts
is deleted from
Department?
Referential Actions – RESTRICT
� RESTRICT stops any action that violates integrity
� You cannot update or delete
Marketing or Accounts
� You can change Personnel
Department
� You can change Personnel
as it is not referenced Employee
Referential Actions – CASCADE
� CASCADE allows the changes made to flow through
� If Marketing’s DID is changed to 16 in Department, then the DIDs for John Smith and Mark Jones also change
Department
also change� If Accounts is deleted then so is
Mary BrownEmployee
Referential Actions – NULLIFY
� NULLIFY sets referenced values to NULL
� If Marketing’s DID changes then John Smith’s and Mark Jones’ DIDs are set to NULL
Department
NULL
� If Accounts is deleted, Mary Brown’s DID becomes NULL
Employee
SQL Facilities
� An SQL candidate key definition takes one of the following:� PRIMARY KEY ( <column name commalist> )� UNIQUE ( <column name commalist> )
� An SQL foreign key definition takes the form:� FOREIGN KEY ( <column name commalist> )� FOREIGN KEY ( <column name commalist> )REFERENCES <base table name> [ (<column name commalist>) ][ ON DELETE <referential action> ][ ON UPDATE <referential action> ]
� Thank you