SQL Overview
SQL, or Structured Query Language, is not really a language. It does not follow all the rules of the typical computer programming language. There are no constructs in SQL to allow decision branches. You can't go to a specific piece of code or execute that code if a decision has been made.
On the other hand, SQL is ideal for getting data out of a database using complicated rules. If you team SQL with other languages such as PHP or C/C++, the combination is very powerful. With this kind of combination, you can accomplish any programming task.
SQL is inherently simple. The most commonly used commands in SQL are CREATE, DROP, GRANT, INSERT, SELECT, and UPDATE. The modifiers for most of these commands are WHERE, AND, OR, LIKE, GROUP BY, or ORDER BY. A few helper functions such as, but not limited to, COUNT(), AVG(), MIN(), MAX(), and SUM() also exist.
After you have mastered this list of commands, you are ready to use SQL for most tasks. In almost all cases, by using some thought, complex statements can be boiled down to simpler statements. Very rarely do you require some of the more complex constructs.
MySQL Implements a Subset of SQL
Not all ANSI SQL features are implemented in MySQL. As of the 3.22 version of MySQL, the following features are left out:
- -- is the start of a comment only if followed by a whitespace. SQL allows it to be a comment in any case.
- In VARCHAR columns, trailing spaces are removed when stored in the database.
- In some cases CHAR columns are changed to VARCHAR columns without notification.
- Privileges stay after a table is deleted. They must be explicitly revoked using the REVOKE command. This is because privileges are stored in the mysql database tables.
- NULL and FALSE both evaluate to NULL.
- Sub-selects do not yet work in MySQL.
Note
Because sub-selects are not allowed, you cannot use the following syntax:
SELECT * FROM MainTable WHERE MyID IN (SELECT MyID FROM SecondTable).
You can rewrite the command to look like this:
SELECT MainTable.* FROM MainTable,SecondTable WHERE MainTable.MyID = SecondTable.MyID;
The following syntax is also not allowed:
SELECT * FROM MainTable WHERE MyID NOT IN (SELECT MyID FROM SecondTable).
Instead, use the following syntax:
SELECT MainTable.* FROM MainTable LEFT JOIN SecondTable ON MainTable.MyID = SecondTable.MyID WHERE SecondTable.MyID is NULL;
- MySQL 3.22 doesn't support transactions. However, it does do its work using "atomic operations." The authors of MySQL believe this provides "equal or better integrity," with better performance. MySQL version 3.23 is having transactions added to it.
- MySQL does not support stored procedures or triggers.
- The FOREIGN KEY statement mostly does nothing.
- Views are not supported.
- COMMIT and ROLLBACK are not supported.
Note
You can simulate COMMIT and ROLLBACK. A COMMIT on an operation is a way of saying all the conditions are good, store the data. To do this in a multiuser environment, you LOCK the tables to prevent any other user from changing them. You then make your changes and test all the conditions. If the conditions look good, you UNLOCK the tables, simulating a COMMIT.
During the process of testing and making changes, you must keep enough information to put the tables back to their original condition if the test for all the conditions fails. This simulates the ROLLBACK.
SQL Keywords
Now is a good time to review the list of SQL keywords. I also include all MySQL keywords. These keywords should not be used in any table or column name, even though MySQL will allow you to do this under certain conditions. Some keywords are reserved for use in future versions of ANSI SQL or MySQL.
If you decide to modify the IMP database, or create your own database, do not use any of these keywords as column or table names. MySQL will permit you to do this, but it is a very bad idea. It also prevents portability to other databases.
action |
add |
aggregate |
all |
alter |
after |
and |
as |
asc |
avg |
avg_row_length |
auto_increment |
between |
bigint |
bit |
binary |
blob |
bool |
both |
by |
cascade |
case |
char |
character |
change |
check |
checksum |
column |
columns |
comment |
constraint |
create |
cross |
current_date |
current_time |
current_timestamp |
data |
database |
databases |
date |
datetime |
day |
day_hour |
day_minute |
day_second |
dayofmonth |
dayofweek |
dayofyear |
dec |
decimal |
default |
delayed |
delay_key_write |
delete |
desc |
describe |
distinct |
distinctrow |
double |
drop |
end |
else |
escape |
escaped |
enclosed |
enum |
explain |
exists |
fields |
file |
first |
float |
float4 |
float8 |
flush |
foreign |
from |
for |
full |
function |
global |
grant |
grants |
group |
having |
heap |
high_priority |
hour |
hour_minute |
hour_second |
hosts |
identified |
ignore |
in |
index |
infile |
inner |
insert |
insert_id |
int |
integer |
interval |
int1 |
int2 |
int3 |
int4 |
int8 |
into |
if |
is |
isam |
join |
key |
keys |
kill |
last_insert_id |
leading |
left |
length |
like |
lines |
limit |
load |
local |
lock |
logs |
long |
longblob |
longtext |
low_priority |
max |
max_rows |
match |
mediumblob |
mediumtext |
mediumint |
middleint |
min_rows |
minute |
minute_second |
modify |
month |
monthname |
myisam |
natural |
numeric |
no |
not |
null |
on |
optimize |
option |
optionally |
or |
order |
outer |
outfile |
pack_keys |
partial |
password |
precision |
primary |
procedure |
process |
processlist |
privileges |
read |
real |
references |
reload |
regexp |
rename |
replace |
restrict |
returns |
revoke |
rlike |
ruow |
rows |
second |
select |
set |
show |
shutdown |
smallint |
soname |
sql_big_tables |
sql_big_selects |
sql_low_priority_updates |
sql_log_off |
sql_log_update |
sql_select_limit |
sql_small_result |
sql_big_result |
sql_warnings |
straight_join |
starting |
status |
string |
table |
tables |
temporary |
terminated |
text |
then |
time |
timestamp |
tinyblob |
tinytext |
tinyint |
trailing |
to |
type |
use |
using |
unique |
unlock |
unsigned |
update |
usage |
values |
varchar |
variables |
varying |
varbinary |
with |
write |
when |
where |
year |
year_month |
zerofill |
Typical SQL Statements
Let's examine a few SQL statements. I will assume that you need to create a demographic information table to store the information that advertisers most want to see. The potential advertisers have told you they want to see the age, income level, cost of housing, and type of automobile for our customer pool.
We will use a fictitious table called demographics to store this information. This table has a list of incomes, ages, automobile types, and housing costs for each of our customers. We have decided not to identify the particular customer in this database. The advertisers will be presented with composite information not targeted to an individual.
First, the demographics table will be created. Then, the table will have a few items added to it. Finally, we will pretend the table is full, and do several data retrievals on it. We will use MySQL features where possible. You will see how many common problems that arise during the use of MySQL are handled.
In creating the table, we want to add columns named income, age, auto, and housing. The age column is a TINYINT, because we don't need to register an age greater than 127. The other two columns are dollars and cents, and we will make them DECIMAL columns. The auto column will be a text column. I will make a few mistakes along the way so you can see what happens.
First, we create the demographics database. Then we create the demographics table. Note that the database and table have the same name.
mysql> create database demographics; Query OK, 1 row affected (0.00 sec) mysql> use demographics; Database changed mysql> create table demographics(DECIMAL(9,2) income, TINYINT age, mysql>DECIMAL(10,2) house_price, TEXT auto_type); ERROR 1064: You have an error in your SQL syntax near 'DECIMAL(9,2) income, TINYINT age, DECIMAL(10,2) house_price, TEXT auto_type)' at line 1 mysql> create table demographics(income DECIMAL(9,2), age TINYINT, mysql>house_price DECIMAL(10,2), auto_type TEXT ); Query OK, 0 rows affected (0.00 sec)
Notice that MySQL pointed to my error. I had reversed the order of the column named income and the description of the column. It printed the string beginning where it found the error.
Now it is time to add the user(s) who are allowed access to the database and give them the privileges necessary to run the database. The user myname will have full access, including the ability to set up other users. The user part will have partial access. We will use the SQL GRANT command. Notice that when the entire table_priv table is dumped, a wrap-around of information occurs on an 80-column screen. Listing 3.4 is a little messy, but decipherable.
Listing 3.4 Providing Access to Demographics Databaseresume
mysql> use mysql Database changed mysql> GRANT ALL ON demographics.* TO myname@localhost IDENTIFIED BY 'mypass' -> WITH GRANT OPTION; Query OK, 0 rows affected (0.21 sec) mysql> select host,user,password from user; +-----------+--------+------------------+ | host | user | password | +-----------+--------+------------------+ | localhost | root | 6f8c114b58f2ce9e | | winbook | root | 6f8c114b58f2ce9e | | localhost | | | | winbook | | | | localhost | impmgr | 5567401602cd5ddd | | localhost | myname | 6f8c114b58f2ce9e | +-----------+--------+------------------+ 6 rows in set (0.00 sec) Query OK, 0 rows affected (0.08 sec) mysql> select * from tables_priv; +------+--------------+--------+--------------+---------------- +----------------+--------------------------------------------- -------------------------+-------------+ | Host | Db | User | Table_name | Grantor | Timestamp | Table_priv | Column_priv | +------+--------------+--------+--------------+---------------- +----------------+--------------------------------------------- -------------------------+-------------+ | % | demographics | myname | demographics | root@localhost | 20000325085822 | Select,Insert,Update,Delete,Create,Drop,Grant ,References,Index,Alter | | +------+--------------+--------+--------------+---------------- ¬+----------------+---------------------------------------------- ¬------------------------+-------------+ 1 row in set (0.30 sec)
Notice that the output from the mysql program wraps. This is the worst part about using the command-line program. The only solutions that I like are to use small character fonts and make a very wide xterm window, or to do small selects that only show part of the database at a time.
At this point, quit mysql. As the superuser, in the same terminal window, use the adduser command to add the user myname. You can give this user any password you choose. While you are at it, add the user part. Then use the su -l command to assume the identity of the user myname.
Note
You are not required to create the users as described in the previous paragraph to log in to the MySQL system as those users. I am showing you how MySQL uses the authentication system of the host operating system, and assumes that user is the one logging into MySQL.
MySQL picks up the current logged in user's context when determining what access to give, when using tools such as mysqladmin or mysql. This can cause you problems if you tend to use the database as one user, and connect to it remotely as a different user. You might not understand what is going wrong when you connect to the database remotely. The easiest way to discover what is wrong is to log in to the system as that user and use the database tools at their default settings.
Let's work through the following example for user myname. First, we will look at the tables and insert some values. Then we will insert a user-called part with only SELECT, INSERT, and DELETE privileges. Finally, we will add a single table called test to the demographics database.
Then the user part will modify the demographics database. The user part will also try to modify the test table we have created (see Listing 3.5).
Listing 3.5 Attempting to Modify Table Demographics Without Enough Permission
[root@winbook root]# su -l myname [myname@winbook myname]$ mysql ERROR 1045: Access denied for user: 'myname@localhost' (Using password: NO) [myname@winbook myname]$ mysql -pmypass Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 to server version: 3.22.32 Type 'help' for help. mysql> use demographics; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +------------------------+ | Tables in demographics | +------------------------+ | demographics | +------------------------+ 1 row in set (0.00 sec) mysql> show columns from demographics -> ; +-------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------+------+-----+---------+-------+ | income | decimal(9,2) | YES | | NULL | | | age | tinyint(4) | YES | | NULL | | | house_price | decimal(10,2) | YES | | NULL | | | auto_type | text | YES | | NULL | | +-------------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> insert into demographics ( income, age) -> values ( -> 32756.32, 42 ); Query OK, 1 row affected (0.15 sec) mysql> select * from demographics -> ; +----------+------+-------------+-----------+ | income | age | house_price | auto_type | +----------+------+-------------+-----------+ | 32756.32 | 42 | NULL | NULL | +----------+------+-------------+-----------+ 1 row in set (0.00 sec) mysql> GRANT SELECT,INSERT,UPDATE ON demographics mysql>TO part@"%" IDENTIFIED BY 'mypass'; ERROR 1044: Access denied for user: 'myname@localhost' to database 'mysql'
Note
The error that user myname received when trying to assign a password to user part shows an interesting side effect of the MySQL security system. Because user myname was not given global access to all databases by the root user, no password can be assigned to user part.
With no password assigned, user part is allowed to access the database from anywhere with no password. This is a very undesirable side effect. Unless you are dealing with a totally open, noncritical database, you should never give a user GRANT options on a single database.
To allow user myname to assign passwords when granting access to a database, the user must be given global access to the mysql database. This allows modification of privileges for all databases. This side effect might not be what you intended, so be careful.
The GRANT command to give global access would look like this:
mysql> GRANT ALL ON *.* TO myname@localhost IDENTIFIED BY 'mypass' -> WITH GRANT OPTION;
There should only be one database system superuser. Be very careful to whom you give superuser privileges.
mysql> GRANT SELECT,INSERT,UPDATE ON demographics TO part@"%"; Query OK, 0 rows affected (0.03 sec) mysql> GRANT SELECT,INSERT,UPDATE ON mysql>demographics.demographics TO part@localhost;
Whenever you GRANT privileges to a user from anywhere (represented by username@"%"), you must also GRANT privileges to the same user @localhost (represented by username@localhost) if you want to grant command-line access privileges. This is because of the anonymous user that is in the mysql user database.
If you don't create a username@localhost entry, the default user's privileges will apply when the user is using mysql from the command line. This will prevent use of the database as you intended.
mysql> delete from demographics;
Output
Query OK, 0 rows affected (0.08 sec) mysql> select * from demographics; Empty set (0.00 sec) mysql> insert into demographics ( income, age) -> values ( -> 32756.32, 42 ); Query OK, 1 row affected (0.15 sec) mysql> CREATE TABLE test (TEST INTEGER); Query OK, 0 rows affected (0.01 sec) mysql> show tables; +------------------------+ | Tables in demographics | +------------------------+ | demographics | | test | +------------------------+ 2 rows in set (0.01 sec) mysql>quit
When you use GRANT, your privilege changes take effect immediately. If you use a tool such as mysqladmin to add or modify a user, the database privileges must be flushed (or reloaded) by the superuser. An example of reloading the privilege table is shown in Listing 3.6. Note that the mysqladmin flush-priv command is not necessary in our case.
Listing 3.6 Using the Demographics Database
[root@winbook /root]# mysqladmin -pmypass flush-priv [part@winbook part]$ mysql demographics; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27 to server version: 3.22.32 Type 'help' for help. mysql> select * from demographics -> ; +----------+------+-------------+-----------+ | income | age | house_price | auto_type | +----------+------+-------------+-----------+ | 32756.32 | 42 | NULL | NULL | +----------+------+-------------+-----------+ 1 row in set (0.01 sec) mysql> show tables; +------------------------+ | Tables in demographics | +------------------------+ | demographics | +------------------------+ 1 row in set (0.00 sec)
The user part cannot even see the database test. This hiding of information is good to know about from a database design point of view. You must be careful to ensure that tables visible to a user in a database do not require information from hidden tables. If you do this, errors will occur. Now let's try to access the test table as user part.
mysql> select * from test; ERROR 1142: select command denied to user: 'part@localhost' for table 'test'
To round out the demonstration of SQL statements, let's add a few rows to the demographics database, create an index, and print out some sorted information (see Listing 3.7). We will have to be user myname to do this.
Listing 3.7 Inserting and Retrieving Demographics Table Data
mysql> insert into demographics (income, age) values (11111.11, 22); Query OK, 1 row affected (0.00 sec) mysql> insert into demographics (income, age) values (21111.11, 25); Query OK, 1 row affected (0.00 sec) mysql> insert into demographics (income, age) values (31111.11, 35); Query OK, 1 row affected (0.00 sec) mysql> insert into demographics (income, age) values (41111.11, 45); Query OK, 1 row affected (0.00 sec) mysql> select * from demographics where income < 30000; +----------+------+-------------+-----------+ | income | age | house_price | auto_type | +----------+------+-------------+-----------+ | 11111.11 | 22 | NULL | NULL | | 21111.11 | 25 | NULL | NULL | +----------+------+-------------+-----------+ 2 rows in set (0.03 sec) mysql> select * from demographics where income < 30000 and age > 22; +----------+------+-------------+-----------+ | income | age | house_price | auto_type | +----------+------+-------------+-----------+ | 21111.11 | 25 | NULL | NULL | +----------+------+-------------+-----------+ 1 row in set (0.01 sec) mysql> update demographics set house_price=75000+age where income > 30000; Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> select * from demographics; +----------+------+-------------+-----------+ | income | age | house_price | auto_type | +----------+------+-------------+-----------+ | 32756.32 | 42 | 75042.00 | NULL | | 11111.11 | 22 | NULL | NULL | | 21111.11 | 25 | NULL | NULL | | 31111.11 | 35 | 75035.00 | NULL | | 41111.11 | 45 | 75045.00 | NULL | +----------+------+-------------+-----------+ 5 rows in set (0.00 sec)
Note
A very powerful feature of MySQL is the capability to do math in the SET or WHERE clauses. In the updates example shown, the database engine found the row we requested, pulled the information from that row's age column, and added it to the number we supplied for the house price.
mysql> update demographics set house_price=100000+age where income < 30000; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> update demographics set auto_type='miata' where age > 40; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> update demographics set auto_type='ford' where age < 40; Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> update demographics set auto_type='chevy' where age < 25; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from demographics where income < 40000 ; +----------+------+-------------+-----------+ | income | age | house_price | auto_type | +----------+------+-------------+-----------+ | 32756.32 | 42 | 75042.00 | miata | | 11111.11 | 22 | 100022.00 | chevy | | 21111.11 | 25 | 100025.00 | ford | | 31111.11 | 35 | 75035.00 | ford | +----------+------+-------------+-----------+ 4 rows in set (0.00 sec)
The previous SELECT statement produced a listing that had no ordering to it whatsoever. This is the natural order of the database. If we want the information to be ordered, we must impose order on it using the ORDER BY statement, as shown by Listing 3.8.
Note
The natural order of a database is the order in which the data was entered into the database, combined with deletions and additions to the database. It is not a predictable order, and cannot be used in any predictable fashion. It does provide the fastest retrieval of data in most database systems, but even that is not guaranteed.
Listing 3.8 Using an Order-By Clause
mysql> select * from demographics where income < 40000 order by income; +----------+------+-------------+-----------+ | income | age | house_price | auto_type | +----------+------+-------------+-----------+ | 11111.11 | 22 | 100022.00 | chevy | | 21111.11 | 25 | 100025.00 | ford | | 31111.11 | 35 | 75035.00 | ford | | 32756.32 | 42 | 75042.00 | miata | +----------+------+-------------+-----------+ 4 rows in set (0.00 sec)
In the following command, note that the initial index creation failed. Indexes cannot be created on columns that can contain null values. We must modify the index column to make sure it cannot contain null values. To do this, the ALTER TABLE command must be used. Following the application of this command, the index can be created.
mysql> create index age_index on demographics (age); ERROR 1121: Column 'age' is used with UNIQUE or INDEX but is not defined as NOT NULL mysql> alter table demographics modify age TINYINT NOT NULL; Query OK, 5 rows affected (0.02 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> create index age_index on demographics (age); Query OK, 5 rows affected (0.00 sec) Records: 5 Duplicates: 0 Warnings: 0
The information we have covered gives you a brief overview of SQL. It is by no means an exhaustive example of use.