0

I am new to MySQL and hope someone can help me with this.

I want to create a simple table with two special settings:

  1. a two-column primary key including the columns "de" and "location"
  2. an auto incrementing column "tID" that generates a unique ID, starting with 1

I tried the following (and several other approaches) but this always returns the below error:

The SQL:

CREATE TABLE TranslationsMain (
    de VARCHAR(100) NOT NULL,
    tID INT UNSIGNED NOT NULL AUTO_INCREMENT,
    location VARCHAR(50) NOT NULL,
    classes VARCHAR(100) NOT NULL,
    info VARCHAR(100) NOT NULL,
    sortOrder INT NOT NULL,
    en VARCHAR(100) NOT NULL,
    PRIMARY KEY(de, location)
)

The error message:

"Incorrect table definition; there can be only one auto column and it must be defined as a key."

It works if I leave the auto incrementing column ("tID") out so there seems to be something wrong here. Can someone help me with this ?

Many thanks in advance, Mike

2
  • 1
    The auto_incement must be a part of the primary key. Commented Jun 17, 2015 at 7:58
  • @Jens: Thanks for that. Think we got it sorted with the answers provided below. Commented Jun 17, 2015 at 7:59

2 Answers 2

3

Try below query, I think this will solve your problem

CREATE TABLE TranslationsMain (
 de VARCHAR(100) NOT NULL,
 tID INT UNSIGNED NOT NULL AUTO_INCREMENT,
 location VARCHAR(50) NOT NULL,
 classes VARCHAR(100) NOT NULL,
 info VARCHAR(100) NOT NULL,
 sortOrder INT NOT NULL,
 en VARCHAR(100) NOT NULL,
 PRIMARY KEY(tID),
 UNIQUE(de, location))

OR

CREATE TABLE TranslationsMain12 (
     de VARCHAR(100) NOT NULL,
     tID INT UNSIGNED NOT NULL AUTO_INCREMENT,
     location VARCHAR(50) NOT NULL,
     classes VARCHAR(100) NOT NULL,
     info VARCHAR(100) NOT NULL,
     sortOrder INT NOT NULL,
     en VARCHAR(100) NOT NULL,
     unique(tID),
     primary key(de,location)
    )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot - this works great as well ! As the other answer seems to be the more general approach I decided to accept that one but voted you up as well, hope that's ok. Thanks again !
1

In your SQL what the problem is auto increment must be primary key. Otherwise it will not work.

You can use like these. i think your problem will solve.

  CREATE TABLE TranslationsMain (
     de VARCHAR(100) NOT NULL,
     tID INT UNSIGNED NOT NULL AUTO_INCREMENT,
     location VARCHAR(50) NOT NULL,
     classes VARCHAR(100) NOT NULL,
     info VARCHAR(100) NOT NULL,
     sortOrder INT NOT NULL,
     en VARCHAR(100) NOT NULL,
     PRIMARY KEY(tID,de, location))

2 Comments

Thanks for this as well ! It works well with the answer from user1151316.
I voted you up already. :) I tested this and it works great. As this seems to be the more general approach I decided to accept this answer. Thanks again.

Your Answer

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.