Database Management System 1: Data Definition Language

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Database Management System 1

Data Definition Language


Lesson Objective
After completing this lesson, the student should be able
to:
• Categorize differences between database objects.
• Review and analyze the structure of the table.
• Lists and identify the usage of different data types.
• Create a simple table.
• Modify, drop or delete a column after the table is created.
Database Objects
A database objects is composed of the following:
1. Synonym – Gives alternative name to an object.
2. Index – improves the performance of some queries.
3. Sequence – generate numeric values.
4. View – logically represents subsets of data from one or
more table.

5. Table –is also known as basic storage composed of rows


and columns.
Table Naming Rule
Table names and column names must:
• Begin with a letter uppercase, lowercase or a
combination will do.
• At least 30 characters long.
• Must only have a letter from A–Z, or a–z, 0–9, and
special character _(underscore), $ (dollar sign),
and # (sharp symbol)
• Must not duplicate or use the name of another
object owned by the same user
• Not be an Oracle server–reserved word
CREATE TABLE statement
To create a table a user must have

 The CREATE TABLE privilege


 A storage area
 Syntax:
CREATE TABLE tbl_name
(Column_lists datatype (size) [default expression] ;

 Where:
o Tbl_name - is the name of the table
o Column_lists – the lists of columns
o Expression – constraint added on each column (if any).
CREATE TABLE statement cont.
• Example:
CREATE TABLE STUDENTS
(USN_ID NUMBER (6) PRIMARY KEY,
LASTNAME VARCHAR (15) NOT NULL,
FIRSTNAME VARCHAR (15),
COURSE CHAR (4),
YR_LVL CHAR (5));
Data type
1. VARCHAR- Variable-length character data
2. CHAR - Fixed-length character data
3. NUMBER - Variable-length numeric data
4. DATE - Date and time values
5. LONG - Variable-length character data (up to 2 GB
6. CLOB - Character data (up to 4 GB)
7. RAW and LONG RAW - Raw binary data
8. BLOB - Binary data (up to 4 GB)
9. BFILE - Binary data stored in an external file (up to 4 GB)
10. ROWID - A base-64 number system representing the unique address of a row in its
table
Use the ALTER TABLE statement to:
• Add a new column after the table has been
created
• Modify an existing column definition
• Define a default value for the new column
• Drop existing column
• Rename a column
• Change table to a read-only status
Three (3) types of ALTER statement

1.Alter to ADD – add new column after the


table has been created
2.Alter to DROP – delete specific column
3.Alter to MODIFY – modify an existing column
either change the data type size and data
type.
ALTER to ADD
 Syntax:
ALTER TABLE tbl_name
ADD column_name datatype(size);

• Example:
ALTER TABLE STUDENTS
ADD ADDRESS VARCHAR(15);
ALTER to MODIFY
 Syntax:
ALTER TABLE tbl_name
MODIFY column_name [new]datatype[new](size);

 Example:
ALTER TABLE STUDENTS

MODIFY LASTNAME VARCHAR(20);


ALTER to DROP
 Syntax:
ALTER TABLE tbl_name
DROP COLUMN column_name;

 Example:
ALTER TABLE STUDENTS
DROP COLUMN ADDRESS;
Rename the table
 To rename the table or change the existing
name of the table use the RENAME statement;
 Syntax:
RENAME old_tbl_name TO new_tbl_name;
 Example:
RENAME STUDENTS TO STUDYANTE;
Truncate Statement
 To delete the values in the STUDENTS but leaving
it structure you may use the TRUNCATE
statement.
 Syntax:
TRUNCATE TABLE tbl_name;
 Example:
TRUNCATE TABLE STUDYANTE;
Dropping a table
 Using DROP table statement will lose all the data in the
table and all the indexes associated in it.
 Even if a rollback statement is issued the record in the
table will not be restored.
 Syntax
DROP TABLE table ;
• Example:
DROP TABLE STUDYANTE;
Lesson Summary:

In this lesson, you should have learned the following.


 Create new table by using the CREATE table statement.
 Add, modify or drop exiting column using the ALTER statement.
 Change the name of the table using the RENAME statement.
 Delete the table using the DROP statement
 Remove all rows in the table using the TRUNCATE statement.

You might also like