Lab - 11
Lab - 11
CAMPUS
Department Of Computer Science
LAB 11
Summary
Items Description
Course Title Database Management System
Lab Title Using DDL Statements to Create and Manage Tables
Duration 3 Hours
Operating System Windows Operating System/ MS Access 2007
/Tool/Language
Objective To get familiar with DDL statements for creating and managing
tables and review the table structure
INSERT COMMAND
EXAMPLE:
UPDATE COMMAND
EXAMPLE:
UPDATE tblCustomers
SET Email = 'None'
WHERE [Last Name] = 'Smith'
DELETE COMMAND:
SYNTAX:
EXAMPLE:
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.
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 “users” having four columns of different data types.
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.
Modifying a Column
Modifying a column include change of data type, size etc. A change to the default value affects
only subsequent insertion of the table.
Example:
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:
This statement will drop the coulmn named gpa
Alter table studentss drop gpa;
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.
SYNTAX
Example:
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:
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:
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. Create the “Employee2” table based on the structure of “Employee_Detail” table.
6. Drop the Employee_Detail table.
7. Rename the “Employee2” table as “Employee_Detail” table.
8. Add a comment to both table definitions describing the tables. Confirm your additions in
the data dictionary.
9. Drop the First_Name column from the Employee_Detail table. Confirm your
modifications by checking the description of the table.
10. 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.
11. Drop all the UNUSED columns from the Employee_Detail table. Confirm your
modifications by checking the description of the table.