+ All Categories
Home > Documents > Unit 3: Microsoft Transact SQL and the Query Analyzer

Unit 3: Microsoft Transact SQL and the Query Analyzer

Date post: 15-Jan-2016
Category:
Upload: nowles
View: 54 times
Download: 0 times
Share this document with a friend
Description:
IT390 Business Database Administration. Unit 3: Microsoft Transact SQL and the Query Analyzer. Objectives. Briefly trace the history of SQL Apply Key commands and features of Microsoft SQL Database Server and Transact SQL. - PowerPoint PPT Presentation
102
© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Unit 3: Microsoft Transact SQL and the Query Analyzer IT390 Business Database Administration
Transcript
Page 1: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 1

Unit 3:Microsoft Transact SQL and the Query Analyzer

IT390 Business Database Administration

Page 2: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 2

Objectives

• Briefly trace the history of SQL• Apply Key commands and features of Microsoft SQL

Database Server and Transact SQL.• Explain other key commands associated with the Microsoft

SQL Server and Transact SQL language.• Create and Manage databases with Transact SQL and

Enterprise Manager.• Create a Database with Transact SQL and Enterprise

Manager. • Explain how to create and manage Microsoft Databases

through both the Query Analyzer and Enterprise Manager.• Explain Normalization with Data Models.• Describe the fundamentals of Normalization with the

Relational Data Model and basic Relational Design.• Describe normalization anomalies and their results.

Page 3: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 3

History

SQL came into existence when Dr. E. F. Codd, who worked for IBM, defined the relational model for databases.

In 1986 and 1987, American National Standards Institute (ANSI) and International Standards Organization (ISO) formed SQL Standards committees. As a result, SQL became an open standard known as ANSI-SQL.

To implement the designs in a relational database management system (RDBMS), you need a tool. Structured query language (SQL) is one such tool.

Databases: An Overview

Page 4: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 4

Think about it…

• What is SQL?• Why is SQL known as a non-procedural language? Is it just

another language?

Page 5: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 5

SQL Products and Application Developers

Page 6: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 6

You can perform various database operations by using SQL commands. These operations include updating data in a database and retrieving information from a database.

When you query a database using an SQL command, the SQL query is sent to an RDBMS.

The SQL query process can be summarized as follows: • A user makes a request by using an SQL statement.• The RDBMS receives the request and forwards it to the database engine.• The database engine processes the request and sends the result to the RDBMS.• The RDBMS forwards the result to the user.

Processing of a SQL Command

Page 7: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 7

• SQL provides a number of commands to perform various database-related tasks. You perform database-related tasks in SQL by using three different primary types of SQL (sub)languages:

Data Definition language (DDL) Data Manipulation Language (DML) Data Control Language (DCL) For instance, a table that is created using a DDL

command stores the : Name of a table Columns of a table Constraints on the table

Processing of a SQL Command

Page 8: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 8

Sub-Languages of SQL

Sub-Languages of SQL

DML TCLDDL DCL

Page 9: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 9

Activity • Identify the sub-languages of SQL.

1) Joe, the database administrator of Ethnic Blends Inc., wants to create a new

table named Brands, which will be a part of the organization’s database.

2) Joe wants to insert a

fresh set of values

in the Garments table

3) Joe wants to undo

a fake transaction

that occurred in

the database.

4) The developers of Ethnic Blends

Inc. want to assign permissions

to certain MIS level users.

Page 10: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 10

Solution

• For case 1: DDL• For case 2: DML• For case 3: TCL• For case 4: DCL

Page 11: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 11

Data Definition Language

Page 12: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 12

• The syntax for creating a database in T-SQL is:CREATE DATABASE <db_name>[ON [PRIMARY]        [ <filename> [,...n] ]        [, <filegroupname> [,...n] ]][ LOG ON { <logfiles> [,...n]} ][ FOR LOAD | FOR ATTACH ]

<filespec> ::=    ( [ NAME = file_name, ]

  FILENAME = 'os_filename'  [, SIZE = filesize]  [, MAXSIZE = { maximum_size | UNLIMITED } ]  [, FILEGROWTH = file_growthincrement] ) [,...n]

<filegroup> ::= FILEGROUP name_filegroup <filsespec>

[,...n]

Creating a Database (RAW SQL CODE!)

Page 13: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 13

• You can use also use Enterprise Manager to create a database.

Creating a Database (cont.)

Page 14: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 14

• They are of the following types:

master

model

msdb

tempdb

System Databases (some Databases already exist!)

Page 15: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 15

• You can use the CREATE TABLE command to do the following:

Create a new table. Create a new table with columns. Create a new table with constraints defined on

columns.• You use the CREATE TABLE command to create a new table. The syntax for creating a new table is:

CREATE TABLE <table name>

DDL: Using the CREATE TABLE Command

Page 16: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 16

• You can use the CREATE TABLE command to create a new table with columns:

CREATE TABLE <table name>

<column name> <data type>• You can use some optional constraints with the basic CREATE TABLE command. The syntax having the optional constraints is as follows:

CREATE TABLE <table name>

<column name> <data type>

DDL: Using the CREATE TABLE Command

Page 17: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 17

•DDL provides the following commands to modify and delete existing objects in a database:

ALTER DROP TRUNCATE COMMENT

Using Other DDL Commands

Page 18: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 18

Modifying Table and Column

• The syntax to modify an existing table is:

• The syntax to alter a column in a table is:

ALTER TABLE <table name>

ALTER TABLE <table name>ALTER COLUMN <column name>

Page 19: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 19

•ALTER command: You use the ALTER command to add columns, add constraints, or modify columns and constraints that were already created using the CREATE command.• The syntax to use the ALTER TABLE command to add a column to a table is as follows:

ALTER TABLE <table name>

ADD <column name> <data type> <constraint>

Using Other DDL Commands

Page 20: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 20

•DROP Command: You use the DROP command to delete a table or columns that has constraints.• The syntax to use a DROP command to delete an object is as follows:

DROP <OBJECT><object name>.

Here, the <OBJECT> could be a TABLE, COLUMN, or a CONSTRAINT with its respective name.

Using Other DDL Commands (cont.)Deleting Table and Column Objects

DROP OBJECT <object name>

where,object name can be the name of a column or a constraint

Page 21: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 21

•You use the TRUNCATE command to delete only specific rows in a table. • The syntax to use a TRUNCATE command to delete an object is as follows:

TRUNCATE <table name>

Using Other DDL Commands (cont.)

Page 22: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 22

• The COMMENT command enables you add a comment about a table or a column.

COMMENT ON TABLE <table name> IS “………….”

The COMMENT in T-SQL is “ -- “

Using Other DDL Commands (cont.)

Page 23: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 23

SQL Language

• Designed to talk to relational databases• Perform various functions:

DDL-Data Definition

DML-Data Manipulation

DCL-Data Control (Security)• We will talk about DML

Page 24: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 24

The Syntax of the DML “SELECT” statement

Page 25: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 25

Class Activity 2

• Will the following SELECT statements run and what is the difference between them?

select * from garment

SELECT * FROM garment

Page 26: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 26

Solution 2

• Both the statements will run as SQL is not normally case sensitive. BE CAREFUL though…it is good to be in the habit of consistent coding for READABILITY.

Page 27: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 27

Activity

• In an Employee table, there are 3 columns named firstname, lastname, and middlename. Is there any error in the following query?

SELECT firstname lastname middlename FROM Employee

Page 28: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 28

Solution

• The column names should be separated by a comma (,).• The correct code is:

SELECT firstname, lastname, middlename FROM employee

Page 29: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 29

Activity • Which of the following syntax will not give an error?

SELECT * FROMgarment

select * From garment

select * from garment

Select* From garment

Page 30: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 30

Solution

• All the statements will work.

Page 31: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 31

Normalization Premise

• We have received one or more tables of existing data• The data is to be stored in a new database• QUESTION: Should the data be stored as received, or

should it be transformed for storage?

Page 32: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 32

How Many Tables?

Should we store these two tables as they are, or should we combine them into one table in our new database?

Page 33: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 33

But first -

• We need to understand MUCH more about:

The relational model Relational model terminology

Page 34: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 34

The Relational Model

• Introduced in 1970• Created by E.F. Codd

He was an IBM engineer The model used mathematics known as “relational algebra”

• Now the standard model for commercial DBMS products

Page 35: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 35

Important Relational Model Terms

• Entity• Relation• Functional Dependency• Determinant• Candidate Key• Composite Key• Primary Key• Surrogate Key• Foreign Key• Referential integrity constraint• Normal Form• Multivalued Dependency

Page 36: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 36

Entity

• An entity is some identifiable thing that users want to track: Customers Computers Sales

Page 37: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 37

Relation

• Relational DBMS products store data about entities in relations, which are a special type of table

• A relation is a two-dimensional table that has the following characteristics: Rows contain data about an entity Columns contain data about attributes of the entity All entries in a column are of the same kind Each column has a unique name Cells of the table hold a single value The order of the columns is unimportant The order of the rows is unimportant No two rows may be identical

Page 38: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 38

A Relation

Page 39: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 39

A Relation with Values of Varying Length

Page 40: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 40

Tables That Are Not Relations:Multiple Entries per Cell

Page 41: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 41

Tables That Are Not Relations:Table with Required Row Order

Page 42: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 42

Alternative Terminology

• Although not all tables are relations, the terms table and relation are normally used interchangeably

• The following sets of terms are equivalent:

Page 43: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 43

Functional Dependency

• A functional dependency occurs when the value of one (a set of) attribute(s) determines the value of a second (set of) attribute(s):

StudentID StudentName

StudentID (DormName, DormRoom, Fee)• The attribute on the left side of the functional

dependency is called the determinant.• Functional dependencies may be based on equations:

ExtendedPrice = Quantity X UnitPrice

(Quantity, UnitPrice) ExtendedPrice• Function dependencies are not equations!

Page 44: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 44

Functional Dependencies Are Not Equations

ObjectColor Weight ObjectColor Shape ObjectColor (Weight, Shape)

Page 45: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 45

Composite Determinants

• Composite determinant: A determinant of a functional dependency that consists of more than one attribute

(StudentName, ClassName) (Grade)

Page 46: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 46

Functional Dependency Rules

• If A (B, C), then A B and A C• If (A,B) C, then neither A nor B determines C by itself

Page 47: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 47

Functional Dependencies in the SKU_DATA Table

Page 48: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 48

Functional Dependencies in the SKU_DATA Table

SKU (SKU_Description, Department, Buyer)

SKU_Description (SKU, Department, Buyer)

Buyer Department

Page 49: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 49

Functional Dependencies in the ORDER_ITEM Table

Page 50: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 50

Functional Dependencies in the ORDER_ITEM Table

(OrderNumber, SKU) (Quantity, Price,

ExtendedPrice)

(Quantity, Price) (ExtendedPrice)

Page 51: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 51

What Makes Determinant Values Unique?

• A determinant is unique in a relation if, and only if, it determines every other column in the relation

• You cannot find the determinants of all functional dependencies simply by looking for unique values in one column: Data set limitations Must be logically a determinant

Page 52: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 52

Keys

• A key is a combination of one or more columns that is used to identify rows in a relation

• A composite key is a key that consists of two or more columns

Page 53: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 53

Candidate and Primary Keys

• A candidate key is a key that determines all of the other columns in a relation

• A primary key is a candidate key selected as the primary means of identifying rows in a relation: There is one and only one primary key per relation The primary key may be a composite key The ideal primary key is short, numeric and never

changes

Page 54: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 54

Surrogate Keys

• A surrogate key as an artificial column added to a relation to serve as a primary key: DBMS supplied Short, numeric and never changes – an ideal primary

key! Has artificial values that are meaningless to users Normally hidden in forms and reports

Page 55: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 55

Surrogate Keys

NOTE: The primary key of the relation is underlined below:

• RENTAL_PROPERTY without surrogate key:RENTAL_PROPERTY (Street, City,State/Province, Zip/PostalCode, Country,

Rental_Rate) • RENTAL_PROPERTY with surrogate key:

RENTAL_PROPERTY (PropertyID, Street, City, State/Province, Zip/PostalCode, Country, Rental_Rate)

Page 56: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 56

Foreign Keys

• A foreign key is the primary key of one relation that is placed in another relation to form a link between the relations:

A foreign key can be a single column or a composite key

The term refers to the fact that key values are foreign to the relation in which they appear as foreign key values

Page 57: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 57

Foreign Keys

NOTE:

The primary keys of the relations are underlined and any foreign keys are in italics in the relations below.

DEPARTMENT (DepartmentName, BudgetCode, ManagerName)

EMPLOYEE (EmployeeNumber, EmployeeName, DepartmentName)

Page 58: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 58

The Referential Integrity Constraint

• A referential integrity constraint is a statement that limits the values of the foreign key to those already existing as primary key values in the corresponding relation

Page 59: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 59

Foreign Key with aReferential Integrity Constraint

NOTE: The primary key of the relation is underlined and any foreign keys are in italics in the relations below:

SKU_DATA (SKU, SKU_Description, Department, Buyer)

ORDER_ITEM (OrderNumber, SKU, Quantity, Price, ExtendedPrice)

Where ORDER_ITEM.SKU must exist in SKU_DATA.SKU

Page 60: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 60

Modification Anomalies

• Deletion Anomaly• Insertion Anomaly• Update Anomaly

Page 61: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 61

Modification Anomalies

• The EQUIPMENT_REPAIR table before and after an incorrect update operation on AcquisitionCost for Type = Drill Press:

Page 62: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 62

Normal Forms

• Relations are categorized as a normal form based on which modification anomalies or other problems that they are subject to:

Page 63: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 63

Normal Forms

• 1NF – A table that qualifies as a relation is in 1NF• 2NF – A relation is in 2NF if all of its nonkey attributes

are dependent on all of the primary key• 3NF – A relation is in 3NF if it is in 2NF and has no

determinants except the primary key• Boyce-Codd Normal Form (BCNF) – A relation is in

BCNF if every determinant is a candidate key

“I swear to construct my tables so that all nonkeycolumns are dependent on the key, the whole key and nothing but the key, so help me Codd.”

Page 64: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 64

Eliminating Modification Anomalies from Functional Dependencies in Relations

• Put all relations into Boyce-Codd Normal Form (BCNF):

Page 65: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 65

Putting a Relation into BCNF:EQUIPMENT_REPAIR

Page 66: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 66

Putting a Relation into BCNF:EQUIPMENT_REPAIR

EQUIPMENT_REPAIR (ItemNumber, Type, AcquisitionCost,

RepairNumber, RepairDate, RepairAmount)

ItemNumber (Type, AcquisitionCost)

RepairNumber (ItemNumber, Type, AcquisitionCost, RepairDate, RepairAmount)

ITEM (ItemNumber, Type, AcquisitionCost)

REPAIR (ItemNumber, RepairNumber, RepairDate, RepairAmount)

Where REPAIR.ItemNumber must exist in ITEM.ItemNumber

Page 67: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 67

Putting a Relation into BCNF:New Relations

Page 68: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 68

Putting a Relation into BCNF:SKU_DATA

Page 69: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 69

Putting a Relation into BCNF:SKU_DATA

SKU_DATA (SKU, SKU_Description, Department, Buyer)

SKU (SKU_Description, Department, Buyer)

SKU_Description (SKU, Department, Buyer)

Buyer Department

SKU_DATA (SKU, SKU_Description, Buyer)

BUYER (Buyer, Department)

Where BUYER.Buyer must exist in SKU_DATA.Buyer

Page 70: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 70

Putting a Relation into BCNF:New Relations

Page 71: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 71

Multi-valued Dependencies

• A multi-valued dependency occurs when a determinant determines a particular set of values:

Employee Degree

Employee Sibling

PartKit Part• The determinant of a multivaled dependency can never

be a primary key

Page 72: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 72

Multi-valued Dependencies

Page 73: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 73

Eliminating Anomalies from Multi-valued Dependencies

• Multi-valued dependencies are not a problem if they are in a separate relation, so: Always put multivaled dependencies into their own

relation This is known as Fourth Normal Form (4NF)

Page 74: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 74

Normalization Premise• We have received one or

more tables of existing data

• The data is to be stored in a new database

• QUESTION: Should the data be stored as received, or should it be transformed for storage?

Should we store these two tables as they are, or should

we combine them into one table in our new database?

Page 75: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 75

Assessing Table Structure

Page 76: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 76

Counting Rows in a Table

• To count the number of rows in a table use the SQL built-in function COUNT(*):

SELECT COUNT(*) AS NumRows FROM SKU_DATA;

Page 77: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 77

Examining the Columns

• To determine the number and type of columns in a table, use an SQL SELECT statement

• To limit the number of rows retreived, use the SQL TOP {NumberOfRows} keyword:

SELECT TOP (10) *

FROM SKU_DATA;

Page 78: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 78

Checking Validity of Assumed Referential Integrity Constraints

• Given two tables with an assumed foreign key constraint:

SKU_DATA (SKU, SKU_Description, Department, Buyer)

BUYER(BuyerName, Department)

Where SKU_DATA.Buyer must exist in BUYER.BuyerName

Page 79: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 79

Checking Validity of Assumed Referential Integrity Constraints

• To find any foreign key values that violate the foreign key constraint:SELECT BuyerFROM SKU_DATAWHERE Buyer NOT IT

(SELECT BuyerFROM SKU_DATA,

BUYERWHERE SKU_DATA.BUYER =

BUYER.BuyerName;

Page 80: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 80

Type of Database

• Updateable database or read-only database?• If updateable database, we normally want tables in BCNF• If read-only database, we may not use BCNF tables

Page 81: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 81

Normalization:Advantages and Disadvantages

Page 82: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 82

Non-Normalized Table:EQUIPMENT_REPAIR

Page 83: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 83

Normalized Tables:ITEM and REPAIR

Page 84: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 84

Copying Data to New Tables

• To copy data from one table to another, use the SQL command INSERT INTO TableName command:

INSERT INTO ITEMSELECT DISTINCT ItemNumber, Type,

AcquisitionCostFROM EQUIPMENT_REPAIR;

INSERT INTO REPAIRSELECT ItemNumber, RepairNumber,

RepairDate, RepairAmmountFROM EQUIPMENT_REPAIR;

Page 85: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 85

Choosing Not to Use BCNF

• BCNF is used to control anomalies from functional dependencies

• There are times when BCNF is not desirable• The classic example is ZIP codes:

ZIP codes almost never change Any anomalies are likely to be caught by normal

business practices Not having to use SQL to join data in two tables will

speed up application processing

Page 86: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 86

Multi-valued Dependencies

• Anomalies from multivalued dependencies are very problematic

• Always place the columns of a multivalued dependency into a separtate table (4NF)

Page 87: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 87

Designing Read-Only Databases

• Read-only databases are non-operational databases using data extracted from operational databases

• They are used for querying, reporting and data mining applications

• They are never updated (in the operational database sense – they may have new data imported form time-to-time)

Page 88: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 88

Denormalization

• For read-only databases, normalization is seldom an advantage Application processing speed is more important

• Denormalization is the joining of data in normalized tables prior to storing the data

• The data is then stored in non-normalized tables

Page 89: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 89

Normalized Tables

Page 90: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 90

Denormalizing the Data

INSERT INTO PAYMENT_DATA

SELECT STUDENT.SID, Name, CLUB.Club,

Cost, AmtPaid

FROM STUDENT, PAYMENT, CLUB

WHERE STUDENT.SID = PAYMENT.SID

AND PAYMENT.Club = CLUB.Club;

Page 91: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 91

Customized Tables

• Read-only databases are often designed with many copies of the same data, but with each copy customized for a specific application

• Consider the PRODUCT table:

Page 92: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 92

Customized Tables

PRODUCT_PURCHASING (SKU, SKU_Description, VendorNumber, VendorName, VendorContact_1, VendorContact_2, VendorStreet, VendorCity, VendorState, VendorZip)

PRODUCT_USAGE (SKU, SKU_Description, QuantitySoldPastYear, QuantitySoldPastQuarter, QuantitySoldPastMonth)

PRODUCT_WEB (SKU, DetailPicture, ThumbnailPicture, MarketingShortDescription, MarketingLongDescription, PartColor)

PRODUCT_INVENTORY (SKU, PartNumber, SKU_Description, UnitsCode, BinNumber, ProductionKeyCode)

Page 93: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 93

Common Design Problems

Page 94: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 94

The Multivalue, Multicolumn Problem

• The multivalue, multicolumn problem occurs when multiple values of an attribute are stored in more that one column:EMPLOYEE (EmpNumber, Name, Email, Auto1_LicenseNumber, Auto2_LicenseNumber, Auto3_LicenseNumber)

• This is another form of a multivalued dependency• Solution: Like the 4NF solution for multivalued

dependencies, use a separate table to store the multiple values

Page 95: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 95

Inconsistent Values

• Inconsistent values occur when different users or different data sources use slightly different forms of the same data value: Different codings:

• SKU_Description = 'Corn, Large Can' • SKU_Description = 'Can, Corn, Large'• SKU_Description = 'Large Can Corn‘

Different spellings:• Coffee, Cofee, Coffeee

Page 96: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 96

Inconsistent Values

• Particularly problematic are primary or foreign key values

• To detect: Use referential integrity check already

discussed for checking keys Use the SQL GROUP BY clause on

suspected columns

SELECT SKU_Description, COUNT(*) AS NameCount

FROM SKU_DATA GROUP BY SKU_Description;

Page 97: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 97

Missing Values

• A missing value or null value is a value that has never been provided

Page 98: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 98

Null Values

Null values are ambiguous: May indicate that a value is inappropriate:

• DateOfLastChildbirth is inappropriate for a male May indicate that a value is appropriate but unknown

• DateOfLastChildbirth is appropriate for a female, but may be unknown

May indicate that a value is appropriate and known, but has never been entered:

• DateOfLastChildbirth is appropriate for a female, and may be known but no one has recorded it in the database

Page 99: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 99

Checking for Null Values

• Use the SQL keyword IS NULL to check for null values:

SELECT COUNT(*) AS QuantityNullCount

FROM ORDER_ITEM

WHERE Quantity IS NULL;

Page 100: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 100

The General-Purpose Remarks Column

• A general-purpose remarks column is a column with a name such as: Remarks Comments Notes

• It often contains important data stored in an inconsistent, verbal and verbose way A typical use is to store data on a customer’s

interests.• Such a column may:

Be used inconsistently Hold multiple data items

Page 101: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 101

Summary• Database Definitions and Manipulations are based on

• Major portion of SQL are divided up into sublanguages.

• DDL is one sublanguage in SQL with which we can create databases, tables and manipulate those entities.

• A key SQL command in DML is the SELECT statement• Normalization with Data Models is a key concept to

understanding how a relational database functions and how it must be properly arranged.

• Without normalization, there will be anomalies and their results will affect the functioning of the database negatively.

• There are many factors to consider when creating a database including the type of database and to what extent it will be normalized.

Page 102: Unit 3: Microsoft Transact SQL  and the Query Analyzer

© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 3 Slide 102

Did you understand the key points from the Lesson?

Do you have any questions?

Summary


Recommended