You are on page 1of 23

CREATING AND MANAGING

TABLES

9-1 Copyright © 2004, Oracle. All rights reserved.


Objectives
After completing this lesson, you should be able to do
the following:

• Review the table structure


• List the data types that are available for columns
• Create a simple table
• Alter table definitions
• Drop, rename, and truncate tables

9-2 Copyright © 2004, Oracle. All rights reserved.


REVIEW: SQL STATEMENTS

9-3 Copyright © 2004, Oracle. All rights reserved.


Naming Rules

Table names and column names:


• Must begin with a letter
• Must be 1–30 characters long
• Must contain only A–Z, a–z, 0–9, _, $, and #
• Must not duplicate the name of another object
owned by the same user
• Must not be an Oracle server reserved word

9-4 Copyright © 2004, Oracle. All rights reserved.


CREATE TABLE Statement

• You must have:


– CREATE TABLE privilege
– A storage area
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr][, ...]);
• You specify:
– Table name
– Column name, column data type, and column size

9-5 Copyright © 2004, Oracle. All rights reserved.


Data Types

Data Type Description


VARCHAR2(size) Variable-length character data
CHAR(size) Fixed-length character data
NUMBER(p,s) Variable-length numeric data
DATE Date and time values
LONG Variable-length character data (up to 2 GB)
CLOB Character data (up to 4 GB)
RAW and LONG Raw binary data
RAW
BLOB Binary data (up to 4 GB)
BFILE Binary data stored in an external file (up to 4 GB)
ROWID A base-64 number system representing the unique
address of a row in its table

9-6 Copyright © 2004, Oracle. All rights reserved.


DEFAULT Option
• Specify a default value for a column during an
insert.

... hire_date DATE DEFAULT SYSDATE, ...

• The default data type must match the column data


type.

• Example 1:

CREATE TABLE hire_dates


(id NUMBER(8),
hire_date DATE DEFAULT SYSDATE);
Table created.

9-7 Copyright © 2004, Oracle. All rights reserved.


DEFAULT: Varsayılan değer kısıtı- Tabloda bir alana
herhangi bir değer girilmezse, ( tanımlama sırasında
varsayılan bir değer atanmışsa) alana o değer atanır.
Örnek:

uni niteliğine herhangi bir değer girmezsek, varsayılan


değer olarak uni alanina Atılım’ın yazılmasını bekleriz.

9-8 Copyright © 2004, Oracle. All rights reserved.


Creating Tables

Confirm table creation: To learn the structure of a table,


use DESC command. (Displays the current table columns
and type definitions.)

DESC Lab OR DESRCIBE Lab

9-9 Copyright © 2004, Oracle. All rights reserved.


Including Constraints

• Constraints enforce rules at the table level.

• Constraints prevent the deletion of a table if there


are dependencies.

• The following constraint types are valid:


– NOT NULL
– UNIQUE
– PRIMARY KEY
– FOREIGN KEY

9-10 Copyright © 2004, Oracle. All rights reserved.


NOT NULL Constraint

Ensures that null values are not permitted for the


column:

NOT NULL constraint NOT NULL Absence of NOT NULL


(No row can contain constraint constraint
a null value for (Any row can contain a
this column.) null value for this
column.)

9-11 Copyright © 2004, Oracle. All rights reserved.


UNIQUE Constraint

UNIQUE constraint
EMPLOYEES


INSERT INTO

Allowed
Not allowed:
already exists

9-12 Copyright © 2004, Oracle. All rights reserved.


UNIQUE Constraint

Defined at either the table level or the column level:

CREATE TABLE employees(


employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
...
CONSTRAINT emp_email_uk UNIQUE(email));

9-13 Copyright © 2004, Oracle. All rights reserved.


PRIMARY KEY Constraint

DEPARTMENTS
PRIMARY KEY


Not allowed INSERT INTO
(null value)

Not allowed
(50 already exists)

9-14 Copyright © 2004, Oracle. All rights reserved.


FOREIGN KEY Constraint
DEPARTMENTS

PRIMARY
KEY

EMPLOYEES
FOREIGN
KEY

… Not allowed
INSERT INTO (9 does not
exist)
Allowed

9-15 Copyright © 2004, Oracle. All rights reserved.


FOREIGN KEY Constraint

Defined at either the table level or the column level:

CREATE TABLE employees(


employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
...
department_id NUMBER(4),
CONSTRAINT emp_dept_fk FOREIGN KEY (department_id)
REFERENCES departments(department_id),
CONSTRAINT emp_email_uk UNIQUE(email));

9-16 Copyright © 2004, Oracle. All rights reserved.


FOREIGN KEY Constraint

9-17 Copyright © 2004, Oracle. All rights reserved.


ALTER TABLE Statement
Use the ALTER TABLE statement to:

• Add a new column: To add a column in a table, use


the following syntax:
• ALTER TABLE table_name
ADD column_name datatype
• Define a default value for the new column
• Drop a column:
• ALTER TABLE table_name
DROP COLUMN column_name
• Modify an existing column

9-18 Copyright © 2004, Oracle. All rights reserved.


Example 3: Alter Table
3.1 ALTER TABLE LAB
ADD CITY CHAR(10)
3.2 DESC LAB

3.3 alter table lab


drop column city;
3.4 DESC LAB

9-19 Copyright © 2004, Oracle. All rights reserved.


Example 3: Alter Table (Define a default value
for the new column)
3.5 ALTER TABLE LAB
ADD Register_date Date default sysdate

3.6 DESC LAB

9-20 Copyright © 2004, Oracle. All rights reserved.


Dropping a Table

• All data and structure in the table are deleted.


• Any pending transactions are committed.
• All indexes are dropped.
• All constraints are dropped.
• You cannot roll back the DROP TABLE statement.

DROP TABLE Orders;


Table dropped.

9-21 Copyright © 2004, Oracle. All rights reserved.


Changing the Name of an Table
RENAME

To change the name of a table, you execute the


RENAME statement.

You must be the owner of the table.

RENAME LAB TO LAB2;


Table renamed.

9-22 Copyright © 2004, Oracle. All rights reserved.


Truncating a Table

The TRUNCATE TABLE statement:


• Removes all rows from a table
• Releases the storage space used by that table

TRUNCATE TABLE table_name;

You cannot roll back row removal when using TRUNCATE.

Alternatively, you can remove rows by using the DELETE


statement.

9-23 Copyright © 2004, Oracle. All rights reserved.

You might also like