Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey guys,

I am running into an issue when I try to set up a domain with a CHECK constraint that has a subquery in it. I am aware that apparently PostgreSQL does NOT support subqueries in CHECK clauses, as I got the error when trying to run the query. But I am trying to figure out if there is an alternative method to accomplishing this with PostgreSQL.
This is what I believe is the issue:

"CREATE DOMAIN hotelNoDD as hotelNoD
CHECK(VALUE IN(SELECT hotelNo FROM Hotel));"

Any ideas?

Here is my work so far:

Created Hotel Table:

SQL
CREATE DOMAIN hotelNoD AS CHAR(8);
CREATE TABLE Hotel(
hotelNo hotelNoD NOT NULL,
hotelName VARCHAR(30) NOT NULL,
city VARCHAR(40) NOT NULL,
PRIMARY KEY (hotelNo)
);


Attempted to Create Room Table:


SQL
CREATE DOMAIN roomNoD AS SMALLINT
    CHECK(VALUE BETWEEN 1 AND 100);
CREATE DOMAIN typeD AS CHAR(1)
    CHECK(VALUE IN('S', 'D', 'F'));
CREATE DOMAIN priceD AS DECIMAL(5,2)
    CHECK(VALUE BETWEEN 10 AND 100);
CREATE DOMAIN hotelNoDD as hotelNoD
    CHECK(VALUE IN(SELECT hotelNo FROM Hotel));

CREATE TABLE Room(
roomNo roomNoD NOT NULL,
hotelNo hotelNoDD NOT NULL,
type typeD NOT NULL,
price priceD NOT NULL,
PRIMARY KEY(roomNo, hotelNo),
FOREIGN KEY(hotelNo) REFERENCES Hotel
);
Posted
Updated 31-Oct-15 23:38pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900