Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Data Definition Language

Data Definition Language is the portion of SQL used for defining the structure of the database and creating objects within the database. DDL uses the following three different SQL commands to work with objects in the database:

These commands are used to work with several types of objects in the database, including tables, indexes, and views.

Tables

Tables are the most commonly used objects in relational databases. They hold the actual data in rows (records) and columns (fields). In many user applications, the tables that you need have already been created for you, whereas other situations require that you create your own tables.

SQL Data Types

Tables are constructed of columns that contain several types of data, so before moving on to the specifics of creating a table, let's look at the available data types for columns.

The SQL data types that you will look at here are grouped according to ODBC SQL conformance levels, although most implementations don't adhere strictly to the conformance-level specifications. Most drivers will support the minimum conformance level, but you should check your system's documentation to see if any others are supported.

The following types are supported by SQL at the minimum conformance level:

In addition, the extended conformance level specifies the following types, although support for these can vary widely between different databases:

Procedures

Procedures are extremely useful functions that are stored inside a database. The general format for a procedure is:

CREATE PROC MyProcedure [optional variable list] AS
    SQL statements go here

For example, to create a routine that returns all the employees from a department, you would write the following MS SQL SERVER procedure:

CREATE PROC GetDepartment @department varchar (10) AS
    SELECT EmpId, EmpName, Salary
    FROM Employee
    WHERE Dept = @department

Procedures can perform a series of any Data Manipulation Language commands. In addition to standard SQL, procedures support decision construct equivalent to an if or switch statement, and looping constructs equivalent to a while statement. Procedures have several benefits over SQL code:

Procedures are executed using either the CALL or EXECUTE command, depending on the database. CALL and EXECUTE are Data Manipulation Language commands that allow the user to execute the procedure. The following command calls the GetDepartment procedure and passes it a string for the MIS department:

CALL GetDepartment 'MIS'

In addition, many database, such as Sybase and Microsoft SQL Server, allow execution with or without the EXECUTE statement:

EXECUTE GetDepartment 'MIS'

GetDepartment 'MIS'
						

Triggers

Triggers are special procedures that are executed automatically if an event occurs in the database. For example, if you want an audit trail to automatically generate an audit trail record every time payroll is updated, you could do this by using an MS SQL Server trigger:

CREATE TRIGGER auditPayroll
ON Employee
FOR Update
AS
INSERT INTO auditTable /* Audit table sets the date automatically */
    (EmpId,
    EmpName,
    Salary,
    Dept)
SELECT
    (EmpId,
    EmpName,
    Salary,
    Dept)
FROM deleted

In this code, deleted is a reserved word in MS SQL Server that indicates the record that existed before the SQL statement. Conversely, the inserted keyword is used for the record that exists after the SQL statement. Most databases allow you to capture both previous and new information.

This audit table example shows how useful triggers can be. Without any additional collaborative programming effort, every database call made to change the contents of a table is tracked. This information can be viewed by personnel to reduce fraudulent behavior, or can be used by system administrators to back up data on a row-by-row basis for important tables.

Share ThisShare This

Informit Network