MySQL - Using Sequences
A sequence is a set of integers 1, 2, 3, ... that are generated in order on a specific demand. Sequences are frequently used in the databases because many applications require each row in a table to contain a unique value and sequences provide an easy way to generate them.
This chapter describes how to use sequences in MySQL.
In MySQL, you can create a column that contains a sequence of numbers (1, 2, 3, and so on) by using the AUTO_INCREMENT attribute. The AUTO_INCREMENT attribute is used when you need to create a unique number to act as a primary key in a table.
The syntax to create a sequence (or use the AUTO_INCREMENT attribute) in MySQL is:
CREATE TABLE table_name
(
column1 datatype NOT NULL AUTO_INCREMENT,
column2 datatype [ NULL | NOT NULL ],
...
);
AUTO_INCREMENT :
The attribute to use when you want MySQL to assign a sequence of numbers automatically to a field (in essence, creating an autonumber field).
NULL or NOT NULL :
Each column should be defined as NULL or NOT NULL. If this parameter is omitted, the database assumes NULL as the default.
Note : You can use the LAST_INSERT_ID function to find last value assigned by the AUTO_INCREMENT field
Let's look at an example of how to use a sequence or the AUTO_INCREMENT attribute in MySQL.
CREATE TABLE contacts
( contact_id INT(11) NOT NULL AUTO_INCREMENT,
last_name VARCHAR(30) NOT NULL,
first_name VARCHAR(25),
birthday DATE,
CONSTRAINT contacts_pk PRIMARY KEY (contact_id)
);
This MySQL AUTO_INCREMENT example creates a table called contacts which has 4 columns and one primary key:
ALTER TABLE table_name AUTO_INCREMENT = start_value;
ALTER TABLE contacts AUTO_INCREMENT = 50;
This MySQL AUTO_INCREMENT example would change the next value in the AUTO_INCREMENT field (ie: next value in the sequence) to 50 for the contact_id field in the contacts table.
There may be a case when you have deleted many records from a table and you want to re-sequence all the records. This can be done by using a simple trick, but you should be very careful to do so if your table is having joins with the other table
If you determine that the resequencing of an AUTO_INCREMENT column is unavoidable, the way to do it is to drop the column from the table, then add it again.
mysql> ALTER TABLE insect DROP id;
mysql> ALTER TABLE insect
-> ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
-> ADD PRIMARY KEY (id);
Starting a Sequence at a Particular Value :
By default, MySQL will start sequence from 1, but you can specify any other number as well at the time of the table creation.
The following program is an example which shows how MySQL will start the sequence from 100.
CREATE TABLE insect
-> (
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT = 100,
-> PRIMARY KEY (id),
-> name VARCHAR(30) NOT NULL, # type of insect
-> date DATE NOT NULL, # date collected
-> origin VARCHAR(30) NOT NULL # where collected
);
Alternatively, you can create the table and then set the initial sequence value with the ALTER TABLE command.
MySQL - Grant_ Revoke Privilege
posted on 2019-11-26 23:15:04 - mysql Tutorials