Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation...

43
Chapter 10 – Database Creation 1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Transcript of Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation...

Page 1: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 1

IT238: Data Modeling and Database Design

Unit 6:Database Creation

Instructor: Qing Yan, M.D., Ph.D.

Page 2: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Unit 5 Review: assignment

Chapter 10 – Database Creation 2

Page 3: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Develop a dependency-preserving solution that eliminates this normal form violation.

Chapter 10 – Database Creation 3

CAR_DESC (Model, #cylinders, origin);CAR_TAX (Origin, Tax);CAR_FEE (#cylinders, Fee)

Page 4: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Assignment question 3

Chapter 10 – Database Creation 4

Page 5: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Develop a solution that eliminates the normal form violations.

Chapter 10 – Database Creation 5

Page 6: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Unit 6 Objectives

• Demonstrate the ability to create, alter, and drop tables using the SQL-92 data definition language

• Demonstrate the ability to populate tables using the SQL-92 INSERT, DELETE, and UPDATE statements

Chapter 1 – Database Systems: Architecture and Components

6

Page 7: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Unit 6 To-Do List

• Complete the reading Textbook and Web

• Participate in the discussion board30 points

• Attend the introductory seminar or complete FLA quiz20 points

• Complete the unit assignment50 points

Chapter 1 – Database Systems: Architecture and Components

7

Page 8: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 8

Page 9: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 9

Page 10: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 10

Page 11: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 11

Page 12: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Key Concepts

• The CREATE TABLE statement is the SQL/DDL statement used to establish the structure of a table.

• The ALTER TABLE statement is the SQL/DDL statement used to modify the structure of a table.

• The DROP TABLE statement is the SQL/DDL statement used to remove a table (structure as well as content) from the database.

Chapter 1 – Database Systems: Architecture and Components

12

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 13: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Key concepts

• Type constraint-A declarative constraint on a column of a table that specifies the type of data (e.g., number, string, date/time, and interval) and the domain associated with the column.

• CONSTRAINT- constraint_name phrase-Used to define the name of a constraint.

• Attribute(column)-level constraint- A constraint imposed on a single column in a table.

• Tuple(row)-level constraint- A constraint imposed on set of columns in a table.

Chapter 10 – Database Creation 13From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 14: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Key concepts

• CHECK clause-To restrict column or domain values.• PRIMARY KEY clause- Used in the specification of

an entity integrity constraint.• UNIQUE clause-Used in the specification of an

alternate key (uniqueness constraint).• FOREIGN KEY clause-Used to define a tuple-level

referential integrity constraint.• REFERENCES clause- Used to define both a tuple-

level and a column-level referential integrity constraint.

Chapter 10 – Database Creation 14From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 15: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 15

Overview

• Structured Query Language (SQL)• Data Definition Language (DDL)• Data Manipulation Language (DML)• Data Control Language (DCL)

• SQL is a standard developed by ANSI and ISO• SQL statements are case-insensitive• The book uses SQL-92 (SQL2) and SQL-99

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 16: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 16

Database Creation

The principal task includes • creation and modification of the database tables and

other related structures• enforcement of integrity constraints • population of the database tables• specification of security and authorizations as a

means of access control and transaction processing controls

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 17: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 17

Data Definition in SQL

Three major constructs:• Create• Alter• Drop

Applied to:• Table• Domain • Schema• Views

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 18: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 18

Example

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 19: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 19

Example (continued)

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 20: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 20

CREATE TABLE Commands: Box 1

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 21: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 21

CREATE TABLE Syntax• CREATE TABLE table_name (comma delimited list of table-elements);

where table_name is a user supplied name for the base TABLE and each table-element in the list is either a column-definition or a constraint-definition. The basic syntax for a column-definition is of the form:

column_name representation [default-definition] [column-constraint list] where

• column_name is a user supplied name for a COLUMN• representation specifies the relevant data type or alternatively the

predefined domain-name • the optional (indicated by [ ] ) default-definition specifies a default value

for the COLUMN which overrides any default value specified in the domain definition, if applicable. In the absence of an explicit default definition (directly or via the domain definition), the implicit assumption of a NULL value for the default prevails. The default-definition is of the form: DEFAULT ( literal | niladic-function| NULL )

• the optional (indicated by [ ] ) column-constraint list specifies constraint-definition for the column. The basic syntax for a constraint-definition follows the form:[CONSTRAINT constraint_name ] constraint-definition

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 22: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 22

SQL Attribute Domains

SQL-92 Data Types

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 23: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 23

SQL Attribute Domains

SQL-92 Data Types (continued)

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 24: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 24

SQL-92 Data Types (continued)

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 25: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 25

CREATE TABLE: Things to Remember

• Data types supported by SQL-92 are grouped under Number, String, Date/time & Interval. However, DBMS vendors often build their own data types based on these four categories.

• Make sure that all attributes names are unique• Some terms are reserved words, i.e., SQL uses

these words in its language (e.g., order, grant, etc.)• Attribute-level constraint vs. table-level constraint

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 26: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 26

CREATE TABLE Commands: Box 2

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 27: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 27

Still Missing…

• Pat_name, Pat_age, Pat_admit_dt, Med_code and Med_qty_onhand are mandatory attributes – i.e., cannot have null values in any tuple

• Med_code is the alternate key since Med_name has been chosen as the primary key of medication table

• Participation of order in the Placed_for relationship is total• Participation of patient in the Placed_for relationship is partial• Participation of order in the is_for relationship is partial• Participation of medication in the is_for relationship is total• The deletion rule for the Is_for relationship is restrict• The deletion rule for the Placed_for relationship is cascade• [Pat_wing, Pat_room] is a molecular attribute• [Pat_wing, Pat_room, Pat_bed] is a molecular attribute

Note: The cardinality ratio of the form (1, n) in a relationship type is implicitly captured in the DDL specification via the foreign key constraint. Any (1, 1) cardinality ratio can be implemented using the UNIQUE constraint definition.

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 28: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 28

CREATE TABLE Commands: Box 3

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 29: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 29

ALTER TABLE Syntax

• where table_name is the name of the base TABLE being altered and the actions possible are:

– Actions pertaining to alteration of a column via the syntax:• ADD [ COLUMN ] column_definition• ALTER [ COLUMN ] column_name { SET default-definition | DROP

DEFAULT } – (Adds the default-definition or replaces an existing default-definition) or– (removes an existing default-definition)

• DROP [ COLUMN ] column_name { RESTRICT | CASCADE }Or

– Alteration of a previously specified table constraint in force via the syntax• ADD table_constraint_definition

– (Permits addition to existing set of constraints, if any)• DROP CONSTRAINT constraint_name { RESTRICT | CASCADE }

– (Removes the named constraint)

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 30: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 30

ALTER TABLE Examples

• Suppose we want to add a column to the base table patient to store the phone number of every patient. The DDL/SQL code to do this is: ALTER TABLE patient ADD Pat_phone# char (10);

• In order to delete the column from the base table, the following code applies:ALTER TABLE patient DROP Pat_phone# CASCADE; or ALTER TABLE patient DROP Pat_phone# RESTRICT;

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 31: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 31

ALTER TABLE Examples (continued)

• Suppose we want to specify a default value of $3.00 for the unit price of all medications. This can be done as follows:ALTER TABLE medication ALTER Med_unitprice SET DEFAULT 3.00;

• The default clause can be removed by: ALTER TABLE medication ALTER Med_unitprice DROP DEFAULT;

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 32: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 32

Best Practices

• Method 1:Pat_age smallint not null

• Method 2: Pat_age smallint constraint nn_Patage not null,CONSTRAINT chk_age CHECK (Pat_age IN (1 through 90))

• Method 3:Pat_age smallint not null CHECK (Pat_age IN (1 through 90))

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 33: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 33

Best Practices (continued)

• If we decide to permit null value for Pat_age, in Method 3, the whole column definition has to be re-specified. In Method 2, we simply drop the “not null” constraint as shown below:

•ALTER TABLE patient DROP CONSTRAINT nn_patage CASCADE; orALTER TABLE patient DROP CONSTRAINT nn_patage RESTRICT;

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 34: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 34

DROP TABLE

• Where table_name is the name for the base TABLE being deleted and the drop behaviors possible are: CASCADE or RESTRICT.

• Example: DROP TABLE medication CASCADE;

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 35: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 35

Data Population Using SQL

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 36: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 36

Sample Tables

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 37: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 37

INSERT

• Single-row INSERT – adds a single row of data to a table

• Multi-row INSERT – extracts rows of data from another part of the database and adds them to a table.

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 38: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 38

INSERT Example

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 39: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 39

DELETE

• The DELETE statement removes selected rows of data from a single table

• Syntax:

• Since the WHERE clause in a DELETE statement is optional, a DELETE statement of the form DELETE FROM <table-name> can be used to delete all rows in a table.

• When used in this manner, while the target table has no rows after execution of the deletion, the table still exists and new rows can still be inserted into the table with the INSERT statement. To erase the table definition from the database, the DROP TABLE statement must be used.

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 40: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 40

DELETE Example

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 41: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Chapter 10 – Database Creation 41

DELETE Example (continued)

From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.

Page 42: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Summary

• Create, alter, and drop tables using the SQL-92 data definition language

• Populate tables using the SQL-92 INSERT, DELETE, and UPDATE statements

Page 43: Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.

Q & A

• Questions?