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

Why do chiropractors use drop table?

674


My sql statement select avg(salary) from emp yields inaccurate results. Why?

653


Why select is not preferred in embedded sql programs?

721


Explain the benefits you can get from mainframe connect?

526


What is buffer pool and list some of them?

632






What is buffer pool in the db2 database?

628


What is meant by explain?

668


What is netezza database?

583


What is performance tuning db2?

630


What is a db2 package?

605


Is db2 a programming language?

614


Mention the different locking levels that are available in db2.

655


What is meant by dclgen?

625


When reorg is used?

686


What are the rules for db2 programming?

653