Slide 03
Slide 03
SQL Overview
• Structured Query Language
Overall table
definitions
Defining attributes and their data types
Non-nullable specification
Primary keys
can never have
Identifying primary key NULL values
Non-nullable specifications
Primary key
Default value
Domain constraint
Identifying foreign keys and establishing relationships
Primary key of
parent table
Foreign key of
dependent table
Changing and Removing Tables
• ALTER TABLE statement allows you to
change column specifications:
– ALTER TABLE CUSTOMER_T ADD (TYPE
VARCHAR(2))
• DROP TABLE statement allows you to
remove tables from your schema:
– DROP TABLE CUSTOMER_T
Insert Statement
• Adds data to a table
• Inserting into a table
– INSERT INTO CUSTOMER_T VALUES (001,
‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’,
‘FL’, 32601);
• Inserting a record that has some null attributes
requires identifying the fields that actually get data
– INSERT INTO PRODUCT_T (PRODUCT_ID,
PRODUCT_DESCRIPTION,PRODUCT_FINISH, STANDARD_PRICE,
PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8);
• Inserting from another table
– INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T
WHERE STATE = ‘CA’;
Delete Statement
• Removes rows from a table
• Delete certain rows
– DELETE FROM CUSTOMER_T WHERE
STATE = ‘HI’;
• Delete all rows
– DELETE FROM CUSTOMER_T;
Update Statement
WHERE
PRODUCT_T.PRODUCT_ID = ORDER_LINE_T.PRODUCT_ID
Using and Defining Views
• Views provide users controlled access to tables
• Base Table–table containing the raw data
• Dynamic View
– A “virtual table” created dynamically upon request by a user
– No data actually stored; instead data from base table made
available to user
– Based on SQL SELECT statement on base tables or other
views
• Materialized View
– Copy or replication of data
– Data actually stored
– Must be refreshed periodically to match the corresponding
base tables
Sample CREATE VIEW
CREATE VIEW EXPENSIVE_STUFF_V AS
SELECT PRODUCT_ID, PRODUCT_NAME, UNIT_PRICE
FROM PRODUCT_T
WHERE UNIT_PRICE >300
WITH CHECK_OPTION;
UPDATE PRODUCT_T
SET SALE_PRICE = .85 * STANDARD_PRICE
WHERE STANDARD_PRICE < 400;
END;