CMT221/CMM222:: Database Organization and Design
CMT221/CMM222:: Database Organization and Design
• SQL
▪ Data definition commands: CREATE
TABLE, CREATE INDEX
▪ Data manipulation commands: INSERT,
COMMIT, UPDATE, DELETE, ROLLBACK
▪ SELECT queries: WHERE
Cool Hardware Co
Business rules
• A customer may generate zero or many invoices. Each
invoice is generated by one customer.
• An invoice contains one or more invoice lines. Each
invoice line is associated with one invoice.
• Each invoice line references one product. A product may
be found in many invoice lines.
• A vendor may supply many products. Some vendors do
not yet supply products.
• If a product is vendor supplied, it is supplied by only a
single vendor.
• Some products are not supplied by a vendor. Some
products may be produced in-house or bought on the open
market.
Database Model
Database Structure
Structured Query Language (SQL)
CREATE TABLE
DROP TABLE
SQL
INSERT
SELECT
COMMIT
Data Manipulation Language (DML)
UPDATE
ROLLBACK
DELETE
Data Dictionary
Table Name Attribute Contents Data Type Format Range Required PK or FK FK
Name Referenced
Table
PRODUCT P_CODE Product Code VARCHAR(10) XXXXXXXXXX NA Y PK
Data Types
Data Type Format Comments
Numeric NUMBER(L,D) The declaration NUMBER(7,2) indicates that numbers will be
stored with 2 decimal places and may be up to 7 digits long,
including the sign and the decimal places (e.g., 12.32 or -134.99).
INTEGER May be abbreviated as INT. Integers are (whole) counting
numbers, so they cannot be used if you want to store numbers
that require decimal places.
SMALLINT Like INT but limited to integer values up to 6 digits.
DECIMAL(L,D) LIKE NUMBER but storage length is a minimum specification.
Character CHAR(L) Fixed-length character data for up to 255 characters.
VARCHAR(L) Variable-length character data.
Date DATE Stores dates in Julian data format.
Data Definition Commands
• Create the database structure
• Create the tables to hold data
CREATE TABLE
DROP TABLE
SQL
INSERT
SELECT
COMMIT
Data Manipulation Language (DML)
UPDATE
ROLLBACK
DELETE
CREATE TABLE
UNIQUE constraint
• Ensures that all values in column are unique
DEFAULT constraint
• Assigns value to attribute when a new row is added to table
CHECK constraint
• Validates data when attribute value is entered
CREATE TABLE
• Create table CUSTOMER
▪ Assign CUS_AREACODE a default value of ‘615’.
▪ Check to ensure CUS_AREACODE are restricted to values 615, 713
and 931.
▪ Assign CUS_BALANCE a default value of 0.00.
▪ Create a UNIQUE constraint named CUS_UI1 on CUS_LNAME and
CUS_FNAME.
CREATE TABLE CUSTOMER
(
CUS_CODE NUMBER PRIMARY KEY,
CUS_LNAME VARCHAR(15) NOT NULL,
CUS_FNAME VARCHAR(15) NOT NULL,
CUS_INITIAL CHAR(1),
CUS_AREACODE CHAR(3) DEFAULT '615' NOT NULL
CHECK(CUS_AREACODE IN ('615','713','931')),
CUS_PHONE CHAR(8) NOT NULL,
CUS_BALANCE NUMBER(9,2) DEFAULT 0.00,
CONSTRAINT CUS_UI1 UNIQUE(CUS_LNAME,CUS_FNAME)
);
CREATE TABLE
• Create table INVOICE
▪ INV_DATE is set to a default date (e.g. today’s date).
▪ check the INV_DATE is greater than 1 Jan 2014.
DESCRIBE TABLE;
DESCRIBE VENDOR;
DESCRIBE PRODUCT;
CREATE INDEX
CREATE [UNIQUE] INDEX indexName
ON tableName(columnN);
Create index named CREATE INDEX P_INDATEX ON
P_INDATEX on P_INDATE in PRODUCT(P_INDATE);
PRODUCT
Create composite index named CREATE INDEX VENPRODX ON
VENPRODX on V_CODE and PRODUCT(V_CODE,P_CODE);
P_CODE in PRODUCT
Create index named CREATE INDEX PROD_PRICEX ON
PROD_PRICEX on P_PRICE PRODUCT(P_PRICE DESC);
in PRODUCT in descending
order
Delete the PROD_PRICEX DROP INDEX PROD_PRICEX;
index
Data Manipulation Commands
CREATE TABLE
DROP TABLE
SQL
INSERT
SELECT
COMMIT
Data Manipulation Language (DML)
UPDATE
ROLLBACK
DELETE
INSERT
UPDATE PRODUCT
SET P_INDATE = '18-JAN-12'
WHERE P_CODE = '13-Q2/P2';
Before UPDATE:
After UPDATE:
UPDATE
UPDATE PRODUCT
SET P_INDATE = '18-JAN-12',
P_PRICE = 17.99,
P_MIN = 10
WHERE P_CODE = '13-Q2/P2';
Before UPDATE:
After UPDATE:
DELETE
• Deletes a table row
DELETE FROM tableName
[WHERE conditionList];
Delete row where P_CODE = ‘BRT-345’ from PRODUCT
DELETE FROM PRODUCT
WHERE P_CODE = 'BRT-345';
Delete rows where P_MIN is equals to 5
DELETE FROM PRODUCT
WHERE P_MIN = 5;
COMMIT
• Permanently saves all changes
COMMIT [WORK];
COMMIT;
ROLLBACK
• Undo changes since last COMMIT
• Bring data back to values before change
ROLLBACK;
COMMIT & ROLLBACK
• COMMIT and ROLLBACK only work with
commands to add, modify, or delete table rows
CREATE TABLE NAMELIST
(
NL_CODE NUMBER PRIMARY KEY,
NL_NAME VARCHAR(15) NOT NULL
);
COMMIT;
ROLLBACK;
COMMIT & ROLLBACK
CREATE TABLE NAMELIST
(
NL_CODE NUMBER PRIMARY KEY,
NL_NAME VARCHAR(15) NOT NULL
);
ROLLBACK;
SELECT Queries
SELECT columnList
FROM tableName;
• Logical operators
▪ AND
▪ OR
▪ NOT
• Example: Get a list of products supplied by
vendor with code 21344 or 24288.
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE V_CODE = 21344 OR V_CODE = 24288;
SELECT Queries
• Get a list of products that are less than $50 and are
stocked after January 15, 2014
▪ Show product description, stocking date, price and vendor code
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE P_PRICE < 50
AND P_INDATE > '15-JAN-14';
• Other examples
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE (P_PRICE < 50 AND P_INDATE > '15-JAN-14')
OR V_CODE = 24288;
SELECT *
FROM PRODUCT
WHERE NOT (V_CODE = 21344);
SELECT Queries
• Special operators
▪ BETWEEN: checks whether attribute value is within a
range
▪ IS NULL: checks whether attribute value is null
▪ LIKE: checks whether attribute value matches given string
pattern
▪ IN: checks whether attribute value matches any value
within a value list
▪ EXISTS: checks if subquery returns any rows, if yes, run
the main query, otherwise, do not
SELECT Queries
SELECT * List all products with
FROM PRODUCT prices between $50 and
WHERE P_PRICE BETWEEN 50.00 AND 100.00; $100