chapter 3 the relational model & the relational constraint · the alter table command finally,...
TRANSCRIPT
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Chapter 3
The Relational Model & The
Relational Constraint
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Relational Model Concepts
Represents data as a collection of relations
Table of values
Row
• Represents a collection of related data values
• Fact that typically corresponds to a real-world entity
or relationship
• Tuple
Table name and column names
• Interpret the meaning of the values in each row
attribute
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Attributes, Tuples, and Relations
Relation schema R
Denoted by R(A1, A2, ...,An)
Made up of a relation name R and a list of
attributes, A1, A2, ..., An
Attribute Ai
Name of a role played by some domain D in
the relation schema R
Degree (or arity) of a relation
Number of attributes n of its relation schema
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Attributes, Tuples, and Relations
Relation (or relation state)
Set of n-tuples r = {t1, t2, ..., tm}
Each n-tuple t
• Ordered list of n values t =<v1, v2, ..., vn
• Each value vi, 1 ≤ i ≤ n, is an element of dom(Ai) or
is a special NULL value
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Characteristics of Relations
Ordering of tuples in a relation
Relation defined as a set of tuples
Elements have no order among them
Ordering of values within a tuple and an
alternative definition of a relation
Order of attributes and values is not that
important
As long as correspondence between attributes
and values maintained
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Characteristics of Relations
Values and NULLs in tuples
Each value in a tuple is atomic
Flat relational model
• Composite and multivalued attributes not allowed
• First normal form assumption
Multivalued attributes
• Must be represented by separate relations
Composite attributes
• Represented only by simple component attributes in
basic relational model
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Characteristics of Relations
NULL values
Represent the values of attributes that may be
unknown or may not apply to a tuple
Meanings for NULL values
• Value unknown
• Value exists but is not available
• Attribute does not apply to this tuple (also known as
value undefined)
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Relational Model Notation
Name of a relation schema: STUDENT
Indicates the current set of tuples in that
relation
Notation: STUDENT(Name, Ssn, ...)
Refers only to relation schema
Attribute A can be qualified with the relation
name R to which it belongs
Using the dot notation R.A
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Relational Model Constraints
Constraints
Restrictions on the actual values in a database
state
Derived from the rules in the miniworld that the
database represents
Inherent model-based constraints or
implicit constraints
Inherent in the data model
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Relational Model Constraints
Schema-based constraints or explicit
constraints
Can be directly expressed in schemas of the
data model
Application-based or semantic
constraints or business rules
Cannot be directly expressed in schemas
Expressed and enforced by application
program
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Domain Constraints
Typically include:
Numeric data types for integers and real
numbers
Characters
Booleans
Fixed-length strings
Variable-length strings
Date, time, timestamp
Money
Other special data types
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Key Constraints and Constraints
on NULL Values Candidate key
Relation schema may have more than one key
Primary key of the relation
Designated among candidate keys
Underline attribute
Other candidate keys are designated as
unique keys
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Relational Databases and
Relational Database Schemas
Invalid state
Does not obey all the integrity constraints
Valid state
Satisfies all the constraints in the defined set of
integrity constraints IC
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Integrity, Referential Integrity,
and Foreign Keys Entity integrity constraint
No primary key value can be NULL
Referential integrity constraint
Specified between two relations
Maintains consistency among tuples in two
relations
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Integrity, Referential Integrity,
and Foreign Keys Foreign key rules:
The attributes in FK have the same domain(s)
as the primary key attributes PK
All integrity constraints should be specified
on relational database schema
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Other Types of Constraints
Semantic integrity constraints
May have to be specified and enforced on a
relational database
Use triggers and assertions
More common to check for these types of
constraints within the application programs
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Update Operations,
Transactions, and Dealing with
Constraint Violations Operations of the relational model can be
categorized into retrievals and updates
Basic operations that change the states of
relations in the database:
Insert
Delete
Update (or Modify)
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Examples Insert, Update
Insert
INSERT INTO EMPLOYEE (empno,Fname,
Lname, Dno)
VALUES (1,‘Robert’, ‘Hatcher’, 5);
Update
UPDATE EMPLOYEE
SET Salary = Salary * 1.1
WHERE Dno = 5;
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The Insert Operation
Provides a list of attribute values for a new
tuple t that is to be inserted into a relation R
Can violate any of the types of constraints
If an insertion violates one or more
constraints
Default option is to reject the insertion
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The Delete Operation
Can violate only referential integrity
If tuple being deleted is referenced by foreign
keys from other tuples
Restrict
• Reject the deletion
Cascade
• Propagate the deletion by deleting tuples that
reference the tuple that is being deleted
Set null or set default
• Modify the referencing attribute values that cause
the violation
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The Update Operation
Necessary to specify a condition on
attributes of relation
Select the tuple (or tuples) to be modified
If attribute not part of a primary key nor of a
foreign key
Usually causes no problems
Updating a primary/foreign key
Similar issues as with Insert/Delete
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Specifying Constraints and Default
Values Because SQL allows NULLs as attribute values, a
constraint NOT NULL may be specified if NULL is not
permitted for a particular attribute.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Specifying Constraints and Default
Values It is also possible to define a default value for an attribute
by appending the clause DEFAULT <value> to an
attribute definition.
The default value is included in any new tuple if an explicit
value is not provided for that attribute.
If no default clause is specified, the default default value is
NULL.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Schema and Catalog Concepts in
SQL A schema is created via the CREATE SCHEMA
statement, which can include all the schema elements’
definitions.
Alternatively, the schema can be assigned a name and
authorization identifier, and the elements can be defined
later.
CREATE DATABASE COMPANY AUTHORIZATION JSMITH;
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The CREATE TABLE Command
The CREATE TABLE command is used to specify a new
relation by giving it a name and specifying its attributes
and constraints.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Specifying Constraints and Default
Values In SQL2, the schema designer can specify the action to
be taken if a referential integrity constraint is violated upon
deletion of a referenced tuple or upon modification of a
referenced primary key value, by attaching a referential
triggered action clause to any foreign key constraint.
The options include SET NULL, CASCADE, and SET
DEFAULT.
An option must be qualified with either ON DELETE or ON
UPDATE.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The DROP DATABASE and DROP
TABLE Commands If a whole schema is not needed any more, the
DROP DATABASE command can be used.
There are two drop behavior options: CASCADE and RESTRICT.
For example, to remove the COMPANY database schema and all its tables, domains, and other elements, the CASCADE option is used as follows:
• DROP DATABASE COMPANY CASCADE;
If the RESTRICT option is chosen in place of CASCADE, the schema is dropped only if it has no elements in it; otherwise, the DROP command will not be executed.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The DROP DATABASE and DROP
TABLE Commands If a base relation within a schema is not needed any
longer, the relation and its definition can be deleted by
using the DROP TABLE command.
For example, we can get rid of the DEPENDENT relation
by issuing the command:
DROP TABLE DEPENDENT CASCADE;
If the RESTRICT option is chosen instead of CASCADE, a
table is dropped only if it is not referenced in any
constraints or views.
With the CASCADE option, all such constraints and views
that reference the table are dropped automatically from
the schema, along with the table itself.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The ALTER TABLE Command
The definition of a base table can be changed by using
the ALTER TABLE command, which is a schema
evolution command.
The possible alter table actions include adding or
dropping a column (attribute), changing a column
definition, and adding or dropping table constraints.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The ALTER TABLE Command
For example, to add an attribute for keeping track of jobs
of employees to the EMPLOYEE base relations in the
COMPANY schema, we can use the command:
ALTER TABLE COMPANY.EMPLOYEE ADD JOB
VARCHAR(12);
We must still enter a value for the new attribute JOB for
each individual EMPLOYEE tuple.
This can be done either by specifying a default clause or by using
the UPDATE command.
If no default clause is specified, the new attribute will have NULLs
in all the tuples of the relation immediately after the command is
executed; hence, the NOT NULL constraint is not allowed in this
case.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The ALTER TABLE Command
To drop a column, we must choose either CASCADE or
RESTRICT for drop behavior.
If CASCADE is chosen, all constraints and views that
reference the column are dropped automatically from the
schema, along with the column.
If RESTRICT is chosen, the command is successful only if
no views or constraints reference the column.
For example, the following command removes the
attribute ADDRESS from the EMPLOYEE base table:
ALTER TABLE COMPANY.EMPLOYEE DROP ADDRESS
CASCADE;
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The ALTER TABLE Command
It is also possible to alter a column definition by dropping
an existing default clause or by defining a new default
clause.
The following examples illustrate this clause:
ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN
DROP DEFAULT;
ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN
SET DEFAULT "333445555";
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The ALTER TABLE Command
Finally, one can change the constraints specified on a
table by adding or dropping a constraint.
To be dropped, a constraint must have been given a
name when it was specified.
ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT
EMPSUPERFK CASCADE;
Adding a new constraint is specified by using the ADD
keyword followed by the new constraint.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The Transaction Concept
Transaction
Executing program
Includes some database operations
Must leave the database in a valid or
consistent state
Online transaction processing (OLTP)
systems
Execute transactions at rates that reach
several hundred per second