Date post: | 04-Apr-2018 |
Category: |
Documents |
Upload: | muhammad-asghar-khan |
View: | 222 times |
Download: | 0 times |
of 38
7/29/2019 OCA 08 - Managing Schema Objects
1/38
08 - Managing Schema Objects
By Muhammad Asghar Khan
Reference: OCA Oracle Database 11g - Admin I Exam Guide by John Watson
7/29/2019 OCA 08 - Managing Schema Objects
2/38
Agenda
http://asghars.blogspot.com2
Create and Modify TablesEXERCISE 8-1: Create and Modify TablesManage Constraints
Unique ConstraintNot Null ConstraintPrimary Key ConstraintsForeign Key Constraints
Check ConstraintsConstraint State
Exercise 8-2: Manage Constraints
1/2
7/29/2019 OCA 08 - Managing Schema Objects
3/38
Agenda
http://asghars.blogspot.com3
Create IndexesB*Tree IndexesBitmap Indexes
EXERCISE 8-3: Create Indexes
Create and Use Temporary Tables
EXERCISE 8-4: Create and Use Temporary Tables
2/2
7/29/2019 OCA 08 - Managing Schema Objects
4/38
Create and Modify Tables
http://asghars.blogspot.com4
Oracle database supports several types of permanent table: heap tables, index-organizedtables, partitioned tables, and clustered tables
When a user account is created, a schema iscreated too
A schema is a container for tables, views, code, andother database objects
The unique identifier for an object of a particulartype is its name, prefixed with the name of theschema to which it belongs, e.g. HR.REGIONS
1/4
7/29/2019 OCA 08 - Managing Schema Objects
5/38
Create and Modify Tables
http://asghars.blogspot.com5
To view schema objects through Database Control, take theappropriate link for the type of object of interest from theSchema tab on the database home page
2/4
7/29/2019 OCA 08 - Managing Schema Objects
6/38
Create and Modify Tables
http://asghars.blogspot.com6
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 beassigned a datatype, which determines the natureof the values that can be inserted into the column
Datatypes for alphanumeric data are VARCHAR2,
NVARCHAR2, CHAR and RAWDatatypes for numeric data are NUMBER, FLOAT,INTEGER
3/4
7/29/2019 OCA 08 - Managing Schema Objects
7/38
Create and Modify Tables
http://asghars.blogspot.com7
Datatypes for data and time are DATE, TIMESTAMP,TIMESTAMP WITH TIMEZONE, TIMESTAMP WITHLOCAL 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 HomePage Schema Tab Database Objects section Tables Link
4/4
7/29/2019 OCA 08 - Managing Schema Objects
8/38
EXERCISE 8-1: Create and Modify Tables
http://asghars.blogspot.com8
1. Connect to your database user HR2. Create a table as follows
3. Remove the columns not relevant
4. Add columns needed in the new table
1/2
7/29/2019 OCA 08 - Managing Schema Objects
9/38
EXERCISE 8-1: Create and Modify Tables
http://asghars.blogspot.com9
5. Navigate to Schema tab Tables link in theDatabase Objects section. Enter HR as the searchcriterion and click Go. This will locate all of HRstables
6. Select the radio button for the table EX_EMPS,Generate DDL in the Actions drop-down box, andclick Go. Study the generated DDL
2/2
7/29/2019 OCA 08 - Managing Schema Objects
10/38
Manage Constraints
http://asghars.blogspot.com10
Table constraints are a means by which thedatabase can enforce business rules and guaranteethat the data conforms to the entity-relationshipmodel
A constraint violation will force an automaticrollback of the entire statement that hit theproblem, not just the single action within the
statement, and not the entire transactionThe constraint types supported by the Oracledatabase are:
1/8
7/29/2019 OCA 08 - Managing Schema Objects
11/38
Manage Constraints
http://asghars.blogspot.com11
Unique ConstraintA unique constraint nominates a column (orcombination of columns) for which the value must bedifferent for every row in the table
When a unique constraint is defined, Oracle will lookfor an index on the key column(s)/unique column(s),and if one does not exist, it will be created
Not Null ConstraintThe not null constraint forces values to be enteredinto the key column
2/8
7/29/2019 OCA 08 - Managing Schema Objects
12/38
Manage Constraints
http://asghars.blogspot.com12
Any attempt to insert a row without specifying valuesfor the not null-constrained columns results in anerrorIt is possible to bypass the need to specify a value byincluding a DEFAULT clause on the column whencreating the table
Primary Key ConstraintsThe primary key is the means of locating a single row
in a tableThe Oracle database deviates from the paradigm (asdo some other RDBMS implementations) bypermitting tables without primary keys
3/8
7/29/2019 OCA 08 - Managing Schema Objects
13/38
Manage Constraints
http://asghars.blogspot.com13
The implementation of a primary key constraint is ineffect the union of a unique constraint and a not nullconstraint
Foreign Key Constraints
A foreign key constraint is defined in the child table ina parent-child relationshipThe constraint nominates a column (or columns) inthe child table that corresponds to the primary keycolumn(s) in the parent tableBothe columns must be of the same data type
4/8
7/29/2019 OCA 08 - Managing Schema Objects
14/38
Manage Constraints
http://asghars.blogspot.com14
If there is not a row in the parent table with a nullvalue. This creates orphan rows and can causedreadful confusionThe constraint may be created as ON DELETECASCADE. This means that if a row in the parent tableis deleted, Oracle will search the child table for all thematching rows and delete them too while; ONDELETE SET NULL the parent table is deleted, Oraclewill search the child table for all the matching rows
and set the foreign key columns to nullA variation on the foreign key constraint is the self-referencing foreign key constraint
5/8
7/29/2019 OCA 08 - Managing Schema Objects
15/38
Manage Constraints
http://asghars.blogspot.com15
Self-referencing FK constraint defines a conditionwhere the parent and child rows exist in the sametable
Check Constraints
A check constraint can be used to enforce simplerules, such as that the value entered in a columnmust be within a range of valuesThe rule must be an expression that will evaluate to
TRUE or FALSEThe not null constraint is in fact implemented as apreconfigured check constraint
6/8
7/29/2019 OCA 08 - Managing Schema Objects
16/38
Manage Constraints
http://asghars.blogspot.com16
Constraint StateAt any time, every constraint is:ENABLE VALIDATE: It is not possible to enter rows thatwould violate the constraint, and all rows in the tableconform to the constraint
DISABLE NOVALIDATE: Any data (conforming or not) canbe entered, and there may already be non-conformingdata in the tableENABLE NOVALIDATE: There may already be non-conforming data in the table, but all data entered now
must conformDISABLE VALIDATE: An impossible situation: all data in thetable conforms to the constraint, but new rows need not.The end result is that the table is locked against DMLcommands
7/8
7/29/2019 OCA 08 - Managing Schema Objects
17/38
Manage Constraints
http://asghars.blogspot.com17
Constraints can be checked as a statement isexecuted (an IMMEDIATE CONSTRAINT) or when atransaction is committed (a DEFERRED constraint)
By default, all constraints are IMMEDIATE and notdeferrable
8/8
7/29/2019 OCA 08 - Managing Schema Objects
18/38
Exercise 8-2: Manage Constraints
http://asghars.blogspot.com18
1. In Database Control, navigate to the listing of HRstables
2. Select the radio button for the table EX_EMPS andclick the Edit button
3. Take the Constraints tab to view the three NOT NULLconstraints that were created with the table
4. In the Constraints drop-down box, select Primary andclick the Add button
5. In the Add PRIMARY constraint window, choose theEMPLOYEE_ID column and click Continue, as in thenext illustration
1/4
7/29/2019 OCA 08 - Managing Schema Objects
19/38
Exercise 8-2: Manage Constraints
http://asghars.blogspot.com19
6. Click the Show SQL button to see the constraint
creation statement, and then the Return button
7. Click the Apply button to run the statement
8. Connect to your database as user HR withSQL*Plus
9. Find the names of the constraints
2/4
7/29/2019 OCA 08 - Managing Schema Objects
20/38
Exercise 8-2: Manage Constraints
http://asghars.blogspot.com20
3/4
7/29/2019 OCA 08 - Managing Schema Objects
21/38
Exercise 8-2: Manage Constraints
http://asghars.blogspot.com21
10. Rename the constraints to something moremeaningful
4/4
7/29/2019 OCA 08 - Managing Schema Objects
22/38
Create Indexes
http://asghars.blogspot.com22
Indexes have two functions: to enforce primary key andunique constraints, and to improve performanceYou should always create indexes on the foreign keycolumns within the child table for performance reasonsAn index is a sorted list of key values, structured in amanner that makes the search very efficientIf the table has no index on the column(s), the only wayto do this would be to scan right through the table,checking every row
As a general rule, indexes will improve performance fordata retrieval but reduce performance for DMLoperations
1/9
7/29/2019 OCA 08 - Managing Schema Objects
23/38
Create Indexes
http://asghars.blogspot.com23
Oracle supports several types of index, two indextypes of concern here are the B*Tree index, whichis the default index type, and the bitmap index
B*Tree Indexes
A B*Tree index (the B stands for balanced) is atree structureThe depth of the tree will be largely determined by
the number of rows in the table and the length of theindex key valuesThe leaf nodes of the index tree store the rows keys,and then uses the pointer to find the row
2/9
7/29/2019 OCA 08 - Managing Schema Objects
24/38
Create Indexes
http://asghars.blogspot.com24
The pointer to the row is the rowid. The rowid is anOracle-proprietary pseudocolumn, which every row inevery table hasEvery row in every table in the whole database will
have a different rowidB*Tree indexes should be used if:
The cardinality (the number of distinct values) in thecolumn is high
The number of rows in the table is highThe column is used in WHERE clauses or JOINconditions
3/9
7/29/2019 OCA 08 - Managing Schema Objects
25/38
Create Indexes
http://asghars.blogspot.com25
Bitmap IndexesA bitmap index stores the rowids associated witheach key value as a bitmapA particular advantage that bitmap indexes have over
B*Tree indexes is that they include NULLsA simple fact is that B*Tree indexes are often uselessin a data warehouse environmentBitmap indexes should be used if:
The cardinality (the number of distinct values) in thecolumn is lowThe number of rows in the table is highThe column is used in Boolean algebra operations
4/9
7/29/2019 OCA 08 - Managing Schema Objects
26/38
Create Indexes
http://asghars.blogspot.com26
You can specify the following options when creatingindexes:
Unique or Non-unique
Non-unique is the default
Reverse keyBuilt on a version of the key column with its bytesreversed : rather than indexing John, it will indexnhoJ This is a powerful technique for avoiding contentionin multiuser systems
5/9
7/29/2019 OCA 08 - Managing Schema Objects
27/38
Create Indexes
http://asghars.blogspot.com27
Compressed
A compressed index will store the key once, followed by astring of all the matching rowidsThe 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
Composite
It is built on the concatenation of two or more columns
Function based
It is built on the result of a function applied to one ormore columns
6/9
7/29/2019 OCA 08 - Managing Schema Objects
28/38
Create Indexes
http://asghars.blogspot.com28
A query will have to apply the same function to thesearch string, or Oracle may not be able to use theindex
Ascending or Descending
By default, an index is ascending, a descending indexreverses this
All these six variations apply to B*Tree indexes but
only the last three (Composite, Function based, andAscending or descending) can be applied to bitmapindexes
7/9
7/29/2019 OCA 08 - Managing Schema Objects
29/38
Create Indexes
http://asghars.blogspot.com29
The basic syntax for creating an index explicitly isCREATE [UNIQUE | BITMAP] INDEX [schema.]indexnameON [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 managesubsequently
ALTER INDEX cannot be used to change thecharacteristics like the type of the index, thecolumns, or whether it is unique or non-unique
8/9
7/29/2019 OCA 08 - Managing Schema Objects
30/38
Create Indexes
http://asghars.blogspot.com30
When a table is dropped, all the indexes andconstraints defined for the table are dropped aswell
9/9
7/29/2019 OCA 08 - Managing Schema Objects
31/38
EXERCISE 8-3: Create Indexes
http://asghars.blogspot.com31
1.Connect to database with SQL*Plus as user HR
2. Determine the name and some othercharacteristics of the primary key index
3. Create a compound B*Tree index on theemployees names
1/3
7/29/2019 OCA 08 - Managing Schema Objects
32/38
EXERCISE 8-3: Create Indexes
http://asghars.blogspot.com32
4.Create bitmap indexes on some low-cardinalitycolumns
5. With Database Control, look at the indexes;Database home page Schema tab Indexes linkin the Database Objects section
6. Enter HR as the schema name and EX_EMPS asthe object name, and click Go
2/3
7/29/2019 OCA 08 - Managing Schema Objects
33/38
EXERCISE 8-3: Create Indexes
http://asghars.blogspot.com33
7. Click on index name and check the option of anindex
3/3
7/29/2019 OCA 08 - Managing Schema Objects
34/38
Create and Use Temporary Tables
http://asghars.blogspot.com34
A temporary table has a definition that is visible to allsessions, but the rows within it are private to thesession that inserted them
In many ways, a temporary table is similar to apermanent 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 areusing themIf the PGA cannot grow sufficiently to store thetemporary table, then the table gets written out to atemporary segment in the users temporary tablespace
1/2
7/29/2019 OCA 08 - Managing Schema Objects
35/38
Create and Use Temporary Tables
http://asghars.blogspot.com35
DML against temporary tables does not generateredo
A typical use of temporary tables is for thereporting systems that run in data warehouses
If you see a lot of TRUNCATE commands, it is likelythat programmers are loading data into tables,working on it, and then removing it, This is a perfect
use for temporary tables
2/2
7/29/2019 OCA 08 - Managing Schema Objects
36/38
EXERCISE 8-4: Create and Use Temporary Tables
http://asghars.blogspot.com36
1.Connect to database with SQL*Plus as user HR
2. Create a temporary table as follows
3. Start a second SQL*Plus session as HR
4. In the second session insert some different rows
1/3
2/3
7/29/2019 OCA 08 - Managing Schema Objects
37/38
EXERCISE 8-4: Create and Use Temporary Tables
http://asghars.blogspot.com37
5. In the first session, truncate the table
6. In the second session, confirm that there are stillrows in that sessions copy of the table
7. In the second session, demonstrate thatterminating the session does clear the rows
2/3
3/3
7/29/2019 OCA 08 - Managing Schema Objects
38/38
EXERCISE 8-4: Create and Use Temporary Tables
h // h bl38
8. Check the tmp_emps table in database constrol
3/3