tags:

views:

22

answers:

1
+2  Q: 

how to use IGNORE?

i have mysql code for input data:

 $sql = "INSERT IGNORE INTO inspection_report ";
  $sql.= "(Model,       Serial_number,       Line,        Shift,     Inspection_datetime,         Range_sampling,       Packing, ";
  $sql.= "Accesories,       Appearance,      Tuner,        General_operation,       Class,      Status,         Remark, ";
  $sql.= "NIK) ";
  $sql.= "VALUES ('";
  $sql.= $Model."','".$Serial_number."', '".$Line."','".$Shift."','".postVar('insp_date')." ".postVar('time')."','".$Range_sampling."','".$Packing."','";
  $sql.= $Accesories."','".$Appearance."','".$Tuner."','".$General_operation."','".$Class."','".$Status."','".$Remark."','";
  $sql.= $NIK."')";

i want to use IGNORE.but how to prevent duplicate only at Model,Serial_number,Line?

+2  A: 

From the documentation

INSERT

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error is issued.

So that tells me that either Model,Serial_number,Line must be the Primary key, or you need to create an Unique index on these columns.

astander
should i change the Model,Serial_number,Line become Unique index?
klox
If Model, Serial_Number, Line is not already your table Primary Key, then yes, you need to create an Unique index for these columns.
astander