Let's say I have a MySQL table and a table has a row with id and it has auto_incremented. Let's say via MySQL query and PHP, I add a row. The first row has id of 1. Then I manually add a second row (via phpmyadmin) with the id of 2. If I do a third MySQL insert via PHP... what would the id be for the third row... 2 or 3?
Question is... does auto_increment take into account manual inputs?
does auto_increment take into account manual inputs?
Yes it does. But I hope you do not really type in the ID manually, right? :-) Just leave this field alone when inserting (manually or programatically), MySQL will take care of it for you.
MySQL does accept manual inputs, and it WILL try to set the value you offer. If the value does not exist, it gets inserted, else you get a duplicate key error.
Put a value when you want to decide a value yourself (for example,
you deleted a line, and now want the exact same line in the table).
Put NULL or leave the column out of the insert to let the database
use the auto-increment.
Just a hint: when your application is choosing the values to put for an autoincrement value, you are probably doing something wrong.
Related
I was just creating a new table using MySQL Query Browser, and noticed there's a tick under Auto Increment Column. How does that work?
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Everytime a NEW user registers on my site, I want their Customer ID (integer only) to auto increment, so I don't have to try and randomly generate a unique number.
Can this be done simply?
Thank you!
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Yes, that's the way auto_increment works.
The value will be incremented for each new row
The value is unique, duplicates are not possible
If a row is deleted, the auto_increment column of that row will not be re-assigned.
The auto_increment value of the last inserted row can be accessed using the mySQL function LAST_INSERT_ID() but it must be called right after the insert query, in the same database connection
mySQL Reference
1 more,
You can insert your own value also (ie your random value).
Yes. Auto_Increment columns work like they say on the tin. Tips
when INSERT - ing, use NULL or omit the column
Use LAST_INSERT_ID() (or API equivalents) to obtain the last generated value.
for security and business logic reasons, it's usually better form to not directly use a key value for a customer identifier. Consider using Hashed / randomised surrogate customer keys instead.
Ta
Yes, that's the exact purpose of AUTO_INCREMENT. It looks at whatever is the current increment value for that table, and stores that value plus 1 for the new row that comes in, automatically. You can omit that field from your INSERT statements and MySQL will handle it for you for every new row that comes in, giving each row its own unique ID.
When you enable Auto Increment an ID will always get automatically added whenever a new record is made.. Example:
If you have 1 record with ID 1 in your table and you add a new record, the ID will automatically be 2.
I have some tables in my phpmyadmin with one column that is auto incremented.
The problem is that when I delete some rows from the table (For example the element Car, with index auto incremented 1) and I create another row into the table, the new row have the index 2, but in the table there is only one row.
I want that this second element created to have the index equal to the position of the row, for example, if I have 3 rows that the third element will have the index equals to 3.
I was looking for a method that let me to use my phpMyAdmin like this but I couldn't find anything.
Is it possible? If it is true, what should I have to do? Do I have to create the table again?
This is generally a bad idea. Auto increment is used for creating unique ID of the row. Imagine you have a record "1 - John". Then you delete it and add another "1 - Jack". From the point of common database logic, it will seem that John was renamed to Jack (it has the same ID = it is the same entity) rather than it is another record. You should let DB assign new ID to each new record, even with leaving gaps after deleted records.
If you really want to do so, you can modify auto increment value using this query:
ALTER TABLE users AUTO_INCREMENT=123
but it is still not the way auto increment is designed for.
It is possible, but you shouldn't do that on production.
The SQL query is:
ALTER TABLE tablename AUTO_INCREMENT = 1
The most important part of this is to prevent overriding. For example you have an user and this user has id of 81 and if you delete this user and the database doesn't remember that this id 81 has ever been taken by an user (and for example, you have some relations - like friend lists) the user that is going to have the same ID will probably have the same data.
So basically, you don't want to reset auto increment values.
Execute this SQL sentence:
ALTER TABLE tablename AUTO_INCREMENT = 1
I struggled with this a bit too, then found in the "Operations" tab, there is a setting under "Auto Increment" where you can set the number it will start from, so you can roll it back if you want to do that.
It is possible. It is not a feature of phpmyadmin, but of mysql. Execute this
ALTER TABLE tablename AUTO_INCREMENT = 1
More info on this on stackoverflow
And in the mysql reference
I can't seem to find an answer to my problem, but I got a database where I would like to change the row in a column to affect all the other rows in the same column, is this possible?
(I can't post an image because of reputation?! - but my column is called Active and there's 2 rows right now - the first one has the value 'ja' and the second one is NULL)
'ja' and NULL are the only two values I will be using, so when the second row is set to 'ja', I would like the first row to change to NULL. But as I can see it might not be possible, but if anyone knows more than me, I would be very happy!
It's Mysql database I use sql queries and php.
I'd keep things simple and run two queries.
One to set everything back to null
The second to set the active record to 'ja'
I'm guessing that there's probably a foreign key in the table too so that would be part of the update statement too.
As a matter of "style" I'd prefer to see an active column having a 1/0 or y/n type of value rather than using null all the time.
That is a complicated database design. The first thing that comes to mind is a trigger updating all other rows whenever a row gets 'ja'. However this will fail, because you cannot update the same table you are already updating.
It is generally a bad idea to design a table such that all rows must contain value y when one row contains value x. I would solve this with by removing the "active" column altogether and replace it with a one row table containing a column active_id. So there is always just one id active and the others are implicitely inactive.
If you want to stick with your database design, a possible update statement would be:
update mytable
set active = case when id = 123 then 'ja' else null end;
However the dbms doesn't guarantee that there will always be exactly one record with 'ja'. It's up to you to be careful about it.
PS: I agree with Sarah King on the NULL issue. NULL means "I don't know", but you know very well that the records are not active. So this should be a non-nullable column with values "ja" and "nein" or whatever you prefer. In MySQL you would rather use a BOOLEAN column.
OK, so here is my scenario - you may disagree with what I'm attempting to do but I have my reasons.
The user is able to upload various information to the database, I also want them to be able to upload a picture at the same time. Now for various reasons I want to store the image with the file name the ID number of the record I'm adding (I have an ID column as the primary which auto-increments). Obviously I would have to store the file extension in the database too.
Now is there a way I can add the record and then know what ID was set so that I can save the image? I don't want to query and select the highest ID as that could go wrong if two people were to submit the form at the same time.
Any ideas?
LAST_INSERT_ID() will give you the ID that was auto-generated:
... returns a BIGINT (64-bit) value representing the first
automatically generated value that was set for an AUTO_INCREMENT
column by the most recently executed INSERT statement to affect such
a column. For example, after inserting a row that generates an
AUTO_INCREMENT value, you can get the value like this:
mysql> SELECT LAST_INSERT_ID();
-> 195
After running an insert in PHP, you can get the AUTO_INCREMENT ID from $mysqli->insert_id (where $mysqli is a MySQLi connection object), $pdo->lastInsertId (where $pdo is a PDO connection object), or mysql_insert_id() (if you're still using MySQL, which you shouldn't be) without having to run another query.
Right now, I have my database setup so that I insert a new row. But if the row I'm trying to insert is a duplicate (based off of my primary key id), then I increment the count field by 1.
Right now I want to know when I was able to create a new row instead of having it increment count. How would I do this in an efficient/proper manner?
The current method I'm thinking of doing this, is by querying the id first and checking if it exists. But I feel like there's a faster/better way. I've also read about triggers but I've heard that they're bad/risky to use.
Use INSERT ... ON DUPLICATE KEY UPDATE...
Then query for affected_rows (as #Tarek Fadel suggested). MySQL will return 1 if the row was inserted, or 2 if existing row were updated.
Use your database AUTO INCREMENT option for your primary ID field. Only propper solution.
Here you have mysql reference, but that exist in just every database engine:
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
How about an auto_increment on the id column?
Otherwise you might use SELECT MAX(id) FROM TABLE to retreive the highest id and add one to it, but that isn't thread-safe since another user might execute the same insert at the same time. Auto_increment fixes that for you.
PHP's mysql_affected_rows()