Вы находитесь на странице: 1из 137

DB2

eds
Electronic Data Systems Version 1.0

DB2

Volume I

Course Objectives
After completing this course you should be able to List and describe the major functions, components and data management techniques for DB2 Describe DB2s SQL and its efficient use with 3GL languages like COBOL Use DB2 associated facilities like LOAD, RUNSTATS

DB2 : Some History


Research : System R

1983

: DB2 Version 1

Research : System R* Starburst 1995 : DB2 version 4

DB2 and MVS


IMS Terminal CICS Terminal TSO Terminal

IMS/DC

CICS

TSO

TSO

IMS Batch Application

IMS Online Application

CICS Online Application

TSO Online Application

TSO Batch Application

CAF Application

IMS/DB

DB2

MVS
IMS Databases DB2 Databases

Course Plan
Introduction to Relational Model DB2 Data Management DB2 Data Management DB2 Functions and Components SQL Data Definition Language SQL Data Manipulation Language DB2 Application Programming DB2 Program Preparation DB2 Efficient SQL Coding DB2 Security

Relational Model

An overview

Relational Model
Relational Data Structure Table
Relational Data Integrity Entity Integrity Referential Integrity

Relational Data Manipulation Relational Algebra

Relational Data Structure.


Attribute

Emp Name
2000 3000 1000 2500 Vijay Gupta Sridevi Patel

Age
33 24 29 29

Sex
M M F M

Dept#
101 102 101 103

Join_Date
01/01/85 01/01/85 01/02/96 01/01/88

Properties of Tables
Each row-column entry in table consists of an atomic data element All the data elements in a given column are of the dame data type Each column in a table has a unique name Sequence of rows in the table is not meaningful All the rows in the table are unique.

Missing Values : NULL


Column Value Not Applicable Not Available

Emp Name 2000 Vijay 3000 Gupta 4000 Sridevi

Project GE ? HP

Join_Date 01/01/85 01/01/96 01/02/95

Relational Data Integrity


Primary Key Uniqueness Minimality Alternate Key Foreign Key

Foreign Key
Department Dept Name 101 Marketing 102 Personnel 103 Admin 104 Training Location Chennai Mumbai Chennai Chennai

Emp Name 2000 Vijay 3000 Gupta 4000 Sridevi

Dept 101 102 103

Join_Date 01/01/85 01/01/96 01/02/95

Dept # Primary Key of Department table Foreign Key of Employee table

Integrity Constraints
Entity integrity
No column in the primary key can have a NULL value Referential Integrity

Every foreign key value must either match a primary key value of its associated parent table or it must be wholly NULL .

Relational Algebra
Restrict Union Project Intersection Properties They operate on all entire table; they do not operate on individual rows of the table. Join Divide Difference Product

When the relational operator is applied to on or more tables, the operation results in another table

RESTRICT
Emp 2000 3000 4000 Name Project Vijay GE Gupta ? Sridevi HP Join_Date 01/01/85 01/01/96 01/02/95
Join_date >= 1/01/96

Emp Name Project Join_Date 3000 Gupta ? 01/01/96

Project
Emp# 2000 3000 4000
Emp # 2000 3000 4000

Name Vijay Gupta Sridevi

Project GE ? HP

Join_Date 01/01/85 01/01/96 01/02/95

Join_Date 01/01/85 01/01/96 01/02/95


Project Emp # Join_date

JOIN
a1 b1

a2 b1
a3 b2
JOIN

a1 a2 a3

b1 b2 b3

c1 c2 c3

b1 c1 b2 c2 b3 c3
INNER LEFT OUTER JOIN OUTER

FULL OUTER RIGHT OUTER

INNER JOIN
Emp# 2000 3000 4000 Name Vijay Gupta Sridevi Dept# 101 102 104 Join on Dept # Emp# 2000 3000 Name Vijay Gupta Dept # Join_Date 101 01/01/85 102 01/01/96 Name Marketing Personnel Location Chennai Mumbai Dept # 101 102 103 Name Marketing Personnel Training Join_Date 01/01/85 01/01/96 01/02/95 Location Chennai Mumbai Chennai

Left Outer JOIN


Emp# Name 2000 Vijay 3000 Gupta 4000 Sridevi Dept# 101 102 104 Dept 101 102 103 Join_Date 01/01/85 01/01/96 01/02/95 Location Chennai Mumbai Chennai

Name Marketing Personnel Training

Join on Dept #

Emp# 2000 3000 4000

Name Vijay Gupta Sridevi

Dept# 101 102 103

Join_Date Name Location 01/01/85 Marketing Chennai 01/01/96 Personnel Mumbai 01/02/95 ? ?

Right Outer JOIN


Emp# 2000 3000 4000 Name Vijay Gupta Sridevi Dept# 101 102 104 Join on Dept # Emp# Name 2000 Vijay 3000 Gupta ? ? Dept 101 102 103 Name Marketing Personnel Training Join_Date 01/01/85 01/01/96 01/02/95 Location Chennai Mumbai Chennai

Dept# 101 102 104

Join_Date Name 01/01/85 Marketing 01/01/96 Personnel ? Training

Location Chennai Mumbai Madras

Full Outer JOIN


Emp# 2000 3000 4000 Dept# 101 102 104 Join on Dept # Emp# 2000 3000 4000 ? Name Vijay Gupta Sridevi ? Dept# 101 102 103 104 Join_Date 01/01/85 01/01/96 01/02/95 ? Name Marketing Personnel ? Training Location Chennai Mumbai ? Madras Name Vijay Gupta Sridevi Name Marketing Personnel Training Dept 101 102 103 Location Chennai Mumbai Chennai Join_Date 01/01/85 01/01/96 01/02/95

DIVIDE

a a a b c

x y z x y

x
y

Divide
a

UNION
Emp # 2000 3000 Name Patel Gupta Emp # 2000 Name Patel

3000

Gupta
Singh Gupta Hari Guru

UNION

2500 3000 5000 6000

Emp #

Name

2500
3000 5000 6000

Singh
Gupta Hari Guru

INTERSECTION
Project 1 Emp # 2000 3000 Name Patel Gupta INTERSECTION

Project 2

Emp # 2500 3000

Name Singh Gupta Emp # 3000 Name Gupta

5000
6000

Hari
Guru

DIFFERENCE
Project1 Emp # 2000 Name Patel

3000

Gupta

Project 2

Difference

Emp # 2500

Name Singh

3000
5000 6000

Gupta
Hari Guru

Emp #

Name

2000

Patel

CARTESIAN PRODUCT
a x y

a b
c
PRODUCT

b
b

x
y

c
x y c

x
y

Relational Model : Some Advantages


* Easy to Understand * Elimination of need to preplan HOW to get to data Flexibility

Data Management in DB2

An Introduction

DB2 Data Objects


Storage Group Databases Tablespaces Indexes Bufferpools EDM pool BSDS Logs

DB2 Tablespaces

Logical Representation

Physical Representation

DB2 Transformation

Pages
SPACE

A page is either 4K or 32K A page is the unit of I/O

PAGE

A page can contain a max. of 127 data records.


A data record does not Span pages

HEADER

FOOTER
Data Records

Simple Tablespaces
Table 1 Table 1 Table 2 Table 2 Table 1 Table 3 Row X Row Y Row A Row B Row Z Row M Table 2 Table 3 Table 2 Table 1 Table 2 Table 3 Row C Row N Row D Row W Row E Row O

Page 1

Page 2

Segmented Tablespaces

Segment 1

Segment 2

Segment 3

Table 1

Table 2

Table 3

Partitioned Tablespaces
Part 1 Part 2 Part 3 Part 4

Indexes
101 102 103 104 1 3 4 2 101 104 102

103

KEY

Position

Indexes : Terminology
Indexing Key Unique Index Primary Index Partitioning Index Composite Key Non-Unique Index Secondary Index Clustering Index

B+ Trees
25

Root Node

Intermediate Nodes 8 17 33 40

138

9 11 17

19 21 25

28 31 33

35 38 40 46 51 55

Leaf NodesPointing to Data Pages

Clustering Index
25

8 17

33 40

138
Data Pages

9 11 17

19 21 25

28 31 33

35 38 40

46 51 55

Non-clustered Index Scan


25

8 17

33 40

138
Data Pages

9 11 17

19 21 25

28 31 33

35 38 40

46 51 55

Stogroup
SG1

SG2

Dev 001

Dev 001

Dev 001
SG3

Dev 001

Database
Table Space Table Table

Table Space Table

Index

Index

Storage Groups

Bufferpools
4 KB Pages 32 KB Pages

DB2 System Objects


Bufferpool EDM Pool ( Environment Descriptor Manager ) BSDS ( Bootstrap Dataset ) Logs ( Active and Archive logs )

Data Types : String


String

Character

Graphic

Fixed Length

Variable Length

Fixed Length

Variable Length

Data Types : Datetime

Datetime

Time

Timestamp

Date

Data Types : Numeric


Numeric

Binary Integer

Decimal

Floating Point

Small

Large

Small

Large

DB2 Functions and Components


An Introduction

DB2 Catalog
Data about data DB2 is managing Metadata Stored in DB2 Tables Can be accessed in the same way as user data

Transactions
SQL Command SQL Command COMMIT SQL Command SQL Command COMMIT ROLLBACK

Begin Transaction End Transaction All Changes DONE Begin Transaction

End Transaciton
All ChangesUNDONE

Transactions : An example
Account 1 Balance = 2000 Transaction Withdraw 1000 from Account 1 Deposit 1000 from Account 2 Account 2 Balance = 5000

Account 1 Balance = 1000

Account 2 Balance = 6000

Transaction : ACID Properties


ATOMICITY
CONSISTENCY ISOLATION DURABILITY

Lost Update Problem


User 1 Quantity = 2000 User 2

Send 1000 units

Quantity = 1000

Receive 2000 units

Quantity = 4000

Uncommited Read
User 1 Quantity = 2000 User 2

Send 1000 units

Quantity = 1000
Quantity = 2000

Receive 2000 units

ROLLBACK

Quantity = 3000

Incorrect Summary Problem


Roll #
101 Marks 80 Percentile

102

77

Read all the rows twice

103

91

104

64

Locking
Lock on data indicates data is being used
Attributes of locks Object : How much data is marked in-use Duration : How long is the lock is needed Mode : How severe is the lock

Lock : Object
PAGE or ROW
Great concurrency More locking overhead TABLE or TABLESPACE

Less locking overhead More data is restricted

Lock : Duration
Different options for ApplicationPrograms
ACQUIRE/RELEASE
ISOLATION LEVEL

Lock : Mode
Shared - Data is being Read Other users can read the same data
Exclusive - Data is being changed No other users allowed

DEADLOCK
User 1
LOCK HELD Lock Requested

User 2

Lock Held

Table 1

Table 2

Failures
* Hardware Failure
* Program Failure * Natural Calamity

Backing up data
* IMAGE COPY * FULL * INCREMENTAL
* MERGECOPY * FUZZYCOPY

System Log
[Begin transaction T1] [T1, table 1, Page 1, Before Image] [T1, table 1, Page 1, After Image] [T1, table 2, Page 3, Before Image] [T1, table 2, Page 3, After Image] [Begin Transaction T2] [T2, table 3, Page 9, Before Image] [T2, table 3, Page 9, After Image] [End Transaction T2 ] [End Transaction T1 ]

Recovery
* RECOVER Utility Tablespace is the unit of recovery * Point of time recovery * Disaster Recovery

Integrity Enforcement
Table Check Constraint Designates the value that columns of a table can contain Referential Constraint The limiting of a set of foreign key values to a set of primary key values.

Security
Outside Db2 - RACF Within DB2 - Privileges - Administrative Authorities

Application Programming Interface


SQL Structured Query Language Embedded SQL DB2 Interactive (DB2l)

SQL

Data Definition Language Data Manipulation Language Data Control Language DDL DML DCL

Utilities
LOAD

UNLOAD
REORG RUNSTATS

SQL - DDL
DATA DEFINITION

LANGUAGE

SQL Structured Query Language


SQL

DDL

DML

DCL

DDL DB2 Objects


Storage Group Database Tablespace Table Index View Synonym

DDL - Operations
CREATE - Defines a new object

ALTER - Modifies an object


DROP Deletes a defined object

Entered interactively or embedded in application programs.

DDL OBJECTS vs OPERATIONS


CREATE

ALTER X

DROP X X

Storage Group Database

X X

Tablespace
Table Index Synonym View

X
X X X X

X
X X

X
X X X X

DDL CREATE Storage Group


Syntax: CREATESTOGROUP Stogroup-name VOLUMES (vol1, vol2,) VCAT catalogname (PASSWORD password)

CREATE STOGROUP TRG1T01 VOLUMES (DBPK01, DBPK02) VCAT DB220TRG

DDL ALTER Storage Group


Syntax: ALTER STOGROUP stogroup-name ADD VOLUMES ( vol4, vol5, ) REMOVE VOLUMES (vol1, vol2, . . . ) ALTER STOGROUP TRG1T01 ADD VOLUMES (DBPK03) REMOVE VOLUMES (DBPK01)

DDL CREATE DATABASE


Syntax: CREATE DATABASE databasename (STOGROUP stogroupname) (BUFFERPOOL bufferpoolname) CREATE DATABASE TRG1T01 STOGROUP TRG1T01 BUFFERPOOL BP0

DDL CREATE TABLESPACE


Syntax : CREATE TABLESPACE tablespacename IN database-name USING STOGROUP stogroup name PRIQTY qty SECQTY qty ERASE YES/NO LOCKSIZE ANY/PAGE/TABLESPACE/TABLE BUFFERPOOL BPO/BP1/BP2/BP32K CLOSE YES/NO DSETPASS password FREEPAGE <amount> PCTFREE <amount>

DDL CREATE TABLESPACE Parameters


PRIQTY & SECQTY * PRIQTY amount of physical storage allocated when tablespace is created * SECQTY space taken amount of data in tablespace grows * specified in KB

DDL CREATE TABLESPACE Parameters


ERASE: * Indicates whether the DB2 defined datasets are to be erased when tablespace is dropped LOCKSIZE: * Indicates type of locking (Page/Tablespace/Table/DB2 decided)

DDL CREATE TABLESPACE Parameters


BUFFERPOOL: * Buffer pool to be associated with tablespace * Default value is default buffer pool of database CLOSE: * Indicates whether data sets associated with tablespace;should be closed when there are no current users of tablespace * Default is YES

DDL CREATE TABLESPACE Parameters


FREEPAGE: * Specified number of pages after which a completely empty page is available * Default is FREEPAGE 0 PCTFREE:

* Specifies percentage of each page available for future inserts * Default is PCTFREE 5

DDL CREATE TABLESPACE


Example: CREATE TABLESPACE IN DSN8DP2AP USING STOGROUP DSNS6200 PRIQTY 12 SECQTY 12 ERASE NO LOCKSIZE ANY CLOSE YES BUFFERPOOL BP0

DDL ALTER TABLESPACE


ALTER TABLESPACE tablespace-name BUFFERPOOL BP0/BP1/BP2 LOCKSIZE ANY/PAGE/TABLESPACE/TABLE CLOSE YES/NO DSETPASS password FREEPAGE amount PRIQTY qty SECQTY qty ERASE YES/NO

DDL- CREATE TABLE


Syntax : (Format 1) CREATE TABLE tablename (column-definition [, column-definition] . . . [, primary-key-definition] [, foreign-key-definition [, foreign-key-definition]. . .]) [other-parameters] ) [ IN databasename.tablespacename / IN DATABASE databasename]

DDL- CREATE TABLE


Syntax : (Format 2) CREATE TABLE tablename LIKE existing-tablename

DDL-CREATE TABLE
COLUMN- definition: colname coltype (NOT NULL/ NULL / NOT NULL WITH DEFAULT) Primary-key definition: PRIMARY KEY(colname1,colname2,)

DDL- CREATE TABLE


foreign-key definition: FOREIGN KEY [constraint-name] (colname1 [, colname2] . . .) REFERENCES base-table [ ON DELETE RESTRICT / CASCADE / SET NULL]

DDL-CREATE TABLE
Other parameters: EDITPROC : edit routine for table

VALIDPROC : validation routine for table

DDL CREATE TABLE


Example CREATE TABLE SUPPLIER (S# CHAR(5) NOT NULL, SNAME CHAR(20) NOT NULL, STATUS SMALLINT NOT NULL WITH DEFAULT CITY CHAR(15) NOT NULL WITH DEFAULT PRIMARY KEY (S#) ) IN TRG1T01.DSN8D2AP

DDL ALTER TABLE


Syntax: ALTER TABLE tablename ADD column-definition VALIDPROC program-name NULL primary-key-definition referential-constraint CRC? PRIMARY KEY CRC? FOREIGN KEY constraint-name

DDL CREATE INDEX


Syntax:

CREATE (UNIQUE) INDEX indexname ON tablename (colname [ASC / DESC], . ) [USING STOGROUP stogroupname PRIQTY qty SECQTY qty ERASE YES/NO] [CLUSTER] [SUBPAGES amount] [BUFFERPOOL BP0/BP1/BP2] [CLOSE YES/NO] [DSETPASS password] [PCTFREE amount] [ FREEPAGE amount]

DDL- CREATE INDEX


Example: CREATE UNIQUE INDEX XS ON SUPPLIER (S#) USING STOGROUP TRG1TO1 PRIQTY 16 SECQTY 4 ERASE NO

DDL ALTER INDEX


Syntax : ALTER INDEX index-name BUFFERPOOL BPO/BP1/BP2 CLOSE YES/NO DSETPASS password FREEPAGE qty PCTFREE qty USING STOGROUP stogroupname PRIQTY amount SECQTY amount ERASE YES/NO

DDL CREATE VIEW


Syntax: CREATE VIEW view-name (column-name, . . .) As subselect WITH CHECK OPTION

DDL CREATE SYNONYM


Syntax : CREATE SYNONYM synonym FOR auth-id.table-name

DDL DROP Statement


Syntax: DROP object-type objectname Example DROP DATABASE TRG1TO1 DROP TABLE SUPPLIER DROP INDEX XS

DDL- DROP Dependencies


DROP DEPENDENCIES
Storage Group Database Table Space Tables

Index

Views

Alias

Synonym

SQL- DML
DATA MANIPULATION LANGUAGE

DML Overview
DML verbs are: * SELECT - Retrieves data * UPDATE - Changes value(s) * DELETE - Remove row(s) * INSERT - Adds new row(s) will be explained using an example

DML SELECT Select Overview


Select statement is used to assess data from one or more tables: (a) Simple Queries (f ) Aggregate (b) Qualified retrieval functions (c) Retrieval with ordering (g) Grouping (d) Join enquiries (h) Union (e) Subqueries

DML SELECT Statement


SUPPLIERS
S# S1 S2 S3 S4 S5
PARTS

SHIPMENTS
S# S1 S1 S1 S1 S1 S1 S2 S2 S3 S4 S4 S4 P# P1 P2 P3 P4 P5 P6 P1 P2 P2 P2 P4 P5 QTY 300 200 400 200 100 100 300 400 200 200 300 400

SNAME Arun Rao Tarun Sanjay Vinod PNAME Nut Bolt Screw Screw Carn Cog

STATUS 20 10 30 20 30 COLOR Red Green Blue Red Blue Green

CITY Madras Hyderabad Hyderabad Madras Calcutta WEIght 12 17 17 14 12 19 CITY Madras Hyderabad Bombay Madras Hyderabad Madras

P# P1 P2 P3 P4 P5 P6

DML SELECT Statement


Syntax:
SELECT [ ALL | DISTINCT ] scalar- expression FROM table(s) [WHERE search-condition] [GROUP BY column(s) ] [HAVING search-condition] [ORDER BY column(s) ]

DML SELECT Statement


Scalar Expressions (LENGTH (SNAME) 1 * 2 SALARY + COMMISSION + BONUS Search Condition (SUBSTR (SNAME,1,3) = VIJ AND CITY = MADRAS

DML- SELECT Statement


(a) Simple Queries SELECT P# FROM SHIPMENTS

SELECT SHIPMENTS. P# FROM SHIPMENTS


SELECT DISTINCT P# FROM SHIPMENTS

DML SELECT Statement


(b) Simple Queried (Contd)
SELECT * FROM SUPPLIER SELECT P#, Weight in grams = , WEIGHT * 454 FROM PARTS

DML SELECT Statement


(b) Qualified Retrieval using WHERE Clause
SELECT S = FROM SUPPLIERS WHERE CITY = MADRAS AND STATUS > 20;

DML SELECT Statement


(b) Qualified Retrieval Using BETWEEN predicate

* SELECT P#, PNAME, COLOR, WEIGHT, CITY FROM PARTS WHERE WEIGHT BETWEEN 16 AND 19 * SELECT P#,PNAME, COLOR, WEIGHT, CITY FROM PARTS WHERE WEIGHT NOT BETWEEN 16 AND 19

DML SELECT Statement


(b) Qualified Retrieval Using IN predicate * SELECT P#, PNAME, COLOR, WEIGHT,CITY FROM PARTS WHERE WEIGHT IN (12,16,17)

DML SELECT Statement


(d) JOIN queries SelfJoin
Join of a table with itself SELECT FIRST.S#, SECOND.S# FROM SUPPLIER FIRST,SUPPLIER SECOND WHERE FIRST.CITY = SECOND.CITY FIRST AND SECOND are Range Variables of Correlation Names

DML SELECT Statement


(d) JOIN queries Multi-table-joins
SELECT DISTINCT SUPPLIER.CITY, PARTS.CITY FROM SUPPLIER, SHIPMENTS, PARTS WHERE SUPPLIER.S# = SHIPMENTS.S# AND SHIPMENTS.P# = PARTS.P#

A maximum of 15 tables can be joined in an SQL statement

DML SELECT statement


(e) Sub queries of Nested Selects
Simple Sub query
SELECT SNAME FROM SUPPLIER WHERE S# IN ( SELECT S# FROM SHIPMENT WHERE P# = P2 )

DML SELECT Statement


(e) Correlated sub query
A nested SELECT statement referring back to columns in outer SELECT statements SELECT SNAME FROM SUPPLIER WHERE P2 IN ( SELECT P# FROM SUPPLIER WHERE S# = SUPLIER.S# )

DML- SELECT Statement


(e) Joins VERSUS Sub queries
A sub query can be converted to an equivalent join Joins are more efficient than sub queries in DB2

DML SELECT Statement


(f) Existential Quantifier
Used to test for existence of certain rows

SELECT SNAME FROM SUPPLIER WHERE EXISTS ( SELECT * FROM SHIPMENT WHERE S# = SUPPLIER.S# AND P# = P2 )

DML SELECT Statement


(g) Aggregate or Column Functions Functions operate on collection of values in a column
Count - number of values in the column SUM - sum of values in the column AVG - average of the values in the column MAX - largest value in the column MIN - smallest value in the column

DML SELECT Statement


(g) Aggregate Function Examples
SELECT COUNT(*) FROM SUPPLIER

SELECT SUM(QTY) FROM SHIPMENT WHERE P# = P2

DML SELECT Statement


(h) Grouping
Partition or Groups the rows based on given criteria

SELECT P#, SUM(QTY) FROM SHIPMENT GROUP BY P#

DML SELECT Statement


(h) Grouping (Counted)
SELECT p#, SUM(QTY), MAX(QTY) FROM SHIPMENT WHERE S# <> S1 GROUP BY P#

DML SELECT Statement


(h) Grouping HAVING Clause
HAVING is to groups what WHERE is to rows

SELECT P# FROM SHIPMENT GROUP BY P# HAVING COUNT(*) > 1

DML SELECT Statement


UNION
Union operation on union-compatible sets of rows

SELECT P# FROM PARTS WHERE WEIGHT > 16 UNION SELECT.P# FROM SHIPMENT WHERE S# = S2

DML DATE / TIME Arithmetic


Duration:
Date duration: yyyymmdd

Time duration:
Timestamp duration:

hhmmss
yyyymmddhhmmssnnnnn

Microsecond duration: nnnnnn

DML DATE / TIME Arithmetic


Labeled Duration:
n units

n : any numeric expression Units : YEAR[S], MONTHS[S],DAY[S],HOUR[S], MINUTE[S],SECONDS[S],MICROSECOND[S]


e.g., 3 YEARS, 90 DAYS, 1 MINUTE

DML DATE / TIME Arithmetic


Extraction of Date / Time Components:
YEAR , MONTH, DAY HOUR,MINUTE,SECOND MICROSECOND

DML DATE / TIME Arithmetic


Conversions To / From other Data Types:
DATE, TIME, TIMESTAMP

CHAR
DAYS

DML DATE / TIME Arithmetic


Special Registers: CURRENT TIMEZONE CURRENT DATE CURRENT TIME CURRENT TIMESTAMP

DML DATE / TIME Arithmetic


Examples: DATE(8/17/1996) DATE(10/28/1996) START_DATE + 1 YEAR + 6 MONTS CURRENT TIMESTAMP + 1 SECOND START_TIME END_TIME < 080000

DML INSERT Statement


Syntax:

INSERT INTO table[ (column [, column] )] Values (literal [,literal].)


Or

INSERT INTO table [ (column[, column] )] subquery

DML INSERT Statement


Examples
INSERT INTO PARTS (P#, CITY, WEIGHT) VALUES (P7, CALCUTTA, 24) INSERT INTO PARTS VALUES (P8,SPROCKET,PINK,14,SHIMLA)

DML INSERT Statement


Insert with a subquery
INSERT INTO TEMP (P#, TOTQTY) SELECT P#,SUM(QTY) FROM SHIPMENT GROUP BY P#

DML INSERT Statement


Insert with Referential Integrity

Referential constraint from SHIPMENTS.S# to SUPPLIERS.S# INSERT INTO SHIPMENTS.S#, P#, QTY) VALUES (S20,.)
* S20 should exist in SUPPLIERS.S#

DML UPDATE Statement


Syntax: UPDATE table SET column = scalar-expression [, column = scalar-expression] [WHERE search-condition]

DML UPDATE Statement


Single Row Update UPDATE PARTS

SET COLOR =YELLOW WEIGHT = WEIGHT + 5 CITY = NULL WHERE P# = P2


Multiple row Update UPDATE SUPPLIER SET STATUS = 2 * STATUS WHERE CITY = MADRAS

DML UPDATE Statement


Update with a subquery UPDATE SHIPMENT SET QTY = 0 WHERE LONDON = (SELECT CITY FROM SUPPLIER WHERE SUPPLIER.S # = SHIPMENT.S#)

DML UPDATE Statement


Update with Referential Integrity

UPDATE SHIPMENTS SET S# =S20 WHERE SUPPLIERS.S# should have an entry S20
UPDATE SUPPLIERS SET S# = S20 WHERE S# = S1

DML DELETE Statement


Syntax: DELETE FROM table [WHERE condition] Single row delete DELETE FROM SUPPLIER WHERE S# = S5

DML DELETE Statement


Multiple row delete DELETE FROM SHIPMENT WHERE QTY > 300 Delete with a subquery DELETE FROM SHIPMENT WHERE MADRAS = (SELECT CITY FROM SUPPLIER WHERE SUPPLIER.S# = SHIPMENT.S#)

DML DELETE Statement


Delete with Referential Integrity DELETE FROM SUPPLIERS WHERE S# = S1 * Execution depends on DELETE RULE CASCADE, RESTRICT OR SET NULL

Вам также может понравиться