Learning Mysql


The AUTO_INCREMENT Feature



Download 4.24 Mb.
View original pdf
Page189/366
Date04.08.2023
Size4.24 Mb.
#61806
1   ...   185   186   187   188   189   190   191   192   ...   366
Learning MySQL
The AUTO_INCREMENT Feature
MySQL’s proprietary
AUTO_INCREMENT
feature allows you to create a unique identifier fora row without running a
SELECT
query. Here’s how it works. Suppose you drop and recreate the artist table as follows:
mysql> DROP TABLE artist;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE artist (
-> artist_id SMALLINT(5) NOT NULL AUTO_INCREMENT,
-> artist_name CHAR) DEFAULT NULL,
-> PRIMARY KEY (artist_id)
-> );
Query OK, 0 rows affected (0.06 sec)
You can now insert rows, without providing an artist_id
:
mysql> INSERT INTO artist VALUES (NULL, "The Shamen");
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO artist VALUES (NULL, "Probot");
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO artist VALUES (NULL, "The Cult");
Query OK, 1 row affected (0.00 sec)
208 | Chapter 6:
Working with Database Structures

When you view the data in this table you can see that each artist has a meaningful artist_id
:
mysql> SELECT * FROM artist;
+-----------+-------------+
| artist_id | artist_name |
+-----------+-------------+
| 1 | The Shamen |
| 2 | Probot |
| 3 | The Cult |
+-----------+-------------+
3 rows inset sec)
Each time an artist is inserted, a unique artist_id is created for the new row.
Let’s consider how the new feature works. You can see that the artist_id column is declared as an integer with the clauses NOT NULL AUTO_INCREMENT
. The
AUTO_INCREMENT
keyword tells MySQL that when a value isn’t provided for this column, the value allocated should be one more than the maximum currently stored in the table. The
AUTO_INCREMENT
sequence begins at 1 for an empty table.
The NOT NULL
is required for
AUTO_INCREMENT
columns; when you insert
NULL
(or though this isn’t recommended, the MySQL server automatically finds the next available identifier and assigns it to the new row. You can manually insert negative values if the column was not defined as UNSIGNED however, for the next automatic increment,
MySQL will simply use the largest (most positive) value in the column, or start from if there are no positive values.
The
AUTO_INCREMENT
feature has the following requirements The column it is used on must be indexed The column that is it used on cannot have a
DEFAULT
value.
• There can be only one
AUTO_INCREMENT
column per table.
MySQL supports different table types we’ll learn more about these in Table Types”
in Chapter 7. When you’re using the default MyISAM table type, you can use the
AUTO_INCREMENT
feature on keys that comprise multiple columns. In our music database example, we could create the album table as follows:
mysql> CREATE TABLE album (

Download 4.24 Mb.

Share with your friends:
1   ...   185   186   187   188   189   190   191   192   ...   366




The database is protected by copyright ©ininet.org 2024
send message

    Main page