Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I have a MySQL table that records classified listings. We don't force users to join to post a listing, and therefore the listing will not always have a user_id associated with it.</p> <p>I therefore need a method of recording the poster's email if they are not signed in.</p> </blockquote> <p>What is the business key of your user entity? Or, more directly: what <em>is</em> your user entity? Is every distinct email address a key for a User entity with some users having registered and their email set in some profile, and others not registered and giving an email address every time they post? Or do you have two distinct entities, RegisteredUser and UnknownPosterWithEmailAddress, with their attributes stored in separate places?</p> <p>In the latter case, you would use a NULLable user_id and a NULLable email field, like you suggested, but then queries like "for a given post, find the email address the reply should be sent to" are going to be awkward, e.g. a list of all post with their respective reply addresses will look like this: </p> <pre><code>select post.id, case when post.user_id is not null then user.email else post.email end as email from post left join user on user.id=post.user_id; </code></pre> <p>This can get real messy after a while.</p> <p>I'd rather use the former approach: each row in User is a dsitinct poster, with an non-NULLable unique email address, and a surrogate key as foreign key in posts:</p> <pre><code>create table user(id integer primary key, email text not null unique, is_registered boolean default false); create table post(id integer primary key, user_id integer not null references user(id), content text); </code></pre> <p>If a non-registered user enters an email address, you look it up in the user table, and retrieve the user.id, adding a new entry in user if necessary. As a result, you can answer questions like: for a given email address, how many posts has this user made in the past week? via the foreign key field, without having to compare strings in some NULLable attribute field.</p> <p>When a user chooses to register, you can add the registration data either in user itself or in some separate table (again with user.id as a foreign key, some might argue that a boolean field is_registered is actually redundant then). Added benefits:</p> <ul> <li>If he has posted before under the same email address, now all of his old posts become associated with his new registered identity automatically.</li> <li>If the user changes his email address in his profile, all replies to older posts of his "see" the new updated email address.</li> </ul>
    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. This table or related slice is empty.
    1. VO
      singulars
      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