Showing posts with label verbose. Show all posts
Showing posts with label verbose. Show all posts

Friday, 13 January 2012

MySQL verbose Option

The verbose option tells MySQL to display SQL before running it. This is useful when using the source command to run SQL from a file. You can see what I mean in the example which follows. Here is a file containing a single SQL statement:
 
UNIX > cat count_star.sql
select count(*) from mysql.user;
UNIX >
 
When you run it from MySQL with the verbose (-v) option, the SQL appears before the output it produces:
 
UNIX > mysql -u root -p -v
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53444
Server version: 5.0.67 Source distribution
 
Reading history-file /usr/local/mysql/.mysql_history
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
mysql> source count_star.sql
--------------
select count(*) from mysql.user
--------------
 
+----------+
| count(*) |
+----------+
|       18 |
+----------+
1 row in set (0.00 sec)
 
mysql> exit
Writing history-file /usr/local/mysql/.mysql_history
Bye
UNIX >

MySQL auto_increment Clause

This post looks slightly different to earlier ones as I created it with the verbose option and ran the SQL from a file using the source command. MySQL allows you to increment column values automatically with the auto_increment clause. You can only auto increment one column per table and that column must be used as a key:
 
--------------
create table andrews_table(
id int(4) auto_increment,
location varchar(10))
--------------
 
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
--------------
create table andrews_table(
id int(4) auto_increment,
location varchar(10),
primary key(id))
--------------
 
Query OK, 0 rows affected (0.00 sec)
 
When you insert rows into the table, you do not need to supply values for the column which is automatically incremented. Note how three rows are being inserted simultaneously:
 
--------------
insert into andrews_table (location)
values ('Redhill'), ('Crawley'), ('Horsham')
--------------
 
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
 
By default, it looks as if the auto incremented column starts at 1 and goes up by 1 each time. I will look at this in more detail in future posts:
 
--------------
select * from andrews_table
--------------
 
+----+----------+
| id | location |
+----+----------+
|  1 | Redhill  |
|  2 | Crawley  |
|  3 | Horsham  |
+----+----------+
3 rows in set (0.01 sec)
 
MySQL allows you to look at the value automatically inserted into the last row. For a multi-row insert, this returns the value inserted into the first row created by that insert:
 
--------------
select last_insert_id()
--------------
 
+------------------+
| last_insert_id() |
+------------------+
|                1 |
+------------------+
1 row in set (0.00 sec)
 
--------------
insert into andrews_table (location)
values ('Croydon')
--------------
 
Query OK, 1 row affected (0.00 sec)
 
--------------
select * from andrews_table
--------------
 
+----+----------+
| id | location |
+----+----------+
|  1 | Redhill  |
|  2 | Crawley  |
|  3 | Horsham  |
|  4 | Croydon  |
+----+----------+
4 rows in set (0.00 sec)
 
--------------
select last_insert_id()
--------------
 
+------------------+
| last_insert_id() |
+------------------+
|                4 |
+------------------+
1 row in set (0.00 sec)
 
mysql>