1. Trang chủ
  2. » Công Nghệ Thông Tin

PHYSICAL DATABASE DESIGN pps

46 180 1
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Physical Database Design pps
Trường học NIIT
Chuyên ngành Database Design and Management
Thể loại lecture notes
Định dạng
Số trang 46
Dung lượng 332,6 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

For example, the following statement creates the customer table: CREATE TABLE customer cust-no CHAR5 NOT NULL, name CHAR15, phone-number CHAR7, city CHAR15, PRIMARY KEY cust-no The C

Trang 1

L ESSON : 3A

Objectives

In this lesson, you will learn to:

Identify the following kinds of relations:

Perform DML operations on views

Identify the different types of views

Identify data integrity constraints

Trang 2

Physical Database Design Lesson 9 / Slide 1 of 13

©NIIT

Physical Database Design

Objectives

In this section, you will learn to:

• Identify the following kinds of relations:

• Perform DML operations on views

• Identify the different types of views

• Identify data integrity constraints

I NSTRUCTOR N OTES

Lesson Overview

The lesson covers the different kinds of relations It also explains how to create, alter, remove, and query tables In addition, the lesson explains the different types of views, the various DML operations that can be performed on them, and data integrity

constraints

Trang 3

Physical Database Design

©NIIT

Physical Database Design

Pre-assessment Questions

1 Functional dependencies represent _ relationships.

2 What is the term used for the intentional introduction of redundancy in a table in order to improve performance?

3 Each value of an attribute A in relation R is associated with precisely one value of attribute B What is this called?

4 A table is said to be in the _ when each cell of the table contains precisely one value

5 In a relation, every determinant is a candidate key This relation is in which normal form?

Lesson 9 / Slide 2 of 13

Trang 4

Physical Database Design

Ans3 Functional dependency

Ans4 First normal form

Ans5 Boyce-Codd normal form

Lesson 9 / Slide 3 of 13

Trang 5

L ANGUAGE S UPPORT FOR THE

R ELATIONAL M ODEL

Physical Database Design Lesson 9 / Slide 4 of 13

©NIIT

Physical Database Design

Language Support for the Relational

Model

• Most relational database systems support a query language named Structured Query Language (SQL).

• SQL is a combination of three subordinate languages:

• Data Definition Language (DDL)

• Data Manipulation Language (DML)

• Data Control Language (DCL)

• The three important types of relations are:

„Data Definition Language (DDL)

„Data Manipulation Language (DML)

„Data Control Language (DCL)

DDL statements include operators for creation and deletion of tables and indexes DML statements are used to enter data into the tables created by using DDL statements DML statements are also used to update and delete data and perform complex queries

on the tables DCL statements are used to control users’ access to the tables

Trang 6

• A base table is a named table that physically exists in a database.

• The SQL statement to create a table is CREATE TABLE.

• You can alter an existing table by using the ALTER TABLE statement.

• You can remove a table by using the DROP TABLE statement.

A base table is a named table that physically exists in a database You can physically create a table after you have mapped the entity-relationship diagram to corresponding tables and normalized the tables DDL statements are used to create tables

Trang 7

Creating Tables

The SQL statement to create a table is CREATE TABLE For example, the following statement creates the customer table:

CREATE TABLE customer

(cust-no CHAR(5) NOT NULL,

name CHAR(15),

phone-number CHAR(7),

city CHAR(15),

PRIMARY KEY (cust-no))

The CREATE TABLE statement is followed by the table name, which is customer in this example Note that everything after the table name is enclosed in parentheses The information in parentheses includes the column names or attributes of the customer table The attributes in this example are cust-no, name, phone-number, and city CHAR signifies that the column cust-no will contain character type of data The size of the column is specified in parentheses after the name of the data type

The primary key is also defined in the above statement by using the PRIMARY KEY clause The clause here signifies that the attribute cust-no is the primary key The primary key should not contain any null values Therefore, the NOT NULL clause has been specified with the attribute cust-no The NOT NULL clause can be used to define any attribute as not null irrespective of whether the attribute is a primary key For example, if you do not want any null values in the name column, the CREATE TABLE statement can be modified as:

CREATE TABLE customer

(cust-no CHAR(5) NOT NULL,

name CHAR(15) NOT NULL,

phone-number CHAR(7),

city CHAR(15),

PRIMARY KEY (cust-no))

Now, you will look at creating a table whose attribute refers to another table For example, the SQL statement to create the sale table is:

CREATE TABLE sale

(cust-no CHAR(5),

prod-no CHAR(5),

qty DECIMAL(8,2),

PRIMARY KEY (cust-no, prod-no),

FOREIGN KEY (cust-no) REFERENCES customer,

FOREIGN KEY (prod-no) REFERENCES product)

If the primary key is made up of more than one attributes, then the attributes are named after the PRIMARY KEY clause and are separated by a comma In the above example, the primary key is made up of two attributes, cust-no and prod-no The FOREIGN KEY clause is followed by the name of the attribute The REFERENCES clause

is followed by the name of the table that the attribute references

Trang 8

Altering Tables

You can alter an existing table by using the ALTER TABLE statement For example, the SQL statement to include a column address in the customer table is:

ALTER TABLE customer ADD address CHAR(20)

The above statement adds a fifth column address in the customer table and assigns null to all values in the column You cannot specify the NOT NULL clause with the ALTER TABLE statement

Removing Tables

You can remove a table by using the DROP TABLE statement For example, the SQL statement to remove the customer table is:

DROP TABLE customer

When a table is created, the description of the table is stored in the system catalog The system catalog stores the names of tables, their attributes, and the data types of the attributes The system catalog also stores other details like the users of the tables Therefore, when you issue the DROP TABLE statement, the description of the specified table is removed from the system catalog

Trang 9

• The results of queries made on a table are also tables.

• DML statements in SQL are used to query tables and work on them

• SELECT is the most powerful DML statement of SQL All relational operations can be performed by using the SELECT statement.

• You can remove the duplicate rows in queries by using the DISTINCT clause.

• SQL can impose an order on the result of a query through the ORDER BY clause.

• A query in which data is retrieved from more than one table is called a join query.

• There are two types of joins, equi-join and self join.

• The aggregate functions in SQL are COUNT, SUM, AVG, MAX, and MIN.

Trang 10

Physical Database Design Lesson 9 / Slide 7 of 13

©NIIT

Physical Database Design

Query Results (Contd )

• SQL provides a clause IS NULL (or IS NOT NULL) for finding a null value

• A query within a query is called a subquery.

• The UNION operator of relational algebra is represented by the UNION clause in SQL.

• You can enter data in a table by using the INSERT statement

• SQL provides the UPDATE statement for updating data.

• SQL provides the DELETE statement to delete a row.

The results of queries made on a table are also tables For example, a query to list the names of all the customers in the customer table will result in the following table:

NAME

Tim Mary Johnson Ray Smith

Query Result

You will now learn about the four DML statements in SQL that are used to query tables and work on them These statements are: SELECT, INSERT, UPDATE, and DELETE

Trang 11

The SELECT Statement

SELECT is the most powerful DML statement of SQL All relational operations can be performed by using the SELECT statement

Query Result

This statement represents the project operator in relational algebra The statement extracts only the columns name and phone-number from the customer table The SELECT clause specifies the column list and the FROM clause specifies the table from which the columns are to be extracted The same query can be reformulated by using qualified column names as follows:

SELECT customer.name, customer.phone-number

WHERE city = “Boston”

This statement represents the restrict operator in relational algebra The asterisk (*) following the SELECT clause means that all the columns of the table will be displayed

Trang 12

However, the display of rows will be restricted to those that satisfy the condition (city

= “Boston”) The result of this statement will be:

Query Result

Notice that this query result does not have any duplicate rows

Trang 13

Retrieval with Ordering

Rows in a relation do not have any order However, SQL can impose an order on the result of a query through the ORDER BY clause For example, if you want to find the model names of the products whose price is less than $200, and you want to display the query result in ascending order of the city name, the SQL statement is:

Query Result

The default order is ascending order However, you can also specify descending order

by using the DESC clause as follows:

SELECT cust-no, name, phone-number, customer.city, prod-no, model, desc, price, product.city

Trang 14

FROM customer, product

WHERE customer.city = product.city

Notice that the connection between the tables customer and product has been defined

by using the join condition customer.city = product.city Also notice that the column

names are qualified to distinguish a column of one table from that of another This

represents the join operator of relational algebra

The result of the above query would be:

1795 New

York

ator 8484 Washington

SELECT customer.name, product.model

FROM customer, product

WHERE customer.city = product.city

The result of this query will have only the names of the customers and product models

that are from the same city

You can also specify an additional condition in a join operation For example,

SELECT customer.name, product.model

FROM customer, product

WHERE customer.city = product.city

AND price > 1500

Trang 15

Self Join

Self join is the joining of a table with itself The self join illustrates the power of SQL to perform complex queries For example, if you require pairs of names of all the customers who are located in the same city, the query would be:

SELECT first.cust-no, second.cust-no

FROM customer first, customer second

WHERE first.city = second.city

AND first.cust-no < second.cust-no

This query involves a self join The join takes place over matching cities To perform the query, you need two copies of the customer table You need to examine all possible pairs of customers, one from the first copy of the customer table and another from the second copy of the customer table

To distinguish between the two copies, two variables have been introduced, first and second The first variable represents the first copy of the customer table and second represents the second copy of the table This query is carried out by examining all possible pairs of values from first and second In each case, the WHERE condition is examined The condition AND first.cust-no < second.cust-no eliminates pairs of customer numbers of the form (x,x) It also ensures that pairs like (x,y) and (y,x) do not appear together

Aggregate Functions

There are some queries that cannot be answered by using the constructs of the SELECT statement that we have seen so far Examples of some such queries are:

„How many products have been sold?

„Which is the most expensive product?

„What is the total quantity that has been sold?

„What is the average price of the company’s products?

„Which is the cheapest product?

„What is the total sale transactions made?

All these queries can be answered by using the aggregate functions of SQL like SUM, AVG, and COUNT The answers to the above queries are as follows:

„How many products have been sold?

SELECT COUNT (DISTINCT prod-no)

FROM sale

Trang 16

The result of this query is 5.

„Which is the most expensive product?

SELECT prod-no, MAX (price)

FROM product

The result of this query is:

PROD-NO

P4900 12791

„What is the total quantity that has been sold?

SELECT SUM (qty)

FROM sale

The result of this query is 1027

„What is the average price of the company’s products?

SELECT AVG (price)

FROM product

The result of this query is 7397.67

„Which is the cheapest product?

SELECT prod-no, MIN (price)

The result of this query is 7

All aggregate functions, except COUNT(*), permit the use of the DISTINCT clause and ignore null values

If you want to know the total quantity sold for each product, the query would be:

SELECT prod-no, SUM (qty)

FROM sale

GROUP BY prod-no

Trang 17

The result of this query is:

PROD-NO

P5690 198 P3478 600 P7439 100 P4721 76

Query Result

The GROUP BY clause forms groups based on the column name you specify with this clause Within one group, all rows have the same value for the GROUP BY column In the above example, the sale table is grouped so that one group contains all the rows for one product number The SELECT clause then works on each group instead of each row in the table The expression in the SELECT clause must be single valued per group It should be the GROUP BY column itself or a function like SUM that reduces the values of the rows in the group to a single value The GROUP BY clause is different from the ORDER BY clause GROUP BY does not rearrange the output to display it in a particular order

If you want to display the product code for all products sold to more than one customer, the query would be:

Some Advanced Features of SQL

Trang 18

SQL provides a clause IS NULL (or IS NOT NULL) for finding a null value Therefore,

the above statement can be changed as:

SELECT *

FROM product

WHERE price IS NULL

The result of this query is:

Query Result

Subqueries

A query within a query is called a subquery For example, if you want the names of

the customers who buy the product P5690, the query would be:

First, the subquery that is given in parentheses is evaluated All the values of the

cust-no for which the prod-cust-no is P5690 are retrieved from the sale table A subset is

returned Then, the higher level query is evaluated All the names of the customers

are retrieved from the customer table for the values in the subset

The IN clause works on a set of values If the set has only one value, then IN will be equivalent to “=”

The result of the above query is the same as the result of the following join:

SELECT name

FROM customer, sale

WHERE customer.cust-no = sale.cust-no

AND sale.prod-no = “P5690”

Thus, there are two forms for the same query One form involves a subquery and the other involves a join This is true for most of the queries

You can nest subqueries to any depth For example, if you want the names of the

customers who have bought a television of any model, the query would be:

SELECT name

FROM customer

WHERE cust-no IN

Trang 19

WHERE desc = “television”))

Query Involving UNION

The UNION operator of relational algebra is represented by the UNION clause in SQL For example, if you want the codes of all products that are refrigerators or have been bought by customer C4171, the query would be:

You have looked at all the clauses used with the SELECT statement The complete syntax of the SELECT statement is:

SELECT [DISTINCT] item(s)

The INSERT Statement

After creating a table, you can enter data in it by using the INSERT statement The INSERT statement can also be used to insert a new row in an existing table For example, if you want to insert a row in the product table, the SQL statement would be:

INSERT INTO product (prod-no, name, desc, price)

VALUES (“P9980”, “Armstrong”, “VCR”, 12900)

This statement adds a new row in the product table with the values P9980, Armstrong, VCR, and 12990 in the prod-no, name, desc, and price columns respectively

Trang 20

The UPDATE Statement

SQL provides the UPDATE statement for updating data For example, if you want to change the price of product number P4721 to $900, the SQL statement is:

The DELETE Statement

SQL provides the DELETE statement to delete a row For example, if you want to

delete the rows of all customers who are based in New York, the SQL statement is:

DELETE

FROM customer

WHERE city = “New York”

Trang 21

• Views are defined by using the SQL statement CREATE VIEW.

• When a user makes a reference to a view, the DBMS finds the definition of the view stored in the database The DBMS then translates the user’s request into an equivalent request against the source tables of the view In this way, the DBMS maintains the illusion of the view.

• Views can be theoretically updatable or non-updatable

A view is a named, derived, virtual table that does not exist physically This is unlike a base table that is a real table and exists in physical storage You can consider a view

as a SQL query that is permanently stored in the database and assigned a name However, for the user, accessing a view is like accessing a base table The DBMS creates an illusion of a table by assigning a name to the view and storing its definition

in the database The tables that are the source of the data visible through the view are referred to as source tables The following figure shows two source tables and a view created by using these tables

Trang 22

EMP-CODE NAME DEPT BASIC AGE

Marketing 24

Mary

Mike Womack

Finance 25

View with Two Source Tables

Defining a View

Views are defined by using the SQL statement CREATE VIEW For example, the

following statement creates the name-dept view shown in the above figure

Trang 23

CREATE VIEW name-dept (name, department, age)

AS SELECT name, dept-name, age

FROM employee, department

WHERE employee.dept = department.dept-cd

The data in the name-dept view comes from the employee and department tables Hence, these two tables are the source tables for the view

When the CREATE VIEW statement is executed, the query following the AS clause is not executed Instead, it is simply saved in the catalog To the user it appears as though there is a real table called name-dept with rows and columns In fact, name-dept is a “window” into the real tables, employee and department Any changes you make to these tables will be visible through the name-dept view

If the names of the columns are not specified while defining a view, the view inherits the columns of its source tables However, sometimes, it is necessary to specify the column names For example, in the following statement, there can be no name inherited for the second column This is because the column is derived from a function Therefore, the column name must be explicitly specified

CREATE VIEW db (dept, avgbasic)

AS SELECT dept, AVG (basic)

The result of this query is:

Ngày đăng: 01/08/2014, 09:21

TỪ KHÓA LIÊN QUAN