0

i have a little problem and i want to ask you for help :)

so to make it simple, im using an oracle database and i want to create an "check constraint" on one of my tables, i.e. below.

Table XY

Attribute A || Attribute B || Attribute C

for attribute A and B the user can add whatever he wants - and for attribute C i want to use a "check constraint" using a user defined function - which checks if the combination of A and B is valid or not.

my problem is, i have no idea how to implement the input parameters for the function so i can make a check while the user creates the entry in the database.

in other words, the user already added 1 to A and 3 to B and once he wants to add an entry for C i want to (example) check if A + B = 4

i hope you can help me, because im going bananas right now x)

kind regards alex

EDIT (copied from comment below)

I have 3 tables:

  1. GROUP
  2. GROUP_LIST
  3. TM

GROUP includes the ID of a GROUP, GROUP_LIST is the connection table between the GROUP and the TM - so in GROUP_LIST I connect a GROUP with a TM - but it's possible to connect 1 GROUP with several TMs f.e.:

GROUPID || TM
1       || 1
1       || 2
1       || 3
2       || 1

and so on and my real issue is following: now, I want a check on the TM attribute which checks if the TM I'm trying to fill in, is already existing in the same GROUPID. I hope now its clear what my intention is ...

2
  • Your question is not very clear. What is the check you want to implement? What is the role of column C? What do you mean by: "once he wants to add an entry for C to check if A + B=4". ?
    – Rene
    Sep 1, 2015 at 13:52
  • The table has 3 attributes - A, B and C If the user adds a new entry - and has already filled A and B, the check on C - should check if "A + B = 4" - otherwise he should deny the entry on attribute C - for example first entry: A = 1, B = 3, (check on C is ok ; A + B = 4!) C = 7 second entry: A = 2, B = 3, (check on C is NOT ok ; A + B != 4!) C = (null[no entry possible duo check])
    – Alex
    Sep 1, 2015 at 13:55

3 Answers 3

3

It seems rather pointless to require users to enter a 'correct' value for a particular column. Instead of defining C as a normal column I suggest defining it as a computed column, e.g.:

CREATE TABLE TBL
  (A    NUMBER,
   B    NUMBER,
   C    AS (A + B));

In this way C will always be computed correctly.

SQLFiddle here

Best of luck.

EDIT

Based on information from OP in the comment below it appears that this answer doesn't address the real need so I've added a second answer (below). I considered deleting this answer but because it contains the comment from OP which adds important information (which I've since edited into the question) I decided to leave this answer in place, but to forfeit the rep gain by making this answer Community Wiki.

2
  • yeah ok - might be necessary to explain my wanting in more detail. i have 3 tables: 1. GROUP 2. GROUP_LIST 3. TM so GROUP includes the ID of a GROUP, GROUP_LIST is the connectiontable between the GROUP and the TM - so in GROUP_LIST i connect a GROUP with a TM - but its possible to connect 1 GROUP with several TMs f.e.: GROUPID || TM 1 || 1 1 || 2 1 || 3 2 || 1 and so on and my real issue is following: now, i want a check on the TM attritbute which checks if the TM im trying to fill in, is already existing in the same GROUPID - i hope now its clear what my intention is ... :/
    – Alex
    Sep 2, 2015 at 5:50
  • @Alex: please don't use comments to add information about your question. You should edit the question to do this. In this case I've already added this information to your question, but keep this in mind for the future. Sep 2, 2015 at 12:01
1

If you use ouf-of-line check constraint syntax you could define a condition with any of the fields of the specific table, something like:

ALTER TABLE xy ADD CONSTRAINT mymulticheck CHECK (A + B = C);

Note that you have many restriction on this type of constraint, as example you cannot use a user defined function, and obviously the condition is fixed and identical for all the rows.

3
  • Note: values that evaluate tu NULL the whole condition are always accepted, so in your scenario as long as C is kept NULL in a row you could freely insert any value on A and B on the same row.
    – dbra
    Sep 1, 2015 at 14:05
  • yeah ok - might be necessary to explain my wanting in more detail. i have 3 tables: 1. GROUP 2. GROUP_LIST 3. TM so GROUP includes the ID of a GROUP, GROUP_LIST is the connectiontable between the GROUP and the TM - so in GROUP_LIST i connect a GROUP with a TM - but its possible to connect 1 GROUP with several TMs f.e.: GROUPID || TM 1 || 1 1 || 2 1 || 3 2 || 1 and so on and my real issue is following: now, i want a check on the TM attritbute which checks if the TM im trying to fill in, is already existing in the same GROUPID - i hope now its clear what my intention is ... :/
    – Alex
    Sep 2, 2015 at 5:45
  • Check contraints cannot work across different tables: the only feasible solution would be a trigger.
    – dbra
    Sep 2, 2015 at 7:05
0

Based on new information from OP it appears that the correct way to solve this issue would be to add a UNIQUE constraint to the GROUP_LIST table to ensure that the combination of GROUP_ID and TM is unique:

ALTER TABLE GROUP_LIST
  ADD CONSTRAINT GROUP_LIST_UNIQUE_1
    UNIQUE (GROUP_ID, TM);

Best of luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.