Mimicking RDBMS uniqueness constraints
Saturday, 9 January 2016 · 4 messages
EJ
E. Wray Johnson9 Jan 2016 02:52 #
What is the best practice in BaseX to implement a uniqueness constraint similar to a RDBMS? Given a database, a path to the documents, an optional RegEx expression on files in the path, and one or more XPaths to the constraint elements/attributes. Can I create an index of this nature? Can the index not allow duplicates?
CG
Christian Grün9 Jan 2016 18:52 #
Hi E. Wray,
Quoted text · E. Wray Johnson · 2 lines
> What is the best practice in BaseX to implement a uniqueness > constraint similar to a RDBMS?
Using UUIDs has become our favorite solution. An alternative is to assign an incrementing integer, and e.g. store the highest assigned value in the root node. If you want to assign arbitrary values, you’ll need to check if the value has already been assigned:
let $id := 'sjdhsj'
return if(//item[@id = $id])) then (
error( (), "ID is not unique: " || $id)
) else ( ... )If your database has an up-to-date attribute index, this should be pretty fast.
Christian
CG
Quoted text · E. Wray Johnson · 3 lines
> We are already using UUIDs for file names. However we want, for > example, guaranteed uniqueness across text names contained in elements > within documents of a collection.
I am not sure how "text names contained in elements" will be represented in your data. If it’s stored in text or attribute nodes, just try the query I’ve sent.
Quoted text · Christian Grün · 21 lines
> >> On Jan 9, 2016, at 12:52 PM, "Christian Grün" <christian.gruen at gmail.com> wrote: >> >> Hi E. Wray, >> >>> What is the best practice in BaseX to implement a uniqueness >>> constraint similar to a RDBMS? >> >> Using UUIDs has become our favorite solution. An alternative is to >> assign an incrementing integer, and e.g. store the highest assigned >> value in the root node. If you want to assign arbitrary values, you’ll >> need to check if the value has already been assigned: >> >> let $id := 'sjdhsj' >> return if(//item[@id = $id])) then ( >> error( (), "ID is not unique: " || $id) >> ) else ( ... ) >> >> If your database has an up-to-date attribute index, this should be pretty fast. >> >> Christian
EJ
We are already using UUIDs for file names. However we want, for example, guaranteed uniqueness across text names contained in elements within documents of a collection.
Quoted text · Christian Grün · 20 lines
> On Jan 9, 2016, at 12:52 PM, "Christian Grün" <christian.gruen at gmail.com> wrote: > > Hi E. Wray, > >> What is the best practice in BaseX to implement a uniqueness >> constraint similar to a RDBMS? > > Using UUIDs has become our favorite solution. An alternative is to > assign an incrementing integer, and e.g. store the highest assigned > value in the root node. If you want to assign arbitrary values, you’ll > need to check if the value has already been assigned: > > let $id := 'sjdhsj' > return if(//item[@id = $id])) then ( > error( (), "ID is not unique: " || $id) > ) else ( ... ) > > If your database has an up-to-date attribute index, this should be pretty fast. > > Christian
Written for BaseX 8.3. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery