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 Foreign Key in SQL Server?

In SQL Server, a foreign key is a column or a set of columns in a table that establishes a relationship between two tables. It ensures referential integrity by enforcing that the values in the foreign key column(s) exist in the referenced table's primary key column(s).

Here are some key points to understand about foreign keys in SQL Server:

  1. Relationship Establishment: A foreign key establishes a relationship between two tables, typically referred to as the referencing table (child table) and the referenced table (parent table). The foreign key column(s) in the referencing table references the primary key column(s) in the referenced table.
  2. Referential Integrity: The foreign key constraint ensures referential integrity by enforcing that the values in the foreign key column(s) exist in the primary key column(s) of the referenced table. This means that any value in the foreign key column(s) must match an existing value in the referenced table's primary key column(s).
  3. Cascading Actions: SQL Server allows you to specify cascading actions to be performed when a referenced row is updated or deleted. Cascading actions include cascading updates and cascading deletes, which automatically propagate changes to the referencing table when a change is made to the referenced table.
  4. Multiple Foreign Keys: A table can have multiple foreign keys, each representing a different relationship with other tables. Each foreign key references a specific primary key column(s) in the referenced table.

Example:

Let's consider two tables, "Orders" and "Customers," where the "Orders" table has a foreign key relationship with the "Customers" table.


CREATE TABLE Customers (
   CustomerID INT PRIMARY KEY,
   FirstName VARCHAR(50),
   LastName VARCHAR(50),
   Email VARCHAR(100)
);

CREATE TABLE Orders (
   OrderID INT PRIMARY KEY,
   OrderDate DATE,
   CustomerID INT,
   FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

In this example, the "Customers" table has a primary key column "CustomerID," which uniquely identifies each customer. The "Orders" table has a foreign key column "CustomerID" that references the "CustomerID" column in the "Customers" table.

The foreign key constraint FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ensures that any value in the "CustomerID" column of the "Orders" table must exist in the "CustomerID" column of the "Customers" table. This maintains the referential integrity between the two tables, allowing for consistent and reliable relationships between customers and their orders.