0
CREATE TABLE IF NOT EXISTS `users` (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL,
    password VARCHAR(255) NOT NULL,
    org_id  int NOT NULL,
    phonenum int NOT NULL
)  ENGINE=INNODB;

INSERT INTO `users` VALUES (1, 'USER','abcd', 1, 1);

this throws an error : ERROR 1062 (23000) at line 13: Duplicate entry '1' for key 'users.PRIMARY'

Thank you for your help!

2
  • Works for me. You must have already inserted the id of 1 into your table.
    – mustaccio
    Commented Jan 6, 2023 at 1:24
  • i tried re-doing everything from scratch and running it fro the first time, it still says the same error
    – user266713
    Commented Jan 6, 2023 at 2:44

1 Answer 1

0

When a field is autoincrement, you generally don't write to it. Change your query to

INSERT INTO `users` (`username`,`password`,`org_id`,`phonenum`)
VALUES ('USER','abcd', 1, 1);

In this you are specifying the field names and their values.

6
  • it didnt work : ERROR 1064 (42000) at line 13: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''id','username','password','org_id','phonenum') VALUES ('USER','abcd' at line 1 mysql Ver 8.0.31 for macos12 on arm64 (MySQL Community Server - GPL)
    – user266713
    Commented Jan 6, 2023 at 1:24
  • Oops, they should have been backticks, have corrected it. Commented Jan 6, 2023 at 1:25
  • ERROR 1136 (21S01) at line 13: Column count doesn't match value count at row 1 now it says the count doesnt match, this is throwing me off i thought auto increment should account for this
    – user266713
    Commented Jan 6, 2023 at 2:44
  • No, its saying that the number of row names (in this 4) does not match the count of items in the values. They do in the sample above. Do they match in your sql ? Commented Jan 6, 2023 at 2:52
  • You're right! I wasn't removing the id column from the left hand side, thank you so much!
    – user266713
    Commented Jan 6, 2023 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.