Creating the Example Tables
In order to follow along with the examples, you need the example database tables. Everything you need to get up and running can be found on this book’s web page at http://www.forta.com/books/0672337924/.
The web page contains two SQL script files that you may download. These files are plain text files that contain SQL statement that create and populate the example tables we’ll be using. Download both files:
create.sql contains the T-SQL statements to create the six database tables (including the statements to define all primary keys and foreign key constraints).
populate.sql contains the SQL INSERT statements used to populate these tables.
After you have downloaded the scripts, you can use them to create and populate the tables needed to follow along with the lessons in this book. Here are the steps to follow:
To be safe, we’ll use a dedicated database to learn T-SQL, this way you won’t be able to mistakenly access or change other data. In Microsoft SQL Server Management Studio, right click on Databases in the Object Explorer and select New Database . . . to display the New Database dialog. Specify learnsql as your new database name (or pick any name of your choice, but do not use any existing database name, just to be on the safe side). You can ignore all other options in the dialog box and just click OK to create the database.
Now that you have a database, you can create the example tables. Click New Query to open a query window. Make sure the new database is selected from the list of databases from the drop-down list (or you’ll create tables in the wrong database).
First you need to execute the create.sql script. You may simply copy and paste the entire contents of the file into the query window, or you can use the File menu options to open create.sql directly. When you have the SQL code in the Query Window, click the Execute button to run the script and create the tables.
Repeat the previous step using the populate.sql file to populate the new tables. Again, make sure you have the right database selected.
And with that you should be good to go!