Vous êtes sur la page 1sur 12

ORACLE SESSION-4 DOCUMENT

___________________________________________________________________________________________________ Copyrights Reserved Page 1 of 12

TOPICS COVERED
CREATING TABLES, SEQUENCES INDEXES AND VIEWS * Tables. * Sequences. * Indexes. * Views.

ORACLE DATA TYPES * Oracle SQL Data Types. * Oracle PL/SQL Data Types.

___________________________________________________________________________________________________ Copyrights Reserved Page 2 of 12

CREATING TABLES, SEQUENCES, INDEXES AND VIEWS.


This chapter helps us to know more about the other parameter while creating tables and it also gives the complete explanation of creating, using and dropping the objects like sequences, indexes and views. CONSTRAINT The conditions or some restrictions placing on a column of a table TYPES OF CONSTRAINT CHECK: Specifies the value for a column or group of columns must satisfy a certain condition. Syntax: create table table_name (column_name data type check [condition]); Example: create table t (id number check (id>10)); NOT NULL: Specifies a column should not accept null values. Syntax: create table table name (column_name data type not null); Example: create table t1 (id number not null); PRIMARY KEY: Specifies a column should be unique. It cannot accept null values. Only one primary key for a single table Syntax: create table table_name (id number primary key); Example: create table t2 (id number primary key);

___________________________________________________________________________________________________ Copyrights Reserved Page 3 of 12

FOREIGN KEY: Specifies a column in another table. Can accept null values. Syntax: create table table_name (column_name data type references parent_table (primary key column)); Syntax: create table t3(id1 number references t2(id));

UNIQUE: Specifies a column should be unique. Can accept null values. Syntax: create table table_name(column_name data type unique); Example: create table t4(id number unique);

CHECK OPTION: Specifies that DML operations on a view must satisfy the sub query. Syntax: create view view_name as select * from table_name where (condition) with check option constraint constraint_name; Example: create view t_v as select * from t where id >5 with check option constraint c_v;

READ ONLY: Specifies that a view may only be read purpose. Syntax: create view view_name as select * from table_name with read only; Example: create view v_t as select * from t with read only;

DEFERRED CONSTRAINT: It is enforced when a transaction is committed. It is not possible to change the existing constraint to DEFERRABLE; we must drop and recreate the constraint.
___________________________________________________________________________________________________ Copyrights Reserved Page 4 of 12

INITIALLY IMMEDIATE: The Constraint is checked whenever we add, update or delete rows from a table. Syntax: create table table_name(column_name data_type constraint constraint_name constraint_type DEFERRABLE INITIALLY IMMEDIATE); Example: create table a (c1 number constraint c_k check(c1>10) DEFERRABLE INITIALLY IMMEDIATE);

INITIALLY DEFERRED: The Constraint is only checked when a transaction is committed. Syntax: create table table_name(column_name data_type constraint constraint_name constraint_type DEFERRABLE INITIALLY DEFERRED); Example: create table a (c1 number constraint c_k check(c1>10) DEFERRABLE INITIALLY DEFERRED); SEQUENCES: It is a database object that generates a order of integers. CREATING A SEQUENCE. Syntax: create sequence sequence_name start with [value] increment by [value] [maxvalue [value]] [minvalue [value]] [[cycle |nocycle] [cache | nocache] [order | noorder]; example: create sequence seq_1 start with 5 increment by 3;

USING A SEQUENCE: It contains two pseudo columns named CURRVAL and NEXTVAL. CURRVAL To get the current value from the sequence NEXTVAL To get the next value from the sequence

___________________________________________________________________________________________________ Copyrights Reserved Page 5 of 12

Syntax: select sequence_name.nextval from dual; select sequence_name.currval from dual; Example: select seq_1.nextval from dual; select seq_1.currval from dual;

MODIFYING A SEQUENCE. Sequence can be modified by using the ALTER SEQUENCE statement. Syntax: alter sequence sequence_name [option]; Example: alter sequence seq_1 increment by 4; LIMITATIONS ON MODIFYING A SEQUENCE We cannot change the start value of a sequence. The minimum value cannot be more than the current value of the sequence(currval). The maximum value cannot be less than the current value of the sequence(currval). DROPPING A SEQUENCE. Sequence can be drop using DROP SEQUENCE statement. Syntax: drop sequence sequence_name; Example: drop sequence seq_1;

___________________________________________________________________________________________________ Copyrights Reserved Page 6 of 12

INDEXES
Used for faster retrieval of data.

CREATING AN INDEX. Syntax: create index index_name on table_name (column_name[, column_name...]); MODIFYING AN INDEX. An Index can be modified by using ALTER INDEX Statement Syntax: alter index index_name RENAME to new_index_name;

DROPPING AN INDEX. An Index can be dropped by using DROP INDEX Statement Syntax: drop index index_name;

VIEW. It is basically a predefined query on one or more tables. It is used for privacy purposes.

BENEFITS OF VIEW * Only allow a user to retrieve data through a view. This allows hiding the underlying base tables on which a view is built from an end user. * Write complex queries as a view. This allows you to hide complexity from an end user. * Only allow a view to access certain rows in the base tables. This allows
___________________________________________________________________________________________________ Copyrights Reserved Page 7 of 12

implementing another layer of security and allows hiding rows from an end user. CREATING A VIEW. Syntax: create [or replace] [force | noforce] view view_name [alias_name[alias_name...]] as subquery [with {check option | read only} constraint constraint_name]; TYPES OF VIEW SIMPLE VIEW: Contains a subquery that retrieves from one base table. Syntax: create view view_name as select [ {* | column_name}] from table_name; COMPLEX VIEW: Contains a subquery that retrieves from multiple base tables. Group rows using GROUP BY or DISTINCT clause. Contains a function call. Syntax: create view view_name as select a.col1, b.col1 from table1 a [JOIN] table2 b; LIMITATIONS ON VIEWS We can perform DML operations with simple views. Complex views cannot support DML operations. MODIFYING A VIEW. View can be modified by using ALTER VIEW statement Syntax: alter view view_name drop constraint constraint_name; DROPPING A VIEW. View can be dropped using DROP VIEW statement. Syntax: drop view view_name;

___________________________________________________________________________________________________ Copyrights Reserved Page 8 of 12

ORACLE DATA TYPES


This chapter gives the complete details about the data types that have to be used with SQL and PL/SQL queries. This also tells to about the different data types for each set like for characters, numbers and large objects and dates.

DATA TYPES IN ORACLE


VARCHAR2 (length): * Variablelength character data of up to length bytes or characters. * Maximum length is 4,000 bytes. NUMBER (precision, scale): * Variable-length number. * Precision the maximum number of digits ( in front of and behind a decimal point) that may be used for the number. The maximum precision supported is 38. * Scale - the maximum number of digits to the right of a decimal point.

DATE: * Date and time with the century, all four digits of year, month, day, hour (in 24hour format), minute and second. * Used to store a date and time between January 1, 4712 B.C and December 31, 4712 A.D. * Default format is specified by the NLS_DATE_FORMAT parameter.

CLOB: Variable length single-byte character data of up to 128 terabytes BINARY_FLOAT:


___________________________________________________________________________________________________ Copyrights Reserved Page 9 of 12

* Stores a single precision 32-bit floating-point number. * Operations involving BINARY_FLOAT are typically performed faster than on NUMBER DATA TYPE. * It requires 5 bytes of storage space.

BINARY_DOUBLE: * Stores a double precision 64-bit floating-point number. * Operations involving BINARY_DOUBLE are typically performed faster than on NUMBER DATA TYPE. * It requires 9 bytes of storage space.

BOOLEAN: Boolean value (TRUE, FALSE or NULL).

BINARY_INTEGER. Integer between -2 (-2,147,483,648) and 2 (2,147,483,648).

1. What is meant by DEFERRED constraints and what are the two means by which it can be marked? 2. You want to make data in the SSN column in a table unique. However, you want the column to be able to accept NULL values. Which of the following constraint would you use to implement the solution? (i) PRIMARY KEY constraint (ii) UNIQUE constraint
___________________________________________________________________________________________________ Copyrights Reserved Page 10 of 12

(iii) FOREIGN KEY constraint (iv) DEFAULT constraint (v) CHECK constraint (vi) NOTNULL constraint (vii) NULL constraint 3. What is a sequence and what are the limitations while modifying the sequences? 4. What is an INDEX? 5. What is meant by views and what are their types? 6. When can an index be placed on a view? A) When you only SELECT from the view B) When you only DELETE from the view C) When there is a WITH CHECK OPTION used to create the view D) When you can UPDATE using the view E) Never 7. Mention the data types that are newly added in oracle10g and explain the differences?

Chapters covered in Session 4:


CHAPTER 10: CREATE TABLES, SEQUENCES, INDEXES AND VIEWS ORACLE DATA TYPES.

Reference Book:
ORACLE DATABASE 10 G SQL
___________________________________________________________________________________________________ Copyrights Reserved Page 11 of 12

Written By Jason Price

___________________________________________________________________________________________________ Copyrights Reserved Page 12 of 12

Vous aimerez peut-être aussi