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

Check Option in View

In SQL Server, the CHECK OPTION is used to enforce the integrity of data modifications through a view. It ensures that any updates or inserts made through the view satisfy the underlying SELECT statement's filter conditions. Let's walk through an example to understand how the CHECK OPTION works:

Suppose we have two tables: "Employees" and "Departments." The "Employees" table contains information about employees, including their ID, name, department ID, and salary. The "Departments" table stores department details, such as department ID and department name.

We want to create a view that only allows updates or inserts for employees who belong to the "Sales" department. Here's how we can accomplish that using the CHECK OPTION:

1-Create the Tables and Insert Sample Data:

First, let's create the "Employees" and "Departments" tables and insert some sample data:


CREATE TABLE Departments (
    DepartmentID INT PRIMARY KEY,
    DepartmentName VARCHAR(50)
);

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    EmployeeName VARCHAR(50),
    DepartmentID INT,
    Salary DECIMAL(10, 2),
    FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

INSERT INTO Departments (DepartmentID, DepartmentName)
VALUES (1, 'Sales'), (2, 'Marketing'), (3, 'Finance');

INSERT INTO Employees (EmployeeID, EmployeeName, DepartmentID, Salary)
VALUES (1, 'John Doe', 1, 5000.00),
       (2, 'Jane Smith', 2, 4500.00);

2-Create the View with CHECK OPTION:

Next, let's create a view that filters the employees based on the department. Only employees from the "Sales" department will be visible through this view:


CREATE VIEW SalesEmployees
AS
SELECT EmployeeID, EmployeeName, DepartmentID, Salary
FROM Employees
WHERE DepartmentID = 1
WITH CHECK OPTION;

In this example, the view "SalesEmployees" is created with the CHECK OPTION specified. It filters the employees based on the DepartmentID column, allowing only employees from the "Sales" department (DepartmentID = 1) to be visible through the view.

3-Test the CHECK OPTION:

Now, let's test the CHECK OPTION by attempting to update or insert data through the view:


-- Attempt to update an employee in the "Sales" department
UPDATE SalesEmployees
SET Salary = 5500.00
WHERE EmployeeID = 1;
-- The update will be successful

-- Attempt to update an employee not in the "Sales" department
UPDATE SalesEmployees
SET Salary = 5000.00
WHERE EmployeeID = 2;
-- An error will be raised: The UPDATE statement conflicted with the CHECK constraint...

-- Attempt to insert a new employee not in the "Sales" department
INSERT INTO SalesEmployees (EmployeeID, EmployeeName, DepartmentID, Salary)
VALUES (3, 'Mark Johnson', 2, 4000.00);
-- An error will be raised: The INSERT statement conflicted with the CHECK constraint...

As shown in the example, the UPDATE statement that modifies an employee's salary in the "Sales" department (EmployeeID = 1) is successful. However, the attempt to update an employee not in the "Sales" department (EmployeeID = 2) or insert a new employee from a different department (DepartmentID = 2) results in an error due to the CHECK OPTION. This ensures that the view's filter condition is maintained and prevents unauthorized modifications.

The CHECK OPTION is useful for maintaining data integrity and enforcing business rules through views, ensuring that only valid data modifications are allowed.