Forum Stats

  • 3,827,736 Users
  • 2,260,814 Discussions
  • 7,897,363 Comments

Discussions

How to create trigger in database

Muhammad Salman Rafique
Muhammad Salman Rafique Member Posts: 111
edited Mar 5, 2015 4:50AM in General Database Discussions

Hi,

how to create trigger in database.

I have two table in database

1. Purchase

2. Stock

when any data add into Purchase table ,,,, it automatically add same data into Stock table

Tagged:
John Stegeman
«1

Answers

  • John Stegeman
    John Stegeman Member Posts: 24,269 Blue Diamond
    edited Mar 5, 2015 2:43AM

    You use the create trigger statement, which is documented quite well http://docs.oracle.com

    Do you have a specific question about something you didn't understand from the docs?

  • Jagadekara
    Jagadekara Member Posts: 1,860 Gold Trophy
    edited Mar 5, 2015 2:43AM

    Use after insert trigger

  • Jagadekara
    Jagadekara Member Posts: 1,860 Gold Trophy
    edited Mar 5, 2015 2:45AM
  • JohnWatson2
    JohnWatson2 Member Posts: 4,452 Silver Crown
    edited Mar 5, 2015 3:50AM
    2875368 wrote:
    
    Hi,
    how to create trigger in database.
    I have two table in database
    1. Purchase
    2. Stock
    when any data add into Purchase table ,,,, it automatically add same data into Stock table
    

    Why use a pl/sql trigger at all? A sql INSERT ALL statement will be faster, simpler, and more reliable.

    --

    John Watson

    Oracle Certified Master DBA

    John Stegeman
  • John Stegeman
    John Stegeman Member Posts: 24,269 Blue Diamond
    edited Mar 5, 2015 3:54AM

    An excellent point, John. Triggers are much more often abused than used properly.

  • JohnWatson2
    JohnWatson2 Member Posts: 4,452 Silver Crown
    edited Mar 5, 2015 3:58AM
    John Stegeman wrote:
    
    An excellent point, John. Triggers are much more often abused than used properly.
    

    I hate triggers! They mean that when I do something, without my knowledge something totally unrelated happens. Working in C, we would call this a "side effect". And programmers who write code with side effects should be shot  

    John Stegeman
  • John Stegeman
    John Stegeman Member Posts: 24,269 Blue Diamond
    edited Mar 5, 2015 4:03AM

    Well said, John Watson.

  • 12cdb
    12cdb Member Posts: 2,971
    edited Mar 5, 2015 4:16AM

    >>I hate triggers! They mean that when I do something, without my knowledge something totally unrelated happens. Working in C, we would call this a "side effect". And programmers who write code with side effects should be shot  

    Well it depends on the requirement right, otherwise every Oracle feature is meant to design to solve some puzzle.

    More it depends on the correct usage. Anywhere used outside not designed for is bad!.

    Pradeep

  • John Stegeman
    John Stegeman Member Posts: 24,269 Blue Diamond
    edited Mar 5, 2015 4:31AM

    The problem with triggers is:

    • As John Watson says, they do mystical, magical, hidden things.
    • They are often *intentionally* used to cause side effects, where the better approach is to do the thing you want in the first place. In other words, people use them as a shortcut to fixing their code.
    • Most people DO NOT use triggers properly. They cause more problems, in aggregate, than they solve. When a beginning developer discovers triggers, they tend to get trigger-happy.

    I would also not be unhappy if triggers were outlawed/removed from Oracle.

  • 12cdb
    12cdb Member Posts: 2,971
    edited Mar 5, 2015 4:44AM

    I am not sure and I wouldn't imagine Oracle will ever remove it.

    Think about system event triggers such as startup, logon etc..  which actually helps a lot.

    Pradeep

This discussion has been closed.