问题
I am developing a project based on XML. I use the Sedna database to store my collection (which contains XML files, and their XSD schema files).
I define the primary/unique keys in those schemes, but till now I can insert duplicate values (via XQuery update insert command) into primary key field.
回答1:
To guarantee uniqueness constraint you should create BEFORE INSERT FOR EACH NODE trigger on proper path. In the trigger action the $NEW
transitive variable can be used to fetch the new key and check if it already exists in the document (see examples in the manual). To raise error fn:error function can be used.
Take the following note regarding triggers:
"It is prohibited to use prolog in statements of the trigger action" — Sedna Programmer's Guide, XQuery Triggers.
See also bug 51 (although, already closed).
来源:https://stackoverflow.com/questions/41141317/uniqness-insertion-in-native-xml-database