Professional Documents
Culture Documents
2011-0021 53 Database System
2011-0021 53 Database System
4 Definition
LEARNING OUTCOMES
When you have completed this Topic you should be able to:
1. Identify the data types supported by SQL.
2. Define integrity constraints using SQL
3. Use the integrity enhancement feature in the CREATE TABLE
statement.
4. Create and delete views using SQL.
TABLE OF CONTENTS
Introduction
4.1 The ISO SQL Data Types
4.1.1 SQL Identifiers
4.1.2 SQL Data Types
4.2 Integrity Enhancement Features
4.2.1 Required Data
4.2.2 Domain Constraints
4.2.3 Entity Integrity
4.2.4 Referential Integrity
4.3.1 Data Definition
4.3.1 Creating a Database
4.3.2 Creating a Table
4.3.3 Changing a Table Definition
4.3.4 Removing a Table
4.4 Views
4.4.1 Creating a View
4.4.2 Removing a View
Summary
Key Terms
References
68 X TOPIC 4 SQL: DATA DEFINITION
X INTRODUCTION
In this Topic we explore the main SQL data definition facilities. We begin this
Topic by examining the ISO SQL data types. The Integrity Enhancement Feature
(IEF) improves the functionality of SQL and allows constraint checking to be
standardized. Section 4.2 examines required data, domains, entity integrity and
referential integrity constraints. In Section 4.3 we discuss the main SQL data
definition features which include database and table creation, the altering and
deleting of a table. This Topic concludes with the creation and the removal of
views.
Table 4.1: ISO SQL Data Types (Connoly and Begg, 2005)
Boolean data
Boolean or logical data consists of two distinct values TRUE or FALSE
Character data
A character string can be defined as having fixed or variable length. To define
fixed characters we declare it as CHAR.Variable length characters are declared as
VARCHAR. For example in our Order Entry Database the EmpNo attribute in
the Employee table has a fixed length of 5 characters. It is declared as:
EmpNo CHAR(5)
This column has a fixed length of 5 characters and we insert less than 5 characters
the string is padded with blanks to make up for up to 5 characters. The column
Name in the Employee relation has a variable length of up to 15 characters. It will
be declared as:
Name VARCHAR(15)
This column has a variable length of 15 characters and if we enter less than 15
characters, only the characters entered are stored.
For example the column Salary in the Employee relation can be declared as:
Salary DECIMAL (7,2)
which can handle a value up to 99,999.99
70 X TOPIC 4 SQL: DATA DEFINITION
For large positive or negative numbers, that is number without a decimal point
we declare them as INTEGER.
For example the column QtyOnHand in the Product table can be declared as:
QtyOnHand INTEGER (4)
Real numbers used for scientific calculations which are of the approximate
numeric data type are declared using FLOAT (p) where p is the precision
parameter which indicates the number of significant digits. The precision in
FLOAT can vary for up to 38 digits, whereas the precision in REAL numbers is
fixed at 18 digits.
Date
The date data type is defined in columns such as the DOB (Date of Birth) column
in the Employee table. This is declared in SQL as:-
DOB DATE
The default format can be specified for example DD-MON-YY) as used in our
example.
SELF-CHECK 4.1
1. Describe SQL identifiers.
2. Identify the data types in SQL.
required data constraint. To ensure that the column position of the Employee
table cannot be null, we define the column as:
Position VARCHAR (15) NOT NULL
When NOT NULL is specified the Position column must have a data value.
To define a composite primary key we specify the columns in the PRIMARY KEY
clause. For example the OrderDetail table has two primary keys OrderNo and
ProductNo.
(parent table). SQL supports referential integrity constraint with the FOREIGN
KEY clause in the CREATE and ALTER TABLE statements. For example to
specify the foreign key SuppNo of the Product table we state it as:
SQL supports four actions when a user attempts to delete a referenced row from
the parent table and there are one or more matching rows in the child table. The
same four actions can be applied when the primary key in the parent table is
updated (Mannino, 2007, Connoly and Begg, 2005).
• CASCADE ă Perform the same action to related rows. For example, if a
SuppNo in the Supplier table is deleted then the related rows in the Product
table will be deleted in a cascading manner.
• SET NULL ă Delete the row from the parent table and set the foreign key
value in the child table to NULL. For example if the SuppNo in the Supplier
table is deleted then the related rows in the Product table will be set to NULL.
This will be valid only if the foreign key values do not have the NOT NULL
clause specified.
• NO ACTION ă Reject the delete operation from the parent table. For example
do not allow the SuppNo in the Supplier table to be deleted if there are
related rows in the Product table.
• SET DEFAULT ă Delete the row from the parent table and set the foreign key
in the child table to its default value. This is valid only if default values are
set. For example if the SuppNo in the Supplier table is deleted then set
SuppNo in the Product table to a default value such as „TENDERS SOON‰.
For example if we do not allow the rows in the Supplier table to be deleted if
there are related rows in the Product table then we can specify it in SQL as:
Similarly if for example, if a SuppNo in the Supplier table is updated then the
related rows in the Product table need be updated in a cascading manner. We
specify this SQL as:
You must also consider the impact of referenced rows on insert operations. A
referenced row (in the parent table) must be inserted before its related rows (in
the child table). For example before inserting a row in the Product table, the
referenced row in the Supplier must exist.
SELF-CHECK 4.2
1. How do you define Primary key and Foreign key?
2. Identify the actions the SQL support. Briefly explain these
actions.
Therefore if the schema is OrderProcessing and the creator is Lim the SQL
statement is:
The CREATE TABLE statement creates a table consisting of one or more columns
of the defined data type.
The NOT NULL is specified to ensure that the column must have a data value.
The remaining clauses are constraints and are headed by the clause:
CONSTRAINT constraintname.
The PRIMARY KEY clause specified the column(s) that comprise the primary
key. It is assumed by default that the primary key value is NOT NULL.
The FOREIGN KEY clause specifies a foreign key in the child table and it
relationship to the parent table. This clause specifies the:
• A listofForeignKeyColumns, the column(s) that form the foreign key.
• A REFERENCES subclause indicting to the parent table that holds the
matching primary key.
• An optional ON UPDATE clause to specify the action taken on the foreign
key value of the child table if the matching primary key in the parent table is
updated. These actions were discussed in section 4.2.4.
• An optional ON DELETE clause to specify the action taken on the child table
if the row(s) in the parent table are deleted whose primary key values
TOPIC 4 SQL: DATA DEFINITION W 75
matches the foreign key value in the child table. These actions were discussed
in section 4.2.4.
The following 3 examples show CREATE TABLE statements for the Order Entry
Database using the tables Customer, Order and OrderDetail.
(i) Creating the Customer table using the features of the CREATE TABLE
statement.
(iv) The reader can now create the rest of the tables in the Order Entry Database
as an exercise.
The DROP TABLE statement should be carried out with care as the total effect
can be damaging to the rest of the database tables. It is recommended that this
clause be used if a table is created with an incorrect structure. Then the DROP
TABLE clause can be used to delete this table and the structure created again.
SELF-CHECK 4.3
1. What does the CREATE TABLE statement do?
2. What does the ALTER TABLE statement do?
3. How can we remove a table from the database?
78 X TOPIC 4 SQL: DATA DEFINITION
4.4 VIEWS
A view is a virtual or derived relation that may be derived from one or more base
relations. Views do not physically exist in the database. It allows users to
customize the data according to their needs and hides part of the database from
certain users. Let us look at how views are created.
This will give us a view known as CustomerIpoh with the same column names as
the Customer table but only those rows where the City is Ipoh. This view is
shown below in Table 4.1.
This view created from the CustomerIpoh View is called the CustomerIpohBal. It
has the columns CustNo, Name and Balance. The columns in this view are
displayed in Table 4.2.
DROP VIEW causes the definition of the view to be deleted from the schema. For
example to remove the view CustomerIpoh we specify it in SQL as:
If CASCADE is specified, DROP VIEW deletes all objects that reference the view.
If RESTRICT option is chosen and there other database items that depend on the
existence of the view being dropped then the command is rejected.
SELF-CHECK 4.4
1. Briefly explain a VIEW.
2. How do you create a VIEW?
80 X TOPIC 4 SQL: DATA DEFINITION
• SQL identifiers can use the letters a-z (upper and lower), numbers and the (_)
for table, view and column names. The identifiers cannot be more than 128
characters, must start with a letter and cannot contain spaces.
• The available data types identified in SQL are Boolean, character, exact
numeric, approximate numeric and date.
• Foreign keys are specified using the FOREIGN KEY clause. The update and
delete actions on referenced rows specified by the ON DELETE and ON
UPDATE subclauses.
• We can remove a table from the database by using the DROP TABLE
statement.
• A View is a derived relation that does not physically exist in the database. It
allows users to customize the data according to their needs
• A view is created by the CREATE VIEW statement. It is not a stored table and
it is not necessary to recreate the view each time it is referenced. The
different types of views that can be created include the horizontal and the
vertical views.
TOPIC 4 SQL: DATA DEFINITION W 81