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
Parts
» This page intentionally left blank
» Reporting and Data Mining Database Applications
» Database Applications and SQL
» Reading Specified Columns and Rows from a Single Table
» “Does Not Work with Microsoft Access ANSI-89 SQL”
» Processing SQL Statements in Microsoft Access 2010
» Using SQL in Microsoft SQL Server 2008 R2
» Using SQL in Oracle Database 11g
» Using SQL in Oracle MySQL 5.5
» Wildcards in SQL WHERE Clauses
» Using SQL Built-in Functions
» SQL Expressions in SQL SELECT Statements
» Querying Multiple Tables with Subqueries
» Querying Multiple Tables with Joins
» Comparing Subqueries and Joins
» Finding Functional Dependencies
» Eliminating Anomalies from Multivalued Dependencies
» The Multivalue, Multicolumn Problem
» The General-Purpose Remarks Column
» R Diagrams Using the IE Crow’s Foot Model
» The Multivalued Attribute Pattern
» The Archetype/Instance Pattern
» The Student Acceptance Letter
» X This is a warning, no further action is required.
» 1:1 Relationships Between Strong Entities
» M Relationships Between Strong Entities
» Relationships in Mixed Entity Designs
» Representing Ternary and Higher-Order Relationships
» Relational Representation of the Highline University Data Model
» Surrogate Key Database Design
» Column Properties for the View Ridge Database Design Tables
» Variations in SQL Data Types
» Implementing Data Constraints
» Populating the View Ridge Database Tables
» Using Triggers to Provide Default Values
» The WORK_AddWorkTransaction Stored Procedure
» • If a PROJECT row is deleted, then the project has been canceled, and it is unneces-
» Reducing Cardinalities (with Data Loss)
» Optimistic Versus Pessimistic Locking
» Declaring Lock Characteristics
» Processing Rights and Responsibilities
» Recovery via Rollback/Rollforward
» Maintaining the Data Repository
» Types of Distributed Databases
» • Express Edition. This free, feature-limited version is available for download. It
» SQL Server 2008 R2 SQL Statements and SQL Scripts
» Creating the View Ridge Database Table Structure
» Populating the VRG Tables with Data
» The Stored Procedure InsertCustomerAndInterests
» The Stored Procedure InsertCustomerWithTransaction
» A Trigger for Setting Default Values
» A Trigger for Enforcing a Data Constraint
» A Trigger for Enforcing a Required Child Constraint
» Creating an ODBC Data Source Name
» Materializing XML Documents with XSLT
» Using the SQL SELECT . . . FOR XML Statement
» Multitable SELECT with FOR XML
» A Schema with Two Multivalued Paths
» Problems with Operational Data
» Using SQL for Market Basket Analysis
Show more