Microsoft Access SQL
|
|
Microsoft Access 97/2000 can utilize Data Definition SQL. The process is a bit convoluted but it does work. Access has the limitation that you can only create one table per Data-Definition Query. I recommend using an external text editor to inhibit MS Access' love for parenthesis and other odd characters. Then copy and paste your pure SQL code directly into the Access SQL Query window. My recommended text editor is the shareware product TextPad. Be sure to download and install the SQL Syntax Definition Add-In to have color-coded SQL. Much of the following information was taken directly from MS Access' own help files. The Access help file item can be viewed by searching doing a search for data definition query. Use SQL data-definition queries to create, delete, or alter tables or create indexes in the current database.
Caution: If you convert a data-definition query to another type of query, such as a select query, you'll lose the SQL statement that you entered. |
CREATE TABLE StatementCreates a database table (entity set) with the specified structure. Syntax:CREATE TABLE tablename ( field1 dataType(size) fieldProperties MS Access SQL Key Words |
||||||||||||||
|
|
Example 1: Standard SQL Data Types that Access RecognizesCREATE TABLE CAR ( |
MS Access Data TypesAccess seems to handle the standard SQL data types used in the example 1 code. However, it is far better to use the data types specified in Access for an optimized table design. Listed in the following table are some of the data types supported by access. Example 2 illustrates the use of these Access specific data types in SQL in a similar problem as the previous example.
MS Access Supported Data Types |
| Item | Synonyms | Description |
|---|---|---|
| TEXT(w) | CHAR(w) VARCHAR(w) | Stores alphanumeric text with field width (w). Limited to 255 character maximum. Each character requires 1 byte storage. No difference between CHAR and VARCHAR. |
| MEMO | Memo data type supports up to 65,535 characters. No field width is specified as allocated dynamically. No Synonyms. | |
| INTEGER | INT | Stores 4-byte long integer (whole number) value with permissible range ±2 billion. |
| SMALLINT | Stores 2-byte integer (whole number) value with permissible range ±32,000. | |
| BYTE | Stores 1-byte integer (whole number) value with permissible range +127 to -128. | |
| SINGLE | Stores 4-byte floating point (real number) value with 6 significant digits accuracy. | |
| DOUBLE | NUMBER | Stores 8-byte floating point (real number) value with 13 significant digits accuracy. |
| CURRENCY | Stores 8-byte currency value with 15 significant digits accuracy. | |
| YESNO | Stores a Boolean true/false value. | |
| DATETIME | DATE | Stores Date and Time in this field. |
Example 2: Standard Access SQL Data Types (Do This)CREATE TABLE CAR ( |
MS Access -Table Relationships |
|
|
Defining relationships between two tables requires a foreign key specification. MS Access allows you to specify the foreign key in the data definition query using the REFERENCES keyword. This is best illustrated by creating a second table which specifies the car type details and then creating a relationship in the CAR table using a foreign key. The two CREATE TABLE statements must be stored in seperate data definition queries in Microsoft Access. |
The Relationship
|
Example 3: Statements must be stored in seperate queries
|