Re: Unique Enforcement
  Home FAQ Contact Sign in
microsoft.public.sqlserver.xml only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: Unique Enforcement         

Group: microsoft.public.sqlserver.xml · Group Profile
Author: Denis Ruckebusch [MSFT]
Date: Mar 30, 2007 14:41

As Kent already says SQL Server 2005 doesn't support xsd:unique.

The only way I know to enforce uniqueness within the schema is to use the xs:ID
type. However you have to know that uniqueness is enforced at the "cell" level.
If you use have a table with one typed XML column, an xs:ID value that appears
in one row cannot appear in the same row but can appear in a different row.
If you have a table with multiple typed XML columns, an xs:ID value can appear
only once in each "cell" but can apear on the same row in different columns.

Here's a quick example.

CREATE XML SCHEMA COLLECTION SCID AS '
http://www.w3.org/2001/XMLSchema">












'
go

CREATE TABLE T (iCol int primary key, xmlCol XML(SCID))
go

INSERT INTO T VALUES (0, '')
-- XML Validation: ID constraint check failed. Found attribute named 'a' with
duplicate ID value 'ID000'. Location: /*:e[2]/@*:a

INSERT INTO T VALUES (1, '')
-- succeeds

INSERT INTO T VALUES (2, '')
-- succeeds; we reuse the same ID value but in a different row

UPDATE T SET xmlCol.modify('insert as first into /') WHERE iCol =
1
-- XML Validation: ID constraint check failed. Found attribute named 'a' with
duplicate ID value 'ID001'. Location: /*:e[2]/@*:a

I hope this helps

Denis Ruckebusch
http://blogs.msdn.com/denisruc
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Neal Walters" discussions.microsoft.com> wrote in message
news:F3CAA4AE-EF90-48B6-B1B6-ED20632085F0@microsoft.com...
> Is there a way to enforce uniqueness on an xml element or attribute stored in
> an xml data field? For example, I have a "Submitter" schema, and I want to
> make sure SubmitterId (an XML attribute) is unique.
>
> I thought about "promoting" the field to an SQL column, but I am storing
> multiple schemas in the same XML data column.
>
> Thanks in advance,
>
> Neal Walters
> http://Biztalk-Training.com
> http://CMSTrainingVideos.com
no comments
diggit! del.icio.us! reddit!