Tài liệu Exploring the Northwind Database - Pdf 87


Exploring the Northwind Database
A database may have many tables, some of which are related to each other. For example,
the North-wind database contains many tables, four of which are named Customers,
Orders, Order Details, and Products. Figure 2.11
is a repeat of the diagram shown earlier
that illustrates how these tables are related.

Figure 2.11: Relationships between the Customers, Orders, Order Details, and Products
tables
The columns for each table are shown within each box. For example, the Customers table
contains 11 columns:

CustomerID

CompanyName

ContactName

ContactTitle

Address

City

Region

PostalCode

Country


many order details.
One-to-many relationships are modeled using foreign keys. For example, the Orders table
has a column named CustomerID. This column is related to the CustomerID column in
the Customers table through a foreign key. This means that every row in the Orders table
must have a corresponding row in the Customers table with a matching value for the
CustomerID column. For example, if a row in the Orders table has a CustomerID of
ALFKI, then there must also be a row in the Customers table with a CustomerID of
ALFKI. Since the relationship between the Customers and Orders table is one-to-many,
this means that there can be many rows in the Orders table with the same CustomerID
column. Conceptually, you can think of the foreign key as a pointer from the Orders table
to the Customers table.
Often, the table containing the foreign key is known as the child table, and the table with
the column referenced by the foreign key is known as the parent table. For example, the
Orders table is the child table, and the Customers table is the parent table. Foreign key
relationships are often known as parent-child relationships.

Note The relational term from "relational database" comes from the fact that tables can be
related to each other through foreign keys.
You can manage the relationships for a table from Enterprise Manager by selecting the
table from the Tables node, clicking the right mouse button, and selecting Design Table.
You then click the Manage Relationships button on the toolbar of the table designer. For
example, Figure 2.12
shows the relationship between the Customers and Orders tables.

Figure 2.12: Relationship between the Customers and Orders table
The Customers and Orders tables are related through the CustomerID column. The
CustomerID column in the Orders table is the foreign key. The relationship between the
two tables is named FK_Orders_Customers.
Null Values
Databases must also provide the ability to handle values that are not set, or are otherwise

Indexes/Keys button.

Figure 2.13: Indexes for the Customers table
The Customers table has five indexes: one each on the CustomerID, City,
CompanyName, PostalCode, and Region columns.
You'll learn how to add an index to a table in the "Creating an Index" section later.
Column Types
Each column in a table has a specific database type. This type is similar to the type of a
variable in C#, except that a database type applies to the kind of value you can store in a
table column. Table 2.3
lists the SQL Server database types.
Table 2.3: SQL SERVER DATABASE TYPES
TYPE DESCRIPTION
bigint Integer value from -2
63
(-9,223,372,036,854,775,808) to 2
63
-1
(9,223,372,036,854,775,807).
int Integer value from -2
31
(-2,147,483,648) to 2
31
-1 (2,147,483,647).
smallint Integer value from 2
15
(-32,768) to 2
15
-1 (32,767).
tinyint Integer value from 0 to 255.

nchar Fixed-length Unicode characters with a maximum length of 4,000
characters.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status