Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Database Management System

Lab 7

Using DDL Statements to Create and Manage Tables


In this lab, we will explore DDL statements for creating and managing tables and review the table
structure.

Database Objects

An Oracle database can contain multiple data structures/database objects. The database objects are given
in the table below.

Object Description

Table Basic unit of storage; composed of


rows
View Logically represents subsets of data
from one or more tables
Sequence Generates numeric values

Index Improves the performance of some


queries
Synonym Gives alternative names to objects

Tables in Oracle Database

Oracle database contain two categories of tables o


User Tables
o Data Dictionary

User tables are a collection of tables created and mantained by the user and contain’s user
information, where as data dictionary is a collection of tables created and mantained by the Oracle
Server and contain’s database information. Information stored in data dictionary including names
of the Oracle Server users, privileges granted to users, database object names, table constraints and
auditing information.
Querying Data Dictionary

To view the name of the tables owned by the user, select table name from user tables.

In order to view th distinct objects owned by the user.

To view tables, view, synonyms, and sequences owned by the user

Creating Tables

Tables are created to store the data. For creating table, execute the SQL CREATE TABLE
statement which is one of the statement of Data Definition Language (DDL). For creating table,
user must have the previlige to create the table and the storage area in which to create objects.

CREATE TABLE [schema.]table (column


datatype [DEFAULT expr][, ...]);

For creating the table, specify the table name, column name, data type and column size.
For naming tables, and columns, table names and column names:
o Must begin with a letter o Must be 1–30 characters
long
o Must contain only A–Z, a–z, 0–9, _, $, and #
o Must not duplicate the name of another object owned by the same user o
Must not be an Oracle server reserved word

Example:

In the query, table is created named as “hiredates” having two columns named as “id” having
datatype number and “hire_date” having datatype date, and the date in which data is entered is
added by default. The default data type must match the column data type.

The query creates another table named “department” having four columns of different data types.

In order to confirm table creation, one can review the schema of the table.

Data Types
Data types that can be used in oracle are as follows:

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

Following are the datatypes related to date.

Data Type Description

TIMESTAMP Date with fractional seconds

INTERVAL YEAR TO Stored as an interval of years


MONTH and months

INTERVAL DAY TO Stored as an interval of days, hours, minutes, and


SECOND seconds

Alter Table

After creating a table, one can change the structure of the table. The possible actions that can be
performed with the altering table are: o Add a new column o Modifying the existing column
o Drop a column
o Define the default value for the new column
Adding a Column

One can add the new column by using the ALTER table statement. If a table contains rows when a
column is added, then the new column is initially null for all rows.

Example:

In the query below, new column is added in table named deptt and column named job_id of datatype
varchar2 is added.

Modifying a Column

Modifying a column include change of data type, size and default value. A change to the default value
affects only subsequent insertion of the table.

Example:
The query change the data type of job_id from varchar2 to number.
Dropping a Column

DROP COLUMN clause in ALTER TABLE statement remove the column that is no longer
needed in the table. Once a column is dropped, it cannot be recovered. Only one column can be
dropped at a time.

Example:
The query drop the column job_id from the deptt table.

Droping Table

Dropping table include removing the table. All data and structure in the table are deleted. Any
pending transactions are committed.All indexes and constraints are dropped. One cannot roll back the
DROP TABLE statement.

Example:
The query displays all the user tables.
The query below drops the table “HIRE_DATES”

Again displays the user tables to assure that table is not now in the list of user tables.

Changing the Name of the Object

In order to change the name of the object, use the RENAME statement. To change the name of the object,
you must be the owner of the object.

Example:

The example rename the table “dept” to “dept_detail”.


Truncating Table

The TRUNCATE TABLE statement remove all rows from a table. It also releases the storage
space used by that table. If the table is the parent of referntial integrity constraint, you cannot
truncate the table. Disable the constraint before issuing the TRUNCATE statement.

Example:

The query below truncated the table hire_dates1.

Adding Comments to the Table:

One can add the comments to a table or column by using the COMMENT statement. Comments
can be viewed through data dictionary. o ALL_COL_COMMENTS o
USER_COL_COMMENTS o ALL_TAB_COMMENTS
o USER_TAB_COMMENTS

Example:

The query places the comments on table emp.

The SET UNUSED Option:

The SET UNUSED option marks one or more columns as unused. Specifying this clause does not
actually remove the target columns from each row in the table. After a column has been marked as
unused, you have no access to that column. In addition, the names and types of the columns
marked unused will not be displayed during a DESCRIBE.

Example:

The query sets the table emp_is as unused.


You can also drop the unused column.

Summary:

The overall summary of Data Definition Language (DDL) statements are given in the table.

LAB TASKS
1. Create “Department_Detail” table with the schema given below. Conform that the table is created.
2. Create the “Employee_Detail” table based on the following table instance chart. Confirm that the
table is created.

3. Modify the “Employee_Detail” table to allow for longer employee last names. Confirm your
modifications.
4. Confirm that both tables created above are stored in the data dictionary.
5. Drop the Employee_Detail table.
6. Rename the “Employee2” table as “Employee_Detail” table.
7. Add a comment (of your choice) to Employee_Detail. Confirm your additions in the data
dictionary.
8. Drop the First_Name column from the Employee_Detail table. Confirm your modifications by
checking the description of the table.
9. In the Employee_Detail table, mark the DEPT_ID column in the Employee_Detail table as
unused. Confirm your modifications by checking the description of the table.
10. Drop all the UNUSED columns from the Employee_Detail table. Confirm your modifications by
checking the description of the table.

You might also like