MODIFY COLUMN error in MySQL

时间:2015-05-24 20:27:29

标签: mysql sql

I have this sql script that creates a table in my MySql database.

CREATE TABLE USER_ACCOUNT
(ID             DECIMAL(10)     NOT NULL,
 USERNAME       VARCHAR (50)    NOT NULL,
 PASSWORD       VARCHAR (25)    NOT NULL,
 EMAIL          VARCHAR (50)    NOT NULL,
 FIRST_NAME     VARCHAR (25)    NULL DEFAULT NULL,
 MIDDLE_NAME    VARCHAR (25)    NULL DEFAULT NULL,
 LAST_NAME      VARCHAR (25)    NULL DEFAULT NULL,
 CREATE_DATE    TIMESTAMP       NULL DEFAULT CURRENT_TIMESTAMP);

ALTER TABLE USER_ACCOUNT
  ADD CONSTRAINT USER_ACCOUNT_PK_ID
      PRIMARY KEY (ID);

ALTER TABLE USER_ACCOUNT
  ADD CONSTRAINT USER_ACCOUNT_UK_USERNAME
      UNIQUE (USERNAME);

ALTER TABLE USER_ACCOUNT 
  MODIFY COLUMN ID DECIMAL(10) NOT NULL AUTO_INCREMENT;

I understand that AUTO_INCREMENT has to be added to a KEY. I am trying to add AUTO_INCREMENT to the primary key ID but the line

ALTER TABLE USER_ACCOUNT 
  MODIFY COLUMN ID DECIMAL(10) NOT NULL AUTO_INCREMENT;

Has the following syntax error:

Syntax error: unexpected 'DECIMAL' (decimal)

And then has this error when the script is run:

Error Code: 1063. Incorrect column specifier for column 'ID'

What's going on with my statement? I've looked all over online and it looks like the correct syntax.

3 个答案:

答案 0 :(得分:1)

Use for the ID integer datatype.

ALTER TABLE `user_account` CHANGE `ID` `ID` BIGINT  NOT NULL ;
ALTER TABLE `user_account` CHANGE `ID` `ID` BIGINT NOT NULL AUTO_INCREMENT ;

For auto increment you have to use a integer data type: TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT

A typical data type for a index is INT or BIGINT.

https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

https://dev.mysql.com/doc/refman/5.0/en/integer-types.html

答案 1 :(得分:0)

If possible with out data loss, DROP this column and add new columns with your required attributes,

to my experience, altering PK column always teases :(

答案 2 :(得分:0)

As what Giorgos Betsos mentioned in the comments above, I changed DECIMAL(10) to BIGINT and it worked fine.