Populating the VRG Tables with Data

Populating the VRG Tables with Data

You can enter data into SQL Server either by entering data into a table grid in the Microsoft SQL Server Management Studio GUI display or by using SQL INSERT statements. The Microsoft SQL Server Management Studio GUI display is more useful for occasional data edits than for populating all the tables of a new database. You can open a table grid for data entry by right-clicking the table name to display a shortcut menu, and then clicking the Edit Top 200 Rows command.

However, we will use the same method for populating the VRG database tables that we used to create the table structure: an SQL script. But before we do that, we need to address the surrogate key values issue raised in Chapter 7. The data shown in Figure 7-16 is sample data, and the primary key values of CustomerID, ArtistID, WorkID, and TransactionID shown in that figure are nonsequential. Yet, the Transact-SQL IDENTITY(seed, increment) property that we use to populate SQL Server surrogate primary keys creates sequential numbering.

This means that if we write and execute SQL INSERT statements to put the artist data shown in Figure 7-16 (b) into the ARTIST table, the values of ArtistID that will be added to the table will be (1, 2, 3, 4, 5, 6, 7, 8, 9) instead of the values of (1, 2, 3, 4, 5, 11, 17, 18, 19) listed in the figure. How can we enter the needed nonsequential values?

The answer is the Transact-SQL IDENTITY_INSERT property. When IDENTITY_INSERT is set to OFF (the default setting), only the SQL Server DBMS can enter data into the controlled ID column in the table. When IDENTITY_INSERT is set to ON, values can be input into the controlled column in the table. However, IDENTITY_INSERT can only be set to ON for only one table at a time. Further, IDENTITY_INSERT requires the use of a column list containing the name of the

Figure 10-24

surrogate key in each INSERT command.

The Completed New Index Dialog Box

The New Index dialog box

The General page The Index name

The included columns The OK button

Part 4 Multiuser Database Processing

Thus, instead of using an SQL INSERT statement that automatically enters the surrogate value, such as:

/* *** EXAMPLE CODE - DO NOT RUN *** */ INSERT INTO ARTIST VALUES('Miro', 'Joan', 'Spanish', 1893, 1983);

we have to use a set of SQL statements similar to the following:

/* *** EXAMPLE CODE - DO NOT RUN *** */ SET IDENTITY_INSERT dbo.ARTIST ON INSERT INTO ARTIST

(ArtistID, LastName, FirstName, Nationality, DateOfBirth, DateDeceased) VALUES (1, 'Miro', 'Joan', 'Spanish', 1893, 1983);

SET IDENTITY_INSERT dbo.ARTIST OFF

Note how we set INDENTITY_INSERT to ON, insert the date, and then set IDENTITY_INSERT to OFF. Of course this is a lot of work if we are inserting one row of data at

a time, but when used in an SQL script that inserts a lot of data into a table it makes sense. So, we will use an SQL script.

The set of SQL INSERT statements needed to populate the VRG database with the View Ridge Gallery data shown in Figure 7-16 is shown in Figure 10-25. Create and save a new SQL script named DBP-e12-VRG-Table-Data.sql based on Figure 10-25, testing it with the Parse command (use the Parse button) until you have corrected any errors. Save the corrected script, and then run the script (use the Execute button) to populate the tables. Close the script window after the script has been successfully run.

Figure 10-25

The SQL Statements to Populate the VRG Tables

Chapter 10 Managing Databases with SQL Server 2008 R2

(continued)

Figure 10-25

Continued

Part 4 Multiuser Database Processing

Figure 10-25

Continued