Wednesday, 1 October 2014

Retrieve automatically generated keys

In MySQL, AUTO_INCREMENT attribute can be used to generate a unique identity for new rows.

Example
CREATE TABLE employee (id int NOT NULL AUTO_INCREMENT, name varchar(30), PRIMARY KEY(id));

INSERT INTO employee (name) VALUES
('Krishna'),('Arjun'),('Abdul'),
('Adam'),('Rama'),('Ptr');

SELECT * FROM employee;

returns
1 Krishna
2 Arjun
3 Abdul
4 Adam
5 Rams
6 Ptr

Statement interface provides 'getGeneratedKeys()' method to get the auto-generated key(s) generated by the execution of this Statement object.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment