Database Reference
In-Depth Information
understand, especially when you code the transaction-related activity in a C# application later in this
book.
Warning Using ROLLBACK and COMMIT inside stored procedures typically requires careful consideration of what
transactions may already be in progress and have led to the stored procedure call. The example runs by itself, so
you don't need to be concerned with this here, but you should always consider whether it's a potential issue.
Try It: Creating a Parent-Child Relationship
Before you code a transaction, let's create two tables.
1. Open SQL Server Management Studio, and in Object Explorer, select the
previously created database SQL2012Db, right-click, and click New Query.
2. Enter the SQL statement in Listing 8-1 to create tables with a primary key and
foreign key (in other words, the parent-child relationship). The Person table
will have a primary key column that will be referenced by the PersonDetails
table via a foreign key column.
Listing 8-1. Create Parent-Child Relationship create table Person
(
PersonID nvarchar(5) primary key not null,
FirstName nvarchar(10) not null,
Company nvarchar(15)
)
create table PersonDetails
(
PersonID nvarchar(5) foreign key references dbo.Person(PersonID),
Address nvarchar(30)
)
3. Now click Execute. You will see the status as “Command(s) completed
successfully,” as shown in Figure 8-2.
 
 
Search WWH ::




Custom Search