0

I have these 4 tables, one is called register and has columns:

ID
Name
Location

Then, I have these 2 kind of users - Normal User and Shelter User. As both are related to the register table because both of them need that information, these 2 type of users have their respective fk to the register table.

Now, I have this table called post, and this post can be made for either a Normal User, or a Shelter User. The thing here is, that for this table to be related to both users is going to need both fk, I mean, a fk from Normal User, and a fk from Shelter User, and these fk have to be nullable yo cover the 2 cases.

So, yeah, I'd like to avoid having 2 nullable fk, and I thought that instead of having both fk, I better use a fk from register, but then how could I reach any of the 2 users from the table post.

Maybe I'm misunderstanding something about SQL, or normalizartion, but if you can help me I'd be very grateful.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Darsh
  • 11
  • 1
    are both tables user and sheltered user so dofferent that it warrants two tables, of can you only use one table with flags and columns specific to user_type? – nbk Jul 03 '22 at 00:37
  • 2
    `user table: id, type(normal or shelter), registerId (fk)` – Bryan Dellinger Jul 03 '22 at 00:38
  • @nbk well, yeah, i mean, there more tables, for example, a Shelter User can be related to a donation table, and the normal User can't. I'm not sure if the User type is a good solución since in an ERD this relation will ve displayed for the User either is declares as normal or shelter – Darsh Jul 03 '22 at 00:45
  • Does this answer your question? [How can you represent inheritance in a database?](https://stackoverflow.com/questions/3579079/how-can-you-represent-inheritance-in-a-database) – philipxy Jul 03 '22 at 00:46
  • Such radio button FKs are an anti-pattern for DB/SQL subtypes/inheritance. And rearranging designs for that is not DB normalization. Also both the antipattern & good designs are faqs. – philipxy Jul 03 '22 at 00:49
  • Please before considering posting read the manual/reference & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. Reflect research in posts. SO/SE search is poor & literal & sometimes limited to titles, but read the help. Google re googling/searching, including in Q&A at [meta] & [meta.se]. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/3404097) [ask] [Help] – philipxy Jul 03 '22 at 00:51
  • If a post belong to either an Normal user or a Shelter user (but not both) then make the nullable, and add a `CHECK` constraint to ensure they are not both present at the same time. – The Impaler Jul 03 '22 at 01:59

0 Answers0