Chapter 4: Implementing SQL Server Databases and Tables Lesson 2: Identifying Data Types Exercise 2: Identifying Column Data Types
page 153
To identify the data types for the Authors table
Refer to the data constraints that you identified for the Authors table when you developed your database design.Which data constraints apply to the AuthorID column of the Authors table?The column must contain a value, and the value must be unique.
Review the database design and the data constraints for the FirstName and LastName columns.What type of data will you store in this column?Character data
Review the database design and the data constraints for the YearBorn and YearDied columns.You can assume that each column will contain only four characters. Because date and time data types do not include a year-only data type, you decide to use a character data type.Which data type should you use for the YearBorn and YearDied columns?char(4), although which data type to use is not always a clear-cut decision
Review the database design and the data constraints for the Description column.What type of data will you store in this column?Character data
page 154
To identify the column data types for tables in the BookShopDB database
Identify the data type for each column.What is the data type for each column in the BookShopDB tables?
| Note | It is sometimes difficult to predict exactly what length you should use for data types such as char and varchar. You can get a feel for lengths, however, by reviewing column properties in existing databases, such as the Pubs database or the Northwind database. |
The following table lists the suggested data types for each column in the tables:
Table | Column | Data Type |
|---|---|---|
Books | TitleID | char(8) |
Title | varchar(70) | |
Publisher | varchar(50) | |
PubDate | char(4) | |
Edition | char(4) | |
Cost | money | |
SRP | money | |
ConditionID | tinyint | |
Sold | bit | |
BookCondition | ConditionID | tinyint |
ConditionName | char(10) | |
Description | varchar(50) | |
Authors | AuthorID | smallint |
FirstName | varchar(30) | |
LastName | varchar(30) | |
YearBorn | char(4) | |
YearDied | char(4) | |
Description | varchar(200) | |
BookAuthors | AuthorID | smallint |
TitleID | char(8) | |
Employees | EmployeeID | smallint |
FirstName | varchar(30) | |
LastName | varchar(30) | |
Address1 | varchar(60) | |
Address2 | varchar(60) | |
(Employees, continued) | City | varchar(15) |
State | char(2) | |
Zip | varchar(12) | |
Phone | varchar(24) | |
DOB | datetime | |
HireDate | datetime | |
PositionID | tinyint | |
Positions | PositionID | tinyint |
Title | varchar(30) | |
JobDescrip | varchar(80) | |
Customers | CustomerID | smallint |
FirstName | varchar(30) | |
LastName | varchar(30) | |
Phone | varchar(24) | |
Address1 | varchar(60) | |
Address2 | varchar(60) | |
City | varchar(15) | |
State | varchar(7) | |
Zip | varchar(12) | |
Orders | OrderID | smallint |
CustomerID | smallint | |
EmployeeID | smallint | |
Amount | money | |
OrderDate | datetime | |
DeliveryDate | datetime | |
PaymentID | tinyint | |
StatusID | tinyint | |
OrderStatus | StatusID | tinyint |
StatusDescrip | varchar(25) | |
FormOfPayment | PaymentID | tinyint |
PaymentDescrip | varchar(12) | |
BookOrders | OrderID | smallint |
BookID | char(8) |
| Note | Notice that the State column in the Customers table uses the varchar(7) data type rather than char(2), as in the Employees table. Because a value is not required for this column in the Customers table, a default value of 'unknown' will be defined (rather than permitting a null value). Nullability and default values are discussed in more detail in Lesson 3. USE bookshopdb |