Duplicate entry '0' for key 'PRIMARY'

前端 未结 2 1757
孤独总比滥情好
孤独总比滥情好 2021-01-25 18:41

I don\'t understand why I\'m getting this error when trying to populate this table. There is nothing in the table at the moment so I don\'t understand why there would be a dupli

2条回答
  •  攒了一身酷
    2021-01-25 19:05

    With your table you can get the error like "Incorrect Integer Value", but depending on MySQL server configuration it can do conversion(string->int) automatically for your query string must become "0" as result of this it makes 2 rows with 0 as supp_id and get error Duplicate entry '0' for key 'PRIMARY'. I guess you are using InnoDB as table type, in this case query will run as transaction and it will rollback after first error(for this example it will be second row).

    DROP TABLE suppliers; -- Will drop your old table
    CREATE TABLE suppliers(
    supp_id varchar(30) NULL, -- You can set length as you wish
    company_name character(15) NOT NULL,
    town character(15),
    phone character(15),
    primary key(supp_id)
    );
    
    INSERT INTO Suppliers
    (supp_id,company_name,town,phone)
    Values
    ("ADT217","AdTec","Birmingham","0121-368-1597"),
    ("CPS533","CPS","Maidenhead","01382-893715"),
    ("FCL162","ForComp Ltd","Nottingham","01489-133722"),
    ("KBC355","KBC Computers","Glasgow","0141-321-1497");
    

    After changing type insert will work without problems.

提交回复
热议问题