3. Activity – Recap - 1
What is the Entity integrity rule?
What is referential integrity?
4. Activity – Recap - 2
What is the Entity integrity rule?
The primary key of an entity cannot contain nulls.
What is referential integrity?
If a foreign key contains a value then that value
must refer to an existing tuple in the source table
5. The Art Suppliers data model - where will
there need
to be constraints?
Order OrderItem Item
Supplier
1 1
1
0...*
0...* 0...*
Customer
1
0...*
6. Source or Parent Table
Create table customers
(CustomerNo integer(5) not null,
first_name varchar(30),
last_name varchar(30),
primary key emp_no);
7. Referencing or Child Table
Create table Orders
(OrderID integer(5) not null,
CustomerID integer(5) not null,
OrderDate datetime,
primary key OrderID,
foreign key (CustomerID) references Customer
(CustomerID);
8. Referential Integrity Constraint – Another
Example
Create table workers
(emp_no integer(5) not null,
first_name varchar(30),
last_name varchar(30),
job_title varchar(30),
age integer(3),
dept_no integer(5),
primary key emp_no,
foreign key (dept_no) references Departments (dept_no)o)
9. Propagation Constraint
What happens if we delete a Item from our Art Supply database?
There are lots of OrderItem records that reference it. What happens to
them?
11. Table with Propagation Constraint
with no action and cascade
Create Table Item
(ItemID integer NOT NULL,
SupplierID integer NOT NULL,
Price float,
Primary Key (ItemID),
Foreign Key (SupplierID) REFERENCES Supplier(SupplierID)
On delete no action
On update cascade);
13. Table with Propagation Constraint
with no action and cascade
Create Table Item
(ItemID integer NOT NULL,
SupplierID integer NOT NULL,
Price float,
Primary Key (ItemID),
Foreign Key (SupplierID) REFERENCES Supplier(SupplierID)
On delete set Null
On update set Null);
14. Domain Constraints
Product Type could be enforced as...
a check constraint
separate domain using Create Domain statement
as a foreign key to another table
15. Check Constraint
Create Table ProductType
(ProductTypeID integer NOT NULL,
ProductTypeName varchar (30) NOT NULL,
ProductTypeColour varchar (20),
Primary Key (ProductTypeID)
Check (ProductTypeColour in ‘Red’,’Blue’,’Green’));
16. As a Separate Domain
Create Domain ProductTypeColour As varchar(20)
Default ‘Red’
Check (Value in (‘Red’,’Blue’,’Green’));
The table’ProductType’ will set the
ProductTypeColour attribute as this domain
ProductTypeColour
Create Table ProductType
(ProductTypeID integer NOT NULL,
ProductTypeName varchar (30) NOT NULL,
ProductTypeColour BoatType,
Primary Key (ProductTypeID));
17. As a Separate Table
Create Table ProductTypeColour(
(ProductTypeColourCode Varchar(3),
ProductTypeColourDescription Varchar(20)
Primary Key (ProductTypeColourCode));
With the corresponding Foreign Key in Boat
Create Table ProductType
(ProductTypeID integer NOT NULL,
ProductTypeName varchar (30) NOT NULL,
ProductTypeColour varchar (3),
Primary Key (ProductTypeID)
Foreign Key (ProductTypeColourCode)
References ProductTypeColour (ProductTypeColourCode));
18. Business Rules Enforced by Constraints
Business rules are derived from requirements analysis and
documented in logical design
They depend on the operations of the ‘real world’ business
19. Business Rule: No order may contain
more than 10 OrderItems
Order OrderItem Item
Supplier
1 1
1
0...*
0...10 0...*
Customer
1
0...*
20. Table Constraints
Create Table Orders
(
(OrderID integer NOT NULL,
CustomerID integer NOT NULL,
OrderDate datetime NOT NULL
Constraint MaximumOrderItems
Check(Not Exists(Select OrderID
From OrderItems
Group By OrderID
Having Count(*) >10)),
Primary Key (OrderID)
Foreign Key (CustomerID) REFERENCES Customer(CustomerID)
On delete no action
On update cascade);
21. Table Constraints – Another Example
Create Table Rental
(
(BoatID integer NOT NULL,
CustomerID integer NOT NULL,
RentalStartDate datetime NOT NULL,
RentalEndDate datetime NOT NULL
Constraint MaximumRentals
Check(Not Exists(Select BoatID
From Rentals
Group By BoatID
Having Count(*) >10)),
Primary Key (BoatID, CustomerID, RentalStartDate)
Foreign Key (CustomerID) REFERENCES Customer(CustomerID),
Foreign Key (BoatID) REFERENCES Boat (BoatID)
On delete no action
On update cascade);
23. View of
selected rows
or columns of
these tables
Improving Performance with the use of
Views
Table 1
Table 2
Table 3
Query
24. Example of Creating a View
Create View OrderSummary as
(Select O.OrderID, O.OrderDate, I.ItemName, I.Price, OI.Quantity, O.Total
From Orders O, OrderItems OI, Items I
Where O.OrderID = OrderItems.OrderID
And I.ItemID = OI.ItemID);
What will be the purpose of this view?
27. Primary Index
Built around a key field that is used for ordering. A unique value for
every entry in the index.
28. Secondary Index
Defined on a non-ordering field
May not contain unique values
Improves the performance of queries that use columns other than
primary key
29. Use of Secondary Indexes
Mechanism for specifying additional key columns.
For example Customer would be searched often on Customer Name as
well as the primary key and so a secondary index could be used on it.
30. Examples of Creating Indexes in SQL
To create primary index
CREATE UNIQUE INDEX CustomerIDIndex
ON Customer(CustomerID)
To create clustering index
CREATE INDEX OrderDateIndex
ON Order(OrderDate) CLUSTER( for Oracle)
To create secondary index use CREATE INDEX syntax without specifying
unique.
31. Clustered Indexes
A clustered index alters the way that the rows are physically stored.
When you create a clustered index on a column the database server
sorts the table’s rows by that column(s).
It is like a dictionary, where all words are sorted in an alphabetical order.
(**) Note, that only one clustered index can be created per table i.e.
Primarary Key. It alters the way the table is physically stored, it couldn’t
be otherwise.
32. Non-Clustered Indexes
It creates a completely different object within the table, that contains the
column(s) selected for indexing and a pointer back to the table’s rows
containing the data.
It is like an index in the last pages of a book. All keywords are sorted and
contain a reference back to the appropriate page number. A non-
clustered index on the computer_id column, in the previous example,
would look like the table below:
34. Overheads of Use of Indexes
A record is added to the index table every time a new record is added
to the table where there is a secondary index.
Updating the indexed record means an update to the index table
More disk space need to store index tables
Impact on performance if indexes are all consulted every time a query
is run
35. Sequences
Sequential numbers that can be used
to increment an ID number
Equivalent to an auto-number(Identity)
in MS-Sql Server.
36. De-normalisation
We have created a database following all the rules of normalisation...
Now we can break them to make the database work quicker and
perform better...
37. Roles in a System
Not every user is the same
The will need to access different parts of the system and access it in
different ways
38. SQL Facilities to Manage Roles
Grant – gives access to an object (such as a table) to a particular role
or user in the database system.
Revoke – removes access to an object (such as a table) to a particular
role or user in the database system
42. Past Questions Revision
Why does physical design require an understanding of the chosen
DBMS product?
In physical design the database is designed with the target
Database Management System (DBMS) in mind. Therefore it
entails knowledge of the chosen DBMS whether it is Oracle, MySQL,
SQL Server etc. The particular features of the chosen system might
specify how structures are set up and stored physically new
features get added and older features become obsolete.
43. Past Questions Revision
Define the types of information that should be recorded for each table and column at the physical design stage.
For each table:
• Table name
• List of columns
• Primary key and foreign keys. Any alternate keys
• Referential integrity constraints for each of the foreign keys that have been identified
For each column:
• The domain of that column including the data-type, length and any additional
constraints that apply to that column
• A default value for the column
• Whether the column is derived and if it is derived then how it is computed
• Whether or not the column can contain null values
44. Past Questions Revision
Explain the concept of derived data.:
Derived data is defined as a column whose value is derived
from the value of one or more other columns in the database.
These might be columns within the same table or columns
from one or more other tables.
45. Past Questions Revision
How can an organization's derived data make the process of
normalization and data design.
In an organization there is usually a lot of derived data such
as totals for the number of products ordered.
When looking at documents during normalization we
sometimes come across derived data so we must make sure
that we map its derivation rather than simply duplicating it..
With regard to design there are similar problems to those in
normalization. Also the derived columns will need to be
modeled. Decisions as to how they are modeled need to take
into account aspects like performance.
46. Past Questions Revision
What role can SQL views play with regard to derived data?
Views are ways of storing queries across tables They can be
used to store the results of calculations such as those used
with aggregate functions.
47. Past Questions Revision
Explain with an example the function of database triggers
Database triggers are pieces of procedural logic that are
attached to database objects that operate (‘fire’) upon some
event happening such as a new row being inserted into the
database.
48. Past Questions Revision
Discuss the ways in which a business rule can be enforced in a
database management system.
Built into the structure of the database through the normal
constraints e.g. a business rule like ‘Every person must have an
address’ would mean that address fields are specified as not null.
A rule like ‘A student must be enrolled on a course’ would be a
non-null referential integrity constraint (foreign key).
More complex business rules can be enforced with check
constraints. Applications can also be used to enforce business
rules through the logic of whatever programming language they are
built in.
Database triggers can also be used to enforce business rules.
49. Past Questions Revision
Explain what an index is in a database system.
Indexes in databases operate in much the same way as an
index in a book. They are separate files that contain location
pointers to the actual rows in the database.
Instead of a topic title like in a book index they usually
contain one or more attributes that belong to a row.
They could, for example, contain just customer numbers from
a customer table, rather than all the data for the customers
as the actual database contains.
50. Past Questions Revision
Identify and describe THREE (3) different types of index.
The different types of indexes available are primary index,
secondary index and clustering index.
A primary index is built around a key field with a unique value
for every entry.
A secondary index is used for specifying additional key
columns.
A clustering index is built around a field which can have many
corresponding rows in the table.
51. Past Questions Revision
What is a sequence in a database system and what is it used
for?
A sequence is a structure that generates a number one after
the other. It is used to populate fields such serial numbers
52. Past Questions Revision
Outline the process of enforcing referential integrity in a
database system.
Foreign keys are identified during the design stage of
development when relationships on an ER diagram are
mapped between foreign keys and their referenced parent key
It is enforced by the use of constraints known as referential
integrity constraints that are created either when the table is
created or later using he alter table command
53. Past Questions Revision
With the use of an example define and explain the purpose of
de-normalisation
De-normalisation is another way of improving performance
De-normalisation involves using replication in a controlled
way.
Attributes that do not belong to a particular table but are
often retrieved with that table in queries are replicated on that
that table.
Editor's Notes
clustered index determines the physical order of the rows in the database
clustered index determines the physical order of the rows in the database
clustered index determines the physical order of the rows in the database