views:

70

answers:

4

I am doing my first database project.

I would like to know why you should use NOT NULL in the following query

...
TITLE nvarchar(60) NOT NULL
..

Context

CREATE TABLE Questions 
(
         USER_ID integer FOREIGN KEY 
                         REFERENCES User_info(USER_ID) 
                         PRIMARY KEY 
                         CHECK (USER_ID>0), 
         QUESTION_ID integer FOREIGN KEY REFERENCES Tags(QUESTION_ID) 
                             NOT NULL 
                             CHECK (USER_ID>0), 
         QUESTION_BODY nvarchar(4000) NOT NULL,
         TITLE nvarchar(60) NOT NULL,                               /////// HERE                             
         MODERATOR_REMOVAL boolean NOT NULL,
         SENT_TIME varchar(15) NOT NULL
)

I watched VPuml's tutorial. They put all values in Logical diagram nullable, while all the rest NOT NULL. This suggests me that nullable should be used with logical diagrams.

Is there any other use of not null in databases?

I feel that we can check that the user gives value by JS, for instance, not at a database level.

+1  A: 

NOT NULL is often used for foreign keys (i.e.-links to other tables). It ensures that a row links to some other table.

It is almost always used on primary keys, meaning the unique identifier in the table.

It's also a great constraint for required fields. This ensures that the transaction will fail if a user doesn't enter in a title or a question. Databases are great at enforcing constraints like this very quickly, so you see a lot of that type of business logic put into databases. Do note, however, that you'll want to check for a database error on the application side and handle it accordingly.

Anyway, NOT NULL is really for a column you always want a value in. The cases for that are actually quite a few, but hopefully this gives you some semblance of why we use it.

Eric
*It is almost always used on primary keys, meaning the unique identifier in the table.* - I read from W3Schools that primary key is always `NOT NULL`. I understood their SQL -tutorial such that you cannot put `NOT NULL` declaration to the column which is primary key. Otherwise, you would declare twice `NOT NULL`. --- However, your answer suggests me that I misunderstood the tutorial. **Does primary key -declaration implicitly include `NOT NULL` -declaration?**
Masi
Yes it does. You must have a primary key defined for a row if you have a primary key in the table.
Brian Ramsay
+4  A: 

If a null value is not allowed, you should check on user entry and have NOT NULL in the database. Having NOT NULL in the database allows you to make absolutely sure that no bad data is entered into the database, regardless of mistakes in front-end code.

However, since database errors are generally bad to show your users, you should check to make sure that a null value is not being submitted before it gets to the database check.

Brian Ramsay
How would you check that the null value is not submitted by the user to the database? --- I cannot lean only to JS, since not all users allow it.
Masi
Use whatever server-side language you are using. Before you execute the code to insert into the database, ensure that your database parameters are correct.
Brian Ramsay
+1  A: 

NOT NULL means that a value must be placed in the column. It is used when it makes no sense for the data to exist without a value.

For instance, in your "SO" database, it makes no sense for a question to exist without the user_id column being known. A question must always have been created by a user.

John Saunders
+1  A: 

Occasionally, a query depends on whether a certain field has a value. Having the field be NOT NULL means you only have to check against a single empty value, in this case '' (the empty string). Allowing NULL values means you may have to check for that, too, which can complicate the query.

eswald