• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default values

 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks!,

I need to create a table with 3 columns.
the 3rd column's value should always same as 2nd column if 3rd's value is NOT mentioned.
For example



If I insert a row using following


The result of
should be following


My Question is
How do i create a table with my requirement?
All I need is the table declaration.

I've tried following and it's not working


Thanks!
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this doesn't work in your database, then you'll have to do it differently.

The best I can think of would be to create a view over your table, which would use the name value for alias if the alias was not specified:This way, if you change the name and the alias was not specified, the alias will be changed as well. This sounds good t me , but it might not be what you want. A slight disadvantage is that you need to use the table, not the view, to modify the values in the table, and the view to query the table, but it should not be an insurmountable difficulty.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:If this doesn't work in your database, then you'll have to do it differently.

The best I can think of would be to create a view over your table, which would use the name value for alias if the alias was not specified:This way, if you change the name and the alias was not specified, the alias will be changed as well. This sounds good t me , but it might not be what you want. A slight disadvantage is that you need to use the table, not the view, to modify the values in the table, and the view to query the table, but it should not be an insurmountable difficulty.


Uh.... Thanks!

But I was hoping some thing related to TRIGGERS.

Anyway thanks for your time
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It probably would be possible to do it with triggers. I wouldn't recommend it, though, and I intentionally didn't mention them.

Triggers are a sort of a "magic" that happens in the background. They are hidden in the database, so easy to forget about, and they tend to interfere with new functionality. If you don't document them thoroughly, the next developer won't have a clue what's happening. Also, there are usually some situations in which triggers don't fire, which may complicate database administration or prevent use of some programming techniques.

There are lots and lots of questions over the internet (on AskTom, for example) by people who were desperate to resolve their database problem, only to find out there is a forgotten trigger somewhere that they didn't account for.

What you're trying to do with a trigger can be easily achieved by having a single point (say, a method in your project, but even a stored procedure would be better than a trigger) responsible for creating new records in the table. It's a business logic, after all, and it's generally not a good idea to have business logic code scattered all over the place (database triggers and your code, in this case).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic