0% found this document useful (0 votes)
204 views

Oracle 10g Cheat Sheet: 1 ER Model

The document provides an overview of common SQL commands and functions for working with Oracle databases, including creating and modifying tables, inserting and updating records, working with views and sequences, and performing string and date operations. Examples are given for connecting to a database, changing passwords, loading scripts, describing tables, listing tables and constraints, and performing data manipulation and conversion. Common functions covered include INITCAP, LENGTH, UPPER, LOWER, SUBSTR, TRIM, TO_NUMBER, TO_CHAR, and TO_DATE.

Uploaded by

Simo M Bentaleb
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
204 views

Oracle 10g Cheat Sheet: 1 ER Model

The document provides an overview of common SQL commands and functions for working with Oracle databases, including creating and modifying tables, inserting and updating records, working with views and sequences, and performing string and date operations. Examples are given for connecting to a database, changing passwords, loading scripts, describing tables, listing tables and constraints, and performing data manipulation and conversion. Common functions covered include INITCAP, LENGTH, UPPER, LOWER, SUBSTR, TRIM, TO_NUMBER, TO_CHAR, and TO_DATE.

Uploaded by

Simo M Bentaleb
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

HMP 669: Db Mgmt & Internet Apps in HC, Kai Zheng gender CHAR(1) NOT NULL

CHECK (gender = ’F’ OR gender = ’M’),


Oracle 10g Cheat Sheet dob DATE NOT NULL,
ssn CHAR(11) UNIQUE
);
1 ER Model
1.1 Logical Model 3.1 Common Data Types
 IsPCPOf
NUMBER (5,2) Numeric (e.g. 999.99) Max. size 38 digits
0..* 0..1 VARCHAR2(50) Variable length string Max. size 4000
Patient Has  Appointment
 AssignedTo  AssignedTo Physician
patientId {PK}
1..1 0..*
appointmentId {PK}
0..* 1..1
physicianId {PK}
CHAR(5) Fixed length string Max. size 2000
pcpId {FK}
nameFirst
patientId {FK}
physicianId {FK}
nameFirst
nameLast
DATE Date/time -4712–9999
nameLast appointmentDateTime
1..1 reasonForVisit
4 Work with Existing Tables
 Has

0..3
4.1 Modify Tables
PatientPhoneNumber ALTER TABLE physician DROP COLUMN salary;
uniqueId {PK}
patientId {FK} ALTER TABLE physician ADD salary NUMBER(8,2);
phoneNumber
phoneType

ALTER TABLE physician MODIFY salary NUMBER(10,2);


2 Work with Oracle via SQL*Plus
2.1 Connect using Instant Client DROP TABLE patient;
sqlplus username/password@server/database
DROP TABLE patient CASCADE CONSTRAINTS;
2.2 Change Password -- remove even if referenced by other tables
ALTER USER kzheng IDENTIFIED BY hmp669;
2.3 Load Script from File PURGE RECYCLEBIN;
START h:\script.sql
4.2 Modify Constraints
2.4 Record Sessions ALTER TABLE patient DISABLE CONSTRAINT pat_phy_fk;
SPOOL h:\log.txt ALTER TABLE patient ENABLE CONSTRAINT pat_phy_fk;
...
SPOOL OFF ALTER TABLE patient DROP CONSTRAINT pat_phy_fk;

2.5 Miscellaneous SET CONSTRAINTS ALL DEFERRED;


DESC patient; SET CONSTRAINTS ALL IMMEDIATE;
-- describe table structure
ALTER TABLE patient
SELECT table_name FROM user_tables; ADD CONSTRAINT pat_phy_fk FOREIGN KEY
-- list of all tables (pcp_id) REFERENCES physician(physician_id)
ON DELETE CASCADE;
SELECT constraint_name, constraint_type,
table_name FROM user_constraints; 5 Insert and Update Records
-- list of all constraints INSERT INTO physician
VALUES (’S01’,’Robert’,’Jones’, ’M’,98191.77);
3 Create Tables and Constraints
CREATE TABLE physician ( INSERT INTO physician
physician_id VARCHAR2(10) PRIMARY KEY, (salary, name_first, name_last, gender, physician_id)
name_first VARCHAR2(20) NOT NULL, VALUES (83351.28, ’Mary’,’Jones’, ’F’,’P02’);
name_last VARCHAR2(20) NOT NULL,
gender CHAR(1) NOT NULL INSERT INTO patient
CHECK (gender IN(’F’,’M’)), VALUES (’389029113’,NULL,’John’,’Smith’,
salary NUMBER(7,2) ’M’,’12-MAY-1983’,’419-29-4892’);
); -- DD-MON-YYYY is the default date format

CREATE TABLE patient ( INSERT INTO patient


patient_id VARCHAR2(10) PRIMARY KEY, VALUES (’389029114’,’S01’,’Rebecca’,’Lee’,
pcp_id VARCHAR2(10) ’F’,TO_DATE(’04/01/1981’,’mm/dd/yyyy’),
CONSTRAINT pat_phy_fk ’148-23-7326’);
REFERENCES physician(physician_id)
ON DELETE CASCADE, COMMIT;
name_first VARCHAR2(20) NOT NULL, /* make uncommitted changes permanent,
name_last VARCHAR2(20) NOT NULL, only apply in record manipulation*/

1
8.4 Common String Functions
UPDATE physician INITCAP() Capitalize the first letter
SET salary = salary*1.2 LENGTH()
WHERE physician_id = ’S01’; UPPER()
LOWER()
ROLLBACK; SUBSTR() e.g. SUBSTR(zip,1,5)
-- undo uncommitted changes TRIM() Remove leading and trailing spaces
Example:
5.1 Delete Records
SELECT LENGTH(TRIM(’ count me ’)) FROM dual;
DELETE patient WHERE patient_id = ’389029113’;
-- 8
DELETE physician; 8.5 Common Conversion Functions
TO_NUMBER()
6 Work with Views TO_CHAR(input,format)
TO_DATE(input,format)
CREATE OR REPLACE VIEW rich_physician AS
SELECT * FROM physician WHERE salary > 100000; 8.6 Common Date Format
DD-MON-YYYY (default) e.g. 01-JAN-2007
SELECT * FROM rich_physician; MM/DD/YYYY e.g. 01/01/2007
DROP VIEW rich_physician; 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
7 Work with Sequences HH24:MI e.g. 13:30
CREATE SEQUENCE seq_appt_id Example:
START WITH 1000000000
MAXVALUE 9999999999 SELECT TO_CHAR(sysdate,’MM/DD/YYYY HH:MI:SS A.M.’)
INCREMENT BY 1; FROM dual;
-- 03/05/2007 01:31:17 A.M.
INSERT INTO appointment
VALUES (seq_appt_id.NEXTVAL, ...); 9 Query a Single Table
-- how many female patients have PCP?
DROP SEQUENCE seq_appt_id; SELECT COUNT(*) FROM patient
WHERE gender = ’F’ AND pcp_id IS NOT NULL;
8 Data Manipulation
-- average physician salary, rounded to 1 decimal
8.1 Comparison Operators SELECT ROUND(AVG(salary),1) FROM physician;
=, >, <, >=, <=
<> Not equal to -- full name of those born in April
IS NULL -- || is used to concatenate strings
IS NOT NULL SELECT name_last || ’, ’ || name_first
LIKE Pattern match in strings FROM patient WHERE TO_CHAR(dob,’MON’) = ’APR’;

-- dob ordered by last name then by first name


8.2 Basic Arithmetic Operators
SELECT TO_CHAR(dob,’DD/MON/YYYY’) FROM patient
+, -, *, / ORDER BY name_last, name_first;

8.3 Common Number Functions /*number of distinct patient last names


COUNT() # of matched records of those born after 1979*/
SUM() SELECT COUNT(DISTINCT name_last)
AVG() Average FROM patient
MAX() WHERE dob >= ’01-JAN-1980’;
MIN()
ABS() Absolute 9.1 Pattern Match in Strings (LIKE)
ROUND(a,b) e.g. ROUND(129.29,1) = 129.3 -- last name contains ’mm’
POWER(a,b) e.g. POWER(5,2) = 25 SELECT name_last FROM patient
Example: WHERE name_last LIKE ’%mm%’;

-- last name contains ’mm’, ’Mm’, ’mM’, or ’MM’


SELECT ABS(ROUND(-1.237,2)) FROM dual; SELECT name_last FROM patient
-- 1.24 WHERE UPPER(name_last) LIKE ’%MM%’;

2
FROM appointment
-- first name starts with ’A’ GROUP BY patient_id
SELECT name_first FROM patient )
WHERE name_first LIKE ’A%’; );

-- first name ends with ’a’


SELECT name_first FROM patient
WHERE name_first LIKE ’%a’;

/*first name contains two i’s


separated by exactly one letter*/
SELECT name_first FROM patient
WHERE name_first LIKE ’%i_i%’;

9.2 GROUPING
-- average physician salary by gender
SELECT gender, AVG(salary) FROM physician
GROUP BY gender;

-- which gender group’s average salary is over 90k?


SELECT gender, AVG(salary) FROM physician
GROUP BY gender
HAVING AVG(salary) > 90000;

10 Query from Multiple Tables


-- patient last name and their PCP last name
SELECT patient.name_last, physician.name_last
FROM patient, physician
WHERE pcp_id = physician_id;

-- include patients who don’t have PCP (outer join)


-- alias may be used to speed up coding
SELECT p1.name_last, p2.name_last
FROM patient p1, physician p2
WHERE pcp_id = physician_id(+);

-- include physicians who are not PCP of any patients


SELECT p1.name_last, p2.name_last
FROM patient p1, physician p2
WHERE pcp_id(+) = physician_id;

-- patient last name(s) with more than 1 appointment


SELECT name_last
FROM patient
WHERE patient_id IN
(
SELECT patient_id
FROM appointment
GROUP BY patient_id
HAVING COUNT(*) >= 1
);

-- patient last name(s) who visited most often


SELECT name_last FROM patient WHERE patient_id IN
(
SELECT patient_id
FROM appointment
GROUP BY patient_id
HAVING COUNT(*) =
(
SELECT MAX(COUNT(*))

You might also like