The following information introduces SQL, describes basic SQL syntax, and contains examples of SQL statements. so that you can begin to use ColdFusion. For complete SQL information, see the SQL reference that ships with your database.
A query is a request to a database. The query can ask for information from the database, write new data to the database, update existing information in the database, or delete records from the database.
Structured Query Language (SQL) is an ANSI/ISO standard programming language for writing database queries. All databases supported by ColdFusion support SQL, and all ColdFusion tags that access a database let you pass SQL statements to the tag.
The most commonly used SQL statement in ColdFusion is the SELECT statement. The SELECT statement reads data from a database and returns it to ColdFusion. For example, the following SQL statement reads all the records from the employees table:
SELECT * FROM employees
You interpret this statement as "Select all rows from the table employees" where the wildcard symbol (*) corresponds to all columns.
In many cases, you do not want all rows from a table, but only a subset of rows. The next example returns all rows from the employees table, where the value of the DeptID column for the row is 3:
SELECT * FROM employees WHERE DeptID=3
You interpret this statement as "Select all rows from the table employees where the DeptID is 3".
SQL also lets you specify the table columns to return. For example, instead of returning all columns in the table, you can return a subset of columns:
SELECT LastName, FirstName FROM employees WHERE DeptID=3
You interpret this statement as "Select the columns FirstName and LastName from the table employees where the DeptID is 3".
In addition to with reading data from a table, you can write data to a table using the SQL INSERT statement. The following statement adds a new row to the employees table:
INSERT INTO employees(EmpID, LastName, Firstname) VALUES(51, 'Doe', 'John')
The following tables briefly describe the main SQL command elements.
A SQL statement always begins with a SQL verb. The following keywords identify commonly used SQL verbs:
Keyword |
Description |
---|---|
SELECT |
Retrieves the specified records. |
INSERT |
Adds a new row. |
UPDATE |
Changes values in the specified rows. |
DELETE |
Removes the specified rows. |
Use the following keywords to refine SQL statements:
Keyword |
Description |
---|---|
FROM |
Names the data tables for the operation. |
WHERE |
Sets one or more conditions for the operation. |
ORDER BY |
Sorts the result set in the specified order. |
GROUP BY |
Groups the result set by the specified select list items. |
The following basic operators specify conditions and perform logical and numeric functions:
Operator |
Description |
---|---|
AND |
Both conditions must be met |
OR |
At least one condition must be met |
NOT |
Exclude the condition following |
LIKE |
Matches with a pattern |
IN |
Matches with a list of values |
BETWEEN |
Matches with a range of values |
= |
Equal to |
<> |
Not equal to |
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
+ |
Addition |
- |
Subtraction |
/ |
Division |
* |
Multiplication |
Case sensitivity with databases
ColdFusion is a case-insensitive programming environment. Case insensitivity means the following statements are equivalent:
<cfset foo="bar"> <CFSET FOO="BAR"> <CfSet FOO="bar">
However, many databases, especially UNIX databases, are case-sensitive. Case sensitivity means that you must match exactly the case of all column and table names in SQL queries.
For example, the following queries are not equivalent in a case-sensitive database:
SELECT LastName FROM EMPLOYEES SELECT LASTNAME FROM employees
In a case-sensitive database, employees and EMPLOYEES are two different tables.
For information on how your database handles case, see the product documentation.
When writing SQL in ColdFusion, keep the following guidelines in mind: