May be:
CREATE TABLE [IdParents1] (
[Id] [int] IDENTITY (1, 2) NOT NULL ,
[Val] [char] (10) NOT NULL ,
CONSTRAINT [PK_IdParents1] PRIMARY KEY CLUSTERED
(
[Id]
)
)
GO
INSERT INTO IdParents1 ( Val ) values ( 'First' )
INSERT INTO IdParents1 ( Val ) values ( 'Third' )
GO
CREATE TABLE [IdParents2] (
[Id] [int] IDENTITY (2, 2) NOT NULL ,
[Val] [char] (10) NOT NULL ,
CONSTRAINT [PK_IdParents2] PRIMARY KEY CLUSTERED
(
[Id]
)
)
GO
INSERT INTO IdParents2 ( Val ) values ( 'Second' )
INSERT INTO IdParents2 ( Val ) values ( 'Fourth' )
GO
SELECT * FROM [IdParents1]
UNION ALL
SELECT * FROM [IdParents2]
GO
BEGIN
UNION ALL
ELSE
END
GO
CREATE TABLE Mytable ( Id INT NOT NULL, Val CHAR(10) ,
CONSTRAINT FK_ParentCheck CHECK ( dbo.fn_ParentExists (Id, val) = 1) )
GO
--Test Data
-- Valid Values
INSERT INTO Mytable ( Id, Val ) values ( 1, 'First' )
INSERT INTO Mytable ( Id, Val ) values ( 2, 'Second' )
-- Invalid Values
INSERT INTO Mytable ( Id, Val ) values ( 5, 'Fifth' )
INSERT INTO Mytable ( Id, Val ) values ( 1, 'One' )
--Server: Msg 547, Level 16, State 1, Line 1
--INSERT statement conflicted with TABLE CHECK constraint 'FK_ParentCheck'.
The conflict occurred in database 'Test', table 'Mytable'.
--The statement has been terminated.
--Server: Msg 547, Level 16, State 1, Line 1
--INSERT statement conflicted with TABLE CHECK constraint 'FK_ParentCheck'.
The conflict occurred in database 'Test', table 'Mytable'.
--The statement has been terminated.
John
> Hi again,
> --I was able to add these 2 constraint to the table. But --
> --now it only accept the value if match the logic of both -
> --constraint.
> ALTER TABLE mytable
> ADD CONSTRAINT constX
> CHECK ((dbo.fn_ParentExists1 (Id)=1) and (val2 ='x'))
> ALTER TABLE mytable
> ADD CONSTRAINT constY
> CHECK ((dbo.fn_ParentExists2 (Id)=1) and (val2 ='y'))
> >-----Original Message-----
> >Hi
> >In general this is not a valid normalised design.
> >But you can try:
> >CREATE TABLE [IdParents1] (
> > [Id] [int] IDENTITY (1, 2) NOT NULL ,
> > [Val] [char] (10) NOT NULL ,
> > CONSTRAINT [PK_IdParents1] PRIMARY KEY CLUSTERED
> > (
> > [Id]
> > )
> >)
> >GO
> >INSERT INTO IdParents1 ( Val ) values ( 'First' )
> >INSERT INTO IdParents1 ( Val ) values ( 'Third' )
> >GO
> >CREATE TABLE [IdParents2] (
> > [Id] [int] IDENTITY (2, 2) NOT NULL ,
> > [Val] [char] (10) NOT NULL ,
> > CONSTRAINT [PK_IdParents2] PRIMARY KEY CLUSTERED
> > (
> > [Id]
> > )
> >)
> >GO
> >INSERT INTO IdParents2 ( Val ) values ( 'Second' )
> >INSERT INTO IdParents2 ( Val ) values ( 'Fourth' )
> >GO
> >SELECT * FROM [IdParents1]
> >UNION ALL
> >SELECT * FROM [IdParents2]
> >GO
> >BEGIN
> > UNION ALL
> > ELSE
> >END
> >GO
> >CREATE TABLE Mytable ( Id INT NOT NULL CHECK (
> dbo.fn_ParentExists (Id) =
> >1 ), Val CHAR(10) )
> >GO
> >--Test Data
> >-- Valid Values
> >INSERT INTO Mytable ( Id, Val ) values ( 1, 'One' )
> >INSERT INTO Mytable ( Id, Val ) values ( 2, 'Two' )
> >-- Invalid Values
> >INSERT INTO Mytable ( Id, Val ) values ( 5, 'Five' )
> >--Server: Msg 547, Level 16, State 1, Line 1
> >--INSERT statement conflicted with COLUMN CHECK constraint
> >'CK__Mytable__Id__3587F3E0'. The conflict occurred in
> database 'MyDb', table
> >'Mytable', column 'Id'.
> >--The statement has been terminated.
> >John
> >> I have ONE FIELD in the child refering to 2 parents. So,
> >> for me to enter a value in this child fk field, the
> value
> >> must exist in BOTH parent. Otherwise I get a fk
> violation
> >> error message. OK.
> >> However this is what I'm try to do:
> >> If "some_other_field" in the child table = 'X' only
> >> consider child_parent1_fk constraint
> >> if "some_other_field" in the child table = 'Y' only
> >> consider child_parent2_fk constraint
> >> In other word I have a condition that will dictate if
> the
> >> inserted value need to exist IN parent1 OR the parent2
> >> table.
> >> I hope I didn't make too confusing.
> >> Following are the 2 parents and child table I'm using as
> >> an example. Any help is truly apprecited.
> >> -- The PARENT1 table
> >> CREATE TABLE TABLE_LOOKUP_1 (
> >> lk1col1 int not null,
> >> lk1col2 varchar(25) not null,
> >> primary key (lk1col1))
> >> insert into table_lookup_1
> >> values (1, 'any value')
> >> --The PARENT2 table
> >> CREATE TABLE TABLE_LOOKUP_2 (
> >> lk2col1 int not null,
> >> lk2col2 varchar(25) not null,
> >> primary key (lk2col1))
> >> insert into table_lookup_2
> >> values (2, 'any value')
> >> --The CHILD table
> >> CREATE TABLE T1 (
> >> COL1 int not null,
> >> COL2 int not null,
> >> COL3 varchar(25) not null,
> >> PRIMARY KEY (COL1),
> >> FOREIGN KEY (col2)
> >> REFERENCES TABLE_LOOKUP_1,
> >> FOREIGN KEY (col2)
> >> REFERENCES TABLE_LOOKUP_2
> >> )
> >> GO
> >> insert into T1
> >> values (1,1,'any value') -- I get fk violation because 1
> >> only exist in one parent
> >.