Chapter-08-2
Chapter-08-2
Chapter 8
Advanced SQL
Sections 8.4 and 8.5
1
Learning Objectives: Ch 8, Part 2
●
Use SQL to do data manipulation (insert,
update, and delete rows of data)
●
Use SQL to create database views, including
updatable views
2
DML
●
Data Manipulation Language
●
Statements used to insert, delete, and update
data in the database
3
Tables to Use
CREATE OR REPLACE TABLE VENDOR
(
V_CODE INTEGER,
V_NAME VARCHAR(35),
V_CONTACT VARCHAR(25),
V_AREACODE CHAR(3),
V_PHONE CHAR(8),
V_STATE CHAR(2),
V_ORDER CHAR(1),
PRIMARY KEY (V_CODE)
);
4
Tables to Use
CREATE OR REPLACE TABLE PRODUCT
(
P_CODE VARCHAR(10) NOT NULL UNIQUE,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH SMALLINT NOT NULL,
P_MIN SMALLINT NOT NULL,
P_PRICE NUMERIC(8,2) NOT NULL,
P_DISCOUNT NUMERIC(5,2) NOT NULL,
V_CODE INTEGER,
PRIMARY KEY (P_CODE),
FOREIGN KEY (V_CODE)
REFERENCES VENDOR (V_CODE)
);
5
Adding Data
●
Use the INSERT statement
●
Two clauses
– INTO tablename ( column1, column2, ... )
– VALUES ( value1, value2 )
6
INTO Clause
●
Identifies the table to use
●
Identifies the columns to use
– This can not be included if yo use all the columns
– I think it is a best practice to always list the columns
7
Add a Vendor
INSERT
INTO VENDOR(V_CODE,V_NAME,V_CONTACT,
V_AREACODE,V_PHONE,V_STATE,V_ORDER)
VALUES (21225,"Bryson, Inc.","Smithson",
”615","223-3234","TN","Y")
;
●
Note: Strings need quotes (“)
8
Don’t Have to Use All Columns
INSERT
INTO VENDOR(V_CODE,V_NAME,V_CONTACT)
VALUES (22024,"Acme, Inc.","Jones")
;
9
Inserting NULLs
INSERT
INTO VENDOR(V_CODE,V_NAME,V_CONTACT,
V_AREACODE,V_PHONE,V_STATE,V_ORDER)
VALUES (25056,"Bearings, LLC.","Jones",
NULL, NULL, NULL, NULL)
;
10
INSERT Errors
INSERT
INTO PRODUCT(P_CODE,P_DESCRIPT)
VALUES ( 1, "Test Product" )
;
●
Error because NOT NULL fields are not
included in the INSERT
11
Foreign Keys
INSERT
INTO PRODUCT(P_CODE,P_DESCRIPT,P_INDATE,P_QOH,P_MIN,P_PRICE,
P_DISCOUNT,V_CODE)
VALUES ( 1, "Test Product", "2021-04-11",12,5,12.50,.20,21225 )
;
●
Foreign key value (V_CODE=21225) must exist
in the referenced table
12
Foreign Keys
INSERT
INTO PRODUCT(P_CODE,P_DESCRIPT,P_INDATE,P_QOH,P_MIN,P_PRICE,
P_DISCOUNT,V_CODE)
VALUES ( 2, "New Product", "2021-04-11",15,3,1.50,.10,33333 )
;
●
Error if the foreign key value does not exist
13
Changing Data in Tables
●
Use the UPDATE statement
●
Two clauses
– SET: lists the changes to make
– WHERE: just like with SELECT, limits which rows
are changed
●
No where clause, all rows are changed
14
Making Some Changes
UPDATE VENDOR
SET V_AREACODE = "814"
WHERE V_CODE=22024
;
UPDATE VENDOR
SET V_AREACODE = "555",V_PHONE="555-5555"
WHERE V_CODE=22024
;
15
Deleting Rows
●
Use the DELETE statement
●
BE VERY CAREFUL WITH THIS!!
●
One clauses
– WHERE: limits rows to delete
16
Removing A Row
17
Views
●
Definition
– A virtual table based on a SELECT query that is
saved as an object in the database
●
Have many uses including
– Security
– Saving common queries
18
Creating a View
●
Use the CREATE OR REPLACE VIEW
statement
●
One clause
– AS: the SELECT query used for the view
19
Let’s Create One
CREATE OR REPLACE VIEW VENDOR_PRODUCTS
AS SELECT V_CODE,V_NAME,P_CODE,P_DESCRIPT
FROM VENDOR LEFT OUTER JOIN PRODUCT USING
(V_CODE )
;
20