how to set the auto_increment starting value in MySQL using SQL? - mysql

I have tried several ways of setting the auto increment value on the primary key ID on a table with no luck.
Doesn't work for me...
id INT NOT NULL AUTO_INCREMENT = 10000,
Tried this...
UPDATE tablename SET id = id + 10000;
Tried this..
ALTER TABLE tablename AUTO_INCREMENT = 10000;
Tried this..
CREATE TABLE tablename (
...
) ENGINE = MyISAM AUTO_INCREMENT = 10000;
What is the proper way to set this when creating a table using SQL?

On the creation of the table you can use these SQL statement:
CREATE TABLE tablename (
...
) ENGINE = InnoDB AUTO_INCREMENT = 10000;
But, if the table is already created, you can make an ALTER TABLE to let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:
ALTER TABLE tablename AUTO_INCREMENT = 10000;
You can find more on using AUTO_INCREMENT.

Related

How to set AUTO_INCREMENT PRIMARY KEY when using 'AS SELECT'

I am trying to create a temporary table and right now I am doing something like this:
CREATE IF NOT EXISTS TABLE tempdb.student AS (SELECT * FROM student LIMIT 0)
then
ALTER TABLE tempdb.student MODIFY id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY
However, I need to find a way to do this without the ALTER statement because both statements are getting ran together and if the table has already been created, I am getting an error because of the multiple primary keys. Any advice? Thanks
You can define columns, indexes, table options, and partitioning before the AS keyword.
CREATE IF NOT EXISTS TABLE tempdb.student (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(10),
enroll_date DATE,
...etc...
)
AS SELECT * FROM student LIMIT 0
This is the only way you can set some column or table options, such as auto-increment.
But it means you have to define all the columns, not just id.
Since you are using LIMIT 0 it means you don't actually want any rows from the source table. I guess you are just trying to duplicate the table structure, but none of the data.
In that case, you might like to use the following instead:
CREATE IF NOT EXISTS TABLE tempdb.student LIKE student;
This duplicates other column and table options, and indexes, and partitions. The new table will have the AUTO_INCREMENT option for its primary key.
You can explicitly check if the table exists (you need to be careful if the code can be executed in parallel):
SELECT
COUNT(*) INTO #exists
FROM
information_schema.tables
WHERE
table_schema = 'tempdb'
AND table_name = 'student'
;
IF #exists = 0 THEN
CREATE TABLE tempdb.student AS (SELECT * FROM student LIMIT 0); -- or use LIKE syntax
ALTER TABLE tempdb.student MODIFY id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY;
END IF;
If your temporary table needed for the session only, I'd use CREATE TEMPORARY TABLE ... LIKE ... and alter as required.

AUTO_INCREMENT in mysql can be signed by default?

i understand,below column will be signed int by default.
id INT(6);
Can an auto increment column specified below be signed by default? Mysql starts the value from 1 for an auto increment column.
id INT(6) AUTO_INCREMENT PRIMARY KEY
Yes, you can create an auto increment primary key with a signed int. Try this:
CREATE TABLE mytable( id int(6) AUTO_INCREMENT PRIMARY KEY);
Then the following queries are both valid
INSERT INTO mytable values();
INSERT INTO mytable values(-10);
This will result in the table having a row with -10 and another with 1 as values. But you will run into problems if you try this:
ALTER TABLE mytable AUTO_INCREMENT=-10;
yes, you cannot have auto increment values that are negative numbers.
After a lot of searches... I looked for a solution with a TRIGGER called BEFORE INSERT ! I found this : https://stackoverflow.com/a/43441586/2282880
Here is my variant :
CREATE TRIGGER `invertID`
BEFORE INSERT ON `<table>`
FOR EACH ROW SET NEW.id=CONCAT("-", (
SELECT `auto_increment`
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name = '<table>')
)
It worked for me fine.
It was the best way I found to sync in both directions two databases with same schema without same ID's in my tables.

ALTER TABLE tbl AUTO_INCREMENT = 123 as codeigniter active records query?

How to execute
ALTER TABLE tblname AUTO_INCREMENT = 123
as codeigniter active records query?
There is something called dbforge->modify_column() in codeigniter , do i have to use that, if so how?
try this
$this->db->query("ALTER TABLE table_name AUTO_INCREMENT 1");
change value after AUTO_INCREMENT where you want to start auto increment value from like this
$this->db->query("ALTER TABLE table_name AUTO_INCREMENT 12");
Alter table operation called DDL Data Defination Language where you execute queries at database not at data.
$this->db->query('ALTER TABLE tbl_name AUTO_INCREMENT 1'); is working for me.

Is it possible to increase the PK somehow?

Is it possible to increase the PK somehow?
I need to make it start at about 10000. Is there some sql statement I could execute directly in phpMyAdmin?
EDIT:
I need to be a bit more clear...
I need to update all rows with a PK over 2190 (about 2000 rows). Need to update them to start at about 10000 instead... So I do believe it is some sort of programming question...
Quoting the doc.
So the question is about updating some set of values. Well, here's one way to do it:
UPDATE some_table
SET some_pk = 10000 + some_pk
WHERE some_pk > 2190
Still, you have to follow this with ALTER TABLE tbl AUTO_INCREMENT = 10000; otherwise all the rows inserted after this UPDATE still get an old auto_increment. For example:
CREATE TABLE t (id INT AUTO_INCREMENT NOT NULL, s VARCHAR(5), PRIMARY KEY (id));
INSERT INTO t (s) VALUES ('a'), ('b'), ('c'), ('d'), ('e');
UPDATE t SET id = id + 10000 WHERE id > 3;
ALTER TABLE t AUTO_INCREMENT = 10000;
INSERT INTO t (s) VALUES ('f');
Without ALTER TABLE... 'f''s id will be 6; with it - 10006.
Here's SQL Fiddle illustrating this concept.
If your table has already been created with an auto-incr. index, use
ALTER TABLE tbl AUTO_INCREMENT = 10000;

How to manually set seed value as 1000 in MySQL

I am using MySQL 5. I need to set the seed value as 1000 for my auto increment field.
How can I set it?
To set when creating your table:
CREATE TABLE xxx(...) AUTO_INCREMENT = 1000;
To set after creating the table:
ALTER TABLE xxx AUTO_INCREMENT = 1000;
If the table already exists, you can do this:
ALTER TABLE mytable AUTO_INCREMENT = 1000;
But make sure the highest value in the auto_increment column is less than 1000.
If you are creating a new table, you can do this:
CREATE TABLE mytable (id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT)
AUTO_INCREMENT = 1000;