Professional Documents
Culture Documents
BITWeek8 - L12 - ITE2422 V1
BITWeek8 - L12 - ITE2422 V1
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.
Learning outcomes
After completing this lesson, you will be able to create and modify a database schema.
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.
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.
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.
To create a database with a given name in SQL, you can use the following command.
For an example:
As indicated in Figure 12.2, you can type your commands in SQL. Then click “Go” button
to execute the command.
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:
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.
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.
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>;
Now let us consider an actual table that is created in the Company database.
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));
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.
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).
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 two tables (Table 12.1 and Table 12.2) populated with data to
understand the options used with the foreign key.
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.
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.
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.
The relation can no longer be used in queries, updates, or any other commands since
its description no longer exists
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.
Here, you can change the datatype of EMPLOYEE table from varchar (120) to char (100).
Rename an attribute
Here, first you need to include the existing column name (EMAIL) and then the new
column name (EMAIL_ADDRESS).
EMAIL column can be deleted from the EMPLOYEE table using the above SQL command.
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.
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.