sql - Having data from another table put in into check constraint -


i have table table1 colum checkcolumn,

now want put check constraint field checkcolumn allow data there in table table 2 conditions,

i tried this,

alter table table1 add check (checkcolumn=(select field1 table2 field2='abc') ) //the select not scalar 

but realized doesn't allow sub-query string in check condition,i searched little , read should use foreign key,or trigger, or else,but didn't understand how implement examples here,so posting separate question.

unfortunately can not insert sub query context of check constraint. here give suggestion, can use trigger or function , can use foreign key constraint check data dependency share 1 example function. e.g.

create function fn_check_rollnumber (     @rollnumber int ) returns varchar(10) begin     if exists (select rollnumber table_student rollnumber = @rollnumber)         return 'true'     return 'false' end 

now can use function in check context like,

alter table table_fees      check add constraint ck_rollcheck     check (fn_check_rollnumber(rollnumber) = 'true') 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -