Creating Your Own Tables
TABLE CREATION
To create a table, you use the SQL CREATE TABLE command. The basic format for table creation is:
SQL> CREATE TABLE <table_name>
(column_name data_type [not null] [primary key] [REFERENCES table_name(column)],…);
These are the basic elements of the create table command:
The words create table
The name of the table
An opening parenthesis
Column definitions - Remember that expressions shown in square brackets are optional. You may create a table by defining only column_name and data_type for columns.
A closing parenthesis
SQL terminator (;)
Case does not matter in creating tables. Individual column definitions are separated by commas. There is no comma after the last column definition.
Column names must be unique within the table, can be up to 30 characters in length, and cannot be an ORACLE reserved word. All table and column names must start with a letter of the alphabet but may also include numbers and underscores within their names.
The data types you are most likely to use in ORACLE are NUMBER, VARCHAR2 and DATE. Note that CHAR is also available, but VARCHAR2 is more efficient because it stores only the string entered in the column. CHAR pads strings to fill the space specified by size.
|
ORACLE Data Type |
Definition |
|
NUMBER |
Allows space for 40 digits, plus space for a decimal point and sign. May be expressed with digits 0 to 9, + or - sign, and a decimal point, or in scientific notation. |
|
NUMBER(size) |
Allows space for NUMBER of specified size. |
|
NUMBER(size,d) |
Allows space for NUMBER of specified size with d digits after the decimal point. |
|
VARCHAR2(size) |
Variable length character string with maximum size bytes (up to 2000).
|
|
CHAR(size) |
Fixed-length character string, size characters long. Maximum length is 255 characters. Default is 1 byte. Padded on right with blanks to full length of size. |
|
DATE |
Default date format is DD-MON-YY. DD is day of month, dash, MON is the three-letter abbreviation for month, dash, last two digits of year. For example, 01-APR-95. Other date formats may be set in ORACLE. |
The [not null] is optional, columns can be null or not null. If you do not specify 'NOT NULL' the default is 'NULL' and ORACLE will allow the column to contain a null value.
The primary key of a table consists of one or more attributes that will uniquely identify a row in a table. The attribute(s) of the primary key must be not null. For a single column primary key, you may specify the primary key on the column as illustrated above, creating a column constraint. However, to define a composite primary key, you must create a table constraint as shown below.
SQL> CREATE TABLE <table_name>
(column_name data_type [not null] [REFERENCES table_name(column)],…
PRIMARY KEY (column_name1, column_name2,…));
Recall the ENROLLMENT table discussed in chapters 9 and 10 of the IS 331 textbook. The primary key is the combination of the attributes StudentNumber and ClassName. This table could be created in ORACLE with the following command.
SQL> CREATE TABLE enrollment (
studentnumber number(3),
classname char(5),
positionnumber number number(3),
primary key (studentnumber, classname));
REFERENCES identifies the column as a foreign key from the table table_name. A foreign key is a column or combination of columns with values based on the primary key of another table. A foreign key constraint, also known as referential integrity or RI, dictates that the value of a foreign key agrees with the actual value of the primary key in the table it references. If the foreign key is composed of more than one attribute, a table constraint must be created as illustrated in the previous command. The following command would create a table with a composite primary key and a composite foreign key.
SQL> CREATE TABLE <table_name>
(column_name data_type [not null],…
PRIMARY KEY (column_name, …)
FOREIGN KEY (column_name, …)
REFERENCES table2_name (table2column_name,…));
When creating a table, returning after each column definition is not necessary but does promote readability. When you have finished typing in the last line including the ';' then hit return and the table will be created. A 'table created' message should come back to you. You will be the owner of this table and you are the only person (other than a DBA) who can access the table.
To view the table definition use the SQL*Plus DESCRIBE <table_name> command. If you identified a primary key during table creation, it does not appear in the table description.
CREATING A TABLE FROM A TABLE
A table can be created from an existing table. As long as you have select privileges to a table, you can make an image copy of that table under your own user-id. You can also choose to select only specified columns from an existing table for your new table. To create a new table based on an existing table use the following command:
SQL> CREATE TABLE MANAGER AS
SELECT EMPNO, NAME
FROM EMPLOYEE
WHERE JOB = 'Mngr';
Issue the describe command for MANAGER.
SQL> DESCRIBE MANAGER;
Name Null? Type
------------------------------- -------- ----
EMPNO NOT NULL NUMBER(4)
NAME NOT NULL CHAR(8)
Look at the records in the MANAGER table.
SQL> SELECT * FROM MANAGER;
|
EMPNO |
NAME |
|
105 |
Watson |
|
109 |
Allen |
|
200 |
Chen |
|
210 |
Ramirez |
Notice that this table has inherited the column characteristics from the EMPLOYEE table for the columns specified. All the rows from the EMPLOYEE table where the job title was 'Mngr' were also copied into the new table.
Be aware that creating a table in this manner introduces data duplication into the database and exposes the database to anomalies. For example, if a new manager is added to the EMPLOYEE table and the MANAGER table is not updated at the same time, then the database is inconsistent. A better alternative may be to create a view off the EMPLOYEE table rather than create a new table.
A view is a database object, but it is a virtual table. Each time it is queried it will be built off the base table(s). It has no storage of its own but can be used in much the same manner as a table. Views remain dynamic and always reflect the data from the underlying tables. Whenever data within a table is changed any views created from that table will also change. Through a view you can limit the information specific users see. For instance, we are working with the EMPLOYEE table and wish to limit who sees the salary information. Therefore, we can create a view from the EMPLOYEE table that does not contain the salary information.
SQL> CREATE VIEW EMPLOYEE_V
AS SELECT EMPNO, NAME, JOB, DEPTNO, SEX
FROM EMPLOYEE;
Look at the results table returned when you select all columns from this view.
SQL> SELECT * FROM EMPLOYEE_V;
|
EMPNO |
NAME |
JOB |
DEPTNO |
S |
|
101 |
Smith |
Slsm |
40 |
F |
|
100 |
Wilson |
Clrk |
10 |
M |
|
103 |
Reed |
Anlt |
30 |
M |
|
105 |
Watson |
Mngr |
30 |
M |
|
109 |
Allen |
Mngr |
40 |
F |
|
110 |
Turner |
Clrk |
50 |
M |
|
200 |
Chen |
Mngr |
10 |
F |
|
210 |
Ramirez |
Mngr |
50 |
M |
|
213 |
McDonnel |
Clrk |
60 |
M |
|
214 |
Simpson |
Drvr |
60 |
M |
|
215 |
Di Salvo |
Spvr |
60 |
M |
|
220 |
Schwartz |
Slsm |
40 |
F |
Last revised 12/4/1997 by J. Warren, Kennesaw State University
Department of Computer Science and Information Systems
![]()
Return to ORACLE/SQL Tutorial Contents Page | Go to Next Lesson - The Use of Commit and Rollback