Recall from our earlier definition that a database is a collection of structured information that can be efficiently accessed and modified. Databases structure their data by storing it into tables. A table is a combination of columns and rows in the form of a two-dimensional grid. (Sometimes a table's columns are referred to as fields; in this book, we'll use the word columns exclusively; we'll also refer to a table's rows as records.) Each column corresponds to an attribute of the data, whereas each row corresponds to an actual data item. Furthermore, each table is assigned a unique name to differentiate it from other tables in the database. To clarify this concept, imagine that we wanted to use a database to store information about customers. Because we plan to store customer information in this database table, let's name the table Customers. To decide how to store the customer information, we must first decide what information, specifically, describes a customer. For this example, assume that we need to store the customer's name, phone number, and ZIP code, as well as the date the customer made his first purchase from our fictitious company. These customer attributes make up the columns of the Customers table. Figure 13.1 shows a graphical representation of a table designed to store information about customers. Figure 13.1. The Customers table's columns represent attributes of the customer.
Now, imagine that our company has five customers (it's amazing that it stays in business!). These five customers and their associated data might be
Each of these five customers would be represented by one record in the Customers table. Figure 13.2 graphically represents the Customers table after these five records have been added to the table.
Examining a Table's ColumnsThe columns of a database table, like variables in Visual Basic, have a name and a type. In our Customers table example, the names of the four database columns might be Name, Phone, ZipCode, and DateBecameCustomer. In addition to their names, these columns each have a type, which specifies the type of data that can be stored in the column. For example, the Name column would likely have a type of nvarchar(50); the Phone column, a type of nvarchar(12); the ZipCode column, a type of nvarchar(5); and the DateBecameCustomer column, a type of datetime. Despite the type name differences, table columns can have types quite similar to the types that Visual Basic variables can have. That is, the type nvarchar(50) is akin to a String type in Visual Basic, where the string can have, at most, 50 characters. The datetime table column type is synonymous with Visual Basic's DateTime type. In addition to the nvarchar(n) and datetime types, there are a number of other types. Table 13.1 summarizes some of the more common table column types and their Visual Basic parallels.
When we create a database table later in this hour, you will see that there are many more column types than those listed in Table 13.1. However, the types presented in Table 13.1 are the ones you'll find yourself using the most of the time. You can find a complete list of SQL Server 2005's data types at http://msdn.microsoft.com/library/en-us/tsqlref/ts_da-db_7msw.asp.
Regardless of the data type being used, database columns can store a special value referred to as Null. The Null value indicates an "unknown" value. For example, imagine that you had a database table that captured information about customers, such as their name, payment information, shipping address, and so on. If, when entering the customer's information, you didn't know her phone number, you could use a value of Null. When defining a column, in addition to its name and data type, you can also specify whether the column can be set to the Null value. In some circumstances you might not want to allow an "unknown" value to be entered, in which case you can mark the column to not allow Nulls. Primary Key ColumnsIn addition to the various table columns that store pertinent data, database tables often contain a primary key column in addition to their other columns. A primary key column is typically a column of type int that has some special flags set. (We'll see how, specifically, to add a primary key column to a database table in the "Creating Database Tables" section.) A column that is marked as a primary key requires that each record in the table have a unique value for that column. For this reason, primary key columns are often given the name TableName ID. Primary key columns of type int can be marked as Auto-increment columns. An Auto-increment primary key column automatically assigns each record in the table with an increasing integer value. That is, when you add a new record to the table, you can't specify the value for the Auto-incrementing column; it will automatically be given the next sequentially increasing number. To make sense of this information, let's return to our Customers table example and add a primary key. Because primary key columns are usually named TableName ID, let's call the Customers primary key CustomerID. With the addition of this new column, the Customers table would have the structure shown in Figure 13.3. Figure 13.3. A primary key column has been added to the Customers table.
Now, if we were to insert the five records examined earlier, the table's rows would have the data shown in Figure 13.4. Note that the value in the Auto-increment primary key column is unique and increasing for each row in the Customers table. Furthermore, realize that when inserting data, we would not specify the value for the CustomerID column; rather, the database system would automatically do this for us. Figure 13.4. Each row contains a unique CustomerID value. |