Friday, July 18, 2008

Primary Key with Null Value...Amazing

Who said Primary key column cannot have null values...Here is a method you can create primary key with null values...

Create a table with null values.

create table tbl_null (a number);
insert into tbl_null values (null);
insert into tbl_null values (null);
insert into tbl_null values (null);
insert into tbl_null values (null);
commit;

select * from tbl_null;
4 rows selected

Now create a table with primary key column having null values..

Create table tbl_pri_null ( a primary key) as select * from tbl_null;
Table created

desc tbl_pri_null
Name - A
Null ? - Not Null
Type - Number

Select * from tbl_pri_null;
4 rows selected
All null values.....

Thats it. Folks!
Try and have fun....

2 comments:

Sachin said...

SYSTEM: acpm11d1> create table tbl_null (a number);
insert into tbl_null values (null);
insert into tbl_null values (null);
insert into tbl_null values (null);
insert into tbl_null values (null);
commit;

Table created.

Elapsed: 00:00:00.58
SYSTEM: acpm11d1>
1 row created.

Elapsed: 00:00:00.11
SYSTEM: acpm11d1>
1 row created.

Elapsed: 00:00:00.11
SYSTEM: acpm11d1>
1 row created.

Elapsed: 00:00:00.10
SYSTEM: acpm11d1>
1 row created.

Elapsed: 00:00:00.11
SYSTEM: acpm11d1>
Commit complete.

Elapsed: 00:00:00.10
SYSTEM: acpm11d1> Create table tbl_pri_null ( a primary key) as select * from tbl_null;
Create table tbl_pri_null ( a primary key) as select * from tbl_null
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYSTEM"."TBL_PRI_NULL"."A")

Vinod Dhandapani said...

Hi Sachin,

Try that in Oracle 9i. Oracle might have fixed that bug in 10g...

Followers

Powered By Blogger
 

Oracle Database Administration. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com