Can a primary key have null values? If we try to insert a
null value in a primary key column, will it work or give an
error code?

Answer Posted / ratheesh nellikal

In DB2 you can not create a primary key on a column which
is not 'not null'.
If you are trying to create a primary key on a column which
is not 'not null' then dbb2 will return an error like this

SQL0542N "feild_name" cannot be a column of a primary key
or unique key because it can contain null values.
SQLSTATE=42831

But once u alter the table and set the column to 'NOT NULL'
then you can make it as primary key.

Now if u r trying to insert an null value to ur primary key
column then db2 will trows an error like

SQL0407N Assignment of a NULL value to a NOT NULL
column "TBSPACEID=5, TABLEID=404, COLNO=0" is not allowed.
SQLSTATE=23502.

In short you can not make a column which can hold 'NULL'
value as your primary key and you can not insert a 'NULL'
value into your primary key feild.

Cheers,
Ratheesh Nellikal

Is This Answer Correct ?    7 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is utility for parm lib

2118


What are the contents of a dclmgen?

866


How do I delete a table in database?

805


How do I import a csv file into db2?

775


What is load replace in db2?

773


I have a subprogram B calling from mainprogram A.I have opened a cursor in A ,Can i fetch the same cursor in subprogram B , If yes , Please explain the reason?

2170


What are the various data types available in db2?

831


Highlight all the advantages that are attached to a package.

912


What is the syntax for FETCH in DB2 ?

944


What are the contents of dclgen?

871


How do you simulate the explain of an embedded sql statement in spufi/qmf?

863


Explain about dbrm.

807


What is ibm db2 used for?

844


How to run db2 command in windows?

905


How would you find out the total number of rows in a db2 table?

806