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

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH

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

This command is used to insert data in existing table:


SYNTAX:

INSERT INTO tblNAME(coulmn1, coulmn2,…)


VALUES (value1,value,…)

EXAMPLE:

INSERT INTO tblCustomers (CustomerID, [Last Name], [First Name])


VALUES (1, 'Kelly', 'Jill')

UPDATE COMMAND

This command is used to update an existing record based on some condition.


SYNTAX:

UPDATE table name


SET field name = some value
Instructor Manual/Database Management System BS(CS) 1
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

EXAMPLE:

UPDATE tblCustomers
SET Email = 'None'
WHERE [Last Name] = 'Smith'

DELETE COMMAND:

This command is used to delete entire record.

SYNTAX:

DELETE FROM table list

EXAMPLE:

DELETE FROM tblInvoices


WHERE InvoiceID = 3

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

Instructor Manual/Database Management System BS(CS) 2


COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

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.

Instructor Manual/Database Management System BS(CS) 3


COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

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

Instructor Manual/Database Management System BS(CS) 4


COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

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.

Create table hiredates(id number,hire_date date);

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

Create table studentss(id number,f_name varchar(15), l_name varchar(10) ,gpa


number,admission_date date);

Instructor Manual/Database Management System BS(CS) 5


COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

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

Instructor Manual/Database Management System BS(CS) 6


COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

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

Add a new column

Adding a Column

Instructor Manual/Database Management System BS(CS) 7


COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

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:Here a new coulmn is added to a table student

Alter table students add address varchar;

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:

Alter table studentss alter gpa 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:
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

drop table tblName;

Example:

The query below drops the table “mytable”

Instructor Manual/Database Management System BS(CS) 8


COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

drop table mytable;

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.

Summary:

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

Instructor Manual/Database Management System BS(CS) 9


COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

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.

Instructor Manual/Database Management System BS(CS) 10


COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH
CAMPUS
Department Of Computer Science

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.

Instructor Manual/Database Management System BS(CS) 11

You might also like