What is Integrity constraint violation in SQL
×


What is Integrity constraint violation in SQL

0 710

This exception occurs when our code attempts to perform a SQL operation that would violate an integrity constraint defined in the database schema. This error could occur for a variety of reasons, such as attempting to insert a duplicate primary key value into the table or failing to provide values for one or more required columns.

Let's run through an example to understand it better-

CREATE TABLE abc_table(
  user_name varchar(255) NOT NULL  PRIMARY KEY,
  password varchar(255)
);


$user_name="xyz ";
$data=array('user_name'=>$user_name);
DB::table('abc_table')->insert($data);


$data=array('user_name'=>$user_name);
DB::table(' abc_table ')->insert($data);

In the above example abc_table has a column user_name with the primary key constraint.

We are trying to insert duplicate value in the user_name column thus it is giving us an un-called error.



Best WordPress Hosting


Share:


Leave a Reply


Comments
    Waiting for your comments