I was looking around for a quick way to remove an auto increment from the definition of a primary key. As best I can tell the only way to do it is w/ an alter table or dumping all the data into a new schema sans auto_increment.
Just for fun I tried the following to see if it would work.
- Create table test1 (innodb) w/ an auto increment PK
- Insert a couple rows w/o specifying PK values, letting auto inc do its job
- Create table test2 like test1;
- Alter table test2 modify pk_col int(10) unsigned not null; — no auto_inc
- Shutdown mysql
- backup test1.frm; cp test2.frm test1.frm
- Restart mysql
- select * shows all rows as expected, w/ previously auto_inc created PKs 1,2 and 3
- Insert a row w/o a PK specified, gets created w/ PK value as 0 (the default value).
- Insert a row specifying PK of 5
- Shutdown mysql
- restore original auto_increment test1.frm
- Restart; show create table lists auto_inc
- Test insert a row w/o specifying PK, generated auto_inc value of 6 (+1 the highest value even though the last one created by virtue of auto_inc was 3
Seems like that works!
For curiosity I ran both .frms through od and diffed that output. All I got
9c9
< 0010020 000001 015000 002000 000400 003400 001400 000000 001400
---
> 0010020 000001 015000 002100 000400 003400 001400 000000 001400
45c45
< 0020760 002000 005003 000012 000001 015000 007400 000000 020403
---
> 0020760 002000 005003 000012 000001 015000 000100 000000 020403
Thoughts, comments?
Best Answer
I tried something similar just now
Here is MySQL for My PC
I will run this using MyISAM
REPAIR TABLE pamela;
(Rebuild pamela.MYI)SELECT COUNT(1) FROM pamela;
SHOW CREATE TABLE pamela\G
SELECT * FROM pamela;
SELECT * FROM pamela;
Let's see if these steps are kosher.
Here are Steps 1-4
Here is Step 6
Here are the rest of the Steps starting at Step 7
Dangerous game, isn't it ???
Guess what? Stuff like this is actually published in "High Performance MySQL : Optimization, Backups, Replication, and more", Pages 146-148 under the Subheading Speeding Up ALTER TABLE. Page 147 Paragraph 1 says:
I also had an earlier post when someone ask a similar question : Can I rename the values in a MySQL ENUM column in one query?
You got guts, @atxdba !!!