+2  A: 

Edited for clarity

According to the SQL Specification, a primary key can not contain NULL. This means that decorating a column with either "NOT NULL PRIMARY KEY" or just "PRIMARY KEY" should do the same thing. But you are relying on the SQL engine in question correctly following the SQL standard. As an example due to an early bug, SQL Lite does not correctly implement the standard and allows null values in non-integer primary keys (see sqlite.org/lang_createtable.html). That would mean for (atleast) SQLLite the two statements do two different things.

As "NOT NULL PRIMARY KEY" clearly indicates intent and continues to enfore non-null values even if the primary key is removed, that should be the prefered syntax.

Paul Hadfield
I asked about PK without "NOT NULL, not about "NOT NULL" without PK. I could not manage to have NULL in PK field in any possible way.
vgv8
@vgv8: Sorry for the confusion, to be clear I was saying that you could just have "PRIMARY KEY" or "NOT NULL PRIMARY KEY" and they'd both do the same thing. but if you removed "PRIMARY KEY", the first statement would then allow the column to allow nulls, whilst the other wouldn't, In otherwords, only the 2nd truely describes the desired intent, whilst the 1st is taking advantage of a snytax short cut. I'm sure they wouldn't, but if MS changed SQL to allow a single null in a primary key then the two behaviours would be radically different.
Paul Hadfield
I propose to omit formalities. like @Paul Hadfield, if nobody else is participating in discussion. I upvoted your answer more for link given in comment, helpful for insight and revealing some definitions or approaches, than for main answer itself. I also posted subquestion http://stackoverflow.com/questions/3906811/why-to-permit-null-in-primary-key. There are fundamental concepts and principles, which, I believe should be common, and implementations specific to different RDBMS
vgv8
I gave credits to your reply in comments in my mentioned subquestion. Can't you add your link into your answer? It saved me from making from a spectacle there
vgv8
+1  A: 

It isn't required syntactically as this will be the default anyway if not specified for Primary Key columns regardless of the default setting for the other columns.

SET ANSI_NULL_DFLT_ON ON /*Set default for columns to allow NULL if not specified*/

CREATE TABLE #t
(
i INT PRIMARY KEY,
j INT
)

SELECT name,is_nullable
FROM tempdb.sys.columns 
WHERE object_id=object_id('tempdb..#t')


INSERT INTO #t VALUES (1,NULL) /*Succeeds as j allows NULL*/

INSERT INTO #t VALUES (NULL, 1)/*Won't succeed as column doesn't allow nulls*/

DROP TABLE #t
Martin Smith
+1 for supporting my question. I really did run such scripts before posting. Still I'd like to have clear definitions and answers to why. This is fundamental concept and I do not want to make from me clown when answering such simple basic questions to someone
vgv8
A: 

All primary key means it is the identifier for the table and therefor unique by definition. Unique doesn't mean not null, so if you have the requirement that the primary key can't be null you have to specify so explicitly. If you allow null you're just saying that it is ok to have a null value in the column, but you're only allowed to have one since it has to be unique.

mastoj
You don't have to specify it explicitly. It is implied by making it the PK.
Martin Smith
+4  A: 

The following:

CREATE TABLE T1 (
    T1ID int not null primary key
)

is really shorthand for the following:

CREATE TABLE T1 (
    T1ID int not null,
    constraint <system generated name> PRIMARY KEY (T1ID) on [PRIMARY]
)

The column definition is really quite separate from the constraint definition. It's just that the shorthand hides the fact that it's two separate definitions. (Of course, if you use the long-hand form, you can do such things as defining a primary key across multiple columns).

Edit, in response to update. No, you still cannot have a nullable column in the primary key. But given that it's normal to specify the nullability of each column in a create table statement, it just feels odd to omit it (even though it's implied/required by the PRIMARY KEY constraint).

CREATE TABLE T1 (
    T1ID int,
    constraint <system generated name> PRIMARY KEY (T1ID) on [PRIMARY]
)

creates exactly the same table as the previous code sample. Explicitly specifying the "not null"-ability of a column is, then, more an explicit acknowledgement of the outcome, rather than a requirement.

And finally, you'll find it frequently occurring in example code because people have developed their database, and then used the built in SQL tools for generating a script from their database - the scripting tools always put everything explicitly.

Damien_The_Unbeliever
I did not ask whether I can or not. I asked why "NOT NULL" is insistingly scripted in addition to "primary key" TSQL definition by SSMS
vgv8
A: 

I don't know of any DBMS that permits a nullable column to be used in a PRIMARY KEY constraint. The SQL standard specifies that columns in a PRIMARY KEY constraint cannot be nullable.

In the relational model candidate keys consist of unique values, not nulls. It is a peculiarity of SQL that nullable columns can optionally be used in UNIQUE constraints. That feature is probably responsible for a lot of data quality problems. I highly recommend you avoid using nullable columns in UNIQUE constraints. If necessary, make a new table for the non-nullable values and put the UNIQUE constraint on that.

dportas