Should I make a foreign key that can be null or make a new table?

I have a little question regarding how I should create my database. I have table dogs for an animal shelter, and I have table owners. On the dog’s table, all the dogs that were and were once in the shelter are placed. Now I want to establish a relationship between table dogs and table owners.

The problem is that in this example not all dogs have an owner, and since the owner can have more than one dog, a possible foreign key must be placed in table dogs (the dog cannot have more than one owner, at least not in the shelter ) But if I do, some dogs (those that are in the shelter) will have zero as a foreign key. Reading some other topics taught me that this is allowed. (Or maybe I read a few wrong topics)

However, another possibility is to put a table between two tables — for example, “dog owners” —and place the primary key of both tables there if the dog has an owner.

Now my question is (as you might have guessed) what is the best method of these two and why?

+4
source share
6 answers

The only solution that complies with the principles of the relational model is an additional table.

In addition, it is difficult to imagine how you are going to find any equipment that is so slow that the difference in performance when starting queries will be noticeable. After all, this is not a critical application of tens of thousands of transactions per second, is it?

+4
source

I agree with Philip and Erwin that the most reliable and flexible design is to create a new table.

, , SQL- . - , .

+4

.

- , UNIQUE NOT NULL. , , IS NOT NULL, . NULL , , , .

, , : UNIQUE NOT NULL - UNIQUE NOT NULL. UNIQUE NOT NULL , - .

+2

-CELKO-, : " , , , , ." , , , .

() ? , "" .

SQL! , SQL ( sqlvogel) SQL -. - relvar (a.k.a. !). , Fido Dog, DogOwnership, Fido, , .

.

+1

, , , .

"-", , ( ). p >

, , , .

0

. - ( , ), , , , , . NOT_SET .

,

0

Source: https://habr.com/ru/post/1628259/


All Articles