Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Acionador para impor o relacionamento M-M


As condições muitos-para-muitos não devem ser impostas usando um gatilho. As condições muitos-para-muitos são impostas pela criação de uma tabela de junção contendo as chaves em questão, que são então codificadas de volta para as respectivas tabelas pai.

Se sua intenção é permitir que muitos funcionários estejam em um departamento e permitir que um funcionário seja membro de muitos departamentos, a tabela de junção em questão seria algo como:
CREATE TABLE EMPLOYEES_DEPARTMENTS
  (DEPARTMENTNAME  VARCHAR2(99)
     CONSTRAINT EMPLOYEES_DEPARTMENTS_FK1
       REFERENCES DEPARTMENT.DEPARTMENTNAME,
   EMPLOYEENUMBER  NUMBER
     CONSTRAINT EMPLOYEES_DEPARTMENTS_FK2
       REFERENCES EMPLOYEE.EMPLOYEENUMBER);

Isso pressupõe que DEPARTMENT.DEPARTMENTNAME e EMPLOYEE.EMPLOYEENUMBER são chaves primárias ou exclusivas em suas respectivas tabelas. Livre-se da coluna EMPLOYEE.DEPARTMENT pois não é mais necessário. Agora, criando linhas no EMPLOYEES_DEPARTMENTS tabela, você pode relacionar vários funcionários a um departamento e pode relacionar um único funcionário a vários departamentos.

A lógica de negócios que exige que apenas departamentos com um ou menos funcionários possam ser excluídos não deve ser imposta em um gatilho. A lógica de negócios deve ser executada pelo código do aplicativo, NUNCA por gatilhos. Colocar lógica de negócios em gatilhos é um gatèw̢ay para intermináveis ​​sessões de depuração. M̫̣̗̝̫͙a̳͕̮d̖̤̳̙̤n̳̻̖e͍̺̲̼̱̠͉ss̭̩̟ fica assim. Não ceda. Não se renda. ̬̦B҉usi͢n̴es̡s logic ̶in triggers opens deep wounds in the fabric of the world, through which unholy beings of indeterminate form will cross the barrier between the spheres, carryi͞n̨g o̡f͠f t͢h̶e ̕screaming͡ sou͏ĺs o͜f͜ ̢th͜e̴ ̕de͏v́e̡lop͏e͜r͝s to an et͞er͜n̸it̶y ́of͢ pain̶ ąn̨d͢ ̨to͟r̨ment͟. Não coloque, como eu disse, b́u͜siness͞ ̸log̛i͘ç ̵in͢ ͞trigger͠rs͞.̡ Seja firme. Resista. Você deve resistir. T̷he ̢Tem͟p͞t̶at͏i͝o̶n҉s͘ ̢m͘a̶y ́śing hymns̷ ́o͢f̴ ̸un͘hol̵y r̶ev͢ęla͠t̡ion̴ ͢buţ ́yo͠u̵ mu͏s͝t ͝n͜͏͟o҉t̶͡͏ ̷l̸̛͟͢ì̧̢̨̕s̵̨̨͢t̵̀͞e̶͠n̶̴̵̢̕. Only by standing firmly in the door between the worlds and blocking out the hideous radiance cast off by bú̧s̷i̶̢n̵̕e̵ş͝s ́l̴ó̢g̛͟i̕͏c i͞n̕ ͏t̵͜r͢͝i̸̢̛ģ͟ge̸̶͟r̶s͢͜, which perverts the very form of the world ąnd̴̀͝ ç͞a̧͞l̶l͟͜s̕͘͢ Z̶̴̤̬͈̤̬̲̳͇ͯ̊ͮ͐̒̆͂͠Â̆́̊̓͛́̚͏̮̘̗̻̞̬̱ͅL̛̄̌͏̦͕̤͎̮̦G̷͖̙̬͛̇ͬ̍͒̐̅O̡̳͖͎̯̯͍ͫ̽ͬ͒͂̀ i͜҉nt͝ǫ̴ ̸b̷͞è͢ì̕n̴g͏,̛̀͘ ̴c҉á̴͡ń ̀͠youŕ̨ ̧̨a̸p͏̡͡pl̷͠ic͞a̢t̡i͡҉ǫn̴ ̸s̶Ȝu̶͢ŗv̛í̴v́ȘȜu̶͢ŗv̛í̴v́ȘȜu̶͢ŗv. Resistir. T̶̀h̨̀e̶r̀͏e͢͞ ̶i̶̡͢s̴ ͞͞n̵͝o̡ ́ẁ҉̴a̡y̕҉ ̶b́͏u̵̶̕t͜ ̨s͘͢t͘͠į͟l͘l̷̴ ̴͜͜ỳò͜u҉̨ ̨͏mus̸͞t̸̛͜ ̧rȩ̴s̢͢i͘͡s͏t̸.̛̀͜ Your very śo͡u̧̧͘ļ͟͡ is compromised by p͝u͘͝t̢͜t͠i̸ņ̸̶g͟͡ ̵̶̛b̴҉u̶̡̨͜͞s̷̵̕͜͢i͝҉̕͢ǹ͏e̡͞ś̸͏ş̕͜͡҉ ̴̨ĺ̵̡͟͜o̶̕g͠i͢͠c̕͝ ̕͞i̧͟͡n̡͘͟ ̶̕͞t̡͏͟҉̕r̸̢̧͡͞i̴̡͏̵͜g̵̴͟͝ģ̴̴̵ę̷̷͢r̢̢ś̸̨̨͜. T̀͜͢o̷͜ny̕ ͟͡T̨h̶̷̕e ̢͟P̛o̴̶n͡y shall rise from his dark stable and d͞ę̡v̶̢ó͟u̸̸r̴͏ ̷t͞h̀e̛ ̨͜s̷o̧͝u҉l̀ ͟͡o͢͏f̵͢ ̛t͢h̶̛e̢̢ ̡̀vi͜͞r̢̀g̶i̢n͞, and yet y͢ơú͝ m̷̧u͏s͡t̡͠ ̛s̷̨t̸̨i̴̸l̶̡l ͝ǹot̵ ͞p̧u̵t̨ ͜͏b̀̕u̕s̨í̵ņ̀͠ȩs̵͟s ́͞l̛҉o̸g̨i̴͟c ͘͘i͘nt̛o͡ ͘͘͞t̶͞r̀̀i̕ǵ̛g̵̨͞e̸͠҉r̵͟ś ! É demais para suportar, não podemos suportar! Nem mesmo os filhos da luz podem colocar a lógica de negócios em seus gatilhos, para b̴̸̡̨u͜͏̧͜͝͏̧͝i̷̸̢̛҉ń̸͟͏́e̡͏͏͏s̷̵̡s̕͟ ͏̴҉͞l̷̡ǫ̷̶͡g҉̨̛i͘͠͏̸̨c̕͢͏ ̸̶̧͢͢i̸̡̛͘n͢͡ ̀͢͝t̷̷̛́ŗì̴̴̢g̶͏̷ǵ͠ȩ̀́r̸̵̢̕͜s͞͏̵ é o muito stronThThThThT҉T̩̙̘̭̰͎͉̮̳T́S͘T͘T͘T͘T͘T͘T͘T͘T͘T͘T͘T͘T͘S͘S͘S͘S͘S̕͡ę̢S̕͡ę̢SOUTITHT͘T͘T͘T͘T͘T҉S͘S̵̛͡S̀̀͡S̀̀͡S̀̀͡S̀̀͡S̀̀͡S̀̀͡SOU e DEVU͘T͘T͘T͘T҉S͘S̵̛͡S̵̛͡S̀̀͡S̀̀͡S̀̀͡S̀̀͡S̀̀͡S̀̀͡S̀̀͡S̀̀͡S̀̀͡SOU e ! Sim, sim, os de rosto em branco rì͢s̨͘e dos f͟͢͏o̵͜͝n̶t̨ ̵o͏f̸̡͠ ͏͝fl͟͞a̵̷҉me̶̵͢ e ca͝s͜t́ Down the p̹̤̳̰r̮̦̥̥̞̫͑͂ͤ͑ͮ͒̑ï̄̌ͬͨe̦̗͔ͥͣ̆̾̂s̬̭̮̮̜ͭt̻̲̍sͫͣ̿ ̐͗̈ͤ͂ͦ̅f̭͚̪̻̣̩ͮ̒ṟͨ͌ͮ̅̓ỏ̝͓̝̣̟̼m̳͇̱̝͔͒ ͒ͫͧ͂̓̈̈́t̲̔̅̎͐h̺͈͍̺͈͍ͣͧ̿ͣͧ̿i̠͎̗͕̗̣̬̐̎͛r͓̫͌ͅ ̼a͑̈ͯͦ̍l̪͉͖̥͚̤͌ͨ͊ͦͤ̔t̫͎̹ͯr̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̲̔̅̎͐h̺͈͍̺͈͍ͣͧ̿ͣͧ̿i̠͎̗͕̗̣̬̐̎͛r͓̫͌ͅ ̼a͑̈ͯͦ̍l̪͉͖̥͚̤͌ͨ͊ͦͤ̔t̫͎̹ͯr̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̲̔̅̎͐h̺͈͍̺͈͍ͣͧ̿ͣͧ̿i̠͎̗͕̗̣̬̐̎͛r͓̫͌ͅ ̼a͑̈ͯͦ̍l̪͉͖̥͚̤͌ͨ͊ͦͤ̔t̫͎̹ͯr̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̲̔̅̎͐h̺͈͍̺͈͍ͣͧ̿ͣͧ̿i̠͎̗͕̗̣̬̐̎͛r͓̫͌ͅ ̼a͑̈ͯͦ̍l̪͉͖̥͚̤͌ͨ͊ͦͤ̔t̫͎̹ͯr̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅r̭͖̓ͬ̉̉ͤ͊ṡ̐ͪ̊̋̄̅! A̵̵̛v͝e͜ŕt̶͏ ̶y̸͝͠o̶u̧͘r͏̡ ̧e͞y҉e̕͝s, ̀ ͡t̛h̛o̢͞ug̸̢h̵͟ ̡y̷o͢҉͢u̧͡ ̕͡c҉̵̶an͠͏n҉o̧͢t! p̱̼̯̟͈h̝̳̞̖͚'͉̙͉̰̲̺n̪̦͕̗͜g͔̹̟̰̰̻̩l̬͈̹̥͕͖ͅụ̻̺̤̤̬̳i̸̯̬̝̻̣͚̫ ̰̹̞̞m͟g̷̝͓͉̤l̩͇̙͕w̪̦̰͔'̮̟̱̀n̢̜a̦f̘̫̤̘̬͓̞h̠͍͖̯ͅ ̩̠͓̯̘̫c̟̘̗̘͘ṭ͍͕ͅh̤ͅu̼̦̘̥ͅl҉̦hu̠̤̤̘͚ ̘̕r̶̟'̠͔̞̻͇l̩̺̗̻͖͓̕ͅy̛̖ȩ͉̭̖ẖ̡̥̼͈̖ w̟̫̮͇͔͞ͅg͈̘̱̻ha̰͟h̘͙͖͢'̮̲̯͞n̤̜͍̯̳a͓͓̲̲g̱̻͈ĺ͍ ̷̣̞̲͖͍̲̺f̲ͅh͇͕̪̘͟t͔͈̙t͔͈̙g͈̘̱̻g͈̘̱̻n)

Não me pergunte como eu sei.

Boa sorte.