-1

Suppose I have table T1 in which column A is a primary key. Column A values reference to table T2 column B. Can column B contain NULL and duplicate values?

i.e., is this possible?

    Column B       Column A
     111             111
     222             222
     333             333
     111
     NULL
ShippyDippy
  • 63
  • 10
  • This is a faq. Before considering posting please read the manual & google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags; read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. – philipxy Mar 09 '20 at 21:07
  • Does this answer your question? [Can a foreign key reference a non-unique index?](https://stackoverflow.com/questions/588741/can-a-foreign-key-reference-a-non-unique-index) – philipxy Mar 09 '20 at 21:10

2 Answers2

0

Whether or not t2.b can have NULL values depends on how it is defined. Presumably it has a proper foreign key declaration:

foreign key (b) references table1(a)

This has no affect on whether b can have NULL values.

If b is also declared as not null, then it cannot have NULL value. Otherwise (barring check constraints or participation in a primary key), it can.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • I'm sorry if I haven't clarified in the question properly but I wanted to know if B can have NULL/ duplicate values if A reference B i.e., foreign key (a) reference t1(b) – ShippyDippy Mar 09 '20 at 15:30
0

Yes, it can contain NULL. But the FK column should be declared as NOT NULL

Check this: Can a foreign key be NULL and/or duplicate?

django-unchained
  • 844
  • 9
  • 21
  • This doesn't answer all the question. And neither a FK nor its referenced columns need be NOT NULL. And the referenced table must be UNIQUE. And the link is not about this question, this question is about the referenced table. But this is (obviously) a duplicate & duplicates should be closed not answered. And @ is not appropriate in question & answer posts & it doesn't work there, it's for comments. – philipxy Mar 09 '20 at 21:01