Insert zero as PK for table with auto-increment primary key - mysql

I have a table with an auto-increment primary key. I have a special case need to have a record with a PK value of zero. Based on mysql can't insert record with unsigned primary key being zero, I see I can do so by executing SET sql_mode='NO_AUTO_VALUE_ON_ZERO'; before the insert.
In another session where this flag will not be set, I want to be able to utilize the default auto-increment functionality by inserting 0 or NULL.
Will there be any horrific consequences for doing this?

Related

MySQL: obtain primary key value generated by before-insert trigger

In MySQL, is there any way to obtain a primary key value generated by a before-insert trigger?
I know that select last_insert_id() can return the first value of an auto_increment column from a previous insert, but I can't seem to get it to work with a trigger-generated primary key.
There is no other unique key on my table, so I won't be able to perform a subsequent select to determine the generated primary key by just using data that was used in the insert.

auto increment primary key with delete in mysql

I have a table with an ID AUTO INCREMENT PRIMARY KEY. When I delete an entry and re-add one, the new entry does not take the ID of the previous one instead it increments again by one.
How to change this behaviour to get the id of deleted record to newely add record?
This is intended behaviour and can't be changed.
Don't misuse the primary key as indicator of your record order. You can use another colum for that like a datetime with a default value like current_timestamp
First of all, you shouldn't have to care about this; if you want to be really sure that you don't run out of numbers, use BIGINT UNSIGNED for your primary key instead.
Be warned that doing the below is not recommended.
ALTER TABLE mytable SET AUTO_INCREMENT = 123;
This set the number to be used for the next record at 123, so in your case you would set it to the deleted record's identifier.

(MySQL) Why ID column should be set to Primary Key (being unique)?

I've heard Primary Key means to be unique. Correct me please if I'm wrong.
Assume we have a table of users. It has 3 columns of id, username and password. We usually set the id to be AUTO_INCREMENT. So it would technically make a new unique id each time we add a row to the table. Then, why we also set the id column to be Primary Key or Unique?
Having a column as a key offers other aspects. First, if it is primary or unique, this would enforce that no query could enter a duplicate value for that key. Also keys can allow you do things like
INSERT ... ON DUPLICATE KEY UPDATE...
Of course you also want an index on the column for quick lookups.
AUTO_INCREMENT behavior only manifests when the column is not specified during an insert. Consider:
CREATE TABLE ai (
ai int unsigned not null auto_increment,
oi int unsigned,
key (ai),
primary key (oi)
);
INSERT INTO ai VALUES (1,2);
INSERT INTO ai VALUES (1,3);
INSERT INTO ai VALUES (null,5);
This will yield (1,2), (1,3), (2,5). Note how the AUTO_INCREMENT column has a duplicate.
A primary key does two things:
enforce database integrity (uniqueness and not-null of the column)
create an index to implement that, which also makes for fast look-up by the primary key column as a "side-effect".
You may not strictly need (1) if you can ensure that in your application code (for example by only using the auto-increment value), but it does not hurt.
You almost certainly want (2), though.
So it would technically make a new unique id each time we add a row to the table
Well, that is up to you. The unique id only gets inserted if you don't specify an explicit value. And technically, it is not guaranteed to be unique, it is just an auto-increment that does not take into consideration any existing values in the table (that may have somehow ended up in there).

Is there no use in defining auto increment column as primary key

In php myadmin it says it is no use in defining auto increment column as primary key for table. you can remove primary key constrain. (since both do the same job like).
Is this true. should I remove primary key constrain?
won't it good to have a primary key column for where clause rather than auto increment column
Keep The PK constraint. It will save you from some trouble:
if you intend to use some frameworks which check for this constraint to determine which columns are used as PK. They will be lost or require additional configuration if the constraint is not present.
When you'll use DB design software it will work better if your DB is properly designed.
When you'll have to change your DB software (upgrade or change brands) You will be happy to have all the constraints properly defined in your SQL statements.
You can create a column that is auto_incrementing without it being the primary key:
This statement is legal:
mysql> create table silly_but_possible (
id int auto_increment not null,
xx varchar(9),
key(id),
primary key (xx));
Query OK, 0 rows affected (0.15 sec)
So if you want your auto_incrementing column to be the PK, you'd better define it as such.
Note that a table (at present) can only have one auto_incrementing column.
What happens if you don't define a PK?
If you don't MySQL will define a hidden auto_incrementing PK for you.
However if you already have a auto_increment column, that column will always have an index, and because there can only be one auto_incrementing column, MySQL (at present!) has no other choice than to promote that row to the primary key.
when u define a column as a auto_increment it must be key(primary , unique ,index).
take an example
mysql> create table test
-> (id int(5) auto_increment,
-> name char(10)
-> );
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
so when we define a column as auto_increment it should be key.So it is better to define the column as PK.
The primary index is used by the database manager for efficient access to table rows, and allows the database manager to enforce the uniqueness of the primary key.

INSERT ... ON DUPLICATE KEY (do nothing)

I have a table with a unique key for two columns:
CREATE TABLE `xpo`.`user_permanent_gift` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`fb_user_id` INT UNSIGNED NOT NULL ,
`gift_id` INT UNSIGNED NOT NULL ,
`purchase_timestamp` TIMESTAMP NULL DEFAULT now() ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `user_gift_UNIQUE` (`fb_user_id` ASC, `gift_id` ASC) );
I want to insert a row into that table, but if the key exists, to do nothing! I don't want an error to be generated because the keys exist.
I know that there is the following syntax:
INSERT ... ON DUPLICATE KEY UPDATE ...
but is there something like:
INSERT ... ON DUPLICATE KEY DO NOTHING
?
Yes, use INSERT ... ON DUPLICATE KEY UPDATE id=id (it won't trigger row update even though id is assigned to itself).
If you don't care about errors (conversion errors, foreign key errors) and autoincrement field exhaustion (it's incremented even if the row is not inserted due to duplicate key), then use INSERT IGNORE like this:
INSERT IGNORE INTO <table_name> (...) VALUES (...)
HOW TO IMPLEMENT 'insert if not exist'?
1. REPLACE INTO
pros:
simple.
cons:
too slow.
auto-increment key will CHANGE(increase by 1) if there is entry matches unique key or primary key, because it deletes the old entry then insert new one.
2. INSERT IGNORE
pros:
simple.
cons:
auto-increment key will not change if there is entry matches unique key or primary key but auto-increment index will increase by 1
some other errors/warnings will be ignored such as data conversion error.
3. INSERT ... ON DUPLICATE KEY UPDATE
pros:
you can easily implement 'save or update' function with this
cons:
looks relatively complex if you just want to insert not update.
auto-increment key will not change if there is entry matches unique key or primary key but auto-increment index will increase by 1
4. Any way to stop auto-increment key increasing if there is entry matches unique key or primary key?
As mentioned in the comment below by #toien: "auto-increment column will be effected depends on innodb_autoinc_lock_mode config after version 5.1" if you are using innodb as your engine, but this also effects concurrency, so it needs to be well considered before used. So far I'm not seeing any better solution.
Use ON DUPLICATE KEY UPDATE ...,
Negative : because the UPDATE uses resources for the second action.
Use INSERT IGNORE ...,
Negative : MySQL will not show any errors if something goes wrong, so you cannot handle the errors. Use it only if you don’t care about the query.