Вы находитесь на странице: 1из 6

OCA/OCP Oracle database 11g All-in-One Exam Guide

Chapter 7 DDL and Schema Objects


Objectives
Categorize the Main Database Objects Review the Table Structure List the Data Type That Are Available for Columns Create a Simple Table Explain How constraints Are Created at the Time of Table Creation Describe How Schema Objects work Create and Modify Tables Manage Constraints Create Indexes Create and Use Temporary Tables Create Simple and Complex Views Retrieve Data from Views Create, maintain, and use Sequences Create and Maintain Indexes Create Private and Public Synonyms

Lab Preparation
Create Lab user conn system/oracle drop user jchao cascade; create user jchao identified by welcome; grant dba to jchao; conn jchao/welcome

Categorize the Main Database Objects


Object Types Everything created in the database is an object. select object_type, count(*) cnt from dba_objects where owner = 'HR' group by object_type order by cnt desc; Naming Schema Objects

Length: 30 Reserved words: SELECT, ALTER, TABLE Beginning letter: a-z, A-Z Other characters: a-z, A-Z, 0-9, _$# Case: non-sensitive select object_name from dba_objects; Object namespaces create table emp (id number); create synonym emp for hr.employees; create index emp on emp(id); Determine What Objects Are Accessible conn hr/hr select object_type, count(*) from user_objects group by object_type; select object_type, count(*) from all_objects group by object_type; select distinct owner from all_objects; conn jchao/welcome;

List the Data Types That Are Available for Columns


List the types select distinct data_type from dba_tab_columns order by 1; Character VARCHAR2 NVARCHAR2 CHAR Numeric NUMBER FLOAT INTEGER Date and Time DATE TIMESTAMP TIMESTAMP WITH TIMEZONE TIMESTAMP WITH LOCAL TIMEZONE INTERVAL YEAR TO MONTH INTERVAL DAY TO SECOND Large Objects CLOB

NCLOB BLOB BFILE LONG LONG RAW Some Other Types RAW ROWID

Create a Simple Table


Creating Tables with Column Specifications drop table emp; create table emp ( eid number, ename varchar2(20), dob date ); select * from emp; Creating Tables from Subqueries drop table emp; create table emp as select * from hr.employees; create table emp2 as select employee_id eid, email ename from hr.employees; create table emp3 as select * from hr.employees where 1=2; Altering Table Definition after Creation alter table emp add (job_id number); alter table emp modify (commission_pct number(4,2) default 0.05); alter table emp drop column commission_pct; alter table emp set unused column job_id; alter table emp rename column hire_date to recruited; alter table emp read only; Dropping and Truncating Tables select * from user_tables; select * from user_tab_columns; drop table emp3; truncate table emp2; drop table emp;

Create and use Temporary Tables


Reason to user temporary tables

SQL commands are far faster than regular tables: It uses temporary segment (not via buffer cache). It doesnt generate redo. Create, Insert, Select (Between two sessions) create global temporary table tmp_emp on commit preserve rows as select * from hr.employees where 1=2; insert into tmp_emp select * from hr.employees where department_id=30; commit; (start a second sqlplus session) select count(*) from tmp_emp; insert into tmp_emp select * from hr.employees where department_id=50; commit; (query tmp_emp in the 2 sessions and compare result) Disconnect and connect again (disconnect a session and login again, query the tmp_emp table)

Indexes
Why indexes Are Needed Indexes are part of the constraint mechanism. Indexes are critical for performance for where clause, sorting, table join. Types of Index B*Tree (Balanced Tree) Bitmap Unique Reverse key Compressed Composite Function based Ascending or descending Creating and Using indexes create table emp(eno number, ename varchar2(20), dob date, dno number); create unique index ui_emp_ename on emp(ename); create index i_emp_dob on emp(dob); create bitmap index bi_emp_dno on emp(dno); insert into emp select employee_id, email, null, dept_id from hr.employees; commit; select * from emp where eno = 123; alter table emp add constraint pk_emp primary key (eno); (use sql developer to check the execution path to see use of index)

Modifying and Dropping Indexes select * from user_indexes; select * from user_ind_columns; drop index i_emp_dob;

Constraints
The Types of Constraint Unique Not null Primary key Foreign key Check Defining Constraints create table dept(dno number, dname varchar2(20)); alter table dept add constraint pk_dept primary key (dno); alter table dept add constraint uk_dept_dname unique (dname); alter table dept add constraint ck_dept_dno check(dno between 10 and 90); alter table emp add constraint fk_emp_dno foreign key(dno) references dept(dno); Constraint State ENABLE VALIDATE DISABLE NOVALIDATE ENABLE NOVALIDATE DISABLE VALIDATE alter table emp add constraint fk_emp_dno foreign key(dno) references dept(dno) enable novalidate; insert into dept select dept_id, name from hr.departments; commit; alter table emp modify constraint fk_emp_dno enable validate; select * from user_tab_constraints; Constraint Checking alter table emp add constraint nn_emp_dob check(dob is not null) deferrable initially immediate; set constraint nn_emp_dob deferred; insert into emp values (900, Paul, null, 10); commit; set constraint nn_emp_dob immediate;

Views
Why Use Views

Enforce security Simplify user SQL Prevent errors Make data comprehensive Performance Simple and Complex Views Join one or more tables, use functions or not, perform aggregation or not. CREATE VIES, ALTER VIEW, and DROP VIEW CREATE OR REPLACE FORCE, NOFORCE WITH CHECK OPTION WITH READ ONLY CONSTRAINT create view v_emp_cpd as select * from hr.employees where dept_id = 60; create view v_emp_sum as select dept_id, count(1) from hr.emp group by dept_id; select * from user_views;

Synonyms
Create, Select, Drop create synonym employees for hr.employees; select * from employees; select * from user_synonyms; drop synonym employees;

Sequences
Creating Sequences create sequence emp_seq; select * from user_sequences; Using Sequences select emp_seq.nextval from dual; insert into emp(eid, ename) values(emp_seq.nextval, john);

Вам также может понравиться