You are on page 1of 38

08 - Managing Schema Objects

By Muhammad Asghar Khan


Reference: OCA Oracle Database 11g - Admin I Exam Guide by John Watson

1/2

Agenda

Create and Modify Tables EXERCISE 8-1: Create and Modify Tables Manage Constraints Unique Constraint Not Null Constraint Primary Key Constraints Foreign Key Constraints Check Constraints Constraint State

Exercise 8-2: Manage Constraints


http://asghars.blogspot.com

2/2

Agenda

Create Indexes

B*Tree Indexes Bitmap Indexes

EXERCISE 8-3: Create Indexes

Create and Use Temporary Tables


EXERCISE 8-4: Create and Use Temporary Tables

http://asghars.blogspot.com

1/4

Create and Modify Tables

Oracle database supports several types of permanent table: heap tables, index-organized tables, partitioned tables, and clustered tables When a user account is created, a schema is created too A schema is a container for tables, views, code, and other database objects The unique identifier for an object of a particular type is its name, prefixed with the name of the schema to which it belongs, e.g. HR.REGIONS
4 http://asghars.blogspot.com

2/4

Create and Modify Tables

To view schema objects through Database Control, take the appropriate link for the type of object of interest from the Schema tab on the database home page

http://asghars.blogspot.com

3/4

Create and Modify Tables

A namespace defines a group of object types, within which all names must be uniquely identified, by schema and name When creating tables, each column must be assigned a datatype, which determines the nature of the values that can be inserted into the column Datatypes for alphanumeric data are VARCHAR2, NVARCHAR2, CHAR and RAW Datatypes for numeric data are NUMBER, FLOAT, INTEGER
6 http://asghars.blogspot.com

4/4

Create and Modify Tables

Datatypes for data and time are DATE, TIMESTAMP, TIMESTAMP WITH TIMEZONE, TIMESTAMP WITH LOCAL TIMEZONE, INTERVAL YEAR TO MONTH, INTERVAL DAY TO SECOND Datatypes for large objects are CLOB, NCLOB, BLOB, BFILE, LONG and LONG RAW To reach the table management window of Database Control, from the database Home PageSchema Tab Database Objects section Tables Link
7 http://asghars.blogspot.com

1/2

EXERCISE 8-1: Create and Modify Tables


1. 2.

Connect to your database user HR Create a table as follows Remove the columns not relevant

3.

4.

Add columns needed in the new table

http://asghars.blogspot.com

2/2

EXERCISE 8-1: Create and Modify Tables


5.

Navigate to Schema tabTables link in the Database Objects section. Enter HR as the search criterion and click Go. This will locate all of HRs tables Select the radio button for the table EX_EMPS, Generate DDL in the Actions drop-down box, and click Go. Study the generated DDL

6.

http://asghars.blogspot.com

1/8

Manage Constraints

Table constraints are a means by which the database can enforce business rules and guarantee that the data conforms to the entity-relationship model A constraint violation will force an automatic rollback of the entire statement that hit the problem, not just the single action within the statement, and not the entire transaction

The constraint types supported by the Oracle database are:


10 http://asghars.blogspot.com

2/8

Manage Constraints

Unique Constraint

A unique constraint nominates a column (or combination of columns) for which the value must be different for every row in the table When a unique constraint is defined, Oracle will look for an index on the key column(s)/unique column(s), and if one does not exist, it will be created

Not Null Constraint

The not null constraint forces values to be entered into the key column
http://asghars.blogspot.com

11

3/8

Manage Constraints

Any attempt to insert a row without specifying values for the not null-constrained columns results in an error It is possible to bypass the need to specify a value by including a DEFAULT clause on the column when creating the table The primary key is the means of locating a single row in a table The Oracle database deviates from the paradigm (as do some other RDBMS implementations) by permitting tables without primary keys
http://asghars.blogspot.com

Primary Key Constraints

12

4/8

Manage Constraints

The implementation of a primary key constraint is in effect the union of a unique constraint and a not null constraint A foreign key constraint is defined in the child table in a parent-child relationship The constraint nominates a column (or columns) in the child table that corresponds to the primary key column(s) in the parent table Bothe columns must be of the same data type
http://asghars.blogspot.com

Foreign Key Constraints


13

5/8

Manage Constraints

If there is not a row in the parent table with a null value. This creates orphan rows and can cause dreadful confusion The constraint may be created as ON DELETE CASCADE. This means that if a row in the parent table is deleted, Oracle will search the child table for all the matching rows and delete them too while; ON DELETE SET NULL the parent table is deleted, Oracle will search the child table for all the matching rows and set the foreign key columns to null A variation on the foreign key constraint is the selfreferencing foreign key constraint
http://asghars.blogspot.com

14

6/8

Manage Constraints

Self-referencing FK constraint defines a condition where the parent and child rows exist in the same table A check constraint can be used to enforce simple rules, such as that the value entered in a column must be within a range of values The rule must be an expression that will evaluate to TRUE or FALSE The not null constraint is in fact implemented as a preconfigured check constraint
http://asghars.blogspot.com

Check Constraints

15

7/8

Manage Constraints

Constraint State

At any time, every constraint is: ENABLE VALIDATE: It is not possible to enter rows that would violate the constraint, and all rows in the table conform to the constraint DISABLE NOVALIDATE: Any data (conforming or not) can be entered, and there may already be non-conforming data in the table ENABLE NOVALIDATE: There may already be nonconforming data in the table, but all data entered now must conform DISABLE VALIDATE: An impossible situation: all data in the table conforms to the constraint, but new rows need not. The end result is that the table is locked against DML commands
http://asghars.blogspot.com

16

8/8

Manage Constraints

Constraints can be checked as a statement is executed (an IMMEDIATE CONSTRAINT) or when a transaction is committed (a DEFERRED constraint) By default, all constraints are IMMEDIATE and not deferrable

17

http://asghars.blogspot.com

1/4

Exercise 8-2: Manage Constraints


1.

2.
3. 4. 5.

In Database Control, navigate to the listing of HRs tables Select the radio button for the table EX_EMPS and click the Edit button Take the Constraints tab to view the three NOT NULL constraints that were created with the table In the Constraints drop-down box, select Primary and click the Add button In the Add PRIMARY constraint window, choose the EMPLOYEE_ID column and click Continue, as in the next illustration
http://asghars.blogspot.com

18

2/4

Exercise 8-2: Manage Constraints

6.

Click the Show SQL button to see the constraint creation statement, and then the Return button Click the Apply button to run the statement Connect to your database as user HR with SQL*Plus Find the names of the constraints
http://asghars.blogspot.com

7. 8.

9.
19

3/4

Exercise 8-2: Manage Constraints

20

http://asghars.blogspot.com

4/4

Exercise 8-2: Manage Constraints


10.

Rename the constraints to something more meaningful

21

http://asghars.blogspot.com

1/9

Create Indexes

Indexes have two functions: to enforce primary key and unique constraints, and to improve performance You should always create indexes on the foreign key columns within the child table for performance reasons An index is a sorted list of key values, structured in a manner that makes the search very efficient If the table has no index on the column(s), the only way to do this would be to scan right through the table, checking every row As a general rule, indexes will improve performance for data retrieval but reduce performance for DML operations
22 http://asghars.blogspot.com

2/9

Create Indexes

Oracle supports several types of index, two index types of concern here are the B*Tree index, which is the default index type, and the bitmap index B*Tree Indexes

23

A B*Tree index (the B stands for balanced) is a tree structure The depth of the tree will be largely determined by the number of rows in the table and the length of the index key values The leaf nodes of the index tree store the rows keys, and then uses the pointer to find the row
http://asghars.blogspot.com

3/9

Create Indexes

The pointer to the row is the rowid. The rowid is an Oracle-proprietary pseudocolumn, which every row in every table has Every row in every table in the whole database will have a different rowid B*Tree indexes should be used if:

The cardinality (the number of distinct values) in the column is high The number of rows in the table is high The column is used in WHERE clauses or JOIN conditions
http://asghars.blogspot.com

24

4/9

Create Indexes

Bitmap Indexes

A bitmap index stores the rowids associated with each key value as a bitmap A particular advantage that bitmap indexes have over B*Tree indexes is that they include NULLs A simple fact is that B*Tree indexes are often useless in a data warehouse environment Bitmap indexes should be used if:

The cardinality (the number of distinct values) in the column is low The number of rows in the table is high The column is used in Boolean algebra operations
http://asghars.blogspot.com

25

5/9

Create Indexes

You can specify the following options when creating indexes: Unique or Non-unique

Non-unique is the default Built on a version of the key column with its bytes reversed: rather than indexing John, it will index nhoJ This is a powerful technique for avoiding contention in multiuser systems
http://asghars.blogspot.com

Reverse key

26

6/9

Create Indexes

Compressed

A compressed index will store the key once, followed by a string of all the matching rowids The default is not to compress, meaning that if a key value is not unique, it will be stored once for each occurrence, each having a single rowid pointer It is built on the concatenation of two or more columns It is built on the result of a function applied to one or more columns
http://asghars.blogspot.com

Composite

Function based

27

7/9

Create Indexes

A query will have to apply the same function to the search string, or Oracle may not be able to use the index By default, an index is ascending, a descending index reverses this

Ascending or Descending

All these six variations apply to B*Tree indexes but only the last three (Composite, Function based, and Ascending or descending) can be applied to bitmap indexes
28 http://asghars.blogspot.com

8/9

Create Indexes

The basic syntax for creating an index explicitly is

CREATE [UNIQUE | BITMAP] INDEX [schema.]indexname ON [schema.]tablename(column [, column...]);

If the indexes are created explicitly, the creator has full control over the characteristics of the index, which can make it easier for the DBA to manage subsequently ALTER INDEX cannot be used to change the characteristics like the type of the index, the columns, or whether it is unique or non-unique
29 http://asghars.blogspot.com

9/9

Create Indexes

When a table is dropped, all the indexes and constraints defined for the table are dropped as well

30

http://asghars.blogspot.com

1/3

EXERCISE 8-3: Create Indexes


1. 2.

Connect to database with SQL*Plus as user HR Determine the name and some other characteristics of the primary key index

3.

Create a compound B*Tree index on the employees names

31

http://asghars.blogspot.com

2/3

EXERCISE 8-3: Create Indexes


4.

Create bitmap indexes on some low-cardinality columns

5.

With Database Control, look at the indexes; Database home page Schema tabIndexes link in the Database Objects section Enter HR as the schema name and EX_EMPS as the object name, and click Go
http://asghars.blogspot.com

6.

32

3/3

EXERCISE 8-3: Create Indexes

7.

Click on index name and check the option of an index

33

http://asghars.blogspot.com

1/2

Create and Use Temporary Tables

A temporary table has a definition that is visible to all sessions, but the rows within it are private to the session that inserted them In many ways, a temporary table is similar to a permanent table You can execute any DML or SELECT command against it It can have indexes, constraints, and triggers defined They exist only in the PGAs of the sessions that are using them If the PGA cannot grow sufficiently to store the temporary table, then the table gets written out to a temporary segment in the users temporary tablespace
34 http://asghars.blogspot.com

2/2

Create and Use Temporary Tables


DML against temporary tables does not generate redo A typical use of temporary tables is for the reporting systems that run in data warehouses If you see a lot of TRUNCATE commands, it is likely that programmers are loading data into tables, working on it, and then removing it, This is a perfect use for temporary tables

35

http://asghars.blogspot.com

1/3

EXERCISE 8-4: Create and Use Temporary Tables


1. 2.

Connect to database with SQL*Plus as user HR Create a temporary table as follows

3.
4.

Start a second SQL*Plus session as HR


In the second session insert some different rows

36

http://asghars.blogspot.com

2/3

EXERCISE 8-4: Create and Use Temporary Tables


5.

In the first session, truncate the table In the second session, confirm that there are still rows in that sessions copy of the table

6.

7.

In the second session, demonstrate that terminating the session does clear the rows

37

http://asghars.blogspot.com

3/3

EXERCISE 8-4: Create and Use Temporary Tables

8.

Check the tmp_emps table in database constrol

38

http://asghars.blogspot.com

You might also like