I have used on duplicate key in my one project. But when all rows are already there and no data to update then also it increments auto_increment counter. So, previous auto_increment = 5 and after execution of on duplicate key query it automatically increments auto_increment counter = 6. So, my entries in table becomes,
id
__
1
2
3
4
6
There is one solution for "innodb_autoinc_lock_mode" in my.ini but without changing and settings in my.ini, is there any other way I can manage autoincrement using php code only?
id name email
1 name1 name1#mail.com
3 name3 name3#mail.com
I have this table. where id is primarykey and email is uniqueId. Now where i run on duplicate key update with values(name1,name1#mail.com), so,this query will not update or create new entry because it's already existed. but auto_increment counter becomes 3. so, for next insert operation new id is 3.
If you want to use php code to manage your ids then don't use auto increment at all.
Set the id manually by incrementing the max id value (obtained by using select max()...).
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.
When I delete the last row in a MySQL table and then insert a new one, why is the id still incremented as though the first weren't deleted? Is there a way to prevent this from happening so it can be as though the original row weren't there? Here's an example
Teachers
id first_name
0 joe
1 mike
2 jim
If I delete "jim" then insert "bob" then bob's id will be 3 and there will be no entry with an id of 2.
Because your id column was configured as autoincremental, and can't have the same value, unless you specify this manually
Deleting any given row will not change the current auto increment value.
You can view the current value of the auto increment variable for the table by running
SHOW TABLE STATUS LIKE 'mytable';
And you can change the auto increment value by running
ALTER TABLE 'mytable' AUTO_INCREMENT = '1234';
you may also use ALTER TABLE tablename AUTO_INCREMENT = 2 if after deleting the row and before inserting the new one, in order to make the id incrementation start at 2
you need to execute this command after every delete operation , this will reset your auto_increment to 1 witch will be set automatically the the last available id ( 2 in your example )
ALTER TABLE table_name AUTO_INCREMENT = 1
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;
Hi all is there any SQL function to reset all auto_increment ids?
I mean , i have this situation (id =1 , id2 = 2, id3 =3)
once i delete id i would like to be returned this situation (id2 = 1, id3 = 2) and so on.
Need i a script to do that?
You can reset the AUTO_INCREMENT for the whole table:
ALTER TABLE table AUTO_INCREMENT=1;
But you cannot really go back to fill in values in individual rows that have been deleted. The auto_increment values are intended to be unique, but aren't guaranteed to remain sequential if deletions happen or new rows are inserted with an explicit value set for the auto_increment column.
If you needed to modify them to be sequential again, it would have to be done in code, probably looping over all rows and performing UPDATE statements individually.
Id is Id which means it must never change! At least in good database design. Id is something you are born with and you die with.