0

Current Structure in MYSQL:

CREATE TABLE `JOB_ONPREM` (
 `JOB_ID` bigint(20) NOT NULL AUTO_INCREMENT,
 `JOB_NAME` varchar(255) NOT NULL DEFAULT '',
 `SANDBOX` varchar(100) NOT NULL DEFAULT '',
 `JOB_SUB_TYPE` varchar(500) DEFAULT NULL,
 `PARENT_JOB_NAME` varchar(255) NOT NULL DEFAULT '',
 PRIMARY KEY (JOB_ID,`JOB_NAME`,`SANDBOX`,`PARENT_JOB_NAME`)
 ) ENGINE=InnoDB AUTO_INCREMENT=10493 DEFAULT CHARSET=utf8

I want to keep JOB_NAME,SANDBOX,PARENT_JOB_NAME as Primary KEY and JOB_ID as Auto Increment because I am using "ON DUPLICATE KEY UPDATE" and because of auto-increment it is inserting new rows and creating duplicate in table.

And while removing job_id from primary key. I am getting error as "Incorrect table definition; there can be only one auto column and it must be defined as a key"

asked Jun 13, 2020 at 20:13
1
  • 1
    Thta wouldn't come, during deletion, could you provide and example for that behaviour Commented Jun 13, 2020 at 21:40

2 Answers 2

0

The CREATE TABLE documentation says much the same as what your error message says:

There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value.

You need to index the JOB_ID column.

ALTER TABLE JOB_ONPREM
ADD INDEX `dba269102` (`JOB_ID`)

Try that and see if afterwards you can alter the primary key. You could also make it a unique index instead, to help avoid duplicates.

ALTER TABLE JOB_ONPREM
ADD UNIQUE INDEX `dba269102` (`JOB_ID`)
answered Jun 13, 2020 at 22:34
2
  • no the code runs without a problem adding is not problem is no problem, that is why i asked to clarify Commented Jun 13, 2020 at 23:07
  • Used above query and dropped existing primary key and create primary of 3 fields Commented Jun 14, 2020 at 17:29
0

Your question is incomplete.

JOB_ID as Auto Increment because I am using "ON DUPLICATE KEY UPDATE"

Rarely is an AUTO_INCREMENT necessary for IODKU. Please provide an example of why you need it.

Is some other table JOINing to this table on job_id? If not, can't you get rid of it.

Are there duplicate values of this combination (JOB_NAME,SANDBOX,PARENT_JOB_NAME) in the table? If no dups, then you should be able to have it as the sole PRIMARY KEY.

IODKU checks all unique keys (that includes the PK) when checking for ON DUPLICATE KEY. Do you need the 3-col test? 4-col? the auto_inc column? Or more than one of those? Show us the IODKU statement for starters.

While you are at it, do those columns really need to be 255 and 500 characters?

answered Jun 14, 2020 at 5:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.