Answers for "duplicate entry"

0

duplicate entry

The main reason why the error has been generated is because there is already an existing value of 1 for the column ID in which you define it as PRIMARY KEY (values are unique) in the table you are inserting.

Why not set the column ID as AUTO_INCREMENT?

CREATE  TABLE IF NOT EXISTS `PROGETTO`.`UFFICIO-INFORMAZIONI` (
  `ID` INT(11) NOT NULL AUTO_INCREMENT,
  `viale` VARCHAR(45) NULL ,
   .....
and when you are inserting record, you can now skip the column ID

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`viale`, `num_civico`, ...) 
VALUES ('Viale Cogel ', '120', ...)
Posted by: Guest on July-13-2021
0

duplicate entry

# The main Reason you are grtting this error is because 
#there is a unque flag on the field. lets see an example

# CREATE A NEW TABLE CALLED User;
CREATE TABLE User(
  # this will not trigger duplicates because of the auto_increment option
  id int unsigned primary key auto_increment 
  name VARCHAR(255) not null,
  # this field will always trigger the error if you leave it empty or if you
  # insert a user with same email as the one in the table already. 
  email VARCHRA(255) unique 
  )
  # INSERT DAta INTO THE TABLE.
  INSERT INTO User(name, email) VALUES('Sample Name','[email protected]') # This will pass

  INSERT INTO User(name, email) VALUES('Sample2 Name','[email protected]')   # This will not pass
  # the second insert query will yield an error because the email already exists in the database. 
  # to avoid this error make sure that the unique field is always checked before insert into the table.
Posted by: Guest on September-12-2021

Browse Popular Code Answers by Language