Let's say I have one table called ProjectTimeSpan (which I haven't, just as an example!) containing the columns StartDate and EndDate.
And that I have another table called SubProjectTimeSpan, also containing columns called StartDate and EndDate, where I would like to set a Check constraint that makes it impossible to set StartDate and EndDate to values "outside" the ProjectTimeSpan.StartDate to ProjectTimeSpan.EndDate
Kind of a Check constraint that knows about another tables values...
Is this possible?
1. Check constraint cannot allow to refer the columns from other tables.
A Foreign Key is a database key that is used to link two tables together. The FOREIGN KEY constraint identifies the relationships between the database tables by referencing a column, or set of columns, in the Child table that contains the foreign key, to the PRIMARY KEY column or set of columns, in the Parent table.
The condition must be a Boolean expression evaluated using the values in the row being inserted or updated and can't contain sub queries, sequence, the SYSDATE,UID,USER or USERENV SQL functions, or the pseudo columns LEVEL or ROWNUM.
You can add the same constraint, but not with the same name.
In response to your comment on GSerg's answer, here's an example check constraint using a function:
alter table YourTable add constraint chk_CheckFunction check (dbo.CheckFunction() = 1)
Where you can define the function like:
create function dbo.CheckFunction() returns int as begin return (select 1) end
The function is allowed to reference other tables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With