#1067 - Invalid default value for 'bonusid' how can i fix this error?

后端 未结 3 1317
面向向阳花
面向向阳花 2021-01-14 06:03

SQL query:

CREATE TABLE bonus(
bonusid INT( 10 ) DEFAULT  \'0\' NOT NULL AUTO_INCREMENT ,
empid INT( 10 ) DEFAULT  \'0\' NOT NULL ,
datebonus DATE DEFAULT  \         


        
相关标签:
3条回答
  • 2021-01-14 06:57

    default value is not allowed to the primary key because of if you used default value to primary key then some time the record is inserted as same and primary key is not allowed to insert same value in this column.

    check this

    CREATE TABLE bonus(
    bonusid INT( 10 )  AUTO_INCREMENT ,
    empid INT( 10 ) DEFAULT  '0' NOT NULL ,
    datebonus DATE DEFAULT  '0000-00-00' NOT NULL ,
    bonuspayment VARCHAR( 200 ) NOT NULL ,
    note TEXT NOT NULL ,
    PRIMARY KEY ( bonusid )
    );
    

    if you use some column as primary key then it is default not null is not use to declare this. refer this link for auto increment

    http://www.w3schools.com/sql/sql_autoincrement.asp

    0 讨论(0)
  • 2021-01-14 07:05

    Even though the bonusid column is NOT NULL - you don't have to specify a default value if it has the special auto_increment option when you create the table.

    Try As follows:

    CREATE TABLE bonus(
    bonusid INT( 10 ) NOT NULL AUTO_INCREMENT ,
    empid INT( 10 ) DEFAULT  '0' NOT NULL ,
    datebonus DATE DEFAULT  '0000-00-00' NOT NULL ,
    bonuspayment VARCHAR( 200 ) NOT NULL ,
    note TEXT NOT NULL ,
    PRIMARY KEY ( bonusid )
    );
    

    FIDDLE DEMO HERE

    0 讨论(0)
  • 2021-01-14 07:08

    You don't have to give default value for a primary key with auto increment value. Since you have defined bonusid as a primary key and has defined auto increment.So this will automatically create a new value for bonusid whenever a new record is inserted.So try like this

    CREATE TABLE bonus(
       bonusid INT( 10 ) NOT NULL AUTO_INCREMENT ,
       empid INT( 10 ) DEFAULT  '0' NOT NULL ,
       datebonus DATE DEFAULT  '0000-00-00' NOT NULL ,
       bonuspayment VARCHAR( 200 ) NOT NULL ,
       note TEXT NOT NULL ,
       PRIMARY KEY ( bonusid )
    );
    
    0 讨论(0)
提交回复
热议问题