What Is Referential Integrity Inwards Database Or Sql - Mysql Illustration Tutorial

Referential Integrity is laid of constraints applied to unusual fundamental which prevents entering a row inwards pocket-size fry tabular array (where yous convey unusual key) for which yous don't convey whatever corresponding row inwards bring upwards tabular array i.e. entering NULL or invalid unusual keys. Referential Integrity prevents your tabular array from having  incorrect or incomplete human relationship e.g. If yous convey ii tables Order in addition to Customer where Customer is bring upwards tabular array with primary key customer_id in addition to Order is pocket-size fry tabular array with unusual fundamental customer_id. Since every bit per describe of piece of job concern rules yous tin arrive at the axe non convey an Order without a Customer in addition to this describe of piece of job concern dominion tin arrive at the axe last implemented using referential integrity inwards SQL on relational database. Referential Integrity volition drive failure on whatever INSERT or UPDATE SQL disceptation changing value of customer_id inwards pocket-size fry table, If value of customer_id is non acquaint inwards Customer table. By the agency What is Referential Integrity inwards SQL is likewise an of import SQL query similar to finding minute highest salary inwards SQL or difference betwixt truncate in addition to delete  and should last prepared good earlier going for whatever chore interview, where noesis of SQL is i of the requirement.

Referential Integrity trial inwards MySQL tables:

Referential Integrity is laid of constraints applied to unusual fundamental which prevents entering What is Referential Integrity inwards Database or SQL - MySQL Example Tutorial
Another trial of Referential Integrity is Employee in addition to Department relationship. If nosotros convey dept_id every bit unusual fundamental inwards Employee tabular array than past times using referential integrity constraints nosotros tin arrive at the axe avoid creating  Employee without subdivision or non existing department. In curt Referential Integrity makes primary fundamental unusual fundamental human relationship viable. Let's outset create Employee in addition to Department tabular array with primary key, unusual fundamental in addition to referential Integrity constraints.


CREATE TABLE Department (dept_id INT NOT NULL,
                         dept_name VARCHAR(256),                      
                         PRIMARY KEY (dept_id)) ENGINE=INNODB;

CREATE TABLE Employee (emp_id INT NOT NULL,
                       emp_name VARCHAR(256),
                       dept_id INT,
                       FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
                       ON DELETE CASCADE) ENGINE=INNODB;

Above SQL statements volition create both Department in addition to Employee table. dept_id is similar a shot unusual fundamental inwards Employee table. In this SQL, piece creating unusual fundamental nosotros convey specified ON DELETE clause which tells, what needs to done when a tape from bring upwards tabular array is deleted. CASCADE referential activity allows to delete or update all matching rows from pocket-size fry table, later on deleting a tape inwards bring upwards table. This agency Refrential Integrity save information integrity of relationship.

Let's run into How Referential Integrity disallow INSERT in addition to UPDATE for a tape inwards pocket-size fry tabular array for which at that spot is no matching tape inwards bring upwards table. To banking corporation check this Refrential Integrity trial execute next MySQL queries :

INSERT INTO Department VALUES (1, "Sales");
INSERT INTO Employee VALUES (101, "Rajeev", 2)

mysql> INSERT INTO Employee VALUES (101, "Rajeev", 2)
    -> ;
ERROR 1452 (23000): Cannot ADD OR UPDATE a pocket-size fry row: a FOREIGN KEY constraint fails (`test`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES `department` (`dept_id`) ON DELETE CASCADE)

When nosotros inserted outset tape inwards Department tabular array it ran fine only when nosotros insert a tape inwards Employee tabular array with dept_id = 2 which is not acquaint inwards Department i.e. bring upwards table, failed to Referential integrity or unusual fundamental constraint check.

If yous alter your query in addition to right dept_id to 1, query volition run fine, every bit shown below

mysql> INSERT INTO Employee VALUES (101, "Rajeev", 1);
Query OK, 1 row affected (0.05 sec)

Now let's delete our alone tape from Department tabular array in addition to run into if matching records on pocket-size fry tabular array is automatically deleted or not.

mysql> DELETE FROM Department;
Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM Employee;
Empty SET (0.00 sec)
You run into at that spot is no tape inwards Employee tabular array because of ON DELETE CASCADE, matching records inwards pocket-size fry tabular array is delete. Similarly yous tin arrive at the axe usage ON UPDATE CASCADE to automatically propagate UPDATE from bring upwards tabular array to pocket-size fry tables.

Advantage of Referential Integrity inwards RDBMS in addition to SQL

There are several exercise goodness of Referential Integrity inwards relational database in addition to maintaining integrity of information alongside bring upwards in addition to pocket-size fry tables. Here are unopen to of the close noticed advantages of Referential Integrity inwards SQL:

1) Referential Integrity prevents inserting records with wrong details inwards table. Any insert or update functioning volition neglect if it doesn't satisfy referential integrity rule.

2) If a records from bring upwards tabular array is deleted, referential integrity allows to delete all related records from pocket-size fry tabular array using cascade-delete functionality.

3) Similar to minute payoff if a tape i.e. customer_id of a client is updated on bring upwards tabular array (Customer table) , Referential Integrity helps to update customer_id inwards pocket-size fry tabular array (Order) using cascade-update.

That's all on What is referential integrity inwards database, SQL in addition to particularly inwards MySQL. We convey seen trial of How referential integrity or unusual fundamental constraint industrial plant inwards MySQL. We convey likewise seen trial of CASCADE DELETE which automatically delete matching records shape pocket-size fry table.

Further Learning
SQL query to respect all tabular array names from database inwards MySQL

Sumber https://javarevisited.blogspot.com/

0 Response to "What Is Referential Integrity Inwards Database Or Sql - Mysql Illustration Tutorial"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel