3 4
A data-definition query is a SQL-specific query that contains DDL statements. These statements allow you to create or alter objects in the database. Specifically, data-definition queries create, delete, or alter tables or create indexes in the current database. Each data-definition query consists of just one data-definition statement. Access supports the following statements:
Execute the CREATE TABLE statement when you want to create a new table. For example, the following statement creates a new table named TableOne, with two text fields, FirstName and LastName:
CREATE TABLE TableOne (FirstName TEXT, LastName TEXT);
Other Ways to Create Tables
In addition to using the CREATE TABLE statement, there are several other ways to create tables in Access, as follows:
You can add an index by specifying the UNIQUE statement in the following form:
CREATE TABLE TableOne (FirstName TEXT, LastName TEXT UNIQUE (FirstName, LastName));
You can specify a primary key for the table using a statement in the following form:
CREATE TABLE TableOne (LastName TEXT CONSTRAINT MyIndex PRIMARY KEY, FirstName TEXT);
tip
SQL's ALTER TABLE statement performs a number of tricks, from renaming a table, to adding and deleting fields, to moving fields. Table 11-2 lists the different tasks you can complete by using ALTER TABLE.
Table 11-2. ALTER TABLE tasks
Task | Query |
Rename table | ALTER TABLE table1 TO table2 |
Add a field | ALTER TABLEtable ADD field1 type [BEFORE field2] |
Delete a field | ALTER TABLE table DROPfield |
Move a field | ALTER TABLE table MOVE field1 [BEFORE field2] |
Rename a field | ALTER TABLE table RENAME field1 TO field2 |
tip
note
To alter the Employees table, follow these steps:
ALTER TABLE Employees ADD COLUMN PayRate CURRENCY;
Figure 11-24. The ALTER TABLE statement will add a new field to the Employees table.
SQL's DROP statement allows you to delete a table or an index. For example, the following statement deletes the index on the LastName field in the Employees table from the Northwind database:
DROP INDEX LastName ON Employees;
The following statement deletes the Employees table from the Northwind database:
DROP TABLE Employees;
Be sure to use the DROP TABLE statement sparingly and with caution, because you can't recover the table or its data.
tip
The last SQL data definition statement is the CREATE INDEX statement. As you might expect, this statement creates an index. In its simplest form, the statement adds an index, as follows:
CREATE INDEX index ON table (field);
The resulting index is based on a single field, allows duplicate values, and is not based on a primary key. The index can consist of more than one field: Simply list more than one field, and separate the fields with commas.
To avoid duplicates in an indexed field, add the UNIQUE reserved word in the following form:
CREATE UNIQUE INDEX index ON table (field);
The DISALLOW NULL reserved word will require an entry in the field. Simply tack the following qualifier on at the end of the statement:
CREATE INDEX index ON table (field) WITH DISALLOW NULL;
To designate the indexed field as a primary key, use the PRIMARY reserved word in the following form:
CREATE INDEX index ON table (field) WITH PRIMARY;
InsideOut
There's not much advantage to running a pass-through or data-definition query from the query window. You can just as quickly complete most of these tasks manually, unless you're very familiar with SQL. Most people will use these SQL statements in VBA code to programmatically perform these specific tasks.