mysql - 怎样為一個table加一個auto_increment的field而不是primary key
ringa_lee
ringa_lee 2017-04-17 15:59:20
0
1
597
ringa_lee
ringa_lee

ringa_lee

reply all(1)
阿神

1. Get the current maximum id, max(id)+1

INSERT INTO myTable (name,sort) 
  SELECT 'Ali' as name,max(id)+1 as sort FROM myTable;

2. Trigger
CREATE TRIGGER t_afterinsert_on_myTable
AFTER INSERT ON myTable
FOR EACH ROW
BEGIN

 update myTable set sort=id where id=new.id;

END;
Then when inserting, just insert the name value, INSERT INTO myTable (name) VALUES ('Ali');

Of course the first one is simpler. . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!