![]() Capt. Horatio T.P. Webb | Parks -- FALL 2000 Last Updated 12PM 9/10/2006 |
SQL Syntax -- Contents of this page:
![]() | Used to initially define the structure of a table. (this is normally done ONCE). The columns of the table are named and their data type specified. | ||||||||||||||||||||||||||||||||||||||||||||||
[ name_of_database. ] | |||||||||||||||||||||||||||||||||||||||||||||||
[ name_of_owner. ] | |||||||||||||||||||||||||||||||||||||||||||||||
name_of_table | |||||||||||||||||||||||||||||||||||||||||||||||
( | begin the list of column definitions | ||||||||||||||||||||||||||||||||||||||||||||||
column_name_1 datatype |
| ||||||||||||||||||||||||||||||||||||||||||||||
[ NULL | NOT NULL] | NULL allows for inserts which are null (i.e., empty). If NOT NULL you must enter a value during inserts | ||||||||||||||||||||||||||||||||||||||||||||||
[ IDENTITY [ (starting_value , increment ) ] | This is a property of a datatype. The data type must be either: int, smallint, tinyint,decimal(p,0), or numeric(p,0) and must not be NULL. The server generates an integer value (beginning at starting_value) for each row which is added to the table. The next row which is added gets an integer value equal to the previous identity value plus increment. | ||||||||||||||||||||||||||||||||||||||||||||||
, | end of first column specification | ||||||||||||||||||||||||||||||||||||||||||||||
[column_name_2 datatype ... , | next column | ||||||||||||||||||||||||||||||||||||||||||||||
[column_name_3 datatype ... , | next column | ||||||||||||||||||||||||||||||||||||||||||||||
[column_name_4 datatype ... , | etc. | ||||||||||||||||||||||||||||||||||||||||||||||
) | end of column specification | ||||||||||||||||||||||||||||||||||||||||||||||
PRIMARY KEY (first_column [,second_column, ... etc.] ) | Names the columns to be used as the primary key for the table | ||||||||||||||||||||||||||||||||||||||||||||||
General Ledger Table Creation Example:
The following VBScript code snipet (an Active Server Page) creates a table named glmaster with columns named:
The primary key is identified as:
The entire SQL command is stored in a string named create_string.
The string is then passed from the ActiveServerPage to the MS SQL Server database system for execution.
|
![]()
(name_of_column1, VALUES
(value_of_column1,
| Used to add rows to the table. The table must exist (i.e., created as above) before you can perform an INSERT.
Values for the primary key MUST be specified. Values for any other NOT NULL columns must be specified.
The order of the column names must match the order of the values. | |
INSERT Example The following VBScript code snipet (from an Active Server Page) demonstrates the use of the INSERT.
A string named Insert_String is constructed that names six columns to be assigned values in the tabvle named glmaster. Then five values are retrieved from an user's querystring data (i.e., the form) and are in turn added to the VALUES part of the string. The last column is assigned a value of zero. The string is then passed from the Active Server Page to MS SQL 6.5 for execution.
|
![]() CAUTION:This command removes the table references AND the contents of the table. This action is not recoverable
| ||
DROP TABLE Example
The following VBscript code drops a table named je
|
![]() | Used to retrieve rows from a table that conform to specific selection criteria. | |||||
* | gets all columns | |||||
ALL | same as * | |||||
( column_name1, | Name the of the columns to be retrieved. | |||||
FROM name_of_table | ||||||
[ WHERE [NOT] comparison1 |
| |||||
[OR | AND comparison2 ... ] | ||||||
[ WHERE column_name BETWEEN first_value AND second_value ] | May be used like a comparison | |||||
[ ORDER BY
column_name1 [ASC | DESC] [, column_name2 [ASC | DESC] [, column_name3 [ASC | DESC] etc. | ||||||
SELECT Example The following string named Select_String is used to retreive all columns (i.e., *) from a table named glmaster. Only those rows WHERE sub1 is 1000 or 2000 AND sub2 is 1000 are retreived. The recordset should be returned in ascending sub1 order.
The table glmaster is in the database (DSN) named gl001. |
![]()
name_of_column1 = value or expression, WHERE comparison | A comparison is required. Expressions are any valid arithmetic expression using column_names and constants (e.g., SET column_name1= column_name2/3.14159) | |
Update Example:
In the following VBScript snipet, an SQL UPDATE command is executed. In VBScript, a string named Update_String is created thta sets the value of the column named balance to a value from the user's querystring data (i.e., the value of balfield on a FORM). Only the record WHERE major, minor, sub1 and sub2 equal the values defined from the FORM is updated.
|
![]()
WHERE column_name= value | ||
DELETE Example
The following VBscript code removes a row from the table where the major=1000, the minor=1000, sub1=1000 and sub2=1000.
|
![]() or for multiple new columns
ADD (column_name1 datatype1, column_name2 datatype2 [...,column_namen datatypen]) | Adds one or more NEW columns to a table with the specified data type |
![]() or for multiple columns changes
MODIFY (column_name1 datatype1, column_name2 datatype2 [...,column_namen datatypen]) | Modifies existing column(s) data type |
![]()
| Removes an existing columns |
![]()
RENAME column_name old columnname TO new columnname
| Renames an existing column name |
![]()
| Renames an existing table |
ALTER should NOT be used without serious consideration of the impact that can result. Deleting primary key columns is prohibited; data type choices can create conflicts such a varchar to int, etc., etc.... Additionally, the specific database (e.g., MS SQL Server, Oracle, MYSQL, etc.) and version number may impact your decisions. Check your documentation for the rules, limitations and exlusions. Always RTFM.
|
![]() The cn.execute syntax allows a second parameter to be specified. This field returns the "number of rows affected" by the connection execution. The syntax is: cn.execute SQLstring, number-of-rows-affected For updates, inserts and deletes the number-of-rows-affected is likely known ahead of time. Typically, a cn.execute for an insert, update or delete should yield a number-of-rows-affected value of 1 if it works and a value of 0 if it doesn't. We can test this result directly by evaluating number-of-rows-affected after the execution. Based on its value we can take appropriate action.
| ||
Connection Execution Error Trap Example
The following VBscript code examines the result of an insert.
|