17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Note that I’ve chosen to use a date data type rather than the older standard datetime to save space.<br />

The datetime data type will store both date and time information down to fractions of a second. However,<br />

since we’re primarily interested in the date of hire, not the time, the date will meet our needs and<br />

take up half the space.<br />

The date of termination is something we may not know (we’d like to think that some employees are still<br />

working for us), so we’ll need to leave it nullable:<br />

TerminationDate date NULL,<br />

We absolutely want to know who the employee is reporting to (somebody must have hired them!) and<br />

what department they are working in:<br />

)<br />

ManagerEmpID int NOT NULL,<br />

Department varchar(25) NOT NULL<br />

So, just for clarity, let’s look at the entire script required to create this table:<br />

USE Accounting<br />

CREATE TABLE Employees<br />

(<br />

EmployeeID int IDENTITY NOT NULL,<br />

FirstName varchar(25) NOT NULL,<br />

MiddleInitial char(1) NULL,<br />

LastName varchar(25) NOT NULL,<br />

Title varchar(25) NOT NULL,<br />

SSN varchar(11) NOT NULL,<br />

Salary money NOT NULL,<br />

PriorSalary money NOT NULL,<br />

LastRaise AS Salary - PriorSalary,<br />

HireDate date NOT NULL,<br />

TerminationDate date NULL,<br />

ManagerEmpID int NOT NULL,<br />

Department varchar(25) NOT NULL<br />

)<br />

Chapter 5: Creating and Altering Tables<br />

Be aware that the date, time, and datetime2 data types (as opposed to the more venerable<br />

datetime and smalldatetime data types) are new with <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong>. If you<br />

need to remain backward compatible with previous versions, you’ll need to stick<br />

with the datetime and smalldatetime data types.<br />

Again, I would recommend executing sp_help on this table to verify that the table was created as you<br />

expected.<br />

135

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!