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

ITE 2422 – Database Management Systems Week 08

12.0 Data Definition Language

Introduction

In this 12th lesson you will learn about ways in creating and modifying a database or a
table using a data definition language. In the last few lessons you have learnt about
creating a relational schema. In this lesson we will learn, how to start developing a
database in a RDBMS like MySQL.

Let us start the lesson 12.

Learning outcomes

After completing this lesson, you will be able to create and modify a database schema.

From this lesson you will be able to,


• Create, Alter, Drop database and tables
• Use Data types
• Add foreign keys and other constraints

A data definition language (DDL) is a computer language used to create and modify the
structure of database objects in a database. These database objects include views,
schemas, tables, indexes, etc. In this lesson we will learn about creating and modifying
a relational schema in a RDBMS.

SQL (Structured Query Language) is a standard computer language for relational


database management and data manipulation. Most RDBMSs support SQL, thus, it can
be used across several different platforms.

Data Definition Language 1/11


ITE 2422 – Database Management Systems Week 08

You can type your SQL commands in Command Line or in the GUI.

Already, we have installed MySQL in our machines. Now, it is time to create our first
database in MySQL. In this lesson we will be using phpMyAdmin. However, you are free
to use MySQL workbench too. It is good to be familiar with both tools.

As in Figure 12.1, click SQL option in PhpMyAdmin (using XAMPP).

Figure 12.1: PhpMyAdmin Panel

It is important to know commonly used DDL in SQL. CREATE, ALTER and DROP are most
commonly used DDL commands. Accordingly, we will be able to create the databases
as per the relational model we have developed previously.

12.1 Create database

To create a database with a given name in SQL, you can use the following command.

CREATE DATABASE <database_name>;

For an example:

CREATE DATABASE Company;

As indicated in Figure 12.2, you can type your commands in SQL. Then click “Go” button
to execute the command.

Data Definition Language 2/11


ITE 2422 – Database Management Systems Week 08

Figure 12.2: SQL panel in PhpMyAdmin

We can get an error, if the database exists in the RDBMS. So, it is better to check if it
exists before creating the database.

We can remove the existing database and create the new database by:

DROP DATABASE IF EXISTS Company;

CREATE DATABASE Company;

If not, we can create the new database if there is no other database with the same
name. That means, the old database will not be removed.

CREATE DATABASE IF NOT EXISTS Company;

It is important to note that in SQL, at the end of a SQL statement you must put ‘;’.
It indicates the end of the statement.

12.2 Create Table


We will be creating a table using the relational schema we have identified in previous
lessons. Each relation will be converted to a table. In a relation, you must indicate the
attributes and their data types, primary keys, foreign keys and some other constraints.

Data Definition Language 3/11


ITE 2422 – Database Management Systems Week 08

We have learnt about data types in lesson 7. In this lesson, we will be using those data
types to create tables in MySQL RDBMS.

You will be creating, altering or deleting tables in a database. Since there could be
multiple databases, RDBMS will not be able to identify the correct database. As such,
it is important that you select the database name first.
Therefore, use the following command before typing any other command related to the
database.
Use <database_name>;

So, to use our company database:


Use Company;

First let’s consider the structure of creating a database table.


CREATE TABLE <table_name>
( <column_name1> <data_type>,
<column_name2> <data_type>,
…);

Now let us consider an actual table that is created in the Company database.

CREATE TABLE DEPARTMENT


( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME));

As can be seen from the SQL script above:


• A constraint NOT NULL may be specified on an attribute, if null values are not to
be allowed.
• Key attributes can be specified via the PRIMARY KEY and UNIQUE phrases.

Data Definition Language 4/11


ITE 2422 – Database Management Systems Week 08

To define primary key, you can use one of the following options:
A:
CREATE TABLE DEPARTMENT1
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER PRIMARY KEY);
Or
B:
CREATE TABLE DEPARTMENT2
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
PRIMARY KEY (DNUMBER));

If you have a composite primary key:


CREATE TABLE PROJECT_HOURS
( PNUMBER INTEGER NOT NULL,
EMPNO INTEGER NOT NULL,
WORKED_HOURS INTEGER,
PRIMARY KEY (PNUMBER, EMPNO));

Here, PNUMBER and EMPNO both are used as the primary key. Thus, the option A
mentioned above cannot be used if you have multiple attributes as the primary key.

To define a foreign key:


CREATE TABLE EMPLOYEE
( EMPNo CHAR(9),
ENAME VARCHAR(30) NOT NULL,
BDATE DATE,
DNO INTEGER,
PRIMARY KEY (EMPNO),
FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNUMBER));

Here, EMPLOYEE table references DEPARTMENT table. Therefore, a foreign key will be
created in EMPLOYEE table as DNO that will be referring the primary key of
DEPARTMENT table (DNUMBER).

Data Definition Language 5/11


ITE 2422 – Database Management Systems Week 08

It is important to note that data type of DNO in EMPLOYEE and DNUMBER in DEPARTMENT
are the same.

There are several Referential integrity options that we can consider when writing the
SQL script. We can specify RESTRICT, CASCADE, SET NULL or SET DEFAULT on referential
integrity constraints (foreign keys).

Let us consider some examples with the options.

CREATE TABLE EMPLOYEE1


( EMPNO CHAR(9),
ENAME VARCHAR(30) NOT NULL,
BDATE DATE,
DNO INTEGER,
PRIMARY KEY (EMPNO),
FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNUMBER)
ON DELETE SET NULL ON UPDATE CASCADE);

Let us consider two tables (Table 12.1 and Table 12.2) populated with data to
understand the options used with the foreign key.

Table 12.1: EMPLOYEE table


EMPNO EName BDATE DNO

1 Jagath Adikari 1976-12-30 1


2 Radha Shiva 1993-03-14 2
3 Damani Perera 1987-05-10 3
4 Janith Govin 1978-10-24 2

Data Definition Language 6/11


ITE 2422 – Database Management Systems Week 08

Table 12.2: DEPARTMENT table

DNAME DNUMBER

HR 1
Sales 2
Finance 3
Research 4

Here,
• ON DELETE SET NULL - When a record in the DEPARTMENT table is deleted, DNO
of all records in the EMPLOYEE table will be set NULL that have the same DNO
value.
For an example, if we delete the row with DNUMBER = 2 in the DEPARTMENT
table (Table 12.2). Accordingly, in the EMPLOYEE table (in Table 12.1), the rows
with EMPNO = 2 and EMPNO = 4 will be set to NULL value as they are refereeing
to DNO = 2.

• ON UPDATE CASCADE - If the parent primary key (in DEPARTMENT table) is


changed, the DNO value in EMPLOYEE table will also change to reflect that.
For an example, if we change the DNUMBER to 5 in the row with DNAME = Sales
and DNUMBER =2 in the DEPARTMENT table (Table 12.2). Then, in the EMPLOYEE
table (Table 12.1), DNO in the rows with EMPNO = 2 and EMPNO = 4 will be
updated as DNO = 5.

• ON DELETE CASCADE – When a record in the DEPARTMENT table is deleted, all


records in the EMPLOYEE table will also be deleted that have the same DNO
value.
For an example, if we delete the row with DNUMBER = 2 in DEPARTMENT table
(Table 12.2). Accordingly, in the EMPLOYEE table (in Table 12.1), the rows with
EMPNO = 2 and EMPNO = 4 will be deleted as they are refereeing to DNO = 2.

Data Definition Language 7/11


ITE 2422 – Database Management Systems Week 08

The other option is ON DELETE SET DEFAULT:


CREATE TABLE EMPLOYEE2
( EMPNO CHAR(9),
ENAME VARCHAR(30) NOT NULL,
BDATE DATE,
DNO INTEGER DEFAULT 1,
PRIMARY KEY (EMPNO),
FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNUMBER)
ON DELETE SET DAFAULT ON UPDATE CASCADE);

Here,
• ON DELETE SET DAFAULT - When a record in the DEPARTMENT table is deleted,
all records in the EMPLOYEE table with the same DNO value will also be changed
to the default value of DNO. As can be seen in the above SQL script, we have set
the default value of DNO as 1.

Do Activity 12.1, to understand the DDL commands in creating a table by considering a


relational model.

Activity 12.1

Identify the Primary keys, Foreign keys and the data types of attributes of the relations given
below. Then create the tables using SQL in MySQL.

Student (StudentNo, FirstName, LastName, Address, Telephone, Date_of_Birth, Gender)


Enrolment (StudentNo, SubjectNo, EnrolledDate)
Subject (SubjectNo, SubjectName, Credits)
BookAdoption (BookCode, SubjectNo, AdopedDate)
Book (Tile, BookCode, AuthorName, PublicationYear, Publisher, Edition)

Data Definition Language 8/11


ITE 2422 – Database Management Systems Week 08

12.3 Drop Table


If we want to remove an already created table, we can use the following SQL:
DROP TABLE DEPARTMENT2;

The relation can no longer be used in queries, updates, or any other commands since
its description no longer exists

12.4 Alter table


Alter table is used to add, delete, or modify columns in an existing table.

Add an attribute to one of the base tables


ALTER TABLE <table_name>
ADD <column_name> <datatype>;

ALTER TABLE EMPLOYEE


ADD EMAIL VARCHAR(120);

NOT NULL constraint is not allowed for newly added columns. As the existing records
(rows) in the table will have NULL for the new attribute, it is not possible to set NOT
NULL.

Using the UPDATE commands, the database users can enter a value for the new
attribute EMAIL in each EMPLOYEE record. We will talk about UPDATE command in the
next lesson under DML.

Change the data type of an attribute


ALTER TABLE EMPLOYEE
MODIFY COLUMN EMAIL CHAR(100);

Here, you can change the datatype of EMPLOYEE table from varchar (120) to char (100).

Rename an attribute

Data Definition Language 9/11


ITE 2422 – Database Management Systems Week 08

ALTER TABLE EMPLOYEE


CHANGE EMAIL EMAIL_ADDRESS VARCHAR(50);

Here, first you need to include the existing column name (EMAIL) and then the new
column name (EMAIL_ADDRESS).

Delete an attribute from one of the base tables


ALTER TABLE EMPLOYEE
DROP COLUMN EMAIL_ADDRESS;

EMAIL column can be deleted from the EMPLOYEE table using the above SQL command.

12.5 Rename table


You change the name of a table using the rename command in SQL.
RENAME TABLE tb1 TO tb2;

RENAME TABLE EMPLOYEE2 TO EMPLOYEE3;

12.6 Truncate
To remove the table content while keeping the structure of the table we can use the
TRUCATE command in DDL. It will remove all the records from the table. Thus, every
space allocated for the data is removed.

TRUNCATE TABLE <table_name>;

TRUNCATE TABLE EMPLOYEE;

So, it is all about DDL in MySQL.

Data Definition Language 10/11


ITE 2422 – Database Management Systems Week 08

Summary
Now we have completed learning the Lesson 12. In this lesson, we discussed about
the data definition language.

DDL is used to create, alter, drop and truncate a table. We have learnt how to
include primary keys, foreign keys and data types when creating tables. After
completing this lesson, you will be able to create your own database based on a
given relational schema.

In the lesson 13, you will learn about DCL in MySQL. Before you go to the next
lesson, complete the self-assessment activity to check what you have learnt in
the Lesson 12.

Data Definition Language 11/11

You might also like