I have two tables: beds
and room
and I want to make sure that on insert each room can have a maximum of 5 beds (i.e. the company owns 50 beds in total which can get swapped from room to room in various combinations but each room can only have 5 at the most).
The primary key in beds is a composite key of room_id
and bed_id
(room_id
being the foreign key) but I'm not sure how to implement this as a constraint for the table?
TABLES BELOW
CREATE TABLE "ROOM"
("ROOM_NO" NUMBER(*,0),
"ROOM_NAME" VARCHAR2(45 CHAR) NOT NULL ENABLE,
CONSTRAINT "ROOM_NO_PK" PRIMARY KEY ("ROOM_NO")
CREATE TABLE "BED"
("ROOM_NO" NUMBER(*,0),
"BED_NO" NUMBER(*,0),
CONSTRAINT "BED_PK" PRIMARY KEY ("ROOM_NO", "BED_NO")
I'm new to sql and have not covered triggers and sequences please can you explain in simple terms how to do this?