Академический Документы
Профессиональный Документы
Культура Документы
Overview
Oracle terms
Schema logical collection of users
objects
Tablespace logical space used for
storage
Datafile physical file used for
storage
Extent group of contiguous blocks
Block unit of physical storage
Oracle Architecture
database vs. instance
Database
Instance
Parameter files*
Control files**
Data files
Redo Log files
Disk
Memory
MySQL
Three files per table
Permissions based on user, database, and host
Oracle
Many files
Many roles/permissions possible
inserting apostrophes
INSERT INTO emp (name) VALUES (ONeill);
Sysdate
Sysdate returns current system date AND time
use trunc function to remove time piece
Example:
select to_char (adate, dd-mon-yy hh24:mi:ss)
TO_CHAR(ADATE, DD-MON-YY:HH24:MI:SS)
17-feb-00 23:41:50
ROWID
ROWID is an internal number Oracle uses to
uniquely identify each row
NOT a primary key! Is the actual location of a
row on a disk. Very efficient for retrieval.
Format specifies block, row, and file (and
object in 8)
Oracle 7: BBBBBBB.RRRR.FFFFF
Oracle 8: OOOOOO.FFF.BBBBBB.RRR
More functions
nvl()
If NULL, return this instead
Nvl(lastname,Anonymous)
decode()
Sort of like an If/Then statement
Decode(gender,0,Male,1,Female,Unknown)
Constraints
Primary key
Foreign key
Unique, not null
Check
CREATE TABLE
test (
id NUMBER(2),
col2 VARCHAR2(2),
col3 VARCHAR2(3),
CONSTRAINT test_pk PRIMARY KEY(id),
CONSTRAINT col3_ck CHECK (col3 IN ('yes','no'))
);
SELECT
user_constraints.constraint_name name,
constraint_type type,
user_constraints.search_condition
FROM user_constraints, user_cons_columns
WHERE
user_constraints.table_name=user_cons_columns.table_name
AND user_constraints.constraint_name=user_cons_columns.constraint_name
AND user_constraints.owner=user_cons_columns.owner
AND user_constraints.table_name=TEST;
NAME
T SEARCH_CONDITION
--------------- - ------------------------COL3_CK
C col3 IN ('yes','no')
TEST_PK
Constraints
Oracle naming of constraints is NOT
intuitive!
enabling and disabling
disable constraint constraint_name;
More objects
Sequences
creating the sequence
create sequence CustomerID increment by 1
start with 1000;
Synonyms
provide location and owner transparency
Can be public or private
PL/SQL Triggers
Trigger example
SQL> desc all_triggers;
Name
Null?
------------------------------- -------OWNER
TRIGGER_NAME
TRIGGER_TYPE
TRIGGERING_EVENT
TABLE_OWNER
BASE_OBJECT_TYPE
TABLE_NAME
COLUMN_NAME
REFERENCING_NAMES
WHEN_CLAUSE
STATUS
DESCRIPTION
ACTION_TYPE
TRIGGER_BODY
Type
---VARCHAR2(30)
VARCHAR2(30)
VARCHAR2(16)
VARCHAR2(75)
VARCHAR2(30)
VARCHAR2(16)
VARCHAR2(30)
VARCHAR2(4000)
VARCHAR2(128)
VARCHAR2(4000)
VARCHAR2(8)
VARCHAR2(4000)
VARCHAR2(11)
LONG
Trigger example
(cont.)
Trigger example
(cont.)
SQL> /
TRIGGER_NAME
TRIGGER_TYPE
TRIGGERING_EVENT
TABLE_NAME STATUS
DESCRIPTION
-------------------- ---------------- ------------------ ---------- -------- ----------------------TRIGGER_BODY
----------------------------------AFTER_INS_UPD_ON_EMP BEFORE EACH ROW INSERT OR UPDATE
EMP
ENABLED
scott.after_ins_upd_on_emp
before insert or update
on scott.emp
for each row
begin
:new.ename := upper(:new.ename);
end;
Remember those
views?
Query USER_TRIGGERS to get trigger info
Query USER_SOURCE to get source of
procedure, function, package, or package
body
Query USER_ERRORS to get error
information (or use show errors)
col name format a15
col text format a40
select name, type, text
from user_errors
order by name, type, sequence;
Understanding Indexes
Index overhead
impact on inserts, updates and deletes
batch inserts can be slowed by indexes - may
want to drop, then recreate
rebuilding indexes
B*-tree index
Miller
< Miller
> Miller
< Davis
Davis
Jones
Adams
Brown
Culver
Deal
Howard
Isis
Branch
blocks
Smith
Turner
Turner >
Jules
Klein
Main
Moss
Porter
Sikes
Deal ROWID
Howard ROWID Detail of
leaf node
Isis - ROWID
Sykes
Thomas
Topper
Vera
Wagner
Yanks
Leaf
blocks
Bitmap index
Parts table
partno
1
2
3
4
color
size
GREEN
RED
RED
BLUE
MED
MED
SMALL
LARGE
0 0 0 1
color = RED
0 1 1 0
color = GREEN
1 0 0 0
Part number
1 2 3 4