SQL query:
ALTER TABLE `blog` CHANGE `id` `id` BIGINT NOT NULL AUTO_INCREMENT
MySQL said:
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
I am trying to create a blog, and I got the code done. Now, I need to make the id auto increase, but I get this error. Why am I getting this?
MySQL is returning that error (most likely) because there is no unique index defined on the id column. (MySQL requires that there be a unique index. The other possibility, which you would have already figured out, is that there can be only one column defined as AUTO_INCREMENT within the table.)
To get that column to be an AUTO_INCREMENT, you can add either a UNIQUE constraint or a PRIMARY KEY constraint on the id column. For example:
ALTER TABLE `blog` ADD CONSTRAINT `blog_ux` UNIQUE (`id`) ;
(Note that this statement will return an error if any duplicate values exist for the id column.)
Alternatively, you can make the id column the PRIMARY KEY of the table (if the table doesn't already have a PRIMARY KEY constraint defined).
ALTER TABLE `blog` ADD PRIMARY KEY (`id`) ;
(Note that this statement will return an error if any duplicate value exist for the id column, OR if there are any NULL values stored in that column, of if there is already a PRIMARY KEY constraint defined on the table.)
MySQL requires you to make auto increment column the primary key of a table. Add the primary key constraint at the end
ALTER TABLE `blog` MODIFY COLUMN `id` BIGINT NOT NULL AUTO_INCREMENT primary key
To resolve #1075 error message you need to mark atleast one column as primary_key or unique_key. that you have forgot to do.
By defining Primary _key on ID column my error is resolved guys.
thanks
Related
summary
in mysql,
I want add uniqueness to existing column, without drop table, but it spits error because of existing auto-increment / primary key / partition key.
describe
I have table like below. and already have some rows.
CREATE TABLE my_table (
`id` INT NOT NULL AUTO_INCREMENT,
`event_tx_id` varchar(64) NOT NULL,
`amount` int(10),
PRIMARY KEY(id)
)
ENGINE=InnoDB
PARTITION BY KEY(id)
and I changed my mind, I want add constraint to column event_tx_id also unique.
I tried
when I try alter table like this (add unique),
ALTER TABLE my_table MONDIFY COLUMN event_tx_id varchar(64) UNIQUE;
it spits error that
Error Code: 1503. A UNIQUE INDEX must include all columns in the table's partitioning function 0.078 sec
so I tried partition by key, first, then
ALTER TABLE my_table PARTITION BY KEY(id, event_tx_id);
then spits
ALTER TABLE my_table PARTITION BY KEY(id, event_tx_id); Error Code: 1503. A PRIMARY KEY must include all columns in the table's partitioning function
tries above needs changing primary key. so when I try to change primary key (drop -> add, because there isn't modify method)
ALTER TABLE my_table DROP PRIMARY KEY Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key
it complains about YOU CANNOT CHANGE(DROP) PRIMARY KEY THAT HAS AUTO-INCREMENT..
is there any way to handle this .. ?
thanx.
Uniqueness constraints do not work on Partitioned tables, except where the partition key is part of the constraint.
Partitioning rarely provides any performance benefit, or any other benefit. What were you hoping to achieve?
I imported a database in phpmyadmin.
It is a large database nearly size of 990MB.
But after importing, I have a problem.
All the key constraints are there, but primary key is not set to auto increment.
So when I insert data in the table it throws duplicate entry and primary key violation error.
1062 - Duplicate entry '0' for key 'PRIMARY'
It's clearly because it's inserting 0 in case auto_increment is not set
I tried to reset the AUTO_INCREMENT by running
ALTER TABLE users AUTO_INCREMENT=1001;
and then tried to check the auto_increment in the id field but it is giving me this error
Query error:
1062 - ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '1' for key 'PRIMARY'
you can try it
check you indexing and Primary Key
remove indexing and Primary Key
ALTER TABLE `users ` DROP PRIMARY KEY
Add Auto_increment and Add Primary Key
ALTER TABLE tablename AUTO_INCREMENT = 1
For InnoDB you cannot set the auto_increment value lower or equal to the highest current index.
Note that you cannot reset the counter to a value less than or equal to >any that have already been used. For MyISAM, if the value is less than or >equal to the maximum value currently in the AUTO_INCREMENT column, the >value is reset to the current maximum plus one. For InnoDB, if the value >is less than the current maximum value in the column, no error occurs and >the current sequence value is not changed.
so, we can
ALTER TABLE `users` ADD `id` INT( 11 ) NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST
hope it work for you .
I have a column that is used as a FK in an other tables, I wanna try to set Auto_increment value but if fails.
This is what i've tried:
ALTER TABLE `m_datos_medicos`.`ficha_identificacion`
CHANGE COLUMN `ficha_identificacion` `ficha_identificacion` INT(11) NOT NULL AUTO_INCREMENT ;
This is the error:
Cannot change column 'ficha_identificacion': used in a foreign key
constraint 'fk_ficha_identificacion_has_deporte_ficha_identificacion1'
of table 'm_datos_medicos.ficha_identificacion_has_deporte'
How can I set auto_increment value??
I want to update a column which is currently a plain INT(16) so that it references a FK on another table. I've tried the following, but with errors:
ALTER TABLE ts_keys ADD CONSTRAINT FK_account_id FOREIGN KEY (account_id) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE
EDIT: Sorry, forgot to add the error:
Can't create table (errno: 150)
Both tables are Innodb.
EDIT 2: I also tried re-creating the table but same error:
CREATE TABLE ts_keys (
id int PRIMARY KEY AUTO_INCREMENT,
account_id int,
FOREIGN KEY fk_account_id1(account_id) REFERENCES accounts(id)
) ENGINE=InnoDB;
The datatype of the foreign key column must match EXACTLY the datatype of the referenced column.
Do a SHOW CREATE TABLE accounts and look at the definition of the id column.
Whatever that column is defined as INT UNSIGNED, BIGINT, VARCHAR(16), whatever,
the column you want to define as a foreign key (the account_id column in ts_keys table) must match that datatype EXACTLY. (It's just the datatype that has to match. The column comment doesn't have to match, the DEFAULT value doesn't have to match, the NULL/NOT NULL doesn't have to match. But it's required that the datatypes match.
Your syntax for adding the constraint looks correct:
ALTER TABLE ts_keys
ADD CONSTRAINT FK_account_id
FOREIGN KEY (account_id)
REFERENCES accounts(id)
ON UPDATE CASCADE ON DELETE CASCADE
Admittedly, the "Can't create table (errno: 150)" has to be the least helpful message regarding what's actually causing the problem. (At least the error isn't the "check the manual" syntax error.
I have an existing table with lot of rows (around 10k rows) with two columns as primary keys as it is acting as middle table of many-to-many relation between two other table.
For new requirements, I need to assign add new column (say id) which must be primary key with auto increment values. I ran following queries:
ALTER TABLE `momento_distribution` ADD `id` INT( 11 ) NOT NULL FIRST;
ALTER TABLE `momento_distribution` DROP PRIMARY KEY , ADD PRIMARY KEY ( `id` );
First query run successfully but second query generated following error:
1062 - Duplicate entry '0' for key 'PRIMARY'
Reason is obvious, new column id got 0 as default value and Primary key can't have duplicate values.
Now before I can run second query, I need to set incremental value for new column like 1,2,3...
In Oracle, I know, this can be done through rowid. MySQL also have its equivalent #rowid. Can someone please suggest a query to set #rowid as column value for column id?
Please Note: This had to be done through query as I can't change 10000 rows manually.
You need to set it to AUTO_INCREMENT at the same time, that will populate it;
ALTER TABLE momento_distribution
ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
Demo here.
EDIT: If you have an existing primary key, you'll need to drop that at the same time;
ALTER TABLE momento_distribution
DROP PRIMARY KEY,
ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
Same question asked by same user differently. Refer to that question.
MySQL 1062 - Duplicate entry '0' for key 'PRIMARY'
In short,
1. Remove existing FK
2. Remove existing PK
3. Run your first query as
ALTER TABLE `momento_distribution` ADD `id` INT( 11 ) PRIMARY KEY AUTO_INCREMENT NOT NULL FIRST;
which will also assign unique number without depending on #rowid
4. Add FK to earlier columns, if needed.