Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Table Error

Author  Topic 

matta0990
Starting Member

44 Posts

Posted - 2011-04-22 : 09:37:49
I can not see why this table wont create. I think it my be the Foreign keys. Can anyone show me how to include 3 foreign keys if this is the problem.

CREATE TABLE Loan
(LoanID SMALLINT NOT NULL,
CustomerID SMALLINT NOT NULL,
CopyID SMALLINT NOT NULL,
CONSTRAINT Loan_LoanID_FK
FOREIGN KEY (LoanID)
REFERENCES Loan (LoanID),
CONSTRAINT Loan_CustomerID_FK
FOREIGN KEY (CustomerID)
REFERENCES Customer (CustomerID),
CONSTRAINT Loan_CopyID_FK
FOREIGN KEY (CopyID)
REFERENCES Copy (CopyID),
LoanDuration SMALLINT NOT NULL
CONSTRAINT CK_LoanDuration CHECK (LoanDuration IN('1','3','5','7')),
LoanDate DATETIME NOT NULL,
ReturnDate DATETIME NOT NULL,
LoanPrice SMALLMONEY NOT NULL,);

M Atkinson

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-04-22 : 09:41:09
Without looking too closely, you have an extra "," at the end of LoanPrice.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

matta0990
Starting Member

44 Posts

Posted - 2011-04-22 : 10:23:35
i get the error

Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'Loan' that match the referencing column list in the foreign key 'Loan_LoanID_FK'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

M Atkinson
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-22 : 10:34:07
You need to add a primary key or UNIQUE constraint on the LoanID column in the Loan table. Same applies to the other columns and tables you want to set up foreign keys for.
Go to Top of Page
   

- Advertisement -