Академический Документы
Профессиональный Документы
Культура Документы
Professor Chen
Name: __________________
ENHANCED GUIDE TO ORACLE
Chapter 2
A.
I.
II.
Data Types
SQL*PLUS commands are NOT case sensitive (but Query within the single quotation are case sensitive).
There are three types of datatype in Oracle: builit-in, library, or user-defined. Built-in are standard, userdefined are specialized types built by users, and library types are types built by a third party and supplied
to the user. The followings are most commonly used built-in data types:
1.
2.
VARCHAR2
a) variable length character up to <= 4,000 characters
b) must specify maximum width allowed
c) trailing spaces are NOT added ( truncated)
(e.g.) s_name VARCHAR2(20);
an instance: JASON CHEN
CHAR
a) fixed length (<=200 characters)
b) default is 1 if no field size is specified.
c) trailing blank spaces are added.
pros: use data storage space more efficiently and processed faster
cons: causes inconsistent query results in other Oracle applications
e.g. s_class CHAR(2); SR JR SO FR
sstate CHAR(2) DEFAULT WI;
student_gener CHAR;
Question: What data type should be used if there is any chance that all column spaces will NOT be
filled?
When use Query: SELECT slname, sfname, ssn, telephone FROM student
WHERE slname = Smith;
Case is sensitive within the single quotation.
3.
NCHAR
a.) CHAR: ASCII code represents each character up to 8-digit (1 byte), binary value and can
represent a total of 256 different characters.
b.) NCHAR: Similar to CHAR data type but it supports up to 16-digit (2 bytes) binary
character codes for other countrys characters.
4.
3
4
5.
6. LOB: Large Object Data Types used to store binary data such as digitized sound and images.
BLOB - Binary LOB, storing up to 4 GB of binary data in the database.
CLOB Character LOB, storing up to 4 GB of binary data in the database.
BFILE Binary File, storing a reference to a binary file that is located outside the database in a
file maintained by the operating systems.
NCLOB Character LOB that supports 2-byte (16-bit) character codes
Item size is not specified, e.g., item_image BLOB;
7. Others: Format Masks (see Table 3-1; page 70)
Basic SQL Commands
SQL (Structured Query Language) is used to manipulate the database. There are two types of SQL
commands: Data Definition Language (DDL) and Data Manipulation Language (DML). DDL commands
work with the structure of the objects (tables, indexes, views, etc.) in the database. DML commands
work with data in the database.
B.
I.
<tablename>_<fieldname>_<constraint id>
Chen: Enhanced Guide to Oracle page-2
fk
LOC_ID
LOCATION
LOC_ID
Value Constraints
3.
a) credits NUMBER(2) CONSTRAINT course_credits_cc
CHECK ((credits>) AND (credits <=18));
b) s_class CHAR (2) CONSRAINT student_s_class_cc
CHECK ((s_class='FR') OR (s_class='SO')
OR (s_class='JR') OR (s_class='SR'));
4.
5.
STARTING SQL*Plus
I.
II.
);
SELECT TABLE_NAME
FROM USER_TABLES;
SELECT CONSTRAINT_NAME
FROM USER_CONSTRAINTS;
CASE SENSITITY
SELECT CONSTRAINT_NAME
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = <tablename>;
DESCRIBE <tablename>;
-- make sure follow the order of deleting tables
-- see emptynu.sql or northwoo.sql
DROP TABLE <tablename>;
RENAME <oldtablename> TO <newtablename>;
Chen: Enhanced Guide to Oracle page-5
105
FACULTY
pk
f_id
1
5
LOCATION
pk
loc_id
45
57
s_last
Miller
s_pin
8891
Connoly
f_last
Cox
9188
Brown
fk
loc_id
53
57
bldg_code
CR
room
101
LIB
222
fk
f_id
1
f_image
cox.jpg
brown.jpg
capacity
150
TABLE-
(STUDENT)
TABLE-1
(FACULTY)
RULES:
1. You cant add a record to TABLE- (or the table with fk, e.g.,
STUDENT) unless there is a corresponding record in TABLE-1 (or
the table with pk).
2. You cant delete a record in TABLE-1 (or the table with pk, e.g.,
FACULTY) if there is a record in TABLE- (or the table with fk).
Order of entering data into the database: LOCATION FACULTY
STUDENT
Order of deleting data from the database:STUDENT FACULTY
LOCATION
Therefore, the order of entering data (or creating tables) is:
LOCATION ----> FACULTY ----> STUDENT
Chapter 3
Before you start this chapter you should DROP all the existing tables and recreate tables structures (for
the purpose of accuracy), you should:
1.
Copy the following six files from my web site (or Jepson labs server):
a.
Dropnorthwoods.sql (Delete tables structures and data)
b.
Emptynorthwoods.sql (Delete tables structures and data and then recreate their structures
ONLY)
c.
Northwoods.sql (Delete tables structures and data and then recreate both structures and data)
d.
Dropclearwater.sql
e.
Emptyclearwater.sql
f.
Clearwater.sql
2.
A.
(DML)
Note that you may create all SQL commands in the chapter3 with Notepad and save them as file
Commands.SQL. Therefore, you are able to reuse them by copy and paste them.
1.
2.
3.
Commit the changes that were saved on the database buffer temporarily:
SQL> COMMIT;
4.
Rollback enables the user to return the database to its original state by undoing the effects of
all of the commands since the last COMMIT occurred with the command:
SQL> ROLLBACK;
5. ROLLBACK can be used with SAVEPOINT that mark the beginning of a transaction. Any
other commands issued between the savepoint and the rollback will not be saved in the
database.
SQL> SAVEPOINT <savepoint name>
II.
B.
9.
SELECT COMMANDS
-- Retrieving Data from a Single Table
SELECT <column 1, column 2, >
FROM <ownername.tablename>
WHERE <search condition>;
Arithmetic
Number Functions
ABS(n)
POWER(n, power)
e.g., SELECT POWER(3,2) FROM DUAL;
ROUND(n, precision)
e.g., SELECT ROUND(123.476, 1) FROM DUAL;
TRUNC(n, precision)
e.g., SELECT TRUNC(123.476, 1) FROM DUAL;
e.g.,
SELECT sid, slname, TRUNC((SYSDATE sdob)/365.25) AS age
FROM student, DUAL
Result: 1
Result: 9
Result: 123.5
Result: 123.4
AND
;
They all require that both queries have the same number of display columns in the
SELECT statement, and that each column in the first qurey has the same data type as the
corresponding column in the second query.
Database Views
A database view is a logical (virtual) table based on a query. It does not store data, but presents it in
a format different from the one in which it is stored in the underlying tables.
CREATE VIEW <view_name> AS
<view query specification.;
e.g.,
CREATE VIEW faculty_view AS
SELECT f_id, f_last, f_first, f_mi, loc_id, f_phone, f_rank
FROM faculty;
SELECT bldg_code, room
FROM location, faculty_view
WHERE f_last = 'Williams
AND f_first = 'Jerry'
AND location.loc_id = facluty_view.loc_id;
Other commonly used database view commands:
SELECT view_name FROM user_views;
SELECT view_name FROM ALL_VIEWS
WHERE owner=SYSTEM;
DROP VIEW <view_name>.;
Security
Security is the prevention of unauthorized access to the database. Within an organization, the
database administrator determines the types of access various users need for the database. Some
users might be able to retrieve and update data in the database. Other users might be able to
retrieve any data from the database but not make any changes to it. Still other users might be able
to access only a portion of the database.
GRANT <privilege1, privilege2, >
ON <table_name>
TO <user1, user2, >;
where privilege may be
ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE, and ALL
user may be a users name or PUBLIC
Chen: Enhanced Guide to Oracle page-11
Query Builder also generates SQL commands (code generator). See text book for more details.
Keys
Accept
Block Menu
Cancel
Cancel a Query
Clear Block
Clear Field/Item
Clear Form
Clear Record
Count Matching Records
Delete Backward
Delete Record
Display Error
Down
Down
Duplicate Field/Item
Duplicate Record
Edit
Enter Query
Execute Query
Exit
Help
Left
List of Values (LOVs)
New Record
Next Block
Next Field/Item
Next Field/Item
Next Primary
Next Record
Next Set of Records
Previous Block
Previous Field/Item
Previous Field/Item
Previous Record
Print
Return
Right
Run a Form
Scroll Down
Scroll Up
Show Keys
Up
Up
F10
F5
Esc
Ctrl+Q
Shift+F5
Ctrl+u
Shift+F7
Shift+F4
Shfit+F2
Backspace
Shift+F6
Shift+F1
Ctrl+l
Down
F3
F4
Ctrl+e
F7
F8
Ctrl+q
F1
Left
F9
F6
Ctrl+Page Down
Tab
Shift+Tab
Shift+F3
Shift+Down
Ctrl+>
Ctrl+Page Up
Shift+Ctrl+Tab
Shift+Tab
Shift+Up
Shift+F8
Enter
Right
Ctrl+r
Page Down
Page Up
Ctrl+F1
Up
Ctrl+p