The trailing semi-colon, or lack thereof
The first thing about MySQL you
learn is that every line ends with a semi-colon (;). Well…there are
(at least) two exceptions.
In the section PHP submits data to MySQL I
pointed out that when a MySQL line is part of a PHP line, the semi-colon at the
end of the MySQL line of omitted. For example:
mysql_query ("INSERT INTO tablename (first_name, last_name)
VALUES ('$first_name', '$last_name')
");
This is done because PHP lines also end with a
semi-colon, so an extra semi-colon might confuse the PHP parser. You leave off
the semi-colon, and PHP automatically puts it back in for you.
As I wrote, the weird part is that SELECT and
INSERT will work with or without the extra semi-colon, but
UPDATE won't work. SELECT and INSERT are
the first MySQL functions you use, so you're happily coding with both
semi-colons, and then when you want to UPDATE a record everything
stops working. I haven't experimented with DELETE, which is the
fourth SQL command.
The other time you don't use a semi-colon is when you want to see all the
fields (what SQL calls "columns") displayed vertically down your monitor,
instead of horizontally across your monitor. With a terminal emulator (at least
with my old terminal emulator) you have a choice of 80 or 132 columns (of
characters), but you can't scroll over to see stuff farther to the right. So you
end the SQL line with \G instead:
SELECT * FROM PENPALS
WHERE USER_ID = 1\G