Do I need a special database constraint on this table?

We have a comment system in our database. Just like stackoverflow -> every post has a list of comments. Kewl.

Anonymous users can add comments or registered users.

In my table, I am thinking about the following:

  • Userid int NULLABLE
  • AnonymousNickname varchar (100) NULLABLE
  • Anonymous Email varchar (200) NULLABLE

now it can only be one or the other. You are either registered or not.

So I have to enforce some type of constraint that says one or the other .. and if so, how?

NOTE. Database - Microsoft Sql Server 2008.

Hooray!

+1


source to share


2 answers


A simple limitation would be

(Userid is not null and AnonymousNickname is null and AnonymousEmail is null)
or (Userid is null and AnonymousNickname is not null and AnonymousEmail is not null)

      



This will force you to install only one or the other. It looks like it would be reasonable based on your application. It's up to you whether to apply it to your database or your application. If it's a hard constraint that other parts of your application depend on, I would probably use it in a database and detect it through validation in your code.

+2


source


I would not include the limitation. You must rename the name of the anonymous name to its name. When a registered user leaves a comment, put your username in the name field. This will help you view the user table as you list your comments. Also, if a user is removed from the user's table, their comments outlive them.



0


source







All Articles