Database Reference
In-Depth Information
Creating Foreign Keys with the Diagramming Tool
Once you have more than one table in the diagram, you can easily create foreign key constraints between them.
You can accomplish this by dragging and dropping between the columns where you want to place a foreign key
constraint, as shown in Figure 5-29 .
Figure 5-29. Tables with a foreign key constraint
Here are the steps:
1.
Click the foreign key or child column.
2.
With the mouse button still depressed, drag the mouse cursor to the primary key or parent column.
3.
Release the mouse button.
We have seen many struggle with this method; therefore, it may take some practice before you become
comfortable with it. Perhaps one reason for the difficulty is that the diagram tool does not place the foreign key
constraint line directly next to the columns being used but instead shows the connection between the tables
themselves. And although you can move the connecting line toward the columns manually to make it look more
presentable, it is not done automatically. Once you get used to it, you will find this technique is not as confusing
as it may seem at first. And as long as the columns are named similarly, you will easily be able to figure out which
columns are interconnected.
Using the Query Window
You can also use code to create your tables. For some developers, this is the most straightforward way to do so.
Listing 5-6 shows the syntax for creating a table with code.
Listing 5-6. Creating the Fact Table with SQL Code
CREATE TABLE [dbo].[FactSales](
[OrderNumber] [nvarchar](50) NOT NULL,
[OrderDateKey] [int] NOT NULL,
 
Search WWH ::




Custom Search