Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 20th, 2007, 05:38 AM
Authorized User
 
Join Date: Oct 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Why not using a unique constraint in ch 5?

Hi,

 In the [sprocCategoryInsertUpdateSingleItem] stored procedure there is a block of code:
IF (@id IS NULL) -- Insert a new item
BEGIN
  IF NOT EXISTS (SELECT Id FROM Category WHERE Description = @description AND ContentTypeId = @contentTypeId)
  BEGIN
    INSERT INTO Category(Description, ContentTypeId, SortOrder) VALUES(@description, @contentTypeId, @sortOrder)
    SET @returnValue = SCOPE_IDENTITY()
  END
  ELSE
  BEGIN
    SET @returnValue = -1 -- record already exists
  END
END

Why we don't use a unique constraint on the fields instead?

 
Old September 20th, 2007, 06:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You could indeed use a unique constraint to have the database enforce uniqueness.

However, IMO, you would still need to do the check manually. Relying on the constraint results in exceptions being thrown when the item already exists. By manually checking the description you can avoid handling run-time exceptions when they aren't necessary....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 20th, 2007, 09:25 AM
Authorized User
 
Join Date: Oct 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok :D






Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I have a constraint like this.....? callagga SQL Language 7 August 13th, 2008 01:08 AM
Constraint Vs Index carumuga SQL Server 2000 2 December 4th, 2007 02:14 AM
Insert Record Fails W/ Unique Constraint snw C# 0 September 2nd, 2005 02:14 PM
duplicate nulls in a column with unique constraint defiant SQL Server 2000 3 October 22nd, 2003 01:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.