skip to Main Content

I have tried to create a table in a MySQL database with phpmyadmin.

The problem occurred when I try to execute this following query:

CREATE TABLE users(
    account_id INT(20) NOT NULL AUTO_INCREMENT DEFAULT '100001',
    PASSWORD INT(20) NOT NULL DEFAULT '0',
    NAME VARCHAR(250) NOT NULL DEFAULT '0',
    account_type VARCHAR(250) NOT NULL DEFAULT '0',
    balance DOUBLE(20) NOT NULL DEFAULT '0',
    created_at DATE NOT NULL DEFAULT '0',
    PRIMARY KEY(account_id)
) ENGINE = INNODB;

The error says:

MySQL Error: #1067 - Invalid default value for 'account_id'

Please help to fix this thing! I have tried many solutions but none of them seem to work

3

Answers


  1. Remove default from account id.
    Use below.

    CREATE TABLE users(
    account_id INT(20) NOT NULL AUTO_INCREMENT,
    PASSWORD INT(20) NOT NULL DEFAULT '0',
      NAME VARCHAR(250) NOT NULL DEFAULT '0',
     account_type VARCHAR(250) NOT NULL DEFAULT '0',
     balance DOUBLE(20) NOT NULL DEFAULT '0',
    

    created_at DATE NOT NULL DEFAULT ‘0’,
    PRIMARY KEY(account_id)
    ) ENGINE = INNODB;

    Login or Signup to reply.
  2. Your definition is really confused. For instance:

    • Default values should match the type of the column.
    • A default value is not appropriate for an auto-increment column.
    • A length parameter is inappropriate for double. And probably for integers as well.
    • 0 is not an appropriate default value for a date.

    So, you seem to want something like this:

    CREATE TABLE users (
        account_id INT NOT NULL AUTO_INCREMENT,
        PASSWORD INT NOT NULL DEFAULT 0,
        NAME VARCHAR(250) NOT NULL DEFAULT '0',
        account_type VARCHAR(250) NOT NULL DEFAULT '0',
        balance DOUBLE NOT NULL DEFAULT 0,
        created_at DATE NOT NULL,
        PRIMARY KEY (account_id)
    ) ENGINE = INNODB;
    
    Login or Signup to reply.
  3. If you wanna AUTO_INCREMENT then you cant set the default value

    account_id INT NOT NULL AUTO_INCREMENT,
    

    Or if you need a default value then you have to remove AUTO_INCREMENT

    account_id INT(20) NOT NULL  DEFAULT '100001',
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search