FOREIGN KEY

This forum is for any questions about the language Tutorial D or the Rel implementation of it.
Post Reply
ecbrown
Posts: 7
Joined: Fri May 18, 2012 3:11 am

FOREIGN KEY

Post by ecbrown » Fri May 18, 2012 3:22 am

Hi All,

I am trying to get started with Suppliers and Parts. When I use this command (found in Date's SQL and Relational Theory, 2nd edition page 86), I get an error for relation SP.

ERROR: Encountered "FOREIGN" at line 27, column 1.
Was expecting one of:     "KEY" ...     ";" ...

Any advice would be appreciated.

Best regards,
Eric

------------
My input:

VAR S BASE RELATION
{
SNO CHAR,
SNAME CHAR,
STATUS INTEGER,
CITY CHAR
}
KEY { SNO } ;

VAR P BASE RELATION
{
PNO CHAR,
PNAME CHAR,
COLOR CHAR,
WEIGHT RATIONAL,
CITY CHAR
}
KEY { PNO } ;

VAR SP BASE RELATION
{
SNO CHAR,
PNO CHAR,
QTY INTEGER
}
KEY { SNO, PNO }
FOREIGN KEY { SNO }
REFERENCES S
FOREIGN KEY { PNO }
REFERENCES P ;

Dave
Site Admin
Posts: 368
Joined: Sun Nov 27, 2005 7:19 pm

Re: FOREIGN KEY

Post by Dave » Fri May 18, 2012 3:08 pm

"FOREIGN KEY" is not an official part of Tutorial D syntax. Hence, it is not implemented in Rel, especially as "FOREIGN KEY" is simply one instance of a general category of constraints.

In the Scripts folder of a Rel installation, you'll find a script called DateBookSampleRelvars.d that implements Suppliers and Parts, including the appropriate constraints. "FOREIGN KEY {S#} REFERENCES S" is written as "CONSTRAINT SP_FKEY1 SP {S#} <= S {S#}", where '<=' is the IS_SUBSET_OF operator.

ecbrown
Posts: 7
Joined: Fri May 18, 2012 3:11 am

Re: FOREIGN KEY

Post by ecbrown » Fri May 18, 2012 7:16 pm

Thanks for your quick and helpful response. I found the scripts!

uyar
Posts: 3
Joined: Thu Aug 23, 2012 5:35 pm

Re: FOREIGN KEY

Post by uyar » Thu Aug 23, 2012 5:47 pm

Hi,

I'm trying to figure out if I can integrate Rel into my database management systems course. When adapting my example database to Rel, I've had a problem with foreign keys. It seems to me that the attribute names have to be the same in both relations. My definitions are below:

Code: Select all

TYPE MOVIE# POSSREP { VALUE INTEGER };
TYPE PERSON# POSSREP { VALUE INTEGER };

VAR MOVIE REAL RELATION
    { MOVIE# MOVIE#, TITLE CHAR, DIRECTOR# PERSON# }
    KEY { MOVIE# };

VAR PERSON REAL RELATION
    { PERSON# PERSON#, NAME CHAR }
    KEY { PERSON# };

CONSTRAINT MOVIE_FKEY_DIRECTOR MOVIE { DIRECTOR# } <= PERSON { PERSON# };
When I execute this, I get:

ERROR: Attribute DIRECTOR# not found in {PERSON# PERSON#}

Of course I could solve this by renaming the attribute to PERSON# but I would like to use self-explanatory names and the syntax seems to support it. Am I doing something wrong? Thanks.

Dave
Site Admin
Posts: 368
Joined: Sun Nov 27, 2005 7:19 pm

Re: FOREIGN KEY

Post by Dave » Thu Aug 23, 2012 7:30 pm

You can use the RENAME operator in the MOVIE_FKEY_DIRECTOR constraint.

The expression MOVIE { DIRECTOR# } <= PERSON { PERSON# } asks if the result of MOVIE { DIRECTOR# } is a subset of PERSON { PERSON# }. The former is of type RELATION { DIRECTOR# PERSON# }, but the latter is of type RELATION { PERSON# PERSON# }. Because the attribute names differ, they are different types.

The '<=' operator requires that both operands be the same type, so you probably want the constraint to be:

Code: Select all

CONSTRAINT MOVIE_FKEY_DIRECTOR MOVIE { DIRECTOR# } RENAME (DIRECTOR# AS PERSON#) <= PERSON { PERSON# };

uyar
Posts: 3
Joined: Thu Aug 23, 2012 5:35 pm

Re: FOREIGN KEY

Post by uyar » Fri Aug 24, 2012 10:45 am

OK, I get it. Thanks.

Post Reply