Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql disable auto_increment column temporary in Stored procedure

Tags:

mysql

In sql server there is a something like

IDENTITY_INSERT is set to OFF and we can set in ON after

I wanna know is that something similar in mysql where i need to temporary set off and set ON i.e like

create procedure ()

    begin   
      IDENTITY_INSERT is set to OFF
      Some insert statement
      Some insert statement
      IDENTITY_INSERT is set to ON 
    end
like image 856
umang rathod Avatar asked Jun 07 '16 12:06

umang rathod


People also ask

How do you prevent the auto increment being reset when you delete all the rows of a table?

If you delete the row containing the maximum value for an AUTO_INCREMENT column, the value is not reused for a MyISAM or InnoDB table. If you delete all rows in the table with DELETE FROM tbl_name (without a WHERE clause) in autocommit mode, the sequence starts over for all storage engines except InnoDB and MyISAM.

Can we change auto increment value in MySQL?

You can use ALTER TABLE to change the auto_increment initial value: ALTER TABLE tbl AUTO_INCREMENT = 5; See the MySQL reference for more details.

How do I make auto increment start from 1 again?

In MySQL, the syntax to reset the AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = value; table_name. The name of the table whose AUTO_INCREMENT column you wish to reset.

Is auto increment always primary key?

A Primary Key just needs to be a unique value that identifies its entry from other entries, and not null. Save this answer. Show activity on this post. Primary key should be unique but not necessarily need to be auto_increment.


1 Answers

I can't imagine any good reason why you need to switch that off.

If you need to insert any record with your custom id AUTO_INCREMENT is not a barrier.

http://sqlfiddle.com/#!9/4d413/1

CREATE TABLE t1 (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (id));

INSERT INTO t1 VALUES (23);
INSERT INTO t1 VALUES (null);

So why do you need to switch it off?

like image 52
Alex Avatar answered Oct 27 '22 00:10

Alex