Sie sind auf Seite 1von 9

T o p ic 2 X Data

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.

As we have already known, the main step involved in developing a database


application is to create a database schema by defining the data used in a
database. You will also learn how to delete and create tables.
12 X TOPIC 2 DATA DEFINITION LANGUAGE

2.1 DATA RELATIONSHIP STRUCTURE


A university environment is used as a case study to enable the learning of
commands in a language. Figure 2.1 is an entity of STUDENTS, COURSE,
REGISTRATION and TUTOR.

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?

Figure 2.1: Entity relations diagram

Based on the ER Model, the STUDENT, COURSE, REGISTRATION and TUTOR


tables are generated. The tables are presented as follows:

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

P1000 Kamal 3,000 Lecturer

S1500 Sanusi 1,000 Tutor

S2000 Chong 2,700 Assist Lecturer

P2500 Hasnah 3,000 Lecturer

P3000 Salleh 3,500 Lecturer

P3500 Zainab 3,300 Lecturer

S3000 Mary 2,900 Assist Lecturer

* Salary is presented monthly.

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

2.2 DATA DEFINITION IN SQL


You have heard of topics on DDL in the previous topics. It explains the definition
of set relations and information in each relation. The information consists of:
1. Relations schema
2. Table structure
3. Data type and domain for each attribute or column.

ACTIVITY 2.1

In your opinion, why is it important to define set relations and the


information in each relation?

2.2.1 SQL Data Types and Identifiers


Types of data supported by SQL are listed below.

Data String Type


String is divided into chart string and bit string.

Chart String may be declared through two forms:


CHAR (n) String character consisting of n characters.
VARCHAR (n) - Where the number of string characters is changed from
one to n characters.

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.

Numeric Data Type


Numeric data type consists of integer, decimal, float and real numbers. This is
explained in Table 2.1.
TOPIC 2 DATA DEFINITION LANGUAGE W 15

Table 2.1: Numeric Data Type

Numeric Data Details


Integer or INT for integer numbers
SMALLINT for small integers
DECIMAL (m,n) for decimal number with m digit amount and n decimal
points. Simplification of DECIMAL is DEC
NUMERIC (m,n) similar with DEC (m,n)
FLOAT for decimal point number
REAL for exact number
DOUBLE for exact number with required
PRECISION high precision

Date and Time Domain


Date types of domain are declared as DATE and true type of domain as
TIME. This is depicted in Table 2.2.

Table 2.2: Date and Time Domain

Data Type Details


DATE Consists of year, month and day
TIME Consists of hours and minutes

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.

Identifiers can be defined as users or systems. Identifiers consist of set characters


allowed, which are letters a - z, capital lettesr A - Z, digits 0 - 9 and symbols " -".
An identifier can be no longer than 128 characters and must not contain a space.
Example of an identifier and declaration of data type is as follows.

Name CHAR (30)


Address VARCHAR
Working Hours TIME
16 X TOPIC 2 DATA DEFINITION LANGUAGE

2.3 DEFINITION OF A DATABASE SCHEMA


A database schema is a group of objects linked to each other. The objects in a
database schema may consist of tables, domains and a set of characters. The
creation and definition of database schema is implemented by the DBA or other
authorised personnel. The process of creating a database schema depends on the
implementation of a particular DBMS. Certain DBMS defines schema creation as
part of the DBMS process.

2.4 .4CREATING TABLES


The CREATE TABLE definition in the SQL command is used to create a database
schema. This statement enables a basic table and structure details to be
developed. The format is:

CREATE TABLE table_name


(column_name, data_type (NULL, NOT NULL)

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:

CREATE TABLE Student (


MetricNo CHAR (5) NOT NULL,
StudentName VARCHAR (20)
Major VARCHAR (15));
CREATE TABLE Tutor (
TutorNo CHAR(5) NOT NULL
TutorName VARCHAR (20)
Position VARCHAR (15)
Salary DECIMAL (7,2));

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

2.5 REMOVING TABLES


Normally if you delete any files on your computer, it is sent to the Recycle Bin. In the
case of table deletion, will it appear in the Recycle Bin?

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:

DROP TABLE table_name [ RESTRICT | CASCADE ]

Look at the following example to assist you in understanding the above situation.

Example
DROP TABLE Student

Solution

Table 2.3: Command to Remove Table

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.

For further explanation and tutorial, view


http://www.sqlcourse.com/intro.html.
18 X TOPIC 2 DATA DEFINITION LANGUAGE

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

Define the data type used for the following statements.

1. STATUS attribute represents value 1 if the status is active and 0 if


the status is non-active.

2. CGPA attribute consisting of number 0.00 - 4.00.

3. Date of Birth Attribute.

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.

Table 2.4: Types of Data

Data Type Declaration


Character Char
Bit BIT BIT VARYING
Integer INTEGER INT SMALL INT
Point DECIMAL DEC NUMERIC
DOUBLE
Real Number FLOAT REAL
PRECISION
Date / time DATE TIME

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

Das könnte Ihnen auch gefallen