Sunteți pe pagina 1din 53

Database Systems: Design,

Implementation, and
Management
Ninth Edition

Chapter 7
Introduction to Structured Query
Language (SQL)
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Data Manipulation Commands

• INSERT
• SELECT
• COMMIT
• UPDATE
• ROLLBACK
• DELETE

2
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Adding Table Rows

• INSERT
– Used to enter data into table
– Syntax: tablename
• INSERT INTO columnname
VALUES (value1, value2, … , valueN);

3
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Adding Table Rows (cont’d.)

• When entering values, notice that:


– Row contents are entered between parentheses
– Character and date values are entered between
apostrophes
– Numerical entries are not enclosed in
apostrophes
– Attribute entries are separated by commas
– A value is required for each column
• Use NULL for unknown values

4
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Adding Table Rows (cont’d.)
• Examples:
INSERT INTO VENDOR
VALUES (21225,'Bryson, Inc.','Smithson','615','223-
3234','TN','Y');

INSERT INTO VENDOR


VALUES (21226,'Superloo, Inc.','Flushing','904','215-
8995','FL','N');

INSERT INTO PRODUCT


VALUES ('11QER/31','Power painter, 15 psi., 3-nozzle','03-
NOV-11',8,5,109.99,0.00,21225);

INSERT INTO PRODUCT


VALUES ('13-Q2/P2','7.25-in. pwr. saw blade','13-DEC-
11',32,15,14.99, 0.05,21225);
5
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Adding Table Rows (cont’d.)
• Example (inserting rows with NULL attributes):
INSERT INTO PRODUCT
VALUES ('BRT-345','Titanium drill bit', '18-OCT-11', 75, 10,
4.50, 0.06, NULL);

• Example (inserting rows with optional


attributes):
– Assume that only P_CODE and P_DESCRIPT are
required attributes.
INSERT INTO PRODUCT(P_CODE, P_DESCRIPT)
VALUES ('BRT-345','Titanium drill bit');

6
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Saving Table Changes

• Changes made to table contents are not


physically saved on disk until:
– Database is closed
– Program is closed
– COMMIT command is used
• Syntax:
– COMMIT [WORK];
• Will permanently save any changes made to
any table in the database
7
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Listing Table Rows

• SELECT
– Used to list contents of table
– Syntax:
SELECT columnlist
FROM tablename;
• Columnlist represents one or more attributes,
separated by commas
• Asterisk can be used as wildcard character to
list all attributes
8
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Listing Table Rows (cont’d.)
• Examples:
SELECT * FROM PRODUCT;

SELECT P_CODE, P_DESCRIPT, P_INDATE, P_QOH, P_MIN, P_PRICE,


P_DISCOUNT, V_CODE
FROM PRODUCT;

9
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Updating Table Rows

• UPDATE
– Modify data in a table
– Syntax:
UPDATE tablename
SET columnname = expression [, columnname =
expression]
[WHERE conditionlist];
• If more than one attribute is to be updated in
row, separate corrections with commas

10
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Updating Table Rows (cont’d.)
• Examples:
UPDATE PRODUCT
SET P_INDATE = '18-JAN-2012'
WHERE P_CODE = '13-Q2/P2';

UPDATE PRODUCT
SET P_INDATE = '18-JAN-2012',
P_PRICE = 17.99,
P_MIN = 10
WHERE P_CODE = '13-Q2/P2';

11
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Restoring Table Contents

• ROLLBACK
– Undoes changes since last COMMIT
– Brings data back to prechange values
• Syntax:
ROLLBACK;
• COMMIT and ROLLBACK only work with
commands to add, modify, or delete table rows

12
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Deleting Table Rows

• DELETE
– Deletes a table row
– Syntax:
DELETE FROM tablename
[WHERE conditionlist ];
• WHERE condition is optional
• If WHERE condition is not specified, all rows
from specified table will be deleted

13
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Deleting Table Rows (cont’d.)
• Examples:
DELETE FROM PRODUCT
WHERE P_CODE = 'BRT-345';

DELETE FROM PRODUCT


WHERE P_MIN = 5;

14
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Inserting Table Rows with a
SELECT Subquery
• INSERT
– Inserts multiple rows from another table (source)
– Uses SELECT subquery
– Subquery: query embedded (or nested or inner)
inside another query
– Subquery executed first
– Syntax:
INSERT INTO tablename SELECT columnlist
FROM tablename;

15
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Inserting Table Rows with a
SELECT Subquery (cont’d.)
• Examples:
INSERT INTO VENDOR SELECT * FROM V;

INSERT INTO PRODUCT SELECT * FROM P;

16
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
SELECT Queries

• Fine-tune SELECT command by adding


restrictions to search criteria using:
– Conditional restrictions
– Arithmetic operators
– Logical operators
– Special operators

17
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Selecting Rows with
Conditional Restrictions
• Select partial table contents by placing
restrictions on rows to be included in output
– Add conditional restrictions to SELECT
statement, using WHERE clause
• Syntax:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE V_CODE = 21344;
18
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
19
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE V_CODE <> 21344;

SELECT P_DESCRIPT, P_QOH, P_MIN, P_PRICE


FROM PRODUCT
WHERE P_PRICE <= 10;

20
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Selecting Rows with
Conditional Restrictions (cont’d.)
• Using comparison operators on Character
attributes

SELECT P_CODE, P_DESCRIPT, P_QOH, P_MIN, P_PRICE


FROM PRODUCT
WHERE P_CODE < '1558-QW1';

21
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Selecting Rows with
Conditional Restrictions (cont’d.)
• Using comparison operators on dates
– Date procedures are often more software-
specific than other SQL procedures

SELECT P_DESCRIPT, P_QOH, P_MIN, P_PRICE, P_INDATE


FROM PRODUCT
WHERE P_INDATE >= '20-JAN-2012';

22
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Selecting Rows with
Conditional Restrictions (cont’d.)
• Using computed columns and column aliases
– SQL accepts any valid expressions (or formulas) in
the computed columns
– Alias: Alternate name given to a column or table in
any SQL statement
SELECT P_DESCRIPT, P_QOH, P_PRICE, P_QOH*P_PRICE FROM PRODUCT;

SELECT P_DESCRIPT, P_QOH, P_PRICE, P_QOH*P_PRICE AS TOTVALUE


FROM PRODUCT;

SELECT P_CODE, P_INDATE, SYSDATE - 90 AS CUTDATE


FROM PRODUCT
WHERE P_INDATE <= SYSDATE - 90;

SELECT P_CODE, P_INDATE, P_INDATE + 90 AS EXPDATE


FROM PRODUCT; 23
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Arithmetic Operators:
The Rule of Precedence
• Perform operations within parentheses
• Perform power operations
• Perform multiplications and divisions
• Perform additions and subtractions

24
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Logical Operators: AND, OR, and NOT

• Searching data involves multiple conditions


• Logical operators: AND, OR, and NOT
• Can be combined
– Parentheses enforce precedence order
• Conditions in parentheses are always executed
first
• Boolean algebra: mathematical field dedicated
to use of logical operators
• NOT negates result of conditional expression
25
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Logical Operators: AND, OR, and NOT
(cont’d.)
• Examples:
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE V_CODE = 21344 OR V_CODE = 24288;

SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE


FROM PRODUCT
WHERE P_PRICE < 50
AND P_INDATE > '15-JAN-2012';

SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE


FROM PRODUCT
WHERE (P_PRICE < 50 AND P_INDATE > '15-JAN-2012')
OR V_CODE = 24288;

SELECT *
FROM PRODUCT
WHERE NOT (V_CODE = 21344);
26
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Special Operators

• BETWEEN: checks whether attribute value is


within a range
• IS NULL: checks whether attribute value is null
• LIKE: checks whether attribute value matches
given string pattern
• IN: checks whether attribute value matches any
value within a value list
• EXISTS: checks if subquery returns any rows

27
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Special Operators (cont’d.)
• Examples:
SELECT *
FROM PRODUCT
WHERE P_PRICE BETWEEN 50.00 AND 100.00;

SELECT P_CODE, P_DESCRIPT, V_CODE


FROM PRODUCT
WHERE V_CODE IS NULL;

SELECT V_NAME, V_CONTACT, V_AREACODE, V_PHONE


FROM VENDOR
WHERE UPPER(V_CONTACT) LIKE 'SMITH%';

SELECT *
FROM PRODUCT
WHERE V_CODE IN (21344, 24288);

SELECT * FROM VENDOR


WHERE EXISTS (SELECT * FROM PRODUCT WHERE P_QOH < P_MIN*2);
28
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Advanced Data Definition Commands

• All changes in table structure are made by


using ALTER command
• Three options:
– ADD adds a column
– MODIFY changes column characteristics
– DROP deletes a column
• Can also be used to:
– Add table constraints
– Remove table constraints
29
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Changing a Column’s Data Type

• ALTER can be used to change data type


• Some RDBMSs do not permit changes to data
types unless column is empty

ALTER TABLE PRODUCT


MODIFY (V_CODE CHAR(5));

30
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Changing a Column’s Data
Characteristics
• Use ALTER to change data characteristics
• Changes in column’s characteristics are
permitted if changes do not alter the existing
data type
ALTER TABLE PRODUCT
MODIFY (P_PRICE DECIMAL(9,2));

31
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Adding a Column
Dropping a Column
• Use ALTER to add column
– Do not include the NOT NULL clause for new
column
• Use ALTER to drop column
– Some RDBMSs impose restrictions on the
deletion of an attribute
ALTER TABLE PRODUCT
ADD (P_SALECODE CHAR(1));

ALTER TABLE VENDOR


DROP COLUMN V_ORDER;

32
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Advanced Data Updates

• UPDATE command updates only data in


existing rows
• If relationship between entries and existing
columns, can assign values to slots
• Arithmetic operators are useful in data updates
• In Oracle, ROLLBACK command undoes
changes made by last two UPDATE statements

33
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Copying Parts of Tables
• SQL permits copying contents of selected table
columns
– Data need not be reentered manually into newly
created table(s)
• First create the table structure
• Next add rows to new table using table rows
from another table
CREATE TABLE PART AS
SELECT P_CODE AS PART_CODE,
P_DESCRIPT AS PART_DESCRIPT,
P_PRICE AS PART_PRICE,
V_CODE
FROM PRODUCT;
34
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Adding Primary and Foreign Key
Designations
• When table is copied, integrity rules do not copy
– Primary and foreign keys are manually defined on
new table
• User ALTER TABLE command
– Syntax:
• ALTER TABLE tablename ADD PRIMARY
KEY(fieldname);
– For foreign key, use FOREIGN KEY in place of
PRIMARY KEY
ALTER TABLE PART
ADD PRIMARY KEY(P_CODE)
ADD FOREIGN KEY (V_CODE) REFERENCES VENDOR;
35
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Deleting a Table from the Database
• DROP
– Deletes table from database
– Syntax:
• DROP TABLE tablename;
• Can drop a table only if it is not the “one” side
of any relationship
– Otherwise, RDBMS generates an error message
– Foreign key integrity violation

DROP TABLE PART;

36
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Additional SELECT Query Keywords

• Logical operators work well in the query


environment
• SQL provides useful functions that:
– Count
– Find minimum and maximum values
– Calculate averages, etc.
• SQL allows user to limit queries to:
– Entries having no duplicates
– Entries whose duplicates may be grouped
37
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Ordering a Listing
• ORDER BY clause is useful when listing order
is important
• Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[ORDER BY columnlist [ASC | DESC]];
• Ascending order by default

38
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Ordering a Listing (cont’d.)
• Examples:
SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE
FROM PRODUCT
ORDER BY P_PRICE

SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE


FROM PRODUCT
ORDER BY P_PRICE DESC

SELECT EMP_LNAME, EMP_FNAME, EMP_INITIAL, EMP_AREACODE,


EMP_PHONE
FROM EMPLOYEE
ORDER BY EMP_LNAME, EMP_FNAME, EMP_INITIAL

SELECT P_DESCRIPT, V_CODE, P_INDATE, P_PRICE


FROM PRODUCT
WHERE P_INDATE < '21-JAN-2012'
AND P_PRICE <= 50.00
ORDER BY V_CODE, P_PRICE DESC 39
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Listing Unique Values
• DISTINCT clause produces list of only values
that are different from one another
• Example:
SELECT DISTINCT V_CODE
FROM PRODUCT;
• Access places nulls at the top of the list
– Oracle places it at the bottom
– Placement of nulls does not affect list contents
SELECT DISTINCT V_STATE FROM VENDOR;

SELECT DISTINCT V_STATE, V_AREACODE FROM VENDOR;

40
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Aggregate Functions
• COUNT function tallies number of non-null
values of an attribute
– Takes one parameter: usually a column name
• MAX and MIN find highest (lowest) value in a
table
– Compute MAX value in inner query
– Compare to each value returned by the query
• SUM computes total sum for any specified
attribute
• AVG function format is similar to MIN and MAX
41
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Aggregate Functions (cont’d.)
• Examples:
– Q: How many vendors provide products?
SELECT COUNT(DISTINCT V_CODE) FROM PRODUCT;

– Q: How many products with price < 10?


SELECT COUNT(*) FROM PRODUCT
WHERE P_PRICE <= 10.00;
– Q: Select the product with the max/min price in
the product table.
SELECT * FROM PRODUCT
WHERE P_PRICE = (SELECT MAX(P_PRICE) FROM PRODUCT);

SELECT * FROM PRODUCT


WHERE P_PRICE = (SELECT MIN(P_PRICE) FROM PRODUCT);

42
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Aggregate Functions (cont’d.)
• Examples:
– Q: How much is the total customer balance?
SELECT SUM(CUS_BALANCE) AS TOTBALANCE FROM CUSTOMER;
– Q: How much is the total value of our product
inventory?
SELECT SUM(P_QOH*P_PRICE) AS TOTVALUE
FROM PRODUCT;
– Q: What is the average product price?
SELECT AVG(P_PRICE) FROM PRODUCT;
– Q: What products have a price that exceeds the
average product price?
SELECT P_CODE, P_DESCRIPT, P_QOH, P_PRICE, V_CODE
FROM PRODUCT
WHERE P_PRICE > (SELECT AVG(P_PRICE) FROM PRODUCT)
ORDER BY P_PRICE DESC;
43
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Grouping Data

• Frequency distributions created by GROUP BY


clause within SELECT statement.
• Must be used with aggregate functions.
• Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[GROUP BY columnlist]
[HAVING conditionlist]
[ORDER BY columnlist [ASC | DESC] ] ; 44
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Grouping Data (cont’d.)
• Examples:
– Q: How many products each vendor provides?
SELECT V_CODE, COUNT(DISTINCT P_CODE)
FROM PRODUCT
GROUP BY V_CODE;
– Q: List the number of products by vendor with the
average price
SELECT V_CODE, COUNT(DISTINCT P_CODE), AVG(P_PRICE)
FROM PRODUCT
GROUP BY V_CODE;

– List the number of products by vendor with the average


price, include only the rows with price below 10.00.
SELECT V_CODE, COUNT(DISTINCT P_CODE), AVG(P_PRICE)
FROM PRODUCT
GROUP BY V_CODE
HAVING AVG(P_PRICE) < 10;
45
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Joining Database Tables

• Joining tables is the most important distinction


between relational database and other DBs
• Join is performed when data are retrieved from
more than one table at a time
– Equality comparison between foreign key and
primary key of related tables
• Join tables by listing tables in FROM clause of
SELECT statement
– DBMS creates Cartesian product of every table

46
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Joining Database Tables (cont’d.)
• Examples:
– Q: List the product description, price, vendor code,
name, contact, area code and phone for each product.
SELECT P_DESCRIPT, P_PRICE, VENDOR.V_CODE, V_NAME,
V_CONTACT, V_AREACODE, V_PHONE
FROM PRODUCT, VENDOR
WHERE PRODUCT.V_CODE = VENDOR.V_CODE;

– List products with vendor data for products purchased


after 15-JAN-2010.
SELECT P_DESCRIPT, P_PRICE, V_NAME, V_CONTACT,
V_AREACODE, V_PHONE
FROM PRODUCT, VENDOR
WHERE PRODUCT.V_CODE = VENDOR.V_CODE
AND P_INDATE > '15-JAN-2012';

47
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Joining Tables with an Alias

• Alias identifies the source table from which data


are taken
• Alias can be used to identify source table
• Any legal table name can be used as alias
• Add alias after table name in FROM clause
– FROM tablename alias
SELECT P_DESCRIPT, P_PRICE, V_NAME, V_CONTACT,
V_AREACODE, V_PHONE
FROM PRODUCT P, VENDOR V
WHERE P.V_CODE = V.V_CODE
ORDER BY P_PRICE;
48
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Recursive Joins

• Alias is especially useful when a table must be


joined to itself
– Recursive query
– Use aliases to differentiate the table from itself

SELECT E.EMP_MGR, M.EMP_LNAME,E.EMP_NUM, E.EMP_LNAME


FROM EMP E, EMP M
WHERE E.EMP_MGR=M.EMP_NUM
ORDER BY E.EMP_MGR;

49
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary

• SQL commands can be divided into two overall


categories:
– Data definition language commands
– Data manipulation language commands
• The ANSI standard data types are supported by
all RDBMS vendors in different ways
• Basic data definition commands allow you to
create tables and indexes

50
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary (cont’d.)

• DML commands allow you to add, modify, and


delete rows from tables
• The basic DML commands:
– SELECT, INSERT, UPDATE, DELETE,
COMMIT, and ROLLBACK
• SELECT statement is main data retrieval
command in SQL

51
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary (cont’d.)

• WHERE clause can be used with SELECT,


UPDATE, and DELETE statements
• Aggregate functions
– Special functions that perform arithmetic
computations over a set of rows
• ORDER BY clause
– Used to sort output of SELECT statement
– Can sort by one or more columns
– Ascending or descending order
52
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary (cont’d.)

• Join output of multiple tables with SELECT


statement
– Join performed every time you specify two or
more tables in FROM clause
– If no join condition is specified, DBMX performs
Cartesian product
• Natural join uses join condition to match only
rows with equal values in specified columns

53
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition.
May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.

S-ar putea să vă placă și