Tải bản đầy đủ (.pdf) (10 trang)

OCA /OCP Oracle Database 11g A ll-in-One Exam Guide- P31 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (290.86 KB, 10 trang )

OCA/OCP Oracle Database 11g All-in-One Exam Guide
256
17. þ B. If AUDIT_TRAIL is set to NONE, there will be no standard database
auditing.
ý A, C, and D. A is wrong because auditing the SYS user is in addition to
standard database auditing. C is wrong because standard database auditing
will record access to the object, regardless of whether any rows were retrieved.
D is wrong because audits of parameter changes require an instance restart,
not audits of commands.
PART II
SQL
■ Chapter 7 DDL and Schema Objects
■ Chapter 8 DML and Concurrency
■ Chapter 9 Retrieving, Restricting, and Sorting Data Using SQL
■ Chapter 10 Single-Row and Conversion Functions
■ Chapter 11 Group Functions
■ Chapter 12 SQL Joins
■ Chapter 13 Subqueries and Set Operators
This page intentionally left blank
CHAPTER 7
DDL and Schema Objects
Exam Objectives
In this chapter you will learn to
• 051.10.1 Categorize the Main Database Objects
• 051.10.2 Review the Table Structure
• 051.10.3 List the Data Types That Are Available for Columns
• 051.10.4 Create a Simple Table
• 051.10.5 Explain How Constraints Are Created at the Time of Table Creation
• 051.10.6 Describe How Schema Objects Work
• 052.8.1 Create and Modify Tables
• 052.8.2 Manage Constraints


• 052.8.3 Create Indexes
• 052.8.4 Create and Use Temporary Tables
• 051.11.1 Create Simple and Complex Views
• 051.11.2 Retrieve Data from Views
• 051.11.3 Create, Maintain, and Use Sequences
• 051.11.4 Create and Maintain Indexes
• 051.11.5 Create Private and Public Synonyms
259
OCA/OCP Oracle Database 11g All-in-One Exam Guide
260
In terms of the sheer number of exam objectives covered in this chapter, it looks
horrific. Do not worry: there is some duplication in the objectives, and many of the
objectives are revisited in other chapters as well.
Understanding the primitive data types and the standard heap-organized table
structure is the first topic. Then the chapter moves on to defining the object types that
are dependent on tables (indexes, constraints, and views), and then sequences and
synonyms. Objects of all these types will be used throughout the remainder of this
book, sometimes with more detail provided.
Categorize the Main Database Objects
There are various object types that can exist within a database, many more with the
current release than with earlier versions. All objects have a name and a type, and
each object is owned by a schema. Various common object types and the rules to
which they must conform will be discussed.
Object Types
This query lists (in a neatly formatted output), the count by object types for the
objects that happen to exist in this particular database:
SQL> select object_type,count(object_type) from dba_objects
group by object_type order by object_type;
OBJECT_TYPE COUNT(OBJECT_TYPE) OBJECT_TYPE COUNT(OBJECT_TYPE)
CLUSTER 10 PACKAGE 1240

CONSUMER GROUP 12 PACKAGE BODY 1178
CONTEXT 6 PROCEDURE 118
DIMENSION 5 PROGRAM 17
DIRECTORY 9 QUEUE 37
EDITION 1 RESOURCE PLAN 7
EVALUATION CONTEXT 13 RULE 1
FUNCTION 286 RULE SET 21
INDEX 3023 SCHEDULE 2
INDEX PARTITION 342 SEQUENCE 204
INDEXTYPE 12 SYNONYM 26493
JAVA CLASS 22018 TABLE 2464
JAVA DATA 322 TABLE PARTITION 199
JAVA RESOURCE 820 TRIGGER 413
JOB 11 TYPE 2630
JOB CLASS 11 TYPE BODY 231
LIBRARY 177 UNDEFINED 6
LOB 769 VIEW 4669
LOB PARTITION 7 WINDOW 9
MATERIALIZED VIEW 3 WINDOW GROUP 4
OPERATOR 60 XML SCHEMA 93
42 rows selected.
This query addresses the view DBA_OBJECTS, which has one row for every object
in the database. The numbers are low, because the database is a very small one used
only for teaching. A database used for a business application might have hundreds of
Chapter 7: DDL and Schema Objects
261
PART II
thousands of objects. You may not be able to see the view DBA_OBJECTS, depending
on what permissions your account has. Alternate views are USER_OBJECTS, which
will show all the objects owned by you, and ALL_OBJECTS, which will show all the

objects to which you have been granted access (including your own). All users have
access to these views.
The objects of greatest interest to a SQL programmer are those that contain, or
give access to, data. These include: Tables, Views, Synonyms, Indexes, and Sequences.
Tables basically store data in rows segmented by columns. A view is a stored
SELECT statement that can be referenced as though it were a table. It is nothing more
than a query, but rather than running the statement itself, the user issues a SELECT
statement against the view instead. In effect, the user is selecting from the result of
another selection. A synonym is an alias for a table (or a view). Users can execute SQL
statements against the synonym, and the database will map them into statements
against the object to which the synonym points. Indexes are a means of improving
access times to rows in tables. If a query requires only one row, then rather than
scanning the entire table to find the row, an index can provide a pointer to the row’s
exact location. Of course, the index itself must be searched, but this is often faster
than scanning the table. A sequence is a construct that generates unique numbers.
There are many cases where unique numbers are needed. Sequences issue numbers in
order, on demand: it is absolutely impossible for the same number to be issued twice.
The remaining object types are less commonly relevant to a SQL programmer.
Their use falls more within the realm of PL/SQL programmers and database
administrators.
Naming Schema Objects
A schema object is owned by a user and must conform to certain rules:
• The name may be between 1 to 30 characters long (with the exception of
database link names that may be up to 128 characters long).
• Reserved words (such as SELECT) cannot be used as object names.
• All names must begin with a letter of the alphabet.
• Object names can only include letters, numbers, the underscore (_), the dollar
sign ($), or the hash symbol (#).
• Lowercase letters will be automatically converted to uppercase.
By enclosing the name within double quotes, all these rules (with the exception

of the length) can be broken, but to get to the object subsequently, it must always be
specified with double quotes, as in the examples in Figure 7-1. Note that the same
restrictions also apply to column names.
EXAM TIP Object names must be no more than 30 characters. The
characters can be letters, digits, underscore, dollar, or hash.
OCA/OCP Oracle Database 11g All-in-One Exam Guide
262
Although tools such as SQL*Plus and SQL Developer will automatically convert
lowercase letters to uppercase unless the name is enclosed within double quotes,
remember that object names are always case sensitive. In this example, the two tables
are completely different:
SQL> create table lower(c1 date);
Table created.
SQL> create table "lower"(col1 varchar2(2));
Table created.
SQL> select table_name from dba_tables where lower(table_name) = 'lower';
TABLE_NAME

lower
LOWER
TIP While it is possible to use lowercase names and nonstandard characters
(even spaces), it is considered bad practice because of the confusion it can cause.
Object Namespaces
It is often said that the unique identifier for an object is the object name, prefixed
with the schema name. While this is generally true, for a full understanding of naming
it is necessary to introduce the concept of a namespace. A namespace defines a group
of object types, within which all names must be uniquely identified—by schema and
name. Objects in different namespaces can share the same name.
These object types all share the same namespace:
Tables Views Sequences

Private synonyms Stand-alone procedures Stand-alone stored functions
Packages Materialized views User-defined types
Figure 7-1 Using double quotes to use nonstandard names
Chapter 7: DDL and Schema Objects
263
PART II
Thus it is impossible to create a view with the same name as a table—at least, it is
impossible if they are in the same schema. And once created, SQL statements can address
a view as though it were a table. The fact that tables, views, and private synonyms share
the same namespace means that you can set up several layers of abstraction between
what the users see and the actual tables, which can be invaluable for both security and
for simplifying application development.
These object types each have their own namespace:
Indexes Constraints Clusters
Database triggers Private database links Dimensions
Thus it is possible (though perhaps not a very good idea) for an index to have the
same name as a table, even within the same schema.
EXAM TIP Within a schema, tables, views, and synonyms cannot have the
same names.
Exercise 7-1: Determine What Objects Are Accessible to Your
Session In this exercise, query various data dictionary views as user HR to determine
what objects are in the HR schema and what objects in other schemas HR has access to.
1. Connect to the database with SQL*Plus or SQL Developer as user HR.
2. Determine how many objects of each type are in the HR schema:
select object_type,count(*) from user_objects group by object_type;
The USER_OBJECTS view lists all objects owned by the schema to which the
current session is connected, in this case HR.
3. Determine how many objects in total HR has permissions on:
select object_type,count(*) from all_objects group by object_type;
The ALL_OBJECTS view lists all objects to which the user has some sort of

access.
4. Determine who owns the objects HR can see:
select distinct owner from all_objects;
List the Data Types That Are Available
for Columns
When creating tables, each column must be assigned a data type, which determines
the nature of the values that can be inserted into the column. These data types are also
used to specify the nature of the arguments for PL/SQL procedures and functions.
When selecting a data type, you must consider the data that you need to store and the
operations you will want to perform upon it. Space is also a consideration: some data
types are fixed length, taking up the same number of bytes no matter what data is
OCA/OCP Oracle Database 11g All-in-One Exam Guide
264
actually in it; others are variable. If a column is not populated, then Oracle will not
give it any space at all. If you later update the row to populate the column, then the
row will get bigger, no matter whether the data type is fixed length or variable.
The following are the data types for alphanumeric data:
VARCHAR2 Variable-length character data, from 1 byte to 4KB. The data is stored in
the database character set.
NVARCHAR2 Like VARCHAR2, but the data is stored in the alternative national language
character set, one of the permitted Unicode character sets.
CHAR Fixed-length character data, from 1 byte to 2KB, in the database character set.
If the data is not the length of the column, then it will be padded with spaces.
TIP For ISO/ANSI compliance, you can specify a VARCHAR data type, but any
columns of this type will be automatically converted to VARCHAR2.
The following are the data types for numeric data, all variable length:
NUMBER Numeric data, for which you can specify precision and scale. The precision
can range from 1 to 38, the scale can range from –84 to 127.
FLOAT This is an ANSI data type, floating-point number with precision of
126 binary (or 38 decimal). Oracle also provides BINARY_FLOAT and

BINARY_DOUBLE as alternatives.
INTEGER Equivalent to NUMBER, with scale zero.
The following are the data types for date and time data, all fixed length:
DATE This is either length zero, if the column is empty, or 7 bytes. All DATE
data includes century, year, month, day, hour, minute, and second. The
valid range is from January 1, 4712 BC to December 31, 9999 AD.
TIMESTAMP This is length zero if the column is empty, or up to 11 bytes, depending
on the precision specified. Similar to DATE, but with precision of up to
9 decimal places for the seconds, 6 places by default.
TIMESTAMP WITH
TIMEZONE
Like TIMESTAMP, but the data is stored with a record kept of the time
zone to which it refers. The length may be up to 13 bytes, depending
on precision. This data type lets Oracle determine the difference
between two times by normalizing them to UTC, even if the times are
for different time zones.
TIMESTAMP
WITH LOCAL
TIMEZONE
Like TIMESTAMP, but the data is normalized to the database time zone
on saving. When retrieved, it is normalized to the time zone of the
user process selecting it.
INTERVAL YEAR
TO MONTH
Used for recording a period in years and months between two DATEs
or TIMESTAMPs.
INTERVAL DAY TO
SECOND
Used for recording a period in days and seconds between two DATEs
or TIMESTAMPs.

Chapter 7: DDL and Schema Objects
265
PART II
The following are the large object data types:
CLOB Character data stored in the database character set, size effectively unlimited:
4GB multiplied by the database block size.
NCLOB Like CLOB, but the data is stored in the alternative national language
character set, one of the permitted Unicode character sets.
BLOB Like CLOB, but binary data that will not undergo character set conversion by
Oracle Net.
BFILE A locator pointing to a file stored on the operating system of the database
server. The size of the files is limited to 4GB.
LONG Character data in the database character set, up to 2GB. All the functionality
of LONG (and more) is provided by CLOB; LONGs should not be used in a
modern database, and if your database has any columns of this type, they should
be converted to CLOB. There can only be one LONG column in a table.
LONG RAW Like LONG, but binary data that will not be converted by Oracle Net. Any
LONG RAW columns should be converted to BLOBs.
The following are RAW and ROWID data types:
RAW Variable-length binary data, from 1 byte to 4KB. Unlike the CHAR and
VARCHAR2 data types, RAW data is not converted by Oracle Net from the
database’s character set to the user process’s character set on SELECT or the
other way on INSERT.
ROWID A value coded in base 64 that is the pointer to the location of a row in a table.
Within it is the exact physical address. ROWID is an Oracle proprietary data
type, not visible unless specifically selected.
EXAM TIP All examinees will be expected to know about these data types:
VARCHAR2, CHAR, NUMBER, DATE, TIMESTAMP, INTERVAL, RAW, LONG,
LONG RAW, CLOB, BLOB, BFILE, and ROWID. Detailed knowledge will also
be needed for VARCHAR2, NUMBER, and DATE.

The VARCHAR2 data type must be qualified with a number indicating the maximum
length of the column. If a value is inserted into the column that is less than this, it is
not a problem: the value will only take up as much space as it needs. If the value is
longer than this maximum, the INSERT will fail with an error. If the value is updated
to a longer or shorter value, the length of the column (and therefore the row itself)
will change accordingly. If is not entered at all or is updated to NULL, then it will take
up no space at all.
The NUMBER data type may optionally be qualified with a precision and a scale.
The precision sets the maximum number of digits in the number, and the scale is how
many of those digits are to the right of the decimal point. If the scale is negative, this has
the effect of replacing the last digits of any number inserted with zeros, which do not
count toward the number of digits specified for the precision. If the number of digits
exceeds the precision, there will be an error; if it is within the precision but outside the
scale, the number will be rounded (up or down) to the nearest value within the scale.

×