Beruflich Dokumente
Kultur Dokumente
Definition
Language
LEARNING OUTCOMES
At the end of this topic, you should be able to:
1. Create a database scheme;
2. Use the DROP TABLE command to eliminate tables produced by
a database; and
3. Use the CREATE TABLE command to produce tables that will be
used in a database.
X INTRODUCTION
In the previous topic, you learnt what the SQL language is and the components
involved in developing this language. In this topic, you will learn what is meant
by Data Definition Language, a component of the SQL Language, and how this
language is used to define or create a database.
In the previous topic, you learnt about the structure of SQL statements.
Now you will learn the structure of data relations. Do you know what is
meant by data relations?
Student
MatricNo StudentName Major
A1000 John Science Computer
A1001 Fatimah Multimedia
A1002 Ali Science Information
A1003 Muthu Networking
A1004 Ah Chong Multimedia
TOPIC 2 DATA DEFINITION LANGUAGE W 13
Course
CourseNo CourseName Tutor No
SK100 Java P1000
MM100 Graphic P1000
SM100 Search Engine P3000
RK200 Database P3500
MM200 Authoring Tool P3000
Registration
MatricNo CourseNo Grade Value
A1000 RK200 B 3.00
A1000 SK100 C+ 2.33
A1001 MM200 B- 2.67
A1001 SK100 A 4.00
A1002 SM100 A- 3.67
A1004 MM200 B 3.00
A1004 RK200 C 2.00
Tutor
TutorNo TutorName Salary (RM) Position
Data Definition Language (DDL) is explained in the book Database Systems: Concept,
Languages and Architecture written by Atzen et al, published by McGraw-Hillin year
2000.
14 X TOPIC 2 DATA DEFINITION LANGUAGE
ACTIVITY 2.1
For example, if you declare a string as CHAR (10), but if it uses only seven
characters, then the three other spaces are filled with characters on the right and
your storage space in the memory is 10 characters. But if you declare a string as
VARCHAR (10) and only seven characters are used, the storage space in the
memory is seven characters only and as such, storage space can be saved.
Bit String is similar to character string. It can be declared as BIT (n) if the length is
fixed, or BIT VARYING (n) if the length varies. BIT consists of bit 0 or 1. Boolean
value T is represented by bit 1 and Boolean value F is represented by bit 0.
The types of data above are used by SQL to declare the attribute and column data
type. Column name is an identifier in SQL. SQL uses identifiers to identify or
name objects in a database such as a table and column name.
The reserved words NULL and NOT NULL are a choice; a default value (values
keyed in automatically when a new application is built) is NULL. We will have to
determine the key column maintained as NOT NULL.
For example, if we are asked to build and define a table structure in a database
for a university environment, the format is:
In this example, only the definition of STUDENT and TUTOR is shown, the
COURSE and REGISTRATION table is left as an exercise for you. As you can see,
the column of salary is defined as DECIMAL (7,2) consisting of seven digits with
two decimal points.
TOPIC 2 DATA DEFINITION LANGUAGE W 17
Generally, tables are created only once with certain values known as record.
The record can be added through INSERT INTO or deleted by the DELETE
FROM command. Over time, tables will not be required due to changes in
organisational structure. SQL allows a table to be dropped in the statement
below:
Look at the following example to assist you in understanding the above situation.
Example
DROP TABLE Student
Solution
Command Details
DROP Removes tables and structure within it
Safeguards tables from being removed if the attribute is a
RESTRICT
foreign key
CASCADE Allows all attributes dependents on the table to be removed.
The command DROP TABLE is different from DELETE FROM because DELETE
FROM only drops rows but DROP TABLE removes both the table and its
structure. When a table is dropped, you are unable to add a value to it.
These exercises will assist you in better comprehending the topics you have just
learned. This concludes Topic 2. Now refer to the summary part of the topic for
revision.
SELF-CHECK 2.2
4. Remove STAFF table but the system must check if the table
consists of a foreign key and as such, removing is not allowed.
You have learnt to create and define the database schema. In addition, SQL
enables you to remove the defined tables due to changes in the database where
tables are no longer needed. The following table shows types of data used to
define the database schema.
You have successfully completed this topic. The next topic will discuss data
management.
TOPIC 2 DATA DEFINITION LANGUAGE W 19
Database Scheme
Database Relationship Structure
SQL Data Types