Du kan få next
auto-öknings-id genom att använda
SHOW TABLE STATUS FROM tablename LIKE Auto_increment
/*or*/
SELECT `auto_increment` FROM `INFORMATION_SCHEMA.TABLES` WHERE table_name = 'tablename'
Detta ger dig nästa auto_increment-värde.
Gör sedan en trigger före infogning:
DELIMITER $$
CREATE TRIGGER bi_table1_each BEFORE INSERT ON table1 FOR EACH ROW
BEGIN
DECLARE next_id integer;
SELECT `auto-increment` FROM `INFORMATION_SCHEMA.TABLES` INTO Next_id
WHERE TABLE_NAME = 'table1' LIMIT 1;
SET new.sortcolumn = next_id;
END $$
DELIMITER ;
Länkar
http://dev.mysql .com/doc/refman/5.1/en/show-table-status.html
http://dev.mysql.com /doc/refman/5.0/en/tables-table.html
http://dev.mysql.com/doc /refman/5.0/en/triggers.html
http://dev.mysql.com /doc/refman/5.0/en/create-trigger.html