T-SQL Tutorial

Relationships in database


In SQL Server, relationships between tables are established and managed to ensure data integrity and facilitate efficient data retrieval. These relationships are defined using foreign keys, which establish connections between tables based on shared columns. The primary purpose of defining relationships in a database is to enforce referential integrity and maintain data consistency.

Here's an overview of how relationships are implemented in SQL Server using T-SQL:


Primary Key and Foreign Key Constraints

The PRIMARY KEY constraint is used to uniquely identify each row in a table. A FOREIGN KEY constraint is used to establish a relationship between two tables by referencing the primary key of another table. Primary keys and foreign keys are defined when creating or altering tables.

Example of creating a table with a primary key:

CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(255)
);


Example of creating a table with a foreign key referencing another table:

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


Enforcing Referential Integrity

When a foreign key relationship is established, it enforces referential integrity, meaning that the values in the foreign key column of the child table (Orders in the example above) must match values in the primary key column of the parent table (Customers in the example above). This prevents orphaned records in the child table and maintains data consistency.


Cascading Actions

SQL Server allows you to define cascading actions to specify what happens when records in the parent table are updated or deleted. Common options include CASCADE, SET NULL, SET DEFAULT, and NO ACTION.

Example of cascading delete:

ALTER TABLE Orders
ADD CONSTRAINT FK_CustomerID
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
ON DELETE CASCADE


Self-Referencing Relationships

In some cases, tables may have relationships with themselves, such as in hierarchical structures. This is achieved by creating foreign keys that reference the same table.

Example of a self-referencing relationship:

CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
ManagerID INT, -- Self-referencing foreign key
EmployeeName VARCHAR(255)
);

ALTER TABLE Employees
ADD FOREIGN KEY (ManagerID) REFERENCES Employees(EmployeeID)


Indexes for Improved Performance

Indexes can be created on columns involved in relationships to improve query performance. Indexes help SQL Server locate and retrieve data more efficiently.

Example of creating an index on a foreign key column:

CREATE INDEX IX_CustomerID ON Orders(CustomerID);


In conclusion, relationships in SQL Server using T-SQL are crucial for maintaining data integrity and facilitating efficient data retrieval. By defining primary keys, foreign keys, and cascading actions, you can ensure that your database remains consistent and supports complex data structures.
Additionally, the use of indexes can further optimize query performance in databases with relationships.