This article discusses an issue raised in the thread: microsoft.public.sqlserver.programming Sunday, June 29, 2008 'Storing a collection of lines' http://tinyurl.com/56dpd4 Give two columns in a table suppose you want to eliminate the opposite data where there's no mathematical or logical relationship between the columns. For example, consider a trip between two cities. It's equally likely a trip could start and end in either direction. If the table already has: column A column B -------- -------- NEW YORK CHICAGO We want to prevent the opposite from being entered: column A column B -------- -------- CHICAGO NEW YORK If the table has: column A column B -------- -------- CHICAGO NEW YORK We want to prevent the opposite from being entered: column A column B -------- -------- NEW YORK CHICAGO Because there's no relationship between the columns an sql check constraint can't be used. But in Dataphor a simple transition constraint can be used. The Opposite constraint simply checks if the opposite data for columns A and B already exists in the table. If it does the current row is rejected. The constraint works the same way for a single insert as it does for inserting multiple rows (a table). For example: create session table MyTable { A:String,B:String,C:String,key{A,B} }; alter table MyTable { create transition constraint Opposite on insert //The current (row) values for columns A and B are accessed //by prefixing each with 'new', ie. new.A, new.B . not exists (MyTable {A X,B Y} {Y A,X B} where A=new.A and B=new.B) tags //A custom error message can be written using the current //row values (new.A, new.B). { DAE.Message = "'For A: ' + new.A + ' and B: '+ new.B + ' there is a opposite, A: ' + new.B + ' and B: ' + new.A " } }; These insert succeed: insert row{'NEW YORK' A,'CHICAGO' B,'1c' C} into MyTable; insert row{'CALIFORNIA' B,'TEXAS' A,'1d' C} into MyTable; Inserting an opposite will fail and the custom error message will be raised: insert row{'NEW YORK' B,'CHICAGO' A,'1e' C} into MyTable; "For A: CHICAGO and B: NEW YORK there is a opposite, A: NEW YORK and B: CHICAGO" insert row{'CALIFORNIA' A,'TEXAS' B,'1d' C} into MyTable; "For A: CALIFORNIA and B: TEXAS there is a opposite, A: TEXAS and B: CALIFORNIA" Given that the table contains the data "NEW YORK" (A) and "CHICAGO" (B), inserting the following rows as a table will fail: insert table{ row{'DENVER' A,'BOSTON' B,'1c' C}, row{'RENO' B,'MIAMI' A,'1d' C}, row{'CHICAGO' A,'NEW YORK' B,'1e' C} } into MyTable; "For A: CHICAGO and B: NEW YORK there is a opposite, A: NEW YORK and B: CHICAGO. Note that the primary key constraint will eliminate the same A and B cities from being entered twice but entering the opposite cities does not violate it. That's what the Opposite constraint is for. The Opposite constraint is much simpler than an sql solution using triggers.
Dataphor SQL RAC (Relational Application Companion)
A site of hope for those looking for a true relational database system
- a one-one requirement constraint with dataphor (1)
- anatomy of sql server part I - what is a stored procedure (1)
- anatomy of sql server part II - the unit test as part of the database (1)
- anatomy of sql server part III - what does deferred name resolution really mean (1)
- censoring sql posts (1)
- creating an opposite constraint in dataphor (1)
- dataphor (2)
- Dataphor (7)
- dataphor # 13 a table as a parameter (1)
- dataphor - download and start working with it (1)
- dataphor - fixed sized word segments (1)
- dataphor # 10 sql mythology (1)
- dataphor # 11 string differences (1)
- dataphor # 12 trimming a string (1)
- dataphor # 14 sql the meaning of Update..From (1)
- dataphor # 15 views with substance (1)
- dataphor # 16 inclusive vs exclusive solutions (1)
- dataphor # 17 a visual look at ranking queries (1)
- dataphor # 18 data scrubbing using lists (1)
- dataphor # 19 create intervals over strings (1)
- dataphor # 20 browsing an sql window (1)
- dataphor # 21 an example of relational division (1)
- dataphor # 22 reusable procedures (1)
- dataphor # 23 repley to Michel (1)
- dataphor # 24 basics of the table type (1)
- dataphor # 25 extending the dense rank function (1)
- dataphor # 26 query a hierarchy with explode (1)
- dataphor # 27 combine strings with Split and Concat (1)
- dataphor # 28 constants and variables or sql and D4 (1)
- dataphor # 29 another example of relational division (1)
- dataphor #1 introduction (1)
- dataphor #2 splitting strings (1)
- dataphor #3 string concatenation (1)
- dataphor #4 comment (1)
- dataphor #5 comment (1)
- dataphor #6 formal definition (1)
- dataphor #7 sql: table this (1)
- dataphor #8 list to table (1)
- dataphor #9 table constraints (1)
- dataphor creating lists in a query (1)
- extracting numbers from a string with dataphor (1)
- jeff modens dynamic crosstabs for sql server (1)
- linq to sql the what and why (1)
- linq to sql as a window of opportunity to sql users (1)
- linq to sql should be important to sql users (1)
- linq to sql vs. older 4GL attempts (1)
- listing missing table item (1)
- Multiple cascade paths to the same table (1)
- RAC (4)
- RAC #1 comment (1)
- RAC #2 example (1)
- RAC #3 finding the Nth number in a string (1)
- RAC #4 Sql Server 2005 ranking functions vs. Rac ranking (1)
- sorting a delimited string by its numerical string parts (1)
- sql an example of extreme implicit conversions (1)
- sql can't handle complicated cascading updates (1)
- sql CTE should be a variable not a value (1)
- sql dense rank for identifying consecutive runs (1)
- sql is there really a table variable (1)
- sql ranking functions explained by relational types (1)
- sql server triggers are best set based (1)
- sql the idea of using substring to simulate lists (1)
- sql the undefined trigger in Sql Server (1)
- sql vs relational on tables (1)
- sql what the sql CTE covers up (1)
- types and procedures (1)
Showing posts with label creating an opposite constraint in dataphor. Show all posts
Showing posts with label creating an opposite constraint in dataphor. Show all posts
Sunday, July 06, 2008
An opposite constraint
Subscribe to:
Posts (Atom)
