You are on page 1of 14

MODULE OF INSTRUCTION

Data Definition Language


Welcome to the third module of this course Database
Management System 1! For this lesson, we will discuss the
Data Definition Language or also known as DDL.

Data Definition Language is used to create a new table,


modify existing column data type and size, as well as add
new column or drop an existing column.

DML A Data Definition Language or also known as DDL is a


standard command that define the different structures in a
database. DDL statements is used to create, modify, and
remove database objects such as tables, indexes, and users.
The common DDL statements are CREATE, ALTER, and
DROP.

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:

Database Management System 1 1


Week 3 Data Definition Language

1. Synonym – is used to gives an alternative name to an


object.

2. Index – this help improve the performance of some queries.


3. Sequence – generate numeric values when the table has been
created.
4. View – Logically represents subsets of data from one or more
table or used to show a portion of data from based table.
5. Table – is also known as basic storage composed of rows and
columns

Table Structure:
 Tables can be created at any time, even if the users are
using the database at the same time while creating a new
table.
 There is no need to specify the size of a table. The size is
ultimately defined by the amount of space allocated to
the database as a whole. As a database administrator, it is
important to estimate and know how much space a table
will use over time. Sometimes it depends on the data type
size and the amount of data stored in the table.

Table Naming Rule


 The name of the Table and column must not begin with
digit instead it must begin with a letter and be 1–30
characters long. However shorter the name is easy to
remember as long the name of the table is easier to
understand.
 Names must contain only the characters A–Z, a–z, 0–9,
and special character like _ (underscore), $, and # .
 Names must not duplicate the name of another object

_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
owned by the same Oracle server user. Meaning using
oracle reserved word like for example, table, view
database is not allowed, as well table name that has been
created by the other users.
 Names must 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).
 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));
 In the example: the name the table is STUDENTS with 5
column names (USN_ID, LASTNAME, FIRSTNAME,
COURSE and YR_LVL), for the column USN_ID it has a
constraint assigned to it name as PRIMARY KEY which
means that this column will restrict the user to enter a

Database Management System 1 3


Week 3 Data Definition Language

redundant values or this column while LASTNAME


column is set to not null which means this column can
accept redundant values as long as no NULL values
inserted to this column.
 To know the structure of the table create, you may use
the DESC or DESCRIBE command.
 Example:

 Using the DESC command will easily check the


structure of the table like constraints enforce in
each column, data type used and size.

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

_____________________________________________________________________________________
4
MODULE OF INSTRUCTION

After the table has been created, by any chance that a user
might need to change the table structure for any of the
following reasons:
• You omitted a column.
• Your column definition or its name needs to be
changed.
• You need to remove columns.
• You want to put the table into the read-only mode

To do this uses the ALTER TABLE statement.

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 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.

Database Management System 1 5


Week 3 Data Definition Language

ALTER to ADD
 Syntax:
ALTER TABLE tbl_name
ADD column_name datatype(size);

 Suppose that in STUDENTS table it is composed of 3


values as shown in the figure above, then a user need to
add a new column named as ADDRESS to add a new
column without deleting the table afterwards create again
you may use the ALTER to ADD.

 Example:
ALTER TABLE STUDENTS
ADD ADDRESS VARCHAR(15);
Use again DESC command to confirm that the
ADDRESS column is added in the column. From 5
columns it is now compose of 6 columns because of
the ALTER statement. Note the since the table is alter
form 5 to 6 column this only means that when a user
will insert or add a new values to this table, user must
provide now a data for the column ADDRESS.
 Example of Insert:

_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
INSERT INTO STUDENTS VALUES
(400,'ALMAZAN','MARY','BSIM','2','PASIG'
)
 Text in underline is the values for the column
ADDRESS.
 Notice that when a user try to view the whole
values of STUDENTS table the address of the
students who are insert before the user altered the
table are set to NULL.

 The address of the students (SALAMAT,


SUBION, LEE and ALMAZAN) are set to NULL
because during the insertion of their data table
STUDENTS only have 5 columns without the
ADDRESS.

ALTER to MODIFY
 Syntax:
ALTER TABLE tbl_name
MODIFY column_name [new]datatype[new](size);
 Supposed that a user wanted to add additional value for
the STUDENTS table but it so happen that the values
trying to insert in column lastname is too large. See
figure below:

Database Management System 1 7


Week 3 Data Definition Language

 As shown in the output the column lastname only accept


up to 15 characters long. In order to extend the number of
character that this column may accept, you may use the
ALTER to MODIFY statement.

 Example:
ALTER TABLE STUDENTS
MODIFY LASTNAME VARCHAR(20);

 Notice that the size of data type of column LASTNAME


is now modified form 15 to 20. In this case, you may now
add the values of DEL ROSARIO SUR III.

 Since the size of column has been resize to 20 the values


of DELROSARIO SUR III was now successfully
inserted into the STUDENTS table.

_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
ALTER to DROP
 Syntax:
ALTER TABLE tbl_name
DROP COLUMN column_name;
 Supposed that the column address is no longer used and
to save space you may use the ALTER to DROP
statement.
 Example:
ALTER TABLE STUDENTS
DROP COLUMN ADDRESS;
 To view and confirm that the column ADDRESS has
been deleted you may use the DESC command.

 Notice that from 6 columns STUDENTS table


have now 5 columns without the address column.

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;
 This means whenever a SELECT statement is
used FROM clause must call the new name of the

Database Management System 1 9


Week 3 Data Definition Language

table:
 Example:

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;

 Using the TRUNCATE statement will delete the


values in the STUDYANTE table.

 As shown above no rows are selected when


SELECT stamen is issued while when DESC
command is issued the structure of STUDYANTE

_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
table is still intact.

Dropping a Table
 All records in the table will be deleted as well as
it structure when you drop table has been issued,
 A rollback statement will also help retrieve the
record after the DROP statement is issued if
PURGE statement has been applied.
 Syntax
DROP TABLE table
 In the syntax, table is the name of the table.

Guidelines
• All the data is deleted from the table.
• Any views and synonyms remain, but are invalid.
• Any pending transactions are committed.
• Only the creator of the table or a user with the DROP
ANY TABLE privilege can remove a table.
• Example:
DROP TABLE STUDENTS;
 Using this will delete permanently the
STUDYANTE table as shown below.

Database Management System 1 11


Week 3 Data Definition Language

Lesson Summary:
In this lesson you should have learned how to:
 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.

Terms to Remember!
BFILE - Binary data stored in an external file (up to 4 GB)
BLOB - Binary data (up to 4 GB)
CHAR - Fixed-length character data
CLOB - Character data (up to 4 GB)
DATE - Date and time values
DROP TABLE - it is a statement that moves a table to the
recycle bin
Index – improves the performance of some queries.
LONG - Variable-length character data (up to 2 GB
NUMBER - Variable-length numeric data
RAW and LONG RAW - Raw binary data
ROWID - A base-64 number system representing the unique
address of a row in its table
Sequence – generate numeric values.
Synonym – Gives alternative name to an object.
Table – basic unit of storage composed of rows and columns
VARCHAR- Variable-length character data
View – logically represents subsets of data from one or more
_____________________________________________________________________________________
12
MODULE OF INSTRUCTION
table.

Textbook:
References  Oracle Press (2010). Applied Oracle Security

References:
 Pratt, Philip J. (2010). Database management systems
 Rob, Peter & Coronel, Carlo (2009). Database
Management Systems
 Schwalbe, Kathy (2011). Management of Information
Technology Projects
 Wheeler, Evan (2011). Security Risk Management :
Building an Information Security Risk Management
Program from the Ground Up
Supplementary Reading and Video Link

Supplementary Reading
https://www.w3schools.in/mysql/ddl-dml-dcl/
http://www.oracle-dba-
online.com/sql/oracle_data_definition_language.htm
https://www.tutorialspoint.com/sql_certificate/using_ddl_statem
ents.htm

Supplementary Video
https://www.youtube.com/watch?v=XIhAhXda6EE
https://www.youtube.com/watch?v=UvymdJ5IjZA
https://www.youtube.com/watch?v=5dszS8TxTJ4

Suggested Reading
 SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
 Database management system. In Encyclopedia
Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/152201/
database-management-system-DBMS.

Database Management System 1 13


Week 3 Data Definition Language

 SQL. In Encyclopedia Britannica, Retrieved from


http://www.britannica.com/EBchecked/topic/569684/
SQL
 Database Administration. In Encyclopedia.com,
Retrieved from
http://www.encyclopedia.com/topic/Database_admini
stration.aspx
 SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
 Tutorialspoint.com
 oracle.com
 apex.oracle.com

_____________________________________________________________________________________
14

You might also like