CREATE TABLE t1 (id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT )
The above command increases id from 0 ,but i want to increase it from a specific no. say like 1234567890 ..
how can i do that??
How can i set auto increment from a specific no.??
I tried something like this but it did not work
CREATE TABLE t1 AUTO_INCREMENT = 1234567890, (id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT)
If the question is not clear pllzz comment..
CREATE TABLE t1 (
`id` BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT
)
ENGINE = MyISAM
AUTO_INCREMENT = 1234567890;
Edit: A quick note with regards to storage engine support for setting the AUTO_INCREMENT value, which will depend on the version of MySQL you're running.
From MySQL 5.0 Manual,
In MySQL 5.0, this works for MyISAM
and MEMORY tables. It is also
supported for InnoDB as of MySQL
5.0.3.
From MySQL 5.1 Manual,
In MySQL 5.1, this works for MyISAM,
MEMORY, and InnoDB tables. It also
works for ARCHIVE tables as of MySQL
5.1.6.
After your create table script, use the alter table syntax to update the auto_increment value:
ALTER TABLE t1 AUTO_INCREMENT = 12345
You can alter the table:
ALTER TABLE tbl AUTO_INCREMENT = 100;
Just create your table like your first statement, then add
ALTER TABLE tbl AUTO_INCREMENT = 1234567890;
...where 1234567890 is ofc the value you want the value. More details can be found here: http://dev.mysql.com/doc/refman/5.1/en/example-auto-increment.html
Related
AS described in the MySQL documentation here, it should be possible to drop a column instantly with a syntax like this one:
ALTER TABLE tbl_name DROP COLUMN column_name, ALGORITHM=INSTANT;
It is documented that it is only possible with the following constraints:
Dropping a column cannot be combined in the same statement with other ALTER TABLE actions that do not support ALGORITHM=INSTANT.
Columns cannot be dropped from tables that use ROW_FORMAT=COMPRESSED, tables with a FULLTEXT index, tables that reside in the data dictionary tablespace, or temporary tables. Temporary tables only support ALGORITHM=COPY.
Unfortunately, I am unable to use the syntax described above. For example, here is my test code:
CREATE TABLE MyTable (
MyPrimaryKey bigint NOT NULL AUTO_INCREMENT,
UserId char(36) NOT NULL,
Username varchar(254) NOT NULL,
PRIMARY KEY (MyPrimaryKey),
UNIQUE KEY IX_UserId (UserId)
) ENGINE=InnoDB;
ALTER TABLE MyTable DROP COLUMN Username, ALGORITHM=INSTANT;
When I run this against MySQL 8.0.28, I get the following error:
Error Code: 1845. ALGORITHM=INSTANT is not supported for this operation. Try ALGORITHM=COPY/INPLACE.
Am I doing something wrong or the documentation is missing something?
Note that the ROW_FORMAT of my table is Dynamic. Here is how I got the row format:
SELECT table_name, row_format
FROM information_schema.tables
WHERE table_schema=DATABASE() AND table_name = 'MyTable';
This functionality has been added to mysql 8.0.29. See the release notes for more details.
I have a MySQL database with a table that has 2 million rows using innodb engine. I want to add another column, but I keep getting the following error:
Error 1062: Duplicate entry '' for key 'PRIMARY' SQL Statement: ALTER TABLE `mydb`.`table` ADD COLUMN `country` VARCHAR(35) NULL DEFAULT NULL AFTER `email`
How can I add the column without getting this error?
EDIT: Table definition
id int(11) NOT NULL AUTO_INCREMENT,
user_id varchar(45) NOT NULL,
first_name varchar(150) DEFAULT NULL,
last_name varchar(150) DEFAULT NULL,
gender varchar(10) DEFAULT NULL,
email varchar(100) DEFAULT NULL,
created_at bigint(20) DEFAULT NULL,
updated_at bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`,`user_id`),
UNIQUE KEY `user_id_UNIQUE` (`user_id`),
KEY `first_name` (`first_name`),
KEY `last_name` (`last_name`)
EDIT #2: SHOW INDEXES output
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Index_type
table 0 PRIMARY 1 id A 3516446 BTREE
table 0 PRIMARY 2 user_id A 3516446 BTREE
table 0 user_id_UNIQUE 1 user_id A 3516446 BTREE
table 1 first_name 1 first_name A 390716 BTREE
table 1 last_name 1 last_name A 439555 BTREE
it solution will lock table on write, but often suitable for solving the problem if table is not very big
LOCK TABLES my_table WRITE;
ALTER TABLE my_table
ADD COLUMN `ts` DATETIME NULL AFTER `id`;
UNLOCK TABLES;
As described in the documentation, When running an online ALTER TABLE operation:
... the thread that runs the
ALTER TABLE operation will apply an “online log” of DML operations
that were run concurrently on the same table from other connection
threads. When the DML operations are applied, it is possible to
encounter a duplicate key entry error (ERROR 1062 (23000): Duplicate
entry), even if the duplicate entry is only temporary and would be
reverted by a later entry in the “online log”. This is similar to the
idea of a foreign key constraint check in InnoDB in which constraints
must hold during a transaction.
If it's not something a program needs to do (altering tables dynamically), then just wait a moment and then try again! It worked for me. :) I guess there are some InnoDB-specific processes/states (maybe still processing another ALTER queried just a moment ago?), during which the ALTER command would fail, and you just need to catch a moment when it succeeds.
I've runned the same code and it works fine.
http://sqlfiddle.com/#!2/1937e
As a solution, I would try to recreate the table , copy the data into it, and then switch tables using rename.
If that doesn't work then it's clear that it's a bug with your current mysql configuration, and we'll need more details to figure it out or at least reproduce it (mysql version, mysql config, database settings, the actual data, etc.).
If it works then it probably was a problem with tables or indexes and here are a few things you can check (you can also start with these if you don't wish to recreate the table):
Check that you don't have any triggers that are causing other inserts
Check that you are just creating a column and not adding other keys/indexes
Check that the auto_increment value is not overflowing (for int it's over 2,000,000,000)
If none of the above, then you probably have a some corrupt data, or you missed to share some details.
I guess there are some other ALTERs or INSERTs which are still processing.
First, to check the processing triggers with this following queries:
SHOW FULL PROCESSLIST;
SELECT * FROM information_schema.INNODB_TRX\G
Then, kill the locked query by trx_mysql_thread_id: 132092 for example:
KILL 132092;
Finally, here‘s a solution - OnlineSchemaChange (OSC). It built by Facebook supports online alter MySQL table schema with minimal impact.
use change column
ALTER TABLE database.table_name
CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT ,
ADD PRIMARY KEY (id);
Use percona toolkit to change db scheme without needing to lock tables: https://www.percona.com/doc/percona-toolkit/3.0/pt-online-schema-change.html
This allows to continue updating db while scheme change is being applied. This especially useful for large dbs where updating scheme can take a while.
I am setting up a table in mysql of engine type merge in mysql and was wondering if i have to have all my tables created previously that i want to merge. For example:
CREATE TABLE t1 (
a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
message CHAR(20)) ENGINE=MyISAM;
CREATE TABLE t2 (
a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
message CHAR(20)) ENGINE=MyISAM;
INSERT INTO t1 (message) VALUES ('Testing'),('table'),('t1');
INSERT INTO t2 (message) VALUES ('Testing'),('table'),('t2');
CREATE TABLE total (
a INT NOT NULL AUTO_INCREMENT,
message CHAR(20), INDEX(a))
ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;
Now if i have code that automatically created a t3 table i would have to modify the merge table to add this to the union? Would i use an ALTER query for that?
note: i am not using MySQL partitions because i have a mysql version 5.0.
Now if i have code that automatically created a t3 table i would have to modify the merge table to add this to the union? Would i use an ALTER query for that?
From the documentation:
To remap a MERGE table to a different collection of MyISAM tables, you can use one of the following methods:
DROP the MERGE table and re-create it.
Use ALTER TABLE tbl_name UNION=(...) to change the list of underlying tables.
Beginning with MySQL 5.0.60, it is also possible to use ALTER TABLE ... UNION=() (that is, with an empty UNION clause) to remove all of the underlying tables.
I am entering records in the MySQL DB. Now I want to have a "Serial_Number" field that increements automatically whenever a record is entered into the DB.
I don't want this "Serial_Number" field to be the primary key of the DB.
How can I create this field (with the attributes needed to be set).
I am using "SQL YOG" to access MySQL. If you are aware of the SQL YOG then tell me how to do that through SQL YOG.
The AUTO_INCREMENT column has to have a UNIQUE KEY constraint associated to it.
For instance, this will work just fine:
CREATE TABLE AutoNotId
(
Id INTEGER PRIMARY KEY,
Auto INTEGER AUTO_INCREMENT,
UNIQUE (Auto)
);
Edit:
The ALTER statement would look somewhat like this:
ALTER TABLE AutoNotId
MODIFY COLUMN Auto INTEGER AUTO_INCREMENT,
ADD UNIQUE (Auto);
I recommended, however the use of the long-hand syntax to specify the name of the UNIQUE constraint; But you can always refer to MySQL's Reference Manual for the exact specifications.
In MySQL tables can only have one auto increment field and they must be indexed.
There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value.
Is there a reason you don't want it to be the primary key?
If you want an incrementing value, you could fudge it by running updates after each insert:
SELECT MAX(serial) + 1 FROM myTable;
UPDATE myTable SET serial = <that number> WHERE id = ...
I don't think you can have an auto increment field:
CREATE TABLE `t` (`dd` int(11) NOT NULL)
ALTER TABLE `t` CHANGE `dd` `dd` INT( 11 ) NOT NULL AUTO_INCREMENT
MySQL said: Documentation
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
You cannot do this in MySQL. From the doc:
There can be only one AUTO_INCREMENT
column per table, it must be indexed,
and it cannot have a DEFAULT value. An
AUTO_INCREMENT column works properly
only if it contains only positive
values. Inserting a negative number is
regarded as inserting a very large
positive number. This is done to avoid
precision problems when numbers “wrap”
over from positive to negative and
also to ensure that you do not
accidentally get an AUTO_INCREMENT
column that contains 0.
For MyISAM and BDB tables, you can
specify an AUTO_INCREMENT secondary
column in a multiple-column key. See
Section 3.6.9, “Using AUTO_INCREMENT”.
create table mytable (
ID INT IDENTITY(1,1) PRIMARY KEY,
SN INT IDENTITY(1,1)
)
RESOLVED
From the developer: the problem was that a previous version of the code was still writing to the table which used manual ids instead of the auto increment. Note to self: always check for other possible locations where the table is written to.
We are getting duplicate keys in a table. They are not inserted at the same time (6 hours apart).
Table structure:
CREATE TABLE `table_1` (
`sales_id` int(10) unsigned NOT NULL auto_increment,
`sales_revisions_id` int(10) unsigned NOT NULL default '0',
`sales_name` varchar(50) default NULL,
`recycle_id` int(10) unsigned default NULL,
PRIMARY KEY (`sales_id`),
KEY `sales_revisions_id` (`sales_revisions_id`),
KEY `sales_id` (`sales_id`),
KEY `recycle_id` (`recycle_id`)
) ENGINE= MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26759 ;
The insert:
insert into `table_1` ( `sales_name` ) VALUES ( "Blah Blah" )
We are running MySQL 5.0.20 with PHP5 and using mysql_insert_id() to retrieve the insert id immediately after the insert query.
I have had a few duplicate key error suddenly appear in MySql databases in the past even though the primary key is defined and auto_increment. Each and every time it has been because the table has become corrupted.
If it is corrupt performing a check tables should expose the problem. You can do this by running:
CHECK TABLE tbl_name
If it comes back as corrupt in anyway (Will usually say the size is bigger than it actually should be) then just run the following to repair it:
REPAIR TABLE tbl_name
Does the sales_id field have a primary (or unique) key? If not, then something else is probably making inserts or updates that is re-using existing numbers. And by "something else" I don't just mean code; it could be a human with access to the database doing it accidentally.
As the other said; with your example it's not possible.
It's unrelated to your question, but you don't have to make a separate KEY for the primary key column -- it's just adding an extra not-unique index to the table when you already have the unique (primary) key.
We are getting duplicate keys in a table.
Do you mean you are getting errors as you try to insert, or do you mean you have some values stored in the column more than once?
Auto-increment only kicks in when you omit the column from your INSERT, or try to insert NULL or zero. Otherwise, you can specify a value in an INSERT statement, over-riding the auto-increment mechanism. For example:
INSERT INTO table_1 (sales_id) VALUES (26759);
If the value you specify already exists in the table, you'll get an error.
Please post the results of this query:
SELECT `sales_id`, COUNT(*) AS `num`
FROM `table_1`
GROUP BY `sales_id`
HAVING `num` > 1
ORDER BY `num` DESC
If you have a unique key on other fields, that could be the problem.
If you have reached the highest value for your auto_increment column MySQL will keep trying to re-insert it. For example, if sales_id was a tinyint column, you would get duplicate key errors after you reached id 127.