BIS06 Physical Database Models

Embed Size (px)

Citation preview

Kollaborative Klassroom Template

Business Information Systems
Physical Data Modeling

Mapping Logical to Physical Model

the entity names have been changed to table names, changed attribute names to column names, assigned nulls and not nulls, and datatype to each column.

Comment Definition Foreign Key Relationship Check Rule, Default ValueRuleNon Unique IndexInversion Key Unique Key or Unique ConstraintAlternate KeyPrimary Key ConstraintPrimary Key ColumnAttribute TableEntityRepresents the physical implementation of the model in a database. Represents business information and defines business rules Physical Data Model Logical Data Model

The Physical Data Model

Includes all required tables, columns, relationships, database properties for the physical implementation of databases.

Database performance, indexing strategy, physical storage and denormalization are important parameters of a physical model.

Logical data model is approved by functional team and there-after development of physical data model work gets started.

The transformations from logical model to physical model include imposing database rules, implementation of referential integrity, super types and sub types etc.

DBMS

A database is a collection of organized and structured data, stored in the computer as files. Various data types like numeric, textual, image, multimedia etc., can be managed and maintained more efficiently in a database.

Often used databases (RDBMS) in most of the practical applications are Oracle, Sql Server, Informix, Terradata, DB2 etc.,

Some Oracle Objects

Instance

Schema

Table

Column

Datatype

Primary Key Constraint

Unique Constraint

Check Constraint

Null / Not Null

Index

Sequence

View

Materialized View

Synonym

Procedure

Function

Package

Trigger

Overview of objects

A database instance can have many schemas;

A schema

contains multiple database objects like tables, views

Tables

A set of related data, arranged in the form of rows and columns.

Columns

Also known as Field that provides the structure for organizing the rows and contains the related information.

Datatype : This is set of property associated with a column, which helps to store and identify the type of data and its length.

Constructs for constraints

Null / Not Null

A value that indicates that the column contains should or should not have a valid data

Primary Key Constraint

This is a constraint imposed on the column so that all values in the column should be different from each other. This constraint can be imposed on one column or group of columns. he primary key will be always used as a parent key when adding a referential constraint by connecting it to a child table.

Unique Constraint: Unique + Null Values

Primary Key Constraint: Unique + Not Null Values

Foreign Key Constraint

This is a constraint imposed on the child table. Whatever values are present in the child table, their corresponding values should be present in the parent table. This constraint can be imposed on one column or group of columns and NULL values are allowed in the child table.

Check Constraint

This is a constraint that is imposed to validate the data within some value or range of values. This constraint can be imposed on one column or group of columns.

Constructs for better access

Index

Index is a database object that enables faster retrieval of data. Unique Index, Bitmap Index etc., are the different types of Index.

Views

This is a PSEUDO table that is not stored in the database and it is just a query.

Materialized Views are similar to a view but these are permanently stored in the database and often refreshed. This is used in optimization for the faster data retrieval and is useful in aggregation and summarization of data.

Constructs for automatic processing

Procedure

This is a program that contains set of code, which will carry out a specific action when called by other programs.

Functions

This is a program that contains set of code, which will do a specific action when called by other programs. It will return a single VALUE

Trigger

This is a program that contains set of code for doing some useful action when a record is inserted or deleted or updated in a table.

Raw Data on Employees

For the sample data provided in the table, as a Data Modeler you have to design logical data model, physical data model and generate DDL scripts.

In order to do the above tasks, you need to create the following:

Create 2 tables DEPARTMENT, EMPLOYEE.

Assign correct datatypes to the columns.

Create constraints like primary key, unique key, null, not null, check to the columns.

Assign correct PHYSICAL names for tables and columns.

Select DEPARTMENT as parent table and EMPLOYEE as the child table and connect them.

Employee Table

DEPARTMENT Table

Since dept will be repeated for several records in EMPLOYEE table, you have to design this lookup for saving disk space. Sometimes the detail table may not show all information

For example nuances of department name

Department could have additional information, viz location, manager

You have to create a column Dept Code, which is not present in sample data and this column should be assigned Primary Key to validate the detailed data in EMPLOYEE table.

DDL : Creating Tables

Create the Department Lookup Table

CREATE TABLE DEPARTMENT (
DEPT_ID NUMBER(2) NOT NULL,
DEPT_NAME VARCHAR(50) NOT NULL,
LOCATION VARCHAR(10),

MANAGER NUMBER(6),

CONSTRAINT DEPARTMENT_PK
PRIMARY KEY (DEPT_ID ),
);

An Oracle database consists of DDL commands, which are useful to create, modify and drop the database objects. In this section, we will try to explain about important database CREATE commands that are used by a data modeller by relating it with our example data.

The Main Table

Create the final employee detail table

CREATE TABLE EMPLOYEE (
EMP_ID NUMBER NOT NULL,
FIRSTNAME VARCHAR(30) NOT NULL,
LASTNAME VARCHAR(30) NOT NULL, JOBDESC VARCHAR(10),

JOINDATE DATE,

EMP_SAL NUMBER(7,2) NOT NULL,

COMM NUMBER (3,2),

DEPT_CD NUMBER(2)
CONSTRAINT EMPLOYEE_PK
PRIMARY KEY (EMP_ID ),
CONSTRAINT EMPLOYEE_FK01
FOREIGN KEY (DEPT_CD )
REFERENCES DEPARTMENT
);

More Automation

Defining a sequence

CREATE SEQUENCE SEQ_EMPLOYEE_DTL
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOMINVALUE
NOCACHE
NOCYCLE
NOORDER
;

Defining a trigger

CREATE OR REPLACE TRIGGER TRG_SEQ_EMPLOYEE_DTL
BEFORE INSERT ON EMPLOYEE
FOR EACH ROW
BEGIN
SELECT SEQ_EMPLOYEE.NEXTVAL INTO :NEW.EMP_ID FROM DUAL;
END;

Installing a trigger

Whenever a record is inserted into "EMPLOYEE_DTL" table, this trigger selects the next unique number from the sequence "SEQ_EMPLOYEE_DTL" and inserts into the column "EMP_ID".

In our INSERT STATEMENTS example, we have not provided values for the column "EMP_ID" and inserting values into "EMP_DTL_ID is taken care by sequence and trigger.

Altering Tables

Add Columns

ALTER TABLE EMPLOYEE ADD PASSPORT_NUM NUMBER(6);

Rename Columns

ALTER TABLE EMPLOYEE RENAME column PASSPORT_NUM TO EMP_EXTID;

Modify Column

ALTER TABLE EMPLOYEE MODIFY EMP_EXTID VARCHAR2(10);

Drop Column

ALTER TABLE EMPLOYEE DROP COLUMN EMP_EXTID;

Implementing Constraints

Add Constraints

ALTER TABLE EMPLOYEE ADD CONSTRAINT CH_SAL CHECK(EMP_SAL BETWEEN 4000 AND 7000);

Unique Constraints

ALTER TABLE EMPLOYEE ADD CONSTRAINT UN_ID UNIQUE(EMP_EXTID);

ALTER TABLE EMPLOYEE DISABLE CONSTRAINT UN_ID;

ALTER TABLE EMPLOYEE ENABLE CONSTRAINT UN_ID;

ALTER TABLE EMPLOYEE DROP CONSTRAINT UN_ID;

Additional indexes

CREATE INDEX IND_UNID ON EMPLOYEE(UN_ID);

Structured Query Language

A high language to manipulate the database

Data Description Language

Data Manipulation Language

Data Control Language

SQL

Click to edit the title text format

Click to edit the outline text format

Second Outline Level

Third Outline Level

Fourth Outline Level

Fifth Outline Level

Sixth Outline Level

Seventh Outline Level

Eighth Outline Level

Ninth Outline Level

Prithwis Mukerjee

Click to edit the outline text format

Second Outline Level

Third Outline Level

Fourth Outline Level

Fifth Outline Level

Sixth Outline Level

Seventh Outline Level

Eighth Outline Level

Ninth Outline Level

Entity TableAttributeColumnData TypeNull ?PK ?FK ?

EmployeeT_EMPEmployeeIDEmp_IDNumber(6)N YN

EmployeeT_EMPFirst NameFirstNameVarchar(30)N N

EmployeeT_EMPLast NameLastNameVarchar(30)N N

EmployeeT_EMPDesignationJobDescVarchar(10)YN

EmployeeT_EMPDate of JoiningJoinDateDateN N

EmployeeT_EMPSalaryNumber(7,2)N N

EmployeeT_EMPCommissionCommNumber(3,2)YN

EmployeeT_EMPDepartmentDeptNumber(2)YNY

???Page ??? (???)04/02/2008, 16:32:11Page / Entity TableAttributeColumnData TypeNull ?PK ?FK ?

DepartmentDeptDepartment CodeDept_IDNumber(2)NYN

DepartmentDeptDepartment NameDept_NameVarchar(10)N

DepartmentDeptManagerMgrIDNumber(6)YN

DepartmentDeptLocationVarchar(10)YN

???Page ??? (???)04/02/2008, 16:32:11Page /