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

Tài liệu creating tables docx

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 (309.51 KB, 40 trang )

Creating Tables
9
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć2
Schedule: Timing Topic
45 minutes Lecture
25 minutes Practice
70 minutes Total
Class Management Note:
Files required for this lesson are:
Demonstration: None
Practice: p9q2.sql, p9q3.sql
Creating Tables 9Ć3
Objectives
In this lesson, you will create tables. You will also build integrity constraints,
which are rules governing what can and cannot be done with the data.
At the end of this lesson, you should be able to
D
Create a table containing integrity constraints.
D
Identify table naming conventions.
D
Describe the datatypes that can be used when specifying column definitions.
D
Recognize the indexes that are created automatically by constraints.
D
Create a table by populating it with rows from another table.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć4
Creating Tables 9Ć5
Overview
An Oracle7 database can contain multiple data structures. Each structure should be
outlined in the database design so that it can be created during the build stage of


database development.
Data Structures
Structure
Description
Table Stores data.
View Logically represents subsets of data from one or more tables.
Sequence Generates primary key values.
Index Improves the performance of some queries.
Summary of Oracle7 Table Structures
D
Tables can be created at any time, even while users are using the database.
D
You do not need to specify the size of any table. The size is ultimately defined by
the amount of space allocated to the database as a whole. It is important, however,
to estimate how much space a table will use over time.
D
Table structure can be modified online.
Class Management Note:
You should be familiar with the full CREATE TABLE syntax. There are
many parameters that are useful to know, such as PCTINCREASE and
INITIAL.
Technical Note:
Tables can have up to 254 columns and must conform to standard database
object naming conventions.
Column definitions can be omitted when using the AS subquery clause.
Tables are created without data unless a query is specified. Rows are usually
added by using INSERT statements, SQL*Loader, or through a form.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć6
Creating Tables 9Ć7
Creating Tables

Create tables to store data by executing the SQL CREATE TABLE command. This
command is one of the data definition language (DDL) commands, which you will
cover in the next several lessons. DDL commands are a subset of SQL commands
used to create, modify, or remove Oracle7 database structures. These commands have
an immediate effect on the database, and they also record information in the data
dictionary.
In order to create a table, a user must have the CREATE TABLE privilege and a
storage area in which to create objects. The database administrator uses data control
language (DCL) commands, which are covered in a later lesson, to grant privileges to
users.
Abridged Syntax
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr][column_constraint],
...
[table_constraint]);
where: schema is the same as the owner’s name.
table is the name of the table.
DEFAULT expr specifies a default value if a value is omitted in
the INSERT statement.
column is the name of the column.
datatype is the column’s datatype and length.
column_constraint is an integrity constraint as part of the column
definition.
table_constraint is an integrity constraint as part of the table
definition.
For more information, see
Oracle7 Server SQL Reference, Release 7.3, “CREATE TABLE.”
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć8
Creating Tables 9Ć9
Creating Tables

continued
Referencing Another User's Tables
A schema is a collection of objects. Schema objects are the logical structures that
directly refer to the data in a database. Schema objects include tables, views,
synonyms, sequences, stored procedures, indexes, clusters, and database links.
The tables referenced in a constraint must exist in the same database. If the table does
not belong to the user creating the constraint, the owner’s name must be prefixed to
the table referenced in the constraint.
The DEFAULT Option
A column can be given a default value by using the DEFAULT option. This option
prevents null values from entering the columns if a row is inserted without a value for
the column. The default value can be a literal, an expression, or SQL function, such
as SYSDATE and USER, but the value cannot be the name of another column or a
pseudocolumn, such as NEXTVAL or CURRVAL. The default expression must
match the datatype of the column.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć10
Creating Tables 9Ć11
Creating Tables
continued
Naming Rules
Name database tables and columns according to the standard rules for naming any
Oracle7 database object.
D
Table names and column names must begin with a letter and can be 1–30
characters long.
D
Names must contain only the characters A–Z, a–z, 0–9, _ (underscore), $ and #
(legal characters, but their use is discouraged).
D
Names must not duplicate the name of another object owned by the same Oracle7

Server user.
D
Names must not be an Oracle7 Server reserved words.
Naming Guidelines
D
Use descriptive names for tables and other database objects.
D
Name the same entity consistently in different tables. For example, the
department number column is called DEPT_ID in both the S_EMP table and the
S_REGION table.
Note: Names are case-insensitive. For example, EMP is treated as the same name as
eMP or eMp.
For more information, see
Oracle7 Server SQL Reference, Release 7.3, “Object Names and Qualifiers.”
Technical Note:
You can override naming rules by using double quotation marks, for
example, CREATE TABLE “emp”.... To reference that table, use
double quotation marks, for example SELECT * FROM “emp”. Double
quotation marks when creating table names are not recommended.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć12
Creating Tables 9Ć13
Oracle7 Datatypes
There are many different column types. The Oracle7 Server can treat values of one
datatype differently from values of other datatypes. The basic datatypes are character,
number, date, and RAW.
Sample Oracle7 Datatypes
Datatype
Description
VARCHAR2(size) Variable length character values up to maximum length
size. Minimum length is 1, maximum length is 2000.

CHAR(size) Fixed length character values of length size. Default
length is 1, maximum length is 255.
NUMBER Floating point number with precision of 38 significant
digits.
NUMBER(p,s) Number value having a maximum precision of p
ranging from 1 to 38 and a maximum scale of s; the
precision is the total number of decimal digits, and the
scale is the number of digits to the right of the decimal
point.
DATE Date and time values between January 1, 4712 B.C.
and December 31, 4712 A.D.
LONG Variable length character values up to 2 gigabytes.
Only one LONG column is allowed per table.
RAW and LONG RAW Equivalent to VARCHAR2 and LONG, respectively,
but used to store byte-oriented or binary data that is not
to be interpreted by the Oracle7 Server.
Note: The column width determines the maximum number of characters for values
in the column. You must specify the size for VARCHAR2 columns. You can
specify the size for NUMBER and CHAR columns, but default values are
available (38 for NUMBER and 1 for CHAR).
For more information, see
Oracle7 Server SQL Reference, Release 7.3, “Datatypes.”
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć14
Class Management Note:
A guideline of a constraint naming convention is:
<table_name>_<column_name>_<constraint_type>
For example, S_EMP_LAST_NAME_NN and S_DEPT_ID_PK.
Creating Tables 9Ć15
Constraints
Constraints have been available with the Oracle7 Server since Version 6. In Oracle7,

constraints are enforced at the database level.
You can use constraints to
D
Enforce rules at the table level whenever a row is inserted, updated, or deleted
from that table. The constraint must be satisfied for the operation to succeed.
D
Prevent the deletion of a table if there are dependencies from other tables.
D
Provide rules for Oracle tools, such as Developer/2000.
Data Integrity Constraints
Constraint
Description
NOT NULL Specifies that this column may not contain a null value.
UNIQUE Specifies a column or combination of columns whose
values must be unique for all rows in the table.
PRIMARY KEY Uniquely identifies each row of the table.
FOREIGN KEY Establishes and enforces a foreign key relationship
between the column and a column of the referenced
table.
CHECK Specifies a condition that must be true.
For more information, see
Oracle7 Server SQL Reference, Release 7.3, “CONSTRAINT clause.”
Technical Note:
Oracle Version 6 used constraints purely as a documentation device.
NOT NULL constraints are the only constraints displayed by the SQL*Plus
DESCRIBE command.
NOT NULL and UNIQUE constraints are not allowed if the PRIMARY
KEY constraint is specified.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć16

×