You are on page 1of 3

HMP 669: Db Mgmt & Internet Apps in HC, Kai Zheng

Oracle 10g Cheat Sheet


1 ER Model
1.1 Logical Model
Patient Has
patientId {PK}
pcpId {FK}
nameFirst
nameLast
1..1
AssignedTo Physician
physicianId {PK}
nameFirst
nameLast
0..*
Appointment
appointmentId {PK}
patientId {FK}
physicianId {FK}
appointmentDateTime
reasonForVisit
0..*
PatientPhoneNumber
uniqueId {PK}
patientId {FK}
phoneNumber
phoneType
0..3
1..1
Has
1..1
AssignedTo
IsPCPOf
0..* 0..1
2 Work with Oracle via SQL*Plus
2.1 Connect using Instant Client
sqlplus username/password@server/database
2.2 Change Password
ALTER USER kzheng IDENTIFIED BY hmp669;
2.3 Load Script from File
START h:\script.sql
2.4 Record Sessions
SPOOL h:\log.txt
...
SPOOL OFF
2.5 Miscellaneous
DESC patient;
-- describe table structure
SELECT table_name FROM user_tables;
-- list of all tables
SELECT constraint_name, constraint_type,
table_name FROM user_constraints;
-- list of all constraints
3 Create Tables and Constraints
CREATE TABLE physician (
physician_id VARCHAR2(10) PRIMARY KEY,
name_first VARCHAR2(20) NOT NULL,
name_last VARCHAR2(20) NOT NULL,
gender CHAR(1) NOT NULL
CHECK (gender IN('F','M')),
salary NUMBER(7,2)
);
CREATE TABLE patient (
patient_id VARCHAR2(10) PRIMARY KEY,
pcp_id VARCHAR2(10)
CONSTRAINT pat_phy_fk
REFERENCES physician(physician_id)
ON DELETE CASCADE,
name_first VARCHAR2(20) NOT NULL,
name_last VARCHAR2(20) NOT NULL,
gender CHAR(1) NOT NULL
CHECK (gender = 'F' OR gender = 'M'),
dob DATE NOT NULL,
ssn CHAR(11) UNIQUE
);
3.1 Common Data Types
NUMBER (5,2) Numeric (e.g. 999.99) Max. size 38 digits
VARCHAR2(50) Variable length string Max. size 4000
CHAR(5) Fixed length string Max. size 2000
DATE Date/time -4712{9999
4 Work with Existing Tables
4.1 Modify Tables
ALTER TABLE physician DROP COLUMN salary;
ALTER TABLE physician ADD salary NUMBER(8,2);
ALTER TABLE physician MODIFY salary NUMBER(10,2);
DROP TABLE patient;
DROP TABLE patient CASCADE CONSTRAINTS;
-- remove even if referenced by other tables
PURGE RECYCLEBIN;
4.2 Modify Constraints
ALTER TABLE patient DISABLE CONSTRAINT pat_phy_fk;
ALTER TABLE patient ENABLE CONSTRAINT pat_phy_fk;
ALTER TABLE patient DROP CONSTRAINT pat_phy_fk;
SET CONSTRAINTS ALL DEFERRED;
SET CONSTRAINTS ALL IMMEDIATE;
ALTER TABLE patient
ADD CONSTRAINT pat_phy_fk FOREIGN KEY
(pcp_id) REFERENCES physician(physician_id)
ON DELETE CASCADE;
5 Insert and Update Records
INSERT INTO physician
VALUES ('S01','Robert','Jones', 'M',98191.77);
INSERT INTO physician
(salary, name_first, name_last, gender, physician_id)
VALUES (83351.28, 'Mary','Jones', 'F','P02');
INSERT INTO patient
VALUES ('389029113',NULL,'John','Smith',
'M','12-MAY-1983','419-29-4892');
-- DD-MON-YYYY is the default date format
INSERT INTO patient
VALUES ('389029114','S01','Rebecca','Lee',
'F',TO_DATE('04/01/1981','mm/dd/yyyy'),
'148-23-7326');
COMMIT;
/* make uncommitted changes permanent,
only apply in record manipulation*/
1
UPDATE physician
SET salary = salary*1.2
WHERE physician_id = 'S01';
ROLLBACK;
-- undo uncommitted changes
5.1 Delete Records
DELETE patient WHERE patient_id = '389029113';
DELETE physician;
6 Work with Views
CREATE OR REPLACE VIEW rich_physician AS
SELECT * FROM physician WHERE salary > 100000;
SELECT * FROM rich_physician;
DROP VIEW rich_physician;
7 Work with Sequences
CREATE SEQUENCE seq_appt_id
START WITH 1000000000
MAXVALUE 9999999999
INCREMENT BY 1;
INSERT INTO appointment
VALUES (seq_appt_id.NEXTVAL, ...);
DROP SEQUENCE seq_appt_id;
8 Data Manipulation
8.1 Comparison Operators
=, >, <, >=, <=
<> Not equal to
IS NULL
IS NOT NULL
LIKE Pattern match in strings
8.2 Basic Arithmetic Operators
+, -, *, /
8.3 Common Number Functions
COUNT() # of matched records
SUM()
AVG() Average
MAX()
MIN()
ABS() Absolute
ROUND(a,b) e.g. ROUND(129.29,1) = 129.3
POWER(a,b) e.g. POWER(5,2) = 25
Example:
SELECT ABS(ROUND(-1.237,2)) FROM dual;
-- 1.24
8.4 Common String Functions
INITCAP() Capitalize the _rst letter
LENGTH()
UPPER()
LOWER()
SUBSTR() e.g. SUBSTR(zip,1,5)
TRIM() Remove leading and trailing spaces
Example:
SELECT LENGTH(TRIM(' count me ')) FROM dual;
-- 8
8.5 Common Conversion Functions
TO_NUMBER()
TO_CHAR(input,format)
TO_DATE(input,format)
8.6 Common Date Format
DD-MON-YYYY (default) e.g. 01-JAN-2007
MM/DD/YYYY e.g. 01/01/2007
DAY, MONTH DDTH e.g. SUNDAY, JANUARY 04TH
DY, Month DDth e.g. SUN, January 04th
HH:MI:SS AM e.g. 01:30:23 PM
HH24:MI e.g. 13:30
Example:
SELECT TO_CHAR(sysdate,'MM/DD/YYYY HH:MI:SS A.M.')
FROM dual;
-- 03/05/2007 01:31:17 A.M.
9 Query a Single Table
-- how many female patients have PCP?
SELECT COUNT(*) FROM patient
WHERE gender = 'F' AND pcp_id IS NOT NULL;
-- average physician salary, rounded to 1 decimal
SELECT ROUND(AVG(salary),1) FROM physician;
-- full name of those born in April
-- || is used to concatenate strings
SELECT name_last || ', ' || name_first
FROM patient WHERE TO_CHAR(dob,'MON') = 'APR';
-- dob ordered by last name then by first name
SELECT TO_CHAR(dob,'DD/MON/YYYY') FROM patient
ORDER BY name_last, name_first;
/*number of distinct patient last names
of those born after 1979*/
SELECT COUNT(DISTINCT name_last)
FROM patient
WHERE dob >= '01-JAN-1980';
9.1 Pattern Match in Strings (LIKE)
-- last name contains 'mm'
SELECT name_last FROM patient
WHERE name_last LIKE '%mm%';
-- last name contains 'mm', 'Mm', 'mM', or 'MM'