I am getting the error
Error Code: 1062. Duplicate entry '0' for key 1
when inserting a new value to a table where the primary key is set to auto_increment. This column is set to int(11) data type and when you select the max value of the column it is 16000. (Note: I can manually add a new row in with a random high number fine, the issue is purely with auto_increment).
If I do this:
SHOW TABLE STATUS FROM database LIKE 'tablename'
The value of auto_increment is set to NULL.
Strangely enough I tried to replicate the table, with the plan to just copy all the existing data over and when I recreated the same table with a new name and there is no data in the table the auto_increment next value is also set to null.
This particular table has a lot of columns so I wonder if maybe I have hit on another limit of some sort?
I resolved the issue. For some reason the column was no longer set to auto-increment. It had worked fine for the previous 13,000 records so I am not sure why the flag had suddenly been removed.
Related
I have a php script that logs inputs from a form into a mysql database table. I'm looking for a way to insert this data untill 3 rows are created, after which it has to update the existing rows so that the first one updates to the new input, the second one to the former first input and the third one to the former second input.
Table:
CREATE TABLE IF NOT EXISTS inputlog (
id int(11) NOT NULL auto_increment,
userid int(11) NOT NULL default '0',
name text,
value text,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;`
For the first three rows i use:
insert into inputlog (userid,name,value) values('$userid','$name','$value')
After that is has to become:
update inputlog set value = '$value' where userid = '$userid' and name = '$name'
where it has to update all the successive rows.
How can i accomplish this?
Too long for comments, so...
Looks like you want to have only 3 rows in your table because you want the data to be sorted by the id. So id=1 will be the latest value, then id=2 and finally id=3.
In short, do not do that, the id field can be any value. Do not code for that. The danger is if you use the id in another table as a foreign key, you will loose referential integrity. What I propose is:
Add an timestamp column for each row.
Every time you insert a new value, set the timestamp column to NOW()
When selecting, sort on the timestamp and limit to 3 results
If you MUST have only 3 rows, you can then delete the row except for the 3 most recent timestamps.
But... if you must do that...
perform a SELECT with the first 2 lines
truncate the table (delete all rows)
insert the new line, then the 2 stored lines
You will then ahve your 3 rows in the order you want. But without seeing the entire reasoning for your application, my "spider sense" tells me you will hit a wall later on...
And check the comments for other things to worry about.
I have a table with 3000 entries.
I am randomly choosing 25 of those to be shifted to another table.
Once this is done, I want the IDs (which are assigned AUTO_INCREMENT and are primary key) in the original table to be in sequence, so that I could perform next iteration.
How should I do it?
We cannot change the auto_increment value to a value other than the current max value.Auto_Increment is a counter that keeps on incrementing whose value is used for the column for which it is defined.
The only other way to change it to a value just above the current max value is to manually alter the value of the AUTO_INCREMENT Counter to max(value) +1.
So I guess here you have to order the records in ascending by the existing Id after deleting the 25 records and manually set the id value of each record according to an incrementing variable and in the end set the AUTO_INCREMENT count value as the value of variable in the end +1.
Sample :
ALTER TABLE T
SET AUTO_INCREMENT=Some_value;
Though it is advised to leave the AUTO_INCREMENT alone and avoid all this if it's done just for the sake of making ids look great and there is no other probable reason.
I have a table with 5 rows. Every time a user enters data into a form, it is entered into the table. My first column is called id and holds the number of the post. What I want to do is get the value of id from the previous row, add one to it and set it as the value in the current post's id field. How do I do this?
Just set that field as primary key and auto-increment, it will automatically do this for you. You won't have to fetch the previous row and add that field value to next one.
The SQL query you need is:
SELECT max(id)
FROM tableName;
Set attribute auto increment for "ID" field in the table that contains 5 columns.
You can use sql query like
"INSERT INTO my_table (id auto_increment,primary key(id))";
then you can get...
and eachtime you need not worry to insert id ,it will automatically increments
I would not recomend doing this as it could lead to a race condition.
Change the table structure and set the id field to be the primary key and set it to auto increment. This way anytime a new row is added, it will auto-magically be assigned the next ID.
see this answer on details of how to set auto increment.
here is the query to alter your table and it will set your field or column as primary key and also auto increment it.
ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;
So i just truncated my table but now my primary key will not auto increment. Every time I add a 2nd item to the table, it comes back with 1062: Duplicate entry '0' for key 'PRIMARY'
I thought when you truncate a table it will just resets the auto increment back to 1.
I search around on what I could do but I have come across many people saying just truncate.
What I did
"TRUNCATE TABLE mytable;"
I must have done something wrong.
This bug not affects Mysql 5.0 and it is repeatable on 6.0 and 5.1.23 and 5.1BK.
Refer this link for proof
Following is readily available and effective workaround is to ALTER the autoinc value after the table is truncated.
Re-initialize the autoinc value right after truncation
alter table tablename AUTO_INCREMENT = n; /* set n as desired */
From MySQL 5.0.13 , the AUTO_INCREMENT counter is reset to zero by TRUNCATE TABLE, regardless of whether there is a foreign key constraint.
All you need to do is:
SET FOREIGN_KEY_CHECKS=0;
truncate your tables and change it back to
SET FOREIGN_KEY_CHECKS=1;
Once TRUNCATE is fired, the table handler does not remember the last used AUTO_INCREMENT value, but starts counting from the beginning.
This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.
User this Query it will work.
ALTER TABLE table_name CHANGE table_id table_id INT( 11 ) NOT NULL AUTO_INCREMENT
I'm creating a change log db that's the exact same as my active db except it has a changedate DATE field at the end.
The db is basically one primary key id, and about 50 other columns of various data types. The script I have in php is it tries to insert new ids and if it gets the error message for duplicate primary key, then it should get that row, insert it into my backup db with a curdate() call as the final date value, delete the entry from my first db, then insert the new entry.
I have all the other parts of the script finished except the part where I have to insert everything from the first table + an extra column for curdate(). Or if there's a better solution to my problem of inserting into a backup database when a duplicate primary key comes in when there's a fairly high amount of rows please share that.
You could do an INSERT INTO SELECT:
INSERT INTO `backupTable` SELECT *, NOW() FROM `originalTable` WHERE id = '$id';
You have to specify the ID for the entry you wish to copy to your backup db. You have also to be sure, that the IDis not already in your backup table. You can use REPLACE INTO to workaround this case.
REPLACE INTO `backupTable` SELECT *, NOW() FROM `originalTable` WHERE id = '$id';
basicly, you can create a TIMESTAMP column with CURRENT_TIMESTAMP as default value.
when you insert a row to that table, the current date/time will be automaticly inserted.
is that what you were looking for ?
BTW: i would recommend to kill the problem at it source and make sure a duplicate primary key will not be inserted to the datatable..
to do that, you can use the SELECT LAST_INSERT_ID();