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

Give the name of some fields form sqlca.

611


What do you mean by cursor?

586


What is copy pending status in db2?

636


What is the maximum size of a char data type in db2?

588


What is difference between alias and synonym in db2?

643






What is db2 bind?

639


What is reorg and runstats in db2?

694


How does a cursor work?

558


What is the clustering index in the db2 database?

584


Discuss about db2 bind?

670


What is dbrm? When it will be created?

598


Describe major components of db2?

640


What is sqlca’s maximum length?

674


What is the use of dclgen in db2?

588


How is deadlock resolved?

650