Professional Documents
Culture Documents
DB - Lab - 7 DDL Create
DB - Lab - 7 DDL Create
Lab 7
Database Objects
An Oracle database can contain multiple data structures/database objects. The database objects are given
in the table below.
Object Description
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.
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.
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:
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.
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 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:
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 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:
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.