Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null value in a Foreign Key

Tags:

sql

This question has popped up a couple of times, but I'm still not 100% on how it works.

I'm creating some tables for bank infrastructure. This bank provides services to a company. There are groups that exist within the company that can access some or all of these services, depending on permissions. But there are also groups within the bank, that have no relation to the company, that can access these services.

My group table, has an Group_ID(PK), and Company_ID and Bank_ID as (FK). Is it OK for these FKs to be null, depending on whether the group is company group or a bank group?

like image 575
Killian Avatar asked Jun 07 '26 23:06

Killian


1 Answers

Nullable foreign keys are fine (at least in SQL Server, not sure about all RDBMSes). It just means the column can be null, but if it's not, then it needs to meet the constraint.

like image 159
Joe Enos Avatar answered Jun 10 '26 18:06

Joe Enos