Upload
lyre
View
33
Download
0
Embed Size (px)
DESCRIPTION
Database Languages. Part – II Data Manipulation Language. C3. Super-Brat. 30 New St Luton. Cust_No. Name. Address. C1. Nippers Ltd. 25 High St, Leeds. C2. Tots-Gear. 5 Low, Oxford. C3. Super-Brat. 30 New St Luton. C6. Tiny-Togs. 1 Old Rd, Luton. Customer. - PowerPoint PPT Presentation
Citation preview
Database Languages
Part – II
Data Manipulation
Language
Data Manipulation Language
3.5 String matching
Fuzzy matching: NAME LIKE ‘S%’• Select the tuples with name starting with S.
Customer
Cust_No Name Address
C1 Nippers Ltd 25 High St, Leeds
C2 Tots-Gear 5 Low, Oxford
C3 Super-Brat 30 New St Luton
C6 Tiny-Togs 1 Old Rd, Luton
C3 Super-Brat 30 New St Luton
Data Manipulation Language
3.5 String matching
% represents an arbitrary string.- represents a single arbitrary character.[] square brackets may contain any number
of letters, each of which is tested individually.
B%; B_OWN; B[RLO]OWN; [N_P]%
Data Manipulation Language
3.6 Testing for null values
SELECT *FROM PRODUCTWHERE COLOUR IS NULL
SELECT *FROM PRODUCTWHERE COLOUR = ‘ ’
They are not same!
Data Manipulation Language
3.7 Computation
+ add- subtract* multiple/ divide** to the power of
abs absoluteatan arctangentcos cosineexp exponentiallog natural logarith
mmod modulesin sinesqrt square root
Data Manipulation Language
3.8 Summarising functions
AVG averageMAX largest value of an attributeMIN minimum value of an attributeSUM total of the values of an attributesCOUNT count the number of value in an
attribute
Data Manipulation Language
3.8 Summarising functions
Example: SELECT AVG(QTY) FROM STOCK SELECT COUNT (*) FROM STOCK
WAREHOUSE_NO BIN_NO PRODUCT_NO QTY
WH1 1 P1 300
WH1 2 P2 50
WH2 1 P2 250
STOCK
200
3
Data Manipulation Language
3.9 GROUP BY: used to specific groupings of tuples.
SELECT PRODUCT_NO, IN_STOCK = SUM(QTY)FROM STOCKGROUP BY PRODUCT_NO
WAREHOUSE_NO BIN_NO PRODUCT_NO QTY
WH1 1 P1 300
WH1 2 P2 50
WH2 1 P2 250
STOCK
PRODUCT_NO IN_STOCK
P1 300
P2 300
Data Manipulation Language
3.9 ORDER BY: on groupings of tuples.
SELECT * FROM PRODUCT ORDER BY COLOURPRODUCT
PRODUCT_NO NAME COLOUR
P1 Pantaloons blue
P7 Socks bule
P4 Socks harebell
P2 Pantaloons khaki
P3 Socks white
PRODUCT
PRODUCT_NO NAME COLOUR
P1 Pantaloons blue
P2 Pantaloons khaki
P3 Socks white
P4 Socks harebell
P7 Socks bule
Data Manipulation Language
3.10 Retrieval from two relations
The join operator of relational algebra makes it possible to define a relation which is made up of data taken from more than one relation.
Data Manipulation Language
3.10 Equi-join
Combines tuples in two relations when specified attributes have the same value
SELECT PRODUCT.*, ORDER_LINE.*FROM PRODUCT, ORDER_LINEWHERE
PRODUCT.PRODUCT_NO=ORDER_LINE.PRODUCT_NO;
Data Manipulation Language
PRODUCT
PRODUCT_NO NAME COLOUR
P1 Pantaloons blue
P2 Pantaloons khaki
P3 Socks white
P4 Socks harebell
P7 Socks bule
ORDER_LINE
ORDER_NO PRODUCT_NO
QUANTITY
01 P1 100
01 P2 20000
01 P6 20
02 P2 1000
02 P6 10000
PRODUCT.PRODUCT_NO
NAME COLOUR ORDER_NO ORDER_LINE.PRODUCT_NO
QUANTITY
P1 Pantaloons blue 01 P1 100
P2 Pantaloons khaki 01 P2 20000
P2 Pantaloons khaki 01 P2 1000
Data Manipulation Language
3.11 Correlation Names
Sometimes, you may want to use a shorter name
SELECT P.*, O.*FROM PRODUCT P, ORDER_LINE OWHERE P.PRODUCT_NO = O.PRODUCT_NO;
Data Manipulation Language
3.12 Natural join
Combines tuples of relations!SELECT P.*, O.ORDER_NO, QTYFROM PRODUCT P, ORDER_LINE OWHERE P.PRODUCT_NO = O.PRODUCT_NO;
PRODUCT.PRODUCT_NO
NAME COLOUR ORDER_NO QTY
P1 Pantaloons blue 01 100
P2 Pantaloons khaki 01 20000
P2 Pantaloons khaki 01 1000
Data Manipulation Language
3.13 Theta-join
SELECT S.*, O.*FROM STOCK_TOTAL S, ORDER_LINE OWHERE S.QUANTITY > O.QUANTITY;
3.14 Outer-join: retain all the tuples (pp.105)
SELECT S.*, O.*FROM STOCK_TOTAL S, ORDER_LINE OWHERE S.PRODUCT_NO = O.PRODUCT_NO (+);
Data Manipulation Language
3.15 Example
SELECT O.*FROM PRODUCT P, ORDER_LINE OWHERE P.PRODUCT_NO = O.PRODUCT_NO AND P.NAME = ‘Pantaloons’;PRODUCT
PRODUCT_NO NAME COLOUR
P1 Pantaloons blue
P2 Pantaloons khaki
P3 Socks white
P4 Socks harebell
P7 Socks bule
ORDER_LINE
ORDER_NO PRODUCT_NO
QUANTITY
01 P1 100
01 P2 20000
01 P6 20
02 P2 1000
02 P6 10000
Data Manipulation Language
3.16 Comparing Rows
SELECT F.PRODUCT_NO S.PRODUCT_NO, NAMEFROM PRODUCT F, PRODUCT SWHERE F.NAME = S.NAME AND F.PRODUCT_NO > S.PRODUCT_NO;
FPRODUCT_NO
SPRODUCT_NO
SNAME
P2 P1 Pantaloons
P4 P3 Socks
P7 P3 Socks
P9 P3 Socks
P7 P4 Socks
P9 P4 Socks
P9 P7 Socks
Data Manipulation Language
3.17 Queries on more than two relations
SELECT P.NAME, COLOURFROM CUSTOMER C, SALES_ORDER S, ORDER_LINE
O, PRODUCT PWHERE CUSTOMER.NAME = ‘Nippers Ltd’ AND C.CUSTOMER_NO = S.CUSTOMER_NO AND S.ORDER_NO = O.ORDER_NO AND S.PRODUCT_NO= P.PRODUCT_NO;
Data Manipulation Language
3.18 Nested SELECTsA SELECT statement is included within the WHERE
clause.SELECT P.NAME, COLOURFROM PRODUCT P, ORDER_LINE OWHERE P.PRODUCT_NO=O.PRODUCT_NO AND O.ORDER_NO=’01’SELECT NAME, COLOUR
FROM PRODUCTWHERE PRODUCT_NO IN (SELECT PRODUCT_NO FROM ORDER_LINE WHERE ORDER_NO = ’01’);
Data Manipulation Language
3.18.1 Qualified comparisonSELECT target_list_1FROM List_of_relations_1WHERE attribute IN (SELECT target_list_2 FROM list_of_relations_2 WHERE condition);
The inner SELECT retrieves a set of values, and the outer SELECT retrieves tuples where (using IN) specific attribute values are member of that set.
Data Manipulation Language
3.18.1 Qualified comparison
SELECT *FROM EmployeeWHERE AGE > ALL (SELECT AGE FROM Employee WHERE
DEPT_NO=‘D1’);
Understand the ALL and ANY!
SELECT *FROM EmployeeWHERE DEPT_NO <> ‘D1’ AND
AGE < ANY (SELECT AGE FROM Employee WHERE DEPT_NO=‘D1’);
Data Manipulation Language
3.18.2 Nested SELECT on primary key
=, <, >, !=, <=, >= Could be applied for primary key.SELECT *FROM EmployeeWHERE NAME = (SELECT NAME FROM PRODUCT WHERE PRODUCT_NO=‘P1’);
Primary key
Data Manipulation Language
3.19 Changing values in the database
UPDATEDELETEINSERT
UPDATE PRODUCTSET COLOUR = ‘violet’, PRICE = PRICE+0.2WHERE COLOUR =‘blue’;
INSERT INTO PRODUCT (PRODUCT_NO, NAME, PRICE)
VALUES (‘P10’, ‘Shirt’, 4.95);DELETE FROM PRODUCT WHERE COLOUR =
‘blue’;
Data Manipulation Language
3.20 Views of database
A virtual relation is a relation that appears to the user to exist, and can be manipulated as if it were a base relation, but which does not actually exist.
It is a relation retrieved from other relations in the database.
A virtual relation is called a View, and SQL include CREATE VIEW and DROP VIEW to create and delete it.
Data Manipulation Language
3.20 Views of database
WAREHOUSE_NO BIN_NO PRODUCT_NO QTY
WHI 1 P1 100
WH1 2 P2 50
WH2 1 P2 50
CREATE VIEW WH1_STOCK_AAS SELECT BIN_NO, PRODUCT_NO, QTYFROM STOCKWHERE WAREHOUSE_NO = ‘WH1’;
STOCKBIN_NO PRODUCT_NO QTY
1 P1 100
2 P2 50
WH1_STOCK_A
Data Manipulation Language
3.20 Views of database
CREATE VIEW WH1_STOCK_CAS SELECT WAREHOUSE_NO, BIN_NO,
PRODUCT_NO, NAME, QTY FROM STOCK PRODUCT
WHERE STOCK.PRODUCT_NO=PRODUCT.PRODUCT_NO AND WAREHOUSE_NO = ‘WH1’;
Data Manipulation Language
3.21 Integrity constraints
Integrity constraints are rules that must be obeyed by all tuples in the database.
CREATE INTEGRITY ON ORDER_LINE IS QUANTITY > 0 AND QUALTITY <= 10000;
Value out of range (0,10000) will be not allowed.
Database Language
3.22 SUMMARY
SQL has two parts:1) SDL: schema definition language2) DML: data manipulation language
Reading:
Exercises
Chapter 3
3.7-3.13