Is there any way to create a relationship between the columns of a same table in sql server.:c-penguin:.
Let suppose i have 2 Columns in a table named Issued_date and return_data Now i want that if i assign the value of Issued_date the Return_date automatically entered which is Issued_date + 15 .:c-cool:
I want all this at the database level for integrity not at programming level:cool:..
Anyone..!!!:c-smile:
14 replies to this topic
#1
Posted 21 January 2011 - 06:34 AM
|
|
|
#2
Posted 21 January 2011 - 07:11 AM
This is the job for triggers, if your database supports. Triggers are set of commands that automatically executed when insert, update, or delete operations happen on the table (or view). Check your database documentation to see does it support trigger or not.
#3
Posted 21 January 2011 - 04:59 PM
SQL Server does support triggers. It sounds like a bad idea to do it this way, however. If your business logic changes, you'll have to rewrite the trigger, which is a lot less convenient than changing your software. Your software may not have the necessary privileges to modify a trigger for your users as part of an upgrade.
#4
Posted 21 January 2011 - 09:14 PM
^ Lol, sorry I did not catch that he did mention sql server. Could you elaborate better idea if you think using trigger is a bad one for this case.
#5
Posted 22 January 2011 - 05:48 AM
I would have it be part of the code. If it truly is a static difference, he could even avoid the column entirely (for now) and do something like:
SELECT Issued_date, Issued_date+15 as Returned_date FROM table;
That way, he gets the same effect without using the database space. Either way, simply ensuring Returned_date is always populated isn't a huge burden. If it's a mandatory field, setting it to NOT NULL will catch errors, or a trigger could abort the insert/update statements if minimal business logic isn't met by the statement (Issued_date < Returned_date, can't have one null but the other not, etc).
I just know from experience that when things change, it can be very unpleasant have to rework a database structure. Having triggers in place can just make that process even harder.
SELECT Issued_date, Issued_date+15 as Returned_date FROM table;
That way, he gets the same effect without using the database space. Either way, simply ensuring Returned_date is always populated isn't a huge burden. If it's a mandatory field, setting it to NOT NULL will catch errors, or a trigger could abort the insert/update statements if minimal business logic isn't met by the statement (Issued_date < Returned_date, can't have one null but the other not, etc).
I just know from experience that when things change, it can be very unpleasant have to rework a database structure. Having triggers in place can just make that process even harder.
#6
Posted 22 January 2011 - 06:10 AM
Quote
SELECT Issued_date, Issued_date+15 as Returned_date FROM table;
Since your idea advocates doing the "integrity" operation through application level, don't you think it is even more troublesome hunting sql codes cluttered inside the application whenever business rules changes happened? Compare that effort with the effort just to fix a trigger.
#7
Posted 22 January 2011 - 07:45 AM
Unfortunately, business logic has a nasty habit of changing in weird ways.
Rev 1: Returned_date is Issue_date+15
Rev 2: Returned_date is Issue_date+15, unless customer='ABC', in which case Returned_date is Issue_date + 30
Rev 3: Returned_date is Issue_date+15 if priority = 'normal' and customer <> 'ABC', Returned_date is Issue_date+5 if priority = 'high' and customer <> 'ABC', Returned_date is Issue_date+30 if priority = 'normal' and customer='ABC', Returned_date is Issue_date+20 if priority='high' and customer='ABC'
Rev 4: now we add a new special customer...
If this is meant to be generic software to be sold to multiple different companies/facilities, things get even messier, as all this logic gets further splintered.
If this is an application for a small company/application that expects minimal growth, then it probably doesn't matter much. I've dealt with customers that had multiple facilities, where they all used the same software, but separate backend databases and completely independent business logic. When they finally realize they need to merge into a single database, having all that logic embedded at the database level would have been a nightmare. Having it at the application level (especially in configuration files that are separate from the application), made things a lot easier.
I've also seen what happens when you have to completely restructure tables to accommodate increased flexibility. That can get really nightmarish.
Rev 1: Returned_date is Issue_date+15
Rev 2: Returned_date is Issue_date+15, unless customer='ABC', in which case Returned_date is Issue_date + 30
Rev 3: Returned_date is Issue_date+15 if priority = 'normal' and customer <> 'ABC', Returned_date is Issue_date+5 if priority = 'high' and customer <> 'ABC', Returned_date is Issue_date+30 if priority = 'normal' and customer='ABC', Returned_date is Issue_date+20 if priority='high' and customer='ABC'
Rev 4: now we add a new special customer...
If this is meant to be generic software to be sold to multiple different companies/facilities, things get even messier, as all this logic gets further splintered.
If this is an application for a small company/application that expects minimal growth, then it probably doesn't matter much. I've dealt with customers that had multiple facilities, where they all used the same software, but separate backend databases and completely independent business logic. When they finally realize they need to merge into a single database, having all that logic embedded at the database level would have been a nightmare. Having it at the application level (especially in configuration files that are separate from the application), made things a lot easier.
I've also seen what happens when you have to completely restructure tables to accommodate increased flexibility. That can get really nightmarish.
#8
Posted 22 January 2011 - 08:10 AM
Why am I under the impression that you think triggers can not look into other tables or the values entered/updated/deleted? All of those revisions samples could easily be solved with a helper table containing the rules. Simply add rules to this table and let the triggers look which rule it should use. Or did you write unique application for each customer that for that tiny difference?
For the last problem example, in ms sql server the trigger could inspect the session to see what application invoke it, if you want the trigger to act differently based on the application.
For the last problem example, in ms sql server the trigger could inspect the session to see what application invoke it, if you want the trigger to act differently based on the application.
#9
Posted 22 January 2011 - 10:10 AM
I do know about that. Maybe it's just a matter of perspective.
#10
Posted 24 January 2011 - 08:59 PM
And WingedPanther is correct. The database should not be cluttered with things to take care of something that you can easily take care of at a higher level.
#11
Posted 25 January 2011 - 12:24 AM
^ Then you don't know what we are talking about. We are talking about upholding the integrity of the database. Not talking about any code unrelated with keeping the integrity. Or do you think people that providing triggers in their database are bunch of fools?
#12
Posted 25 January 2011 - 03:34 AM
Quote
^ Then you don't know what we are talking about. We are talking about upholding the integrity of the database. Not talking about any code unrelated with keeping the integrity. Or do you think people that providing triggers in their database are bunch of fools?
I think the code the generates the insert query is related to keeping data integrity, but I must be wrong.
Quote
Any applicable integrity constraints and data validation rules must be satisfied before permitting a change to the database.
A trigger is activated by a change in the database, so it goes AGAINST data integrity as I read it. WingedPanther already mentioned why triggers are not the way to go for this at all.
"WingedPanther" said:
SQL Server does support triggers. It sounds like a bad idea to do it this way, however. If your business logic changes, you'll have to rewrite the trigger, which is a lot less convenient than changing your software. Your software may not have the necessary privileges to modify a trigger for your users as part of an upgrade.
The reasons he provides are excellent and should be headed for this case.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









