Check constraints on XML columns
A check constraint allows you to place certain restrictions on XML columns. The constraint is enforced whenever an attempt is made to insert or update data in the XML column; only when the criteria specified by the constraint evaluate as true is the operation performed.
When working with XML documents, an important consideration is whether or not these documents have previously been validated against XML schemas. If you need to ensure that you query, insert, update or delete only those documents that meet certain validation criteria, use the VALIDATED predicate to provide your criteria. Note that a check constraint never validates XML documents, it only tests whether or not XML documents have already been validated.1
The VALIDATED predicate checks the validation state of the value specified by XML-expression, which must have an XML data type. If the optional according-to-clause is not specified, then the XML schema used for validation does not impact the result. Check constraints do not validate the XML documents themselves; only the current validation state of the document is tested by the constraint (IS VALIDATED or IS NOT VALIDATED). If the according-to-clause is specified, then the XML schema used to validate the value specified by XML-expression must be an XML schema identified by the according-to-clause. XML schemas need to be registered with the XML schema repository before they can be referenced in a VALIDATED predicate.
- Check constraints have a dependency on the XML schemas they reference. If the XSR object of an XML schema is dropped, any constraint that references the schema is also dropped.
- XML columns support NOT NULL constraints.
- XML columns support information constraints defined for XML validation.
Evaluation of check constraints
Check constraints test the validation state of documents based on the outcome of the IS VALIDATED predicate. If the condition you specified is satisfied, the constraint evaluates as true; if not satisfied, the outcome evaluates as false. If the value specified by XML-expression is null, the result of the predicate is unknown.
- an according-to-clause was not specified and the value specified by XML-expression has been validated OR
- an according-to-clause was specified and the value specified by XML-expression has been validated using one of the XML schemas identified by the according-to clause.
- an according-to-clause was not specified and the value specified by XML-expression has not been validated OR
- an according-to-clause was specified and the value specified by XML-expression has not been validated using one of the XML schemas identified by the according-to clause.
In those cases where the optional according-to-clause is specified, IS NOT VALIDATED will return true when the value specified by XML-expression has not been validated or the value specified by XML-expression has been validated but not according to any of the specified XML schemas.
Equivalence of expressions
value1 IS NOT VALIDATED optional-clause
NOT(value1 IS VALIDATED optional-clause)
Examples
SELECT XMLCOL FROM T1
WHERE XMLCOL IS VALIDATED
ALTER TABLE T1 ADD CONSTRAINT CK_VALIDATED
CHECK (XMLCOL IS VALIDATED)
CREATE TABLE xmltab (ID INT,
DOC XML, CONSTRAINT INFO_CONSTRAINT CHECK (DOC IS VALIDATED) NOT ENFORCED)
Informational
constraints are used to improve query performance.