tags:

views:

56

answers:

1

Identity(1,1) equalent function in MySql?

A: 

The equivalent of IDENTITY(1,1) for MySQL is AUTO_INCREMENT. Consider the following example:

CREATE TABLE users (
   user_id  int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   name     varchar(100),
   age      int
);

INSERT INTO users (name, age) VALUES ('Joe', 26);
INSERT INTO users (name, age) VALUES ('Peter', 30);
INSERT INTO users (name, age) VALUES ('Steve', 45);
INSERT INTO users (name, age) VALUES ('Paul', 33);

Result:

SELECT * FROM users;
+---------+-------+------+
| user_id | name  | age  |
+---------+-------+------+
|       1 | Joe   |   26 |
|       2 | Peter |   30 |
|       3 | Steve |   45 |
|       4 | Paul  |   33 |
+---------+-------+------+
4 rows in set (0.00 sec)

Further reading:

Daniel Vassallo