Re: Check Constraints using a subquery - SSrvr 2000
From: Dan Guzman (guzmanda_at_nospam-online.sbcglobal.net)
Date: 12/21/04
- Next message: John: "RE: How to append a SP´s result into a TXT File"
- Previous message: Uri Dimant: "Re: How to append a SP´s result into a TXT File"
- In reply to: L Mehl: "Re: Check Constraints using a subquery - SSrvr 2000"
- Next in thread: L Mehl: "Re: Check Constraints using a subquery - SSrvr 2000"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 21 Dec 2004 07:48:27 -0600
Tibor is correct as to the cause of the error message. Foreign keys
typically refer to the primary key of the referenced table, although unique
constraint or unique index references are also allowed.
Assuming your PriceList columns RegionID and ProductTypeID do not allow
nulls (a primary key requirement) you can add the PK constraint with the
script below.
ALTER TABLE SiteEquipment
ADD CONSTRAINT PK_PriceList
PRIMARY KEY (RegionID, ProductTypeID)
In a relational database, all tables should have a primary key. Additional
unique constraints can be added to ensure non-PK data are unique.
-- Hope this helps. Dan Guzman SQL Server MVP "L Mehl" <mehlSCRATCHTHIS@cyvest.com> wrote in message news:KINxd.5834$RH4.3126@newsread1.news.pas.earthlink.net... > Hi Dan -- > > Thanks for responding and for the educational note. > > Executing your code in QueryAnalyzer, I receive the error messages > > Server: Msg 1776, Level 16, State 1, Line 1 > There are no primary or candidate keys in the referenced table 'PriceList' > that match the referencing column list in the foreign key > 'FK_SiteEq_PriceList'. > > Server: Msg 1750, Level 16, State 1, Line 1 > Could not create constraint. See previous errors. > > Creating a small test I saw that non-matching names is not what the error > refers to. > > Can you suggest what to look for to solve the problem? > > Larry > > > Dan Guzman wrote: >> One usually uses a foreign key constraint to enforce a required >> relationship between 2 tables like the example below. >> >> ALTER TABLE SiteEquipment >> ADD CONSTRAINT FK_SiteEquipment_Pricelist >> FOREIGN KEY (RegionID, EqTypeID) >> REFERENCES PriceList (RegionID, ProductTypeID) >>
- Next message: John: "RE: How to append a SP´s result into a TXT File"
- Previous message: Uri Dimant: "Re: How to append a SP´s result into a TXT File"
- In reply to: L Mehl: "Re: Check Constraints using a subquery - SSrvr 2000"
- Next in thread: L Mehl: "Re: Check Constraints using a subquery - SSrvr 2000"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|