Topic 7 SQL
Topic 7 SQL
Topic 7 SQL
Introduction to
Structured Query Language (SQL)
Contents
Nonprocedural language
UNIQUE constraint
Ensures that all values in column are unique
Referential Integrity
◼ FOREIGN KEY
SQL Indexes
Example:
Example:
ALTER TABLE VENDOR
DROP COLUMN V_ORDER;
Changing a Column’s Data Type
ALTER can be used to change data type
Some RDBMSs (such as Oracle) do not permit changes to
data types unless column to be changed is empty
Syntax:
ALTER TABLE tablename
MODIFY columnname datatype;
Example:
ALTER TABLE PRODUCT
MODIFY (V_CODE CHAR(5));
Changing a Column’s Data Characteristics
INSERT
Used to enter data into table
Syntax:
INSERT INTO tablename
VALUES (value1, value2, … , value n);
Example:
INSERT INTO VENDOR
VALUES (21503,‘Bryson, Inc.’,‘Smithson’,‘615’,‘223-3234’,
‘TN’ , ‘Y’);
INSERT DATA INTO A TABLE
INSERT INTO tablename
VALUES (dataColumn1, dataColumn2,…………. .);
Example
UPDATE PRODUCT
SET P_INDATE = ‘2008/1/18’
WHERE P_CODE = ‘13-Q2/P2’;
Updating Table Rows (continued)
If more than one attribute is to be updated in the row,
separate corrections with commas
Example
UPDATE PRODUCT
SET P_INDATE = ‘2008/1/18’, P_PRICE = 17.99, P_MIN =10
WHERE P_CODE = ‘13-Q2/P2’;
Deleting Table Rows
DELETE
Deletes a table row
Syntax:
DELETE FROM tablename
WHERE columname = columnvalue;
or
Syntax:
◼ INSERT INTO tablename SELECT columnlist FROM
tablename;
SELECT QUERIES
Selecting Rows with Conditional Restrictions
Comparison operators
Logical operators
Special operators
Selecting Rows with
Conditional Restrictions
Syntax:
SELECT columnname(s)
FROM tablename
WHERE condition(s);
Selecting Rows with
Conditional Restrictions
Conditional Restrictions can be done using:
Comparison Operators
= Equal to
< Less than
Special Operators
<= Less than or equal
BETWEEN Check whether an attribute value is
to
within the range
> Greater than
IS NULL Check whether an attribute value is null
>= Greater than or
LIKE Check whether an attribute value
equal to
matches a given string pattern
<> or != Not equal to
IN Check whether an attribute value
Logical Operators matches any value within a value list
AND Both conditions must be EXISTS Check whether a subquery returns any
true rows
OR Both or either one
condition must be true
NOT Not equal to
Restriction Using Comparison Operators
Example:
Equal to
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE V_CODE = 21344;
Not equal to
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE V_CODE <> 21344;
Restriction Using Comparison Operators
Example:
Using Comparison Operator on Character Attributes
SELECT P_CODE, P_DESCRIPT, P_QOH, P_MIN, P_PRICE
FROM PRODUCT
WHERE P_CODE < '1558-QW1';
Example:
Computed Columns
SELECT P_DESCRIPT, P_QOH, P_PRICE, P_QOH * P_PRICE
FROM PRODUCT;
Example:
OR Operator
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE V_CODE = 21344 OR V_CODE = 24288;
AND Operator
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE P_PRICE<50 AND P_INDATE > '2008/01/01';
Restriction Using Logical Operators
Example:
Combine AND & OR Operator
Example:
NOT Operator
SELECT *
FROM PRODUCT
WHERE NOT(V_CODE = 21344);
Restriction Using Special Operators
BETWEEN
Used to check whether attribute value is within a range
Example:
SELECT *
FROM PRODUCT
WHERE P_PRICE BETWEEN 20.00 AND 150.00;
Restriction Using Special Operators
IS NULL
Used to check whether attribute value is null
Example:
LIKE
Used to check whether attribute value matches given
string pattern
Used with Wildcards (%) and (_)
LIKE (cont.)
IN
Used to check whether attribute value matches any
value within a value list
SELECT *
FROM PRODUCT
WHERE V_CODE IN (21344, 24288);
Restriction Using Special Operators
EXISTS
Used to check if subquery returns any rows
SELECT *
FROM VENDOR
WHERE EXISTS (SELECT * FROM PRODUCT WHERE
P_QOH <= P_MIN);
Copying Parts of Tables
First you need to create the new table then copy the data
from original table to the newly created one
Copying Parts of Tables
Example
Create a new table PART and Copy rows from PRODUCT
Syntax:
INSERT INTO target_tablename (target_columns)
SELECT source_columns
FROM source_tablename;
Example:
INSERT INTO PART(PART_CODE, PART_DESCRIPT, PART_PRICE, V_CODE)
SELECT P_CODE, P_DESCRIPT, P_PRICE, V_CODE
FROM PRODUCT;
Copying Parts of Tables
Adding Primary and
Foreign Key Designations
When table is copied, integrity rules do not copy, so primary and
foreign keys need to be manually defined on new table
Syntax:
◼ Primary key
ALTER TABLE tablename
ADD PRIMARY KEY(columnname);
◼ Foreign key
ALTER TABLE tablename
ADD FOREIGN KEY(columnname) REFERENCES (tablename);
Adding Primary and
Foreign Key Designations
Example:
ALTER TABLE PART
ADD PRIMARY KEY(PART_CODE);
DROP
Deletes table from database
Syntax:
DROP TABLE tablename;
Example:
DROP TABLE PART;
Note: a table can be dropped if it is not the ‘ONE’ side of the relationship
Advanced Select Queries
❑ MIN
Listing unique values ❑ MAX
DISTINCT ❑ SUM
❑ AVG
Grouping data
GROUP BY
Ordering a listing (ORDER BY)
List table contents in order (used when the listing order is important)
Syntax:
SELECT columnname(s)
FROM tablename
WHERE condition(s)
ORDER BY columnname(s);
Example:
SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE
FROM PRODUCT
ORDER BY P_PRICE;
Ordering a Listing
Ordering a listing
To sort descending
Syntax:
SELECT DISTINCT columnname
FROM tablename;
Example:
SELECT DISTINCT V_CODE
FROM PRODUCT;
Listing Unique Values
Aggregate Functions
COUNT (Output Examples)
MAX and MIN (Output Examples)
SUM (Output Examples)
AVG (Output Examples)
Grouping Data (GROUP BY)
Syntax:
SELECT columnname(s)
FROM tablename
GROUP BY columnname(s);
Example:
SELECT P_SALECODE, MIN( P_PRICE)
FROM PRODUCT
GROUP BY P_SALECODE;
Grouping Data (GROUP BY)
Note:
GROUP BY valid only when used in conjunction with one of the
SQL aggregate function (MIN, MAX, SUM, AVG and COUNT)
Example (Valid) :
SELECT P_SALECODE, AVG( P_PRICE)
FROM PRODUCT
GROUP BY P_SALECODE;
Note:
The SELECT columname(s) must include a combination of
columnname(s) and aggregate function
◼ SELECT P_SALECODE, AVG( P_PRICE)
SELECT columnname(s)
FROM tablename(s)
WHERE condition(s)
GROUP BY columnname(s)
HAVING condition(s)
ORDER BY columnname(s) ASC|DESC;
SQL Syntax (Example)
Syntax:
CREATE VIEW viewname AS SELECT query
Example:
List product description, product price and product quantity on
hand for product with prices more than $50.00
Syntax:
SELECT <table name1>.<column name(s)>,
<table name2>.<column name(s)>
FROM <table name1>, <table name 2>
WHERE <table name1>.<primary key> = <table name 2>.<foreign key>;
The Results of a Join
SQL Command
ORDER BY E.EMP_MGR;
Using an Alias to Join Table to Itself
Subqueries and Correlated Queries
A subquery is a query (SELECT statement) inside a query
A subquery is normally expressed inside parentheses
The first query in the SQL statement is known as the outer query
The query inside the SQL statement is known as the inner query
The inner query is executed first
The output of an inner query is used as the input for the outer query
The entire SQL statement is sometimes referred to as a nested query
UPDATE PRODUCT
SET P_PRICE = (SELECT AVG(P_PRICE) FROM PRODUCT)
WHERE V_CODE IN (SELECT V_CODE FROM VENDOR WHERE V_AREACODE = ‘615’)