Академический Документы
Профессиональный Документы
Культура Документы
Table
User
Index
Database
create table [Oracle SQL]
Prerequisites
For a user to be able to create a table, he needs the create table system privilege, otherwise he'll
receive the ORA-01031: insufficient privileges error message.
Additionally, the user needs to have enough quota on the tablespace where he wants to create the
table.
Heap tables
Usually, if we refer to tables, we mean heap tables, although there are other types as well.
A heap table is created like this:
create table t (
a number,
b varchar2(10)
)
It is possible to create the constraints together with the create statement. As a foreign key
references a known type, it is not necessary to specify the foreign key's column type.
create table orders (
order_id number primary key
order_dt date,
cust_id references customers
)
A primary key needs to have an associated (unique) index. It is possible to specify on what
tablespace this index is going to be created:
create table orders (
order_id number,
order_dt date,
cust_id references customer
constraint pk_orders (order_id) using index tablespace ts_idx
)
Permanent tablespace
create tablespace ts_something
logging
datafile '/dbf1/ts_sth.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
create tablespace data datafile '/home/oracle/databases/ora10/data.dbf'
size 10M
autoextend on maxsize 200M
extent management local uniform size 64K;
Temporary tablespace
create temporary tablespace temp_mtr
tempfile '/dbf1/mtr_temp01.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
Note, a temporary tablespace has tempfiles, not datafiles.
Undo tablespace
create undo tablespace ts_undo
datafile '/dbf/undo.dbf'
size 100M;
Misc
More than one datafile can be created with a single create tablespace command:
create tablespace ts_sth
datafile 'c:\xx\sth_01.dbf' size 4M autoextend off,
'c:\xx\sth_02.dbf' size 4M autoextend off,
'c:\xx\sth_03.dbf' size 4M autoextend off
logging
extent management local;
CREATE SCHEMA
Create multiple tables, views and grants in a single transaction.
Summary of Syntax:
options
CREATE TABLE
CREATE VIEW
GRANT
Example:
CREATE SCHEMA AUTHORIZATION painter
CREATE TABLE paint
(paint_id NUMBER PRIMARY KEY,
paint_size NUMBER,
colour VARCHAR2(10) )
This command (along with create user) is typically used to setup a new, empty set of tables and
views. An alternative method is to use export and import (EXP and IMP)
"In the small matters trust the mind, in the large ones the heart" - Sigmund Freud
CREATE USER
Create a user.
Syntax:
options:
EXAMPLES
-- Create a user with full rights to create objects and save data:
You have to create a user first before you can GRANT permissions, roles or assign a default
ROLE.
"If you bungle raising your children, I don't think whatever else you do well matters very much"
- Jacqueline Kennedy Onassis
Related Commands:
CREATE PROFILE
Create a user profile
Syntax:
KEY
limit = SESSIONS_PER_USER
CPU_PER_SESSION
CPU_PER_CALL
CONNECT_TIME
IDLE_TIME
LOGICAL_READS_PER_SESSION
LOGICAL_READS_PER_CALL
COMPOSITE_LIMIT
PRIVATE_SGA
Syntax:
KEY
pw_limit = PASSWORD_LIFE_TIME
PASSWORD_GRACE_TIME
PASSWORD_REUSE_TIME
PASSWORD_REUSE_MAX
FAILED_LOGIN_ATTEMPTS
PASSWORD_LOCK_TIME
Definitions
CONNECT_TIME - Max. time user may stay connected
IDLE_TIME - Max. time user may stay connected & idle
PRIVATE_SGA - Session space in the shared pool - K or M (bytes)
COMPOSITE_LIMIT - A weighted sum of CPU_PER_SESSION, CONNECT_TIME,
LOGICAL_READS_PER_SESSION, and PRIVATE_SGA.
PASSWORD_LIFE_TIME - Expire password after X no of days
PASSWORD_GRACE_TIME - Lock account X days after LIFE_TIME expires.
PASSWORD_REUSE_TIME - Min. no. days before the same pw may be reused
PASSWORD_REUSE_MAX - Min. no. of number of pw changes before the current
password can be reused
FAILED_LOGIN_ATTEMPS - Max no. of incorrect logins before account is locked
PASSWORD_LOCK_TIME - Max. no. of days an account will be locked
"There are people who have money and people who are rich" - Coco Chanel
Creating an Index: Example The following statement shows how the sample index
ord_customer_ix on the customer_id column of the sample table oe.orders was created:
Creating an Index in NOLOGGING Mode: Example If the sample table orders had been created
using a fast parallel load (so all rows were already sorted), you could issue the following
statement to quickly create an index.
/* Unless you first sort the table oe.orders, this example fails
because you cannot specify NOSORT unless the base table is
already sorted.
*/
CREATE INDEX ord_customer_ix_demo
ON orders (order_mode)
NOSORT
NOLOGGING;
Creating a Cluster Index: Example To create an index for the personnel cluster, which was
created in "Creating a Cluster: Example", issue the following statement:
No index columns are specified, because cluster indexes are automatically built on all the
columns of the cluster key. For cluster indexes, all rows are indexed.
Such an index would greatly improve the performance of queries that select from the table based
on, for example, the square footage of a warehouse, as shown in this statement:
See Also:
The following examples show how to create and use function-based indexes.
See the "Prerequisites" for the privileges and parameter settings required when creating function-
based indexes.
To ensure that Oracle Database will use the index rather than performing a full table scan, be
sure that the value returned by the function is not null in subsequent queries. For example, this
statement is guaranteed to use the index:
Without the WHERE clause, Oracle Database may perform a full table scan.
In the next statements showing index creation and subsequent query, Oracle Database will use
index income_ix even though the columns are in reverse order in the query:
Creating a Function-Based Index on a LOB Column: Example The following statement uses the
function created in "Using a Packaged Procedure in a Function: Example" to create a function-
based index on a LOB column in the sample pm schema. The example then collects statistics on
the function-based index and selects rows from the sample table print_media where that CLOB
column has fewer than 1000 characters.
PRODUCT_ID
----------
3060
2056
3106
2268
Now, if you create a table rect_tab of type rectangle, you can create a function-based index
on the area() method as follows:
You can use this index efficiently to evaluate a query of the form:
The objective is to remove from the index any rows where the promotion_id is not equal to 2.
Oracle Database does not store in the index any rows where all the keys are NULL. Therefore, in
this example, we map both customer_id and promotion_id to NULL unless promotion_id is
equal to 2. The result is that the index constraint is violated only if promotion_id is equal to 2 for
two rows with the same customer_id value.
The creator of the index must have quota on the tablespaces specified. See CREATE
TABLESPACE for examples that create tablespaces tbs_1 and tbs_2.
Creating an Index on a Composite-Partitioned Table: Example The following statement creates a
local index on the composite_sales table, which was created in "Composite-Partitioned Table
Examples". The STORAGE clause specifies default storage attributes for the index. However, this
default is overridden for the five subpartitions of partitions q3_2000 and q4_2000, because
separate TABLESPACE storage is specified.
The creator of the index must have quota on the tablespaces specified. See CREATE
TABLESPACE for examples that create tablespaces tbs_1 and tbs_2.
The following creates a bitmap join index on the table oe.hash_products, which was created in
"Hash Partitioning Example":
The sample table pm.print_media contains a nested table column ad_textdocs_ntab, which is
stored in storage table textdocs_nestedtab. The following example creates a unique index on
storage table textdocs_nestedtab:
You can build an index on attributes of the declared type of a substitutable column. In addition,
you can reference the subtype attributes by using the appropriate TREAT function. The following
example uses the table books, which is created in "Substitutable Table and Column Examples".
The statement creates an index on the salary attribute of all employee authors in the books
table:
The target type in the argument of the TREAT function must be the type that added the attribute
being referenced. In the example, the target of TREAT is employee_t, which is the type that
added the salary attribute.
If this condition is not satisfied, then Oracle Database interprets the TREAT function as any
functional expression and creates the index as a function-based index. For example, the
following statement creates a function-based index on the salary attribute of part-time
employees, assigning nulls to instances of all other types in the type hierarchy.
Here are several ways to manually issue the "create database" syntax for Oracle:
export ORACLE_SID=test
export ORACLE_HOME=/path/to/oracle/home
# $ORACLE_HOME/dbs/init<sid>.ora
control_files = (/path/to/control1.ctl,/path/to/control2.ctl,/path/to/control3.ctl)
undo_management = AUTO
undo_tablespace = UNDOTBS1
db_name = test
db_block_size = 8192
sga_max_size = 1073741824 #one gig
sga_target = 1073741824 #one gig
sqlplus / as sysdba
startup nomount
Note: there's some other things you can do here, like "ARCHIVELOG" "SET TIME_ZONE ="
and "USER SYS IDENTIFIED BY password" and "USER SYSTEM IDENTIFIED BY
password"
@?/rdbms/admin/catalog.sql
@?/rdbms/admin/catproc.sql
export ORACLE_SID=test
export ORACLE_HOME=/path/to/oracle/home
# $ORACLE_HOME/dbs/init<sid>.ora
control_files = (/path/to/control1.ctl,/path/to/control2.ctl,/path/to/control3.ctl)
undo_management = AUTO
db_name = test
db_block_size = 8192
sga_max_size = 1073741824 #one gig
sga_target = 1073741824 #one gig
db_create_file_dest = /path/to/datafile/location #OMF
db_create_online_log_dest_1 = /path/to/first/redo_and_control_file/location #OMF
db_create_online_log_dest_2 = /path/to/second/redo_and_control_file/location #OMF
db_recovery_file_dest = /path/to/flash/recovery/area #OMF
#note it’s a good idea to also have background_dump_dest, user_dump_dest, and core_dump_dest here as
well
sqlplus / as sysdba
startup nomount
You can even do this and it will work, the ultimate in minimalism:
create database test;
Note: There's some other things you can do here, like "ARCHIVELOG" "SET TIME_ZONE ="
and "USER SYS IDENTIFIED BY password" and "USER SYSTEM IDENTIFIED BY
password"
Note 2: This is so minimal because you are using Oracle Managed Files as seen in #2
@?/rdbms/admin/catalog.sql
@?/rdbms/admin/catproc.sql
After creating your init.ora file with the appropriate parameters you can use the "create database"
command in SQL*Plus to create a database:
Make sure that your have your $ORACLE_HOME and $ORACLE_SID set properly and that
you sign-on as SYSDBA:
startup nomount;
This is just an abbreviated sample of the Oracle create database command and there are many
more options: