Data Definition Language (DDL)
The SQL Data Definition Language (DDL) allows database objects such as tables, indexes or views to be created or destroyed. The DDL commands we will use for tables are:
CREATE TABLE:
Used to create the table structure
ALTER TABLE:
Used to alter the table structure
DROP TABLE
: Used to destroy the table
Before looking at how to create a table, we need to understand integrity constraints. Integrity constraints protect a database from becoming incomplete, inaccurate or inconsistent. The next reading from the textbook provides an introduction to integrity constraints. The paragraph on action assertions in this reading is not relevant.
It is important that we understand the steps involved in preparing a
table that is to be created. The next reading covers the seven steps to
follow when creating tables as well as the general syntax of the CREATE
TABLE
statement. This reading will prepare you for most of the
work you will do in this topic.
Using the seven steps
The first step in creating a table is to identify appropriate datatypes, precision and length for all of the fields of the table. Oracle has a number of datatypes available to us. Selecting the appropriate datatype helps to ensure data integrity (allowing only valid data) in the database.
Once the datatypes for each field in the table have been identified, the rest of the steps can then be taken to create the table. These can be summarised as:
- Constraints
-
NULL
values (step two) - Primary keys (step three)
- Foreign keys and referential integrity (step four)
- Default values (step five)
-
- Checks
- Domain constraints (step six)
Step seven, the final step in the process, is creating the table(s).
The next activity implements the seven steps to build the INVOICE
database you have been using.
Manipulating tables and data
Altering tables
Planning and thought in the database design process should
avoid the need to alter datatypes once the tables have been created. If
datatypes are changed after table creation, problems will often arise
with data integrity.
On the other hand, occasions may arise when integrity constraints may be required to be added or dropped after the table has been created. The next reading from the text Changing Table Definitions and Removing Tables provides the syntax for altering tables.
Inserting, updating and deleting data
Having created the table(s) they need to be populated with
data and
once populated the data may need to be modified. The next reading
covers the commands:
INSERT INTO
DELETE FROM using WHERE
UPDATE
The next reading addresses this problem: Inserting, updating and deleting data.
Destroying tables
During the life of a database, tables may need to be removed
from
the database (this is especially so during the development and testing
stage when problems arise).
Before a table can be dropped, all the data in that table must be deleted first. This begs the question, what about referential integrity? Read the short section on dropping tables from the database.
Invoice database
Log on to Oracle and try these queries on the Invoice database.
This exercise is particularly relevant to the first assignment.
1. Drop all of the tables from the INVOICE
database.
2. Recreate all of the tables. Remember the order of creation.
3. What happens if the tables are created out of order? Try it and see.
INVOICEITEM
has two parents (INVOICE
and ITEM
) and you can’t create INVOICEITEM
before INVOICE
and ITEM
are created.4. Populate the tables.
Either select the information from your lecturers
account using:
insert into item
select * from jtutty.item
5. Add yourself as a customer
6. Create an invoice for a customer that doesn't exist in theCUSTOMER
table. What happens?
If you have set up your tables correctly, you will not be allowed
to add an invoice for a non-existent customer. This breaches the
referential constraint that every foreign key (CustABN
in
the INVOICE
table) must have value in the primary table (CustABN
in the CUSTOMER
table)
7. Delete the customer Mellow Yellow Lighting. What happens?
Again if you have set up your tables correctly you will not be
allowed to. Once again you cannot deleting a parent record (Mellow
Yellow) while related records exist in the child table (invoices for
Mellow Yellow in the INVOICE
table)
Try these
These activities relate to the PowerPoint Presentation “Create Tables” refer to “get your hands dirty”
1. Delete 'Genna Bear' from Customer
DELETE
FROM customer
WHERE custname=‘Genna Bear’
2. Add the following invoice line
LineNo | InvoiceNo | ItemNo | Quantity |
---|---|---|---|
1000 | 100 | 8 | 7 |
INSERT INTO InvoiceItem (lineno, invoiceno, itemno, quantity)
VALUES (1000, 100, 8, 7)