Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If we look into the model here, we will see the following:</p> <ol> <li>A user is related to exactly one website <ul> <li>A company is related to exactly one website</li> <li>A website is related to exactly one user or company</li> </ul></li> </ol> <p>The third relation implies existence of a "user or company" entity whose <code>PRIMARY KEY</code> should be stored somewhere.</p> <p>To store it you need to create a table that would store a <code>PRIMARY KEY</code> of a <code>website owner</code> entity. This table can also store attributes common for a user and a website.</p> <p>Since it's a one-to-one relation, website attributes can be stored in this table too.</p> <p>The attributes not shared by users and companies should be stored in the separate table.</p> <p>To force the correct relationships, you need to make the <code>PRIMARY KEY</code> of the <code>website</code> composite with <code>owner type</code> as a part of it, and force the correct type in the child tables with a <code>CHECK</code> constraint:</p> <pre><code>CREATE TABLE website_owner ( type INT NOT NULL, id INT NOT NULL, website_attributes, common_attributes, CHECK (type IN (1, 2)) -- 1 for user, 2 for company PRIMARY KEY (type, id) ) CREATE TABLE user ( type INT NOT NULL, id INT NOT NULL PRIMARY KEY, user_attributes, CHECK (type = 1), FOREIGN KEY (type, id) REFERENCES website_owner ) CREATE TABLE company ( type INT NOT NULL, id INT NOT NULL PRIMARY KEY, company_attributes, CHECK (type = 2), FOREIGN KEY (type, id) REFERENCES website_owner ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload