“ERROR 1406: 1406: Data too long for column” but it shouldn't be?

后端 未结 3 1840
暖寄归人
暖寄归人 2021-02-05 23:54

I have the following table structure:

DROP TABLE IF EXISTS `tblusers`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_         


        
相关标签:
3条回答
  • 2021-02-06 00:05

    IsAdmin has the datatype of bit(1), yet you are assigning the string '1' to it. Indicate that you are assigning a bit value to it by preceeding the '1' with b or use 0b format:

    UPDATE `tblusers` SET `IsAdmin`=b'1'  WHERE `UserID`='79';
    

    or

    UPDATE `tblusers` SET `IsAdmin`=0b1  WHERE `UserID`='79';
    

    The reason for this behaviour is probably that strict_all_tables or strict_trans_tables setting is enabled on the v5.7 mysql server:

    Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.) Strict mode also affects DDL statements such as CREATE TABLE.

    0 讨论(0)
  • 2021-02-06 00:22

    isadmin is a column of type bit and you are storing a value of type varchar in it which is of larger size than bit. modify query as follows:-

    UPDATE `tblusers` SET `IsAdmin`=b'1'  WHERE `UserID`='79';
    
    0 讨论(0)
  • 2021-02-06 00:22

    The BIT data type is used to store bit values. A type of BIT(M) enables storage of M-bit values. M can range from 1 to 64.

    UPDATE tblusers SET IsAdmin=b'1' WHERE UserID='012';

    UPDATE tblusers SET IsAdmin=b'0' WHERE UserID='012';

    0 讨论(0)
提交回复
热议问题