1

Imagine I have several tables in SQL Server 2016:

  1. Cat
  2. Cat carrier
  3. Cat transit box
  4. Cat car

Every object can be stored in higher level, but not necessarily directly one above with N-to-1. So a cat can be put into a carrier, the carrier into a transit box and the box put into a car. But a cat can simply ride in a car. Objects can store numerous types, so for example: Cat transit box can hold simultaneously cats and cat carriers.

There is no need for Cat or Cat Car in all this.

Now, the idea is to create a table joining everything at once:

Cat Cat Carrier Cat Box Cat Car
null 3 5 null
null 4 5 null
1 5 5 null
2 null 5 null

So this is a situation where three carriers are put into a single box. one of those carriers contain a single cat. Also, there is a cat somewhere in the box.

But I have to make sure that there are no illogical data:

Cat Cat Carrier Cat Box Cat Car
null 3 5 null
null 3 6 null
1 3 null null
1 null 8 null
  • Cat cannot be in different carriers.
  • Cat cannot be in different cat boxes.
  • Cat cannot be in different cat cars.
  • Cat carrier cannot be in different cat boxes.
  • Cat carrier cannot be in different cat cars.
  • Cat box cannot be in different cat cars.
  • (null is considered different to any specific value, so cat cannot be in cat carrier 3 and not be in any cat carrier at the same time. Two null values are ok).

Two important questions:

  1. Is this flat structure adequate for what I want to do? Is there any better structure for this task?
  2. How to create unique constraints to make sure an entity does not travel in multiple different containers.
7
  • I have to make sure that there are no illogical data Does the below illogical situations are complete list of possible illogicals? if not then expand the illogical data situations list. Cat carrier cannot be in two boxes at once unique by (carrier, box). Cat cannot be in a carrier and cat box separately Unique (cat).
    – Akina
    Commented May 16 at 12:41
  • @Akina Thanks for the edit, I do not know why th tables were bad. I have also specified the conditions of illogical situations.
    – Macok
    Commented May 16 at 13:30
  • I do not know why th tables were bad. Empty line between text and table is compulsory.
    – Akina
    Commented May 16 at 13:37
  • I have also specified the conditions of illogical situations. First 3 conditions can be combined to "cannot exist 2 rows with the same cat" which needs in one unique (cat). Last 3 conditions can be implemented with according 2-column unique constraints. E.g. for condition 4 this will be unique by (carrier, box).
    – Akina
    Commented May 16 at 13:40
  • @Macok FWIW, this single table is denormalized and not a good way to manage data from an integrity standpoint. Why do you feel the need to use a single joined table instead of the separately normalized tables?
    – J.D.
    Commented May 16 at 14:19

1 Answer 1

0

I have a feeling you only care about cat in the car so why can't you put the cat in the car and make optional hierarchy of other container like

create table car (car_id int primary key)
create table cat (cat_id int primary key)
create table cat_carrier (cat_carrier_id int primary key)
create table cat_box (cat_box_id int primary key)
create table cat_car (car_id int not null references car,
                      cat_id int  not null references cat,
                      cat_carrier_id int null references cat_carrier,
                      cat_box_id int null references cat_box_id)

It's the simplest solution to start with.

2
  • Hi. Unfortunately cat and car are not the only important things. I will have a lot of situations where cat is put into carrier and only later possibly put into a car. Also, boxes are often filled with cats and carriers while being moved one way and empty while going back. And I need to carefully monitor both of those situations.
    – Macok
    Commented May 16 at 21:27
  • in this case you have to put cat_id in every container but can also add de-normalized field in cat_car like effective_cat_id - derived from any container and populated by triggers.
    – SergeyA
    Commented May 16 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.