MySQL and FOREIGN KEY. Disallow NULL on allowed NULL value column -


example of tables

t1:  __________ |id | val| |---+----| | 1 |  | |---+----| | 2 | b  | |---+----| | 3 | c  | |---+----|   t2: __________ |id | val| |---+----| | 1 | d  | |---+----| | 2 | e  | |---+----| | 3 | f  | |---+----| t:     ________________________________     |id | val| fk_t1_id | fk_t2_id |     |---+----|----------+----------|     | 1 | g  |    1     |   null   |     |---+----|----------+----------|     | 2 | h  |    nul   |    1     |     |---+----|----------+----------|     | 3 |  |    3     |   null   |     |---+----|----------+----------| 

both 'fk_t1_id' , 'fk_t2_id' foreign keys fileds , both null allowed. possible make additional constraint allows 1 fk on 1 field , null on another? (to impossible insert row both 'fk_t1_id' , 'fk_t2_id' null or both of not null)

create check constraint on t table having condition:
check( (fk_t1_id not null , fk_t2_id null) or (fk_t1_id null , fk_t2_id not null))


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -