SQL Server BasicsWhat is SQL Server database?What is RDBMS?What is Normalization?Why we use Denormalization?What_is_SQL?What is PL/SQL?Difference between SQL and PL/SQLDatabase TableOne to One RelationshipOne to Many RelationshipMany to Many RelationshipMany to One RelationshipString Data TypesNumber Data TypesDate Data TypesOther Data TypesCreate DatabaseDrop DatabaseCreating and Managing Users in SQL ServerCreate TableAlter TableDrop TableConstraints in SQL serverPrimary KeyForeign KeyUnique KeyCandidate KeyComposite KeyDifference between primary key and candidate keyPrimary key and foreign key relationshipSurrogate KeyCascading Referential Integrity ConstraintsSelf Referential Integrity ConstraintsInsert into statementInsert multiple recordsUpdate statementDelete statementTruncate statementDifference between Delete and TruncateAlias in SQL ServerSelect statementSelect DistinctSelect TopSelect IntoNull Functions(ISNULL(),NULLIF(),COALESCE())Sub QueryIdentity ColumnSequence objectDifference between sequence and identity columnSQL Server ClausesWHERE ClauseOrder By ClauseTop N ClauseGroup By ClauseHaving ClauseDifference between Where and HavingSQL Server OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAny OperatorsAll OperatorsUnion OperatorsUnion All OperatorsDifference between Union and Union AllIntersect OperatorExcept OperatorDifference between Except and IntersectJoinsInner JoinLeft JoinRight JoinFull JoinSelf JoinCross JoinViewsWhat are views?Create views using SSMSIndexed ViewsComplex ViewsCheck Option in ViewCheck Encryption in ViewSchema Binding Option in ViewRead-only ViewsUpdatable ViewsAdvantages and disadvantages of viewsCreate multiple views on one tableCan we implement index on views?Can we Perform Insert, update, delete operation on views?Stored Procedure and FunctionsWhat are Stored Procedures?Why we use stored procedures?Passing parameters to Stored procedureUser-Defined FunctionsDifference between UDF and Stored procedurePre-Defined Functions@@Indentity and Scope_IndentityNULLIF, ISNULL and COALESCE

What is Self Referential Integrity Constraint in SQL Server?

A self-referential integrity constraint in SQL Server is used when a table needs to establish a relationship with itself. It means that a column in the table references another column within the same table. This type of constraint is often used to model hierarchical or recursive data structures.

To implement a self-referential integrity constraint, you need to define a foreign key relationship within the same table, where the foreign key column refers to the primary key column in the same table.

Here's an example to illustrate a self-referential integrity constraint:


CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    EmployeeName VARCHAR(50),
    ManagerID INT,
    CONSTRAINT FK_Employees_Manager FOREIGN KEY (ManagerID) REFERENCES Employees(EmployeeID)
);

In this example, the "Employees" table represents employee data, and it has a self-referential integrity constraint defined by the foreign key constraint FK_Employees_Manager. The ManagerID column is used to establish the relationship between employees and their managers. The ManagerID column references the EmployeeID column within the same table.

Let's insert some sample data to demonstrate the self-referential relationship:


INSERT INTO Employees (EmployeeID, EmployeeName, ManagerID) VALUES (1, 'John', NULL);
INSERT INTO Employees (EmployeeID, EmployeeName, ManagerID) VALUES (2, 'Alice', 1);
INSERT INTO Employees (EmployeeID, EmployeeName, ManagerID) VALUES (3, 'Bob', 1);
INSERT INTO Employees (EmployeeID, EmployeeName, ManagerID) VALUES (4, 'Sarah', 3);

In this example, we have John as the top-level manager (with NULL as the manager ID), Alice and Bob as direct reports to John, and Sarah as a direct report to Bob.

By using the self-referential integrity constraint, we can enforce the relationship between employees and their managers. Each employee's ManagerID must reference a valid EmployeeID in the same table. If an employee is deleted or updated, the constraint ensures that the integrity of the relationship is maintained.

For example, if we try to delete an employee who is a manager of other employees:


DELETE FROM Employees WHERE EmployeeID = 1;

This operation will fail because the foreign key constraint restricts the deletion of an employee who has direct reports.