For the creation of tables, the SQL commands DROP/CREATE TABLE and ALTER/DROP/CREATE TABLESPACE are a very important part of the syntax: CREATE TABLE table name ( Column-name1 datatype1 column1 constraint, Column-name2 datatype2, . . . . . . any primary key combinations... ) TABLESPACE tablespace-nam e ;
We will use these commands extensively in this chapter. Here's a concrete example: CREATE TABLE MY_TABLE ( FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(20), PRIMARY KEY FIRST_NAME, LAST_NAME ) TABLESPACE MY_NAME_TABLESPACE Syntax RulesHere are the specific syntax rules for creating tables:
You need to be very careful with special characters such as the parenthesis, comma, and semicolon. After you get syntax errors a couple of times, you'll catch on. I have tried to make these characters clear by highlighting them in the example at the beginning of this section. You do not need to put them on their own lines, as this example shows: CREATE TABLE NAMES (FIRST_NAME VARCHAR2(20)) TABLESPACE NAMES_SPACE; Remember that constraints include making a column a primary key, making it "not null", and adding referential integrity. Quite a few other variables can be used with table creation, but the ones I have described here will suffice for our purposes. Remember tablespaces? We will be putting all our tables into the tablespace that was created when the Oracle database was built: the CW1 tablespace. TablespacesRemember that a tablespace is just a slice of storage. We separate tables for performance, backup, and other reasons. In a multidisk system, you can balance the input/output load by separating the system tables and the data tables into their own tablespaces on their own disks. Here's the general process:
And here's the syntax: ALTER TABLESPACE MY_NAMES OFFLINE NORMAL ; DROP TABLESPACE MY_NAMES INCLUDING CONTENTS ; CREATE TABLESPACE MY_NAMES DATAFILE 'D:\TABLESPACES\DATA\MY_NAMES' SIZE 500M ONLINE PERMANENT ; Note the following about the syntax with tablespace creation:
Note Now that you're familiar with the syntax for tablespace and table creation, I want to make you aware that tablespaces can be refined further to significantly improve performance. The refinement is to define the tablespaces as locally managed . We do this by adding EXTENT MANAGEMENT LOCAL to the CREATE TABLESPACE syntax. This command gives the tablespace control over space allocation and deallocation as blocks are used and released. I refer you to more technical documentation for an in-depth discussion of locally managed tablespaces and the advantages. |