1

I'm having a problem where I can't really find a solution for because it is very specific and I'm hoping you could help me:

    CREATE TABLE books (
  passagierid NUMBER NOT NULL CONSTRAINT fk_bucht_passagier REFERENCES passagier, --FK
  flightnr VARCHAR2(8) NOT NULL,
  dep_date DATE NOT NULL,
  CONSTRAINT fk_bucht_flug FOREIGN KEY (flightnr, dep_date) REFERENCES flug, --FK
  CONSTRAINT pk_bucht PRIMARY KEY (passagierid, flightnr, dep_date), --PK
  bookingnr NUMBER NOT NULL CONSTRAINT ak_bookingnr UNIQUE,
  [...]
  seatnr VARCHAR2(5) NOT NULL,
  [...]

  -- something like that:
  CONSTRAINT chk_sitzpl_gleiche_buchung CHECK((seatnr,bookingnr,dep_date,flightnr) 
      NOT IN (SELECT seatnr,bookingnr,dep_date,flightnr FROM books))
  );

So inputs like these, where the same flight number, date and seat have to have the same booking number should be checked:

-- insert into books values (...,'PW2345','19.11.2013 15:02:00',1,...,'12c',...)
-- insert into books values (...,'PW2345','19.11.2013 15:02:00',2,...,'12c',...) not ok
8
  • This should be a UNIQUE constraint. Commented Nov 24, 2013 at 14:32
  • But I think the constraint should be on UNIQUE(seatnr,dep_date,flightnr). Do not include bookingnr as this has already a UNIQUE constraint on it. Commented Nov 24, 2013 at 14:49
  • And I guess that you don't want 2 customers on the same flight-number, same day and same seat. Commented Nov 24, 2013 at 14:49
  • but its the combination of all 4 that has to be unique so doesnt it also have to be the four? Commented Nov 24, 2013 at 14:50
  • Your own example shows that the combination of the 3 columns has to be unique. The booking-numbers can be different (like it is, 1 and 2 in your example.) Commented Nov 24, 2013 at 14:52

1 Answer 1

1

What you need is a unique constraint on those columns.

SYNTAX IS:

CREATE TABLE XYZ(
....,
....,
CONSTRAINT <constraint_name> UNIQUE(column-list separated by commas));

You can read more about it here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you soo much!! such a simple answer and ive already spent so much time looking for it, damn..^^ i guess i didnt realize that unique can be applied on multiple columns. Anyways, thanks a lot, especially for such a fast answer! Have a great sunday!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.