Incorrect Collum for a specifier for Collum 'code'

Asked

Viewed 29 times

0

Good morning,

I recently started on mysql server and is giving an error q for more q look for no solution arrangement:

My code is:

CREATE TABLE usuarios(
codigo VARCHAR(30) NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(codigo))
ENGINE= InnoDB
DEFAULT
chartset=utf8
COLLATE=utf8_general_ci

And as much as I try other alternatives I can’t find solution the error is always the same

Incorrect collum for a specifier for collum 'codigo'

If anyone knows how to solve this mistake I appreciate the help :)

  • You have a column varchar which is self incremental. It doesn’t make sense. To be auto_increment needs to be int or variation of it

1 answer

1


the problem I see in your code is on this line:

codigo VARCHAR(30) NOT NULL AUTO_INCREMENT

For the value to be incremented it cannot be of the text type. For primary keys it is commonly used of the type INTrepresenting interim values, in your case the primary key is the field codigo if you change the VARCHAR(30) for INT, as it is well below certain.

codigo INT NOT NULL AUTO_INCREMENT, 

But if you run the code only by changing this line will give another error because the chartset it is not written in this correct way, the right way is charset as it is in the code below:

CREATE TABLE usuarios(
codigo INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(codigo))
ENGINE= InnoDB
DEFAULT
charset=utf8
COLLATE=utf8_general_ci
  • 1

    +1. Only by increasing: auto_increment must be of the whole type (of any size, provided tinyint until bigint), more information (here)[https://dev.mysql.com/doc/refman/8.0/en/integer-types.html]

  • 1

    And a hint: use attribute UNSIGNED, to gain a higher range in exchange for not being able to use negative values (since a negative ID/code would not make sense)

  • Thank you, but is there no way I can make code value readable? for example when we use varchar(40) even when we put code 5 as it does not have 40 characters it will readjust to 1 character and my question was whether it is possible to do this with the integers in the case?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.