Sie sind auf Seite 1von 18

Create

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
)

Index organized tables (IOT)


create table iot_ (
a number,
b varchar2(10),
constraint pk_iot_ primary key (a, b)
)
organization index;
Create tablespace in Oracle
The create tablespace statement is used to create a tablespace.

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:

CREATE SCHEMA AUTHORIZATION schema options

options
CREATE TABLE
CREATE VIEW
GRANT

The schema name must be an existing Oracle username.

Example:
CREATE SCHEMA AUTHORIZATION painter
CREATE TABLE paint
(paint_id NUMBER PRIMARY KEY,
paint_size NUMBER,
colour VARCHAR2(10) )

CREATE VIEW large_paints AS


SELECT paint_id,colour FROM paint WHERE paint_size=100

GRANT select ON large_paints TO scott;

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:

CREATE USER username


IDENTIFIED {BY password | EXTERNALLY | GLOBALLY AS 'external_name'}
options;

options:

DEFAULT TABLESPACE tablespace


TEMPORARY TABLESPACE tablespace
QUOTA int {K | M} ON tablespace
QUOTA UNLIMITED ON tablespace
PROFILE profile_name
PASSWORD EXPIRE
ACCOUNT {LOCK|UNLOCK}

EXAMPLES

-- Create a user with no rights to save data or create objects:

CREATE USER limited IDENTIFIED BY ChangeThis;

-- Create a user with full rights to create objects and save data:

DROP USER MySchemaOwner CASCADE;

CREATE USER MySchemaOwner IDENTIFIED BY ChangeThis


DEFAULT TABLESPACE data
TEMPORARY TABLESPACE temp
QUOTA UNLIMITED ON data;

CREATE ROLE conn;

GRANT CREATE session, CREATE table, CREATE view,


CREATE procedure,CREATE synonym,
ALTER table, ALTER view, ALTER procedure,ALTER synonym,
DROP table, DROP view, DROP procedure,DROP synonym,
TO conn;

GRANT conn TO MySchemaOwner;

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:

CREATE PROFILE profile_name LIMIT limit(s) range

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

range = UNLIMITED | DEFAULT | integer

for PRIVATE_SGA specify K or M


e.g.
CREATE PROFILE MyProfile LIMIT PRIVATE_SGA 50 K

New with Oracle 8 are password related profile limits...

Syntax:

CREATE PROFILE profile_name LIMIT pw_limit(s) range

KEY
pw_limit = PASSWORD_LIFE_TIME
PASSWORD_GRACE_TIME
PASSWORD_REUSE_TIME
PASSWORD_REUSE_MAX
FAILED_LOGIN_ATTEMPTS
PASSWORD_LOCK_TIME

range = UNLIMITED | DEFAULT | expression

Syntax to customise password verification:

CREATE PROFILE profile_name LIMIT PASSWORD_VERIFY_FUNCTION {plsql_function


| NULL | DEFAULT}

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

DEFAULT refers to values set in the DEFAULT user profile.

"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:

CREATE INDEX ord_customer_ix


ON orders (customer_id);

Compressing an Index: Example To create the ord_customer_ix_demo index with the


COMPRESS clause, you might issue the following statement:

CREATE INDEX ord_customer_ix_demo


ON orders (customer_id, sales_rep_id)
COMPRESS 1;

The index will compress repeated occurrences of customer_id column values.

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:

CREATE INDEX idx_personnel ON CLUSTER personnel;

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.

Creating an Index on an XMLType Table: Example The following example creates an index on


the area element of the xwarehouses table (created in "XMLType Table Examples"):

CREATE INDEX area_index ON xwarehouses e


(EXTRACTVALUE(VALUE(e),'/Warehouse/Area'));

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:

SELECT e.getClobVal() AS warehouse


FROM xwarehouses e
WHERE EXISTSNODE(VALUE(e),'/Warehouse[Area>50000]') = 1;

See Also:

EXISTSNODE and VALUE

Function-Based Index Examples

The following examples show how to create and use function-based indexes.

Creating a Function-Based Index: Example The following statement creates a function-based


index on the employees table based on an uppercase evaluation of the last_name column:

CREATE INDEX upper_ix ON employees (UPPER(last_name));

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:

SELECT first_name, last_name


FROM employees WHERE UPPER(last_name) IS NOT NULL
ORDER BY UPPER(last_name);

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:

CREATE INDEX income_ix


ON employees(salary + (salary*commission_pct));

SELECT first_name||' '||last_name "Name"


FROM employees
WHERE (salary*commission_pct) + salary > 15000;

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.

CREATE INDEX src_idx ON print_media(text_length(ad_sourcetext));


ANALYZE INDEX src_idx COMPUTE STATISTICS;

SELECT product_id FROM print_media


WHERE text_length(ad_sourcetext) < 1000;

PRODUCT_ID
----------
3060
2056
3106
2268

Creating a Function-based Index on a Type Method: Example This example entails an object


type rectangle containing two number attributes: length and width. The area() method
computes the area of the rectangle.

CREATE TYPE rectangle AS OBJECT


( length NUMBER,
width NUMBER,
MEMBER FUNCTION area RETURN NUMBER DETERMINISTIC
);

CREATE OR REPLACE TYPE BODY rectangle AS


MEMBER FUNCTION area RETURN NUMBER IS
BEGIN
RETURN (length*width);
END;
END;

Now, if you create a table rect_tab of type rectangle, you can create a function-based index
on the area() method as follows:

CREATE TABLE rect_tab OF rectangle;


CREATE INDEX area_idx ON rect_tab x (x.area());

You can use this index efficiently to evaluate a query of the form:

SELECT * FROM rect_tab x WHERE x.area() > 100;

Using a Function-based Index to Define Conditional Uniqueness: Example  The following


statement creates a unique function-based index on the oe.orders table that prevents a customer
from taking advantage of promotion ID 2 ("blowout sale") more than once:

CREATE UNIQUE INDEX promo_ix ON orders


(CASE WHEN promotion_id =2 THEN customer_id ELSE NULL END,
CASE WHEN promotion_id = 2 THEN promotion_id ELSE NULL END);

INSERT INTO orders (order_id, order_date, customer_id, order_total,


promotion_id)
VALUES (2459, systimestamp, 106, 251, 2);
1 row created.

INSERT INTO orders (order_id, order_date, customer_id, order_total,


promotion_id)
VALUES (2460, systimestamp+1, 106, 110, 2);
insert into orders (order_id, order_date, customer_id, order_total,
promotion_id)
*
ERROR at line 1:
ORA-00001: unique constraint (OE.PROMO_IX) violated

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.

Partitioned Index Examples

Creating a Range-Partitioned Global Index: Example The following statement creates a global


prefixed index cost_ix on the sample table sh.sales with three partitions that divide the range
of costs into three groups:

CREATE INDEX cost_ix ON sales (amount_sold)


GLOBAL PARTITION BY RANGE (amount_sold)
(PARTITION p1 VALUES LESS THAN (1000),
PARTITION p2 VALUES LESS THAN (2500),
PARTITION p3 VALUES LESS THAN (MAXVALUE));

Creating a Hash-Partitioned Global Index: Example The following statement creates a hash-


partitioned global index cust_last_name_ix on the sample table sh.customers with four
partitions:

CREATE INDEX cust_last_name_ix ON customers (cust_last_name)


GLOBAL PARTITION BY HASH (cust_last_name)
PARTITIONS 4;

Creating an Index on a Hash-Partitioned Table: Example The following statement creates a local


index on the product_id column of the hash_products partitioned table (which was created in
"Hash Partitioning Example"). The STORE IN clause immediately following LOCAL indicates that
hash_products is hash partitioned. Oracle Database will distribute the hash partitions between
the tbs1 and tbs2 tablespaces:

CREATE INDEX prod_idx ON hash_products(product_id) LOCAL


STORE IN (tbs_01, tbs_02);

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.

CREATE INDEX sales_ix ON composite_sales(time_id, prod_id)


STORAGE (INITIAL 1M MAXEXTENTS UNLIMITED)
LOCAL
(PARTITION q1_1998,
PARTITION q2_1998,
PARTITION q3_1998,
PARTITION q4_1998,
PARTITION q1_1999,
PARTITION q2_1999,
PARTITION q3_1999,
PARTITION q4_1999,
PARTITION q1_2000,
PARTITION q2_2000
(SUBPARTITION pq2001, SUBPARTITION pq2002,
SUBPARTITION pq2003, SUBPARTITION pq2004,
SUBPARTITION pq2005, SUBPARTITION pq2006,
SUBPARTITION pq2007, SUBPARTITION pq2008),
PARTITION q3_2000
(SUBPARTITION c1 TABLESPACE tbs_02,
SUBPARTITION c2 TABLESPACE tbs_02,
SUBPARTITION c3 TABLESPACE tbs_02,
SUBPARTITION c4 TABLESPACE tbs_02,
SUBPARTITION c5 TABLESPACE tbs_02),
PARTITION q4_2000
(SUBPARTITION pq4001 TABLESPACE tbs_03,
SUBPARTITION pq4002 TABLESPACE tbs_03,
SUBPARTITION pq4003 TABLESPACE tbs_03,
SUBPARTITION pq4004 TABLESPACE tbs_03)
);

Bitmap Index Example

The following creates a bitmap join index on the table oe.hash_products, which was created in
"Hash Partitioning Example":

CREATE BITMAP INDEX product_bm_ix


ON hash_products(list_price)
TABLESPACE tbs_1
LOCAL(PARTITION ix_p1 TABLESPACE tbs_02,
PARTITION ix_p2,
PARTITION ix_p3 TABLESPACE tbs_03,
PARTITION ix_p4,
PARTITION ix_p5 TABLESPACE tbs_04 );
Because hash_products is a partitioned table, the bitmap join index must be locally partitioned.
In this example, the user must have quota on tablespaces specified. See CREATE
TABLESPACE for examples that create tablespaces tbs_2, tbs_3, and tbs_4.

Indexes on Nested Tables: 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:

CREATE UNIQUE INDEX nested_tab_ix


ON textdocs_nestedtab(NESTED_TABLE_ID, document_typ);

Including pseudocolumn NESTED_TABLE_ID ensures distinct rows in nested table column


ad_textdocs_ntab.

Indexing on Substitutable Columns: Examples

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:

CREATE INDEX salary_i


ON books (TREAT(author AS employee_t).salary);

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.

CREATE INDEX salary_func_i ON persons p


(TREAT(VALUE(p) AS part_time_emp_t).salary);
Create Oracle Database Syntax

Here are several ways to manually issue the "create database" syntax for Oracle:

 EXTREMELY minimal Oracle database creation script


 OMF minimal manual create Oracle database syntax
 Standard Oracle: create Oracle database syntax
 

EXTREMELY minimal manual Oracle database creation script

1.  Set your ORACLE_SID

export ORACLE_SID=test

export ORACLE_HOME=/path/to/oracle/home

2.  Create a minimal init.ora

# $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

3.  Create a password file

$ORACLE_HOME/bin/orapwd file=$ORACLE_HOME/dbs/pwd<sid>.ora password=oracle entries=5

4.  Start the instance


 

sqlplus / as sysdba

startup nomount

5.  Create the database

create database test


logfile group 1 ('/path/to/redo1.log') size 100M,
            group 2 ('/path/to/redo2.log') size 100M,
            group 3 ('/path/to/redo3.log') size 100M
character set WE8ISO8859P1
national character set utf8
datafile '/path/to/system.dbf' size 500M autoextend on next 10M maxsize unlimited extent management
local
sysaux datafile '/path/to/sysaux.dbf' size 100M autoextend on next 10M maxsize unlimited
undo tablespace undotbs1 datafile '/path/to/undotbs1.dbf' size 100M
default temporary tablespace temp tempfile '/path/to/temp01.dbf' size 100M;

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"

6.  Run catalog and catproc 

@?/rdbms/admin/catalog.sql

@?/rdbms/admin/catproc.sql
 

7.  Change passwords

alter user sys identified by whatever;

alter user system identified by whatever;

OMF: minimal manual Oracle create database syntax

1.  Set your ORACLE_SID

 
export ORACLE_SID=test
export ORACLE_HOME=/path/to/oracle/home

2.  Create a minimal init.ora

# $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

3.  Create a password file

$ORACLE_HOME/bin/orapwd file=$ORACLE_HOME/dbs/pwd<sid>.ora password=oracle entries=5

4.  Start the instance

sqlplus / as sysdba

startup nomount

5.  Create the database

create database test


character set WE8ISO8859P1
national character set utf8
undo tablespace undotbs1
default temporary tablespace temp;

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

6.  Run catalog and catproc

@?/rdbms/admin/catalog.sql

@?/rdbms/admin/catproc.sql

Standard create Oracle database syntax

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;

CREATE CONTROLFILE REUSE DATABASE "OLDLSQ" NORESETLOGS


NOARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 2
MAXDATAFILES 240
MAXINSTANCES 1
MAXLOGHISTORY 113
LOGFILE
GROUP 1 ('/u03/oradata/oldlsq/log1a.dbf',
'/u03/oradata/olslsq/log1b.dbf') SIZE 30M,
GROUP 2 ('/u04/oradata/oldlsq/log2a.dbf',
'/u04/oradata/oldlsq/log2b.dbf') SIZE 30M
DATAFILE
'/u01/oradata/oldlsq/system01.dbf',
'/u01/oradata/oldlsq/mydatabase.dbf'
;

This is just an abbreviated sample of the Oracle create database command and there are many
more options:

Das könnte Ihnen auch gefallen