3. • Conceptual database design
• Logical database design
• Physical database design
Recap of Phases of Database Design - 2
Activity: give a brief definition of the purposes of each of
these phases and the difference between them.
4. Conceptual Database Design
Conceptual database design involves understanding the data in an
enterprise and modeling it without regard for any physical
considerations or particular data models.
5. Logical Database Design
Logical database design involves designing a model of the data in an
enterprise that is independent of a particular DBMS but does take
account of the chosen data model.
6. Physical Database Design
Physical database design involves producing a design that describes
the base relations and takes into account file organization, indexes,
integrity constraints and security measures.
7. The Purpose of Physical Design
Translates logical database structures (entities, attributes,
relationships, and constraints) into a physical design.
The physical design should be suitable for implementation by the
chosen DBMS
8. Knowledge needed of Chosen DBMS
How to create base tables
Does the system support the definition of primary, foreign keys and
alternate keys
Does the system support definition of required data (i.e. Definition of
columns as NOT NULL)
Does the system support domains
Does the system system support relational integrity rules
Does the system support the definition of business rules
9. Choosing a Database Product
Requirements – what suits the business
Budget
Compatibility with existing technology within organisaton
Support available
Strength of vendor
10. Step 3
3.1 Design Base Tables (covered in this lecture)
3.2 Design representations of derived data (covered in this unit)
3.3 Design remaining business rules (covered in Unit (Unit 8).
11. Step 4
4.1 Analyse transactions (covered in Unit 9)
4.2 Choose file organisations (covered in Unit 9)
4.3 Choose indexes (covered in Unit 8)
12. Steps 5 to 8
Step 5 Design User Views (Covered in Unit 8 and 9)
Step 6 Design security mechanisms (Covered in Unit 8)
Step 7 Consider the introduction of controlled redundancy (de-
normalisation)(Covered in Unit 9)
Step 8 Monitor and tune the operational system (Not covered)
13. Design Base Tables
Collate the information about tables gathered during the logical design stage.
Table: For each column:
Name of Table Domain consisting of data type,
length and any constraints
List of Columns Optional default value
Primary Keys and Foreign Keys Can the column be null?
Referential Integrity constraints Is the column derived? (to be
discussed later)
14. The Entity Relationship Diagram for the Art Supply Shop
Customer CustomerItem Item
Supplier
1 1
1
0...*
0...* 0...*
Outcome of Logical Design
15. Customer CustomerItem Item
Supplier
1 1
1
0...*
0...* 0...*
Initial table structures for ‘Art Suppliers’ database.
Customer (CustomerID, CustomerName, Street, Postcode)
Primary Key CustomerID
CustomerItem or Order
(CustomerID, ItemID, Date, Time, Quanitity)
Primary Key CustomerID, ItemID, Date
Item
(ItemID, ItemName, Price, SupplierID)
Primary Key (ItemID
Supplier
(SupplierID, SupplierName)
Primary Key SupplierID
Tables at the outcome of
Logical design
16. The physical design of the Customer Table using DBDL
Domain CustomerIDS fixed length character string length 5
Domain Street_names variable length character string maximum length 20
Domain Post_codes fixed length character string lenght 8
Customer
( CustomerID CustomerIDS NOT NULL
CustomerName Char 30 NOT NULL
Street Street_names NOT NULL
PostCode Post_codes NOT
NULL)
Primary Key CustomerID
17. The physical design of the Customer Table using DBDL
Domain CustomerIDS fixed length character string length 5
Domain Street_names variable length character string maximum
length 20
Domain Post_codes fixed length character string lenght 8
Customer
( CustomerID CustomerIDS NOT NULL
CustomerName Char 30 NOT NULL
Street Street_names NOT NULL
PostCode Post_codes NOT NULL)
Primary Key CustomerID
Activity: write the SQL to implement this table.
18. Data Definition - 1
Create table customers
(CustomerID char not null,
CustomerName varchar(20) not null,
Street char(20) not null,
PostCode char(8) not null,
primary key CustomerID);
19. Data Definition - 2
Create table customers
(CustomerID char not null,
CustomerName varchar(20) not null,
Street char(20) not null,
PostCode char(8) not null,
primary key CustomerID);
The table name
Here the columns are defined.
The datatypes
are specfied along with the
length in brackets.
If a columns is specified as
NOT NULL then it
is mandatory and must be
populated when a
new row is created.
20. CASE(Computer Aided Software
Engineering) Tools
Has a repository to store definitions of structures like entities and attributes
SQL scripts can be generated from definitions
Developers still need to be able to write their own SQL!
21. CASE Tools Functionality
CASE tools can serve many functions in database design, including:
Collecting and analyzing data
Designing a data model
Feasibility analysis
Requirements definition
Implementing the database
Prototyping
Data conversion
Generating application code
Generating reports
Programming and testing
Maintenance
22. CASE Tools Advantages
CASE tools can provide many advantages when used in database
design, including:
Improved productivity in development
Improved quality through automated checking
Automatic preparation and update of documentation
Encouragement of prototyping and incremental development
Automatic preparation of program code from requirements definition
Reduced maintenance systems
23. CASE Tools Disadvantages
However, there are also some disadvantages to using the tools in
database design, including:
Cost increase
Need for specialized training
Limitations in flexibility of documentation
Inadequate standardization
Slow implementation
Unrealistic expectations
24. CASE Tools
There are three different types of CASE tools: upper, lower, and
integrated. Upper CASE tools focus on concept-level products and
tend to ignore design, lower CASE tools concentrate on details of
design such as physical design and testing, and integrated CASE tools
combine the two to support the entire development.
The most useful type in database design is integrated CASE tools.
Some of the common packages on the market today include:
IEF (Information Engineering Facility)
IEW (Information Engineering Workbench)
Oracle Designer
25. Primary Key
Create table customers
(CustomerID char not null,
CustomerName varchar(20)
not null,
Street char(20) not null,
PostCode char(8) not null,
primary key CustomerID);
Primary Key
defined in
different
places
Create table customers
(CustomerID char not null
primary key,
CustomerName varchar(20)
not null,
Street char(20) not null,
PostCode char(8) not null);
34. Adding a Foreign Key Later
ALTER TABLE ORDERS
ADD FOREIGN KEY (customer_id) REFERENCES
CUSTOMER(customer_id);
35. Example of Adding a Column using
Alter Table
ALTER TABLE job_type
ADD salary FLOAT;
36. Modifying Tables using SQL
Add an extra column
Drop a column from a table
Modify the maximum length of the table
Add a new constraint
Drop a constraint
Set a default for a column
Drop a default for a column
37. Example of Adding a Column using
Alter Table
ALTER TABLE job_type
ADD salary FLOAT;
38. What is Derived Data?
A column whose value can be found be examining the values of other
columns
alter table student add age as (DATEDIFF(YY,DOB,GETDATE()))
alter table item add price_peritem as
(quantity*item_price)
39. Topic Room NoOfStudents
Chemistry 87 30
Mathematics 99 12
English 32 23
Computing 55 30
What is a derived column here?
Where might it be derived from?
Table: AcademicClasses
40. Representation of Derived Data
Not always represented in the data model
Represented when there is a danger of losing the information
Derived attributes shown with a ‘/’ in front
44. What Sort of Documents/ Applications will
use Derived Fields? - 1
Activity: Brainstorm – write down as many documents or applications
that might use derived fields in a business.
45. What Sort of Documents/ Applications will
use Derived Fields? - 2
Activity: Brainstorm – write down as many documents or applications
that might use derived fields in a business.
Receipts
Invoices
Order Forms
46. Using Derived Data to think about a Data
Model
Remember data modelling is about semantics, the meaning of the
data.
47. Customer Order Sheet for an Art Supplier
Customer Number: 37
Customer Name: Jagpal Singh
Customer Type Code: RC
Customer Type Description: Retail Customer
Item Number Item Name Supplier ID Price Supplier Name Quantity
099 Basic Paint Set S1 £3 Smith and Co 1
0100 Sable Brush Set S2 £3.50 Acro 1
0101 Extended Colour
Set
S1 £3.75 Smith and Co 3
098 Metallic Paint Set S1 £3.99 Smith and Co 1
078 Mixed Brush Set S2 £3.99 Acro 2
Original Customer Order Sheet for
Art Supply Shop
48. Customer Order Sheet for an Art Supplier
Customer Number: 37
Customer Name: Jagpal Singh
Customer Type Code: RC
Customer Type Description: Retail Customer
Item Number Item Name Supplier ID Price Supplier Name Quantity
099 Basic Paint Set S1 £3 Smith and Co 1
0100 Sable Brush Set S2 £3.50 Acro 1
0101 Extended Colour
Set
S1 £3.75 Smith and Co 3
098 Metallic Paint Set S1 £3.99 Smith and Co 1
078 Mixed Brush Set S2 £3.99 Acro 2
Total: 29.72
£3.00
£3.50
£11.25
£3.99
£7.98
Item
Total
Order ID: 4343
Adding a Total for Each Item and
the Order Itself
56. Overheads of Derived Data
Additional storage of extra attribute
New calculation every time a value in the source field is changed
Possibility of data becoming inconsistant
57. Calculating at Run Time
The Order Form and similar documents such as invoices and receipts
would be created as applications
The totals calculated as they are needed
Totals not stored in database: no derived fields
61. Aggregate Functions that can be used to
Derive Data
Count – returns number of values in a column
Sum – returns the sum total of values of a column
Avg – returns the mean average of values in column
Min – returns the lowest value in a column
Max – returns the highest value in a column
62. Standard SQL for Derived Column in
Create Table Statement
CREATE TABLE ProductsStock
(
...
Product_itemID INTEGER NOT NULL,
list_price DECIMAL(7,2) NOT NULL,
Quantity INTEGER NOT NULL,
stock_value GENERATED ALWAYS AS (Quanity *
list_price),
...
)
63. Example of Insert Statement with Derived
Data
INSERT INTO employee_year_earnings
SELECT employee_id, salary*12
FROM employees;
64. Derived Attribute or Calculation at Run
Time?
How complicated is the calculation to be made?
Does it involve multiple table joins and so could impact performance?
How often would a derived attribute be updated? Could this affect
performance.
65. Other Types of Derived Fields – Non
Numeric
Potentially any type of business rule could be used to derive data
e.g. Items of a particular sort will all be the same colour
Would need to used more complex embedded procedural logic such
as a database trigger or macro
66. PL(Procedural Language)-SQL
PL/SQL is the language to use when writing code that resides in the database.
The purpose of PL/SQL is to combine database language and procedural
programming language.
The basic unit in PL/SQL is called a block, which is made up of three parts: a
declarative part, an executable part, and an exception-building part.
70. Nested Else If
DECLARE @age INT;
SET @age= (select age from person);
IF @age< 25
PRINT ‘Younger’;
ELSE
BEGIN
IF @age< 50
PRINT ‘Print Something';
ELSE
PRINT ‘Elder';
END;
71. Loop/while
DECLARE @site_value INT;
SET @site_value = 0;
WHILE @site_value <= 10
BEGIN
PRINT 'Inside WHILE LOOP on TechOnTheNet.com';
SET @site_value = @site_value + 1;
END;
PRINT 'Done WHILE LOOP on TechOnTheNet.com';
GO
72. Break and Continue
DECLARE @site_value INT;
SET @site_value = 0;
WHILE @site_value <= 10
BEGIN
IF @site_value = 2
BREAK;
ELSE
PRINT 'Inside WHILE LOOP on TechOnTheNet.com';
SET @site_value = @site_value + 1;
END;
75. Stored Procedure
A stored procedure is a set of Structured Query
Language (SQL) statements with an assigned name,
which are stored in a relational database management
system as a group, so it can be reused and shared by
multiple programs.
76. Stored Procedure
CREATE proc get_all_courses AS
SELECT * FROM courses;
Execute get_all_courses;
-- Creating Stored Procedure with parameters
CREATE proc get_course_name @course_id INT AS
SELECT course_name FROM courses WHERE course_id = @course_id;
EXEC get_course_name 1;
77. Stored Procedure
-- Creating Stored Procedure with 2 parameters
CREATE proc get_course_name @course_id INT , @course_name varchar(20)
as
SELECT course_name FROM courses WHERE course_id = @course_id and
course_name=@course_name;
EXEC get_course_name 1,’Computing Science’;
78. Trigger
Triggers are a specialized type of stored procedure that can be written to act on a table action
such as an INSERT, UPDATE, or DELETE
79. Trigger
CREATE TABLE job_log (
job_log_id INT PRIMARY KEY identity,
job_action TEXT,
action_date_time DATETIME
);
Create table jobs
(
Job_id int primary key identity,
Job_type varchar(30)
)
CREATE TRIGGER job_trigger ON jobs FOR INSERT AS
PRINT 'Data Inserted’;
--Now insert single row of data and see the result.
80. Trigger
CREATE TRIGGER job_insert_trigger ON jobs FOR INSERT AS
PRINT 'Data Inserted’;
INSERT INTO job_log (job_action, action_date_time) VALUES ('Data is Inserted.',
CURRENT_TIMESTAMP);
81. System table use in Trigger
INSERTED: This table is use to store last inserted row.
DELETED: This table is use to store last deleted row.
82. System table use in Trigger
ALTER TRIGGER job_delete_trigger ON jobs FOR DELETE AS
DECLARE @job_id INT = 0;
SELECT @job_id = job_id FROM deleted;
print @job_id;
IF @job_id = ''
print 'No row deleted.';
ELSE
INSERT INTO job_log (job_action, action_date_time) VALUES ('Data is being deleted.',CURRENT_TIMESTAMP);
CREATE TRIGGER job_last_insert_row ON jobs FOR INSERT AS
SELECT * FROM inserted
84. Learning Outcomes
By the end of this unit students will be able to:
Understand the concept of derived data
Design a representation of derived data
Recognise the tradeoffs between different ways of implementing derived data
Did we meet them?
85. References - 1
Connolly, Thomas M., and Begg, Carolyn E., Database Systems: A
Practical Approach to Design and Implementation Addision-Wesley,
Fourth Edition 2005 Chapter 17
Connolly, Thomas and Begg, Carolyn Database Solutions: A step-by-
step guide to building database Addison-Wesley 2nd Edition 2004
Chapter 12
86. References - 2
The Derivation in the Data Model at
http://wiki.webratio.com/index.php/The_derivation_in_the_data_mod
el Retrieved 7th June 2011
SQL derived data
http://forums.mysql.com/read.php?10,361286,361286 Retrieved 20th
July 2011
87. References 3
Connolly, Thomas and Begg, Carolyn Database Solutions: A step-by-step guide
to building database Addison-Wesley 2nd Edition 2004 Chapter 12
Choosing a database
http://databases.about.com/od/administration/a/choosing_a_db.htm
Accessed 1st June 2011
Examples of different vendor’s SQL syntax http://www.1keydata.com/sql/sql-
foreign-key.html
Accessed 5th June 2011