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

SQL - Composite Key

In SQL Server, a composite key is a key that consists of two or more columns in a table. It is used to uniquely identify a row by combining the values of multiple columns. Each column within the composite key contributes to the uniqueness of the key when taken together as a whole.

The composite key ensures that the combination of values in the specified columns is unique within the table. This means that no two rows can have the same combination of values for all the columns in the composite key.

Let's illustrate the concept of a composite key with an example:

Suppose we have a table called "Orders" with the following columns: OrderID, CustomerID, and ProductID. We want to ensure that each order is uniquely identified by the combination of CustomerID and ProductID. In this case, we can create a composite key on the CustomerID and ProductID columns.

Here's an example of creating a table with a composite key in SQL Server:


CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    ProductID INT,
    -- Creating a composite key on CustomerID and ProductID
    CONSTRAINT PK_Orders PRIMARY KEY (CustomerID, ProductID)
);

In this example, we define a composite key constraint using the PRIMARY KEY keyword and specifying the CustomerID and ProductID columns within parentheses. This ensures that the combination of CustomerID and ProductID is unique for each row in the table.

Let's insert some sample data into the "Orders" table to see how the composite key works:


INSERT INTO Orders (OrderID, CustomerID, ProductID)
VALUES (1, 101, 201);

-- This insert will succeed since the combination (102, 201) is unique
INSERT INTO Orders (OrderID, CustomerID, ProductID)
VALUES (2, 102, 201);

-- This insert will fail since the combination (101, 201) already exists
INSERT INTO Orders (OrderID, CustomerID, ProductID)
VALUES (3, 101, 201);

In the above example, the first two inserts succeed because they have unique combinations of CustomerID and ProductID. However, the third insert fails because it violates the uniqueness constraint imposed by the composite key.

By using a composite key, we can ensure that each order in the "Orders" table is uniquely identified by the combination of CustomerID and ProductID, providing data integrity and preventing duplicate entries based on multiple columns.