SQL is a semi-English-like language used to manipulate relational databases. It is based on an ANSI standard, though very few SQL implementations actually adhere to the standard.
SQL statements are mostly case insensitive these days. While most books and references use upper-case, these notes use lower-case throughout for readability, and because the likelihood of needing to deal with older databases which only understand upper-case is becoming increasingly slim.
The syntax given in these coursenotes is cut down for simplicity; for full information, consult your database system's documentation. The MySQL documentation is available on our system in /usr/doc/mysql-doc and /usr/doc/mysql-manual, or by pointing your web browser at http://training.netizen.com.au/.
SQL is case usually insensitive, apart from table and field names (which may or may not be case sensitive depending on what platform you're on -- on Unix they are usually case sensitive, on Windows they usually aren't).
String data can be delimited with either double or single quotes. Numerical data does not need to be delimited.
Wildcards may be used when searching for string data. A % (percent) sign is used to indicated multiple characters (much as an asterisk is used in DOS or Unix filename wildcards) while the underscore character (_) can be used to indicate a single character, similar to the ? under Unix or DOS.
The following comparison operators may be used:
Table 4-7. Comparison Operators
Operator | Meaning |
---|---|
= | Equality |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
<> | Inequality |
like | Wildcard matching |
In the following syntax examples, the term condition is used as shorthand for any expression which can be evaluated for truth, for instance 2 + 2 = 4 or name like "A%".
Conditions may be combined by using and and or; use round brackets to indicate precedence. For instance, name like "A%" or name like "B%" will find all records where the ``name'' field starts with A or B.
An SQL select statement is used to select certain rows from a table or tables. A select query will return as many rows as match the criteria.
select field1 [, field2, field3] from table1 [, table2] where condition order by field [desc] |
select id, name from customer; select id, name from customer order by name; select id, name from customer order by name desc; |
We can use a select statement to obtain data from multiple tables. This is referred to as a ``join''.
select * from customer, sales where customer.id = sales.customer_id |
An insert query is used to add data to the database, a row at a time.
![]() | The columns names are optional to make typing queries easier. This is fine for interactive use, however it is very bad practice to omit them in programs. Always specify column names in insert statements. |
insert into tablename (col_name1, col_name2, col_name3) values (value1, value2, value3); |
insert into stock_item (id, description, price, quantity) values (0, 'doodad', 9.95, 12); |
Note that since the id field is an auto_increment field in the Acme inventory database we've set up, we don't need to specify a value to go in there, and just use zero instead --- whatever we specify will be replaced with the auto-incremented value. Auto-increment fields of some kind are available in most database systems, and are very useful for creating unique ID numbers.
A delete query can be used to delete rows which match a given criteria.
delete from tablename where condition |
delete from stock_item where quantity = 0; |
The update query is used to change the values of certain fields in existing records.
update tablename set field1 = expression, field2 = expression where condition |
update stock_item set quantity = (quantity - 1) where id = 4; |
The create statement is used to create new tables in the database.
create table tablename ( column coltype options, column coltype options, ... primary key (colname) ) |
Data types include (but are not limited to):
Table 4-8. Some data types
INT | an integer number |
FLOAT | a floating point number |
CHAR(n) | character data of exactly n characters |
VARCHAR(n) | character data of up to n characters (field grows/shrinks to fit) |
BLOB | Binary Large OBject |
DATE | A date in YYYY-MM-DD format |
ENUM | enumerated string value (eg "Male" or "Female") |
Data types vary slightly between different database systems. The full range of MySQL data types is outlined in section 7.2 of the MySQL reference manual.
create table contactlist ( id int not null auto_increment, name varchar(30), phone varchar(30), primary key (id) ) |