SQL (usually pronounced "SEQUEL") stands for Structured Query Language

The Structured Query Language (SQL) is, just what its name implies: a structured language for querying relational databases, which are sets of related information stored in two-dimensional (rows and columns) tables. Although many different databases do use their own proprietary language, such as Oracle's PL/SQL, the non-proprietary "standard SQL" is used in almost every database.

Interactive VS Embedded SQL: Interactive SQL is used to operate directly on a database to produce output for human consumption. Embedded SQL consists of SQL commands put inside of programs that are mostly written in some other language (such as COBAL or PASCAL).

The subdivisions of SQL:
  • Data Definition Language (DDL): consists of those commands that create the objects (Tables, Database Diagrams, Indexes, Views, Stored Procedures, Triggers and Full-Text Indexes) in the database.
  • Data Manipulation Language (DML): is a set of commands that determine which values are present in the tables at any given time.
  • Data Control Language (DCL): consists of features that determine whether a user is permitted to perform a particular action.
The various types of data: Not all the types of values that can occupy the fields of a table are logically the same. The most obvious distinction is between numbers and text.
  • Two ANSI number types, INTEGER (INT) and DECIMAL (DEC) an INTEGER can be thought of as a DECIMAL that allows no digits to the right of the decimal point.
  • The type of text is CHAR (CHARACTER), which refers to a string of text. A field of type CHAR has a definite length, which is the maximum number of characters that can be entered into that field. Most implementations also have VARCHAR, which is a text string that can be of any length up to an implementations-defined maximum (typically 8,000 characters). CHAR and VARCHAR values are enclosed in single quotes such as ‘test’.

There are really only four commands & only a few basic rules, you need to know to perform simple database functions: they are SELECT, INSERT, UPDATE, and DELETE and they're self-explanatory.

Strings (any alphanumeric value) must be "wrapped" with tick marks ('). For instance, if I enter my favorite movie into a database, the string should look like: 'Star Wars' -- the database must identify this field as TEXT (or equivalent).

Integers cannot be "wrapped" with anything. So if I enter my favorite number into a database, the integer should look like: 12 -- the database must identify this field as NUMBER (or equivalent).

Date/Time values are a little trickier since it depends on which database you're working with. Usually, however, the date/time values should be "wrapped" with pound signs (#). For instance, if I enter my date of birth, the date/time should look like: #April 12, 1975# -- the database must identify this field as DATE/TIME (or equivalent). Other databases let you "wrap" these values with tick marks or they may not require any such "wrappers."

Commas follow a different rule in SQL compared to English grammar: commas are not placed within the tick marks. For example, if I enter three of my favorite fruits into a database, the command should look something like: 'oranges', 'grapes', 'bananas'. In English grammar, however, the list is typed: "oranges," "grapes," and "bananas." Pay close attention to where the punctuations are placed.

Select
Basic SELECT statements use only two commands: SELECT and WHERE. The asterisk (*) tells the SELECT statement to grab all the fields in the database table and the WHERE command identifies which table in the database to look in.

SELECT  *
FROM myTable

Advance SELECT statements may employ conditional statements using the WHERE command. These conditional statements include relational operators, logical operators, and other complex conditionals.

Relational Operators
Operator: Symbol:
Equal: =
Not equal: <> or !=
Less than: <
Greater than: >
Less than or equal: <=
Greater than or equal: >=

Logical Operators
Precedence: Operator: Example:
high NOT Age NOT BETWEEN 18 AND 21
medium AND Hair = 'black' AND Eyes = 'brown'
low OR Hair = 'red' OR Hair = 'blonde'

Complex Conditionals
Command: Example:
IN Grade IN ( 'A', 'B' )

instead of [Grade = 'A' AND Grade = 'B']

BETWEEN Age BETWEEN 18 AND 21

instead of [Age >= 18 AND Age <= 21]

LIKE Name LIKE 'J%'

to select all names beginning with "J"


Example:
SELECT   field1, field2
FROM myTable
WHERE ( field3 = 'string3' AND field4 = 'string4' ) OR
( field5 >= integer5 AND field6 = 'string6' )

Joins provide good access to information stored in different table. If, for example, you have two distinct tables -- one to store background information (name, birthday, eye color) called "Employee" and another to store variable information (company, position, salary) called "Job" -- but provide one display with these datafields, you need to join the two tables together. Both tables should have an Employee_ID field to tie the data together.

SELECT   First_Name, Last_Name, Birthday, Position, Salary
FROM Employee, Job
WHERE Employee.Employee_ID = Job.Employee_ID AND
Last_Name LIKE 'A%'
ORDER BY   Last_Name, First_Name ASC

This query gets pertinent information about all employees with a last names beginning with "A." Since both tables have the Employee_ID field, you must "tie" the two tables together. That's what's done with Employee.Employee_ID = Job.Employee_ID. The "ORDER BY" command sorts the information in ascending order (alphabetically) by name. Valid values for ORDER BY are ASC for ascending (default) and DESC for descending.

Aggregate functions provide simple, but useful processing with relatively little effort.

Aggregate Functions
Function: Example:
Count(*) Count(*)
SUM() SUM(Items_Sold)
AVG() AVG(July_Sales)
MAX() MAX(Fees)
MIN() MIN(Cost)

Example:
SELECT   Count(*) As Total
FROM Employees
WHERE First_Name LIKE '%and%'

The example counts how many employees have "and" somewhere in their first name. If Andy, John, Candice, and Madeline are in that table, the example above would return a variable named "Total" with a value of "2."

Insert
Basic INSERT statements use only two commands: "INSERT INTO" and VALUES.

INSERT INTO   myTable
VALUES ( 'string1', 'string2' )

Advance INSERT statements ...

INSERT INTO   myTable ( field1, field2 )
VALUES ( 'string1', 'string2' )
WHERE field3 = 'string3' AND
field4 = 'string4'

Update
Basic UPDATE statements use three commands: UPDATE, SET, and WHERE.

Example:
UPDATE   Job
SET Position = 'Manager'
WHERE Employee_ID = 12345678

Delete
Basic DELETE statements use only two commands: "DELETE FROM" and WHERE.

Example:
DELETE FROM   Employee
WHERE First_Name = 'John' AND
Last_Name = 'Smith'
Getting your quotes right in SQL for ASP

In Active Server Pages, all strings are enclosed in double quotes ("). For example, if you wanted to pull in all orders from the database, the SQL string would look like this:

strSQL = "SELECT * FROM orders"

orderID = 101 (Numbers)
strSQL = "SELECT * FROM orders WHERE order_id = " & orderID

orderStatus = "open" (Strings)
strSQL = "SELECT * FROM orders WHERE order_status = ' " & orderStatus & "' "

 
Copyright © 2002 by EzSofTech. All Rights Reserved.