2
Foreign Keys in Microsoft SQL Server One of the most important concepts in databases is creating relationships between database tables. These relationships provide a mechanism for linking data stored in multiple tables and retrieving it in an efficient fashion. In order to create a link between two tables, you must specify a foreign key in one table that references a column in another table. Database Ta bles and Relationships Yo u might already know that databases are simply a series of tables, similar to what you might already use in a spreadsheet program, such as Microsoft Ecel. In fact, you can even convert an Ecel spreadsheet to a database . !here databases diverge from spreadsheets, however, is when it comes to building powerful relationships between tables. "onsider, for eample a database used by a company to track human resources information. That database might have a table called Employees that contains the following information for each member of the company#s staff $ Employee I% &irst'ame (ast'ame Office)hone *ome)hone )ositionI% In this eample, the employee I% is a uni+uely generated integer that is a ssigned to each employee when they are added to the database. The position I% is a ob code used to reference the employee#s position in the company. In this scheme, an employee may only have one position, but multiple -or no employees may fill each position. &or eample, you might have hundreds of employees with a /"ashier0 position. The database might also contain a table called )ositions with the following additional information about each position$ )ositionI% Title 1ob(evel 2kill"ategory (ocation The )osition I% field in this table is similar to the Employee I% field in the Employees table 3 it is a uni+uely generated integer that is created when a position is added to the database. !hen we go to p ull a listing of employees from the database, it would be natural to re+uest each p erson#s name and their title. *owever, this information is stored in multiple

Foreign Keys in Microsoft SQL Server

Embed Size (px)

Citation preview

Page 2: Foreign Keys in Microsoft SQL Server

8/10/2019 Foreign Keys in Microsoft SQL Server

http://slidepdf.com/reader/full/foreign-keys-in-microsoft-sql-server 2/2