Home » SQL & PL/SQL » SQL & PL/SQL » getting error on enabling constraints even using novalidate clause (Oracle ,10g,windows 7)
getting error on enabling constraints even using novalidate clause [message #603965] |
Sat, 21 December 2013 03:19 |
|
CREATE TABLE SCOTT.STUDENT_DETAILS
(
ROLLNO NUMBER,
NAME VARCHAR2(50 BYTE),
CITY VARCHAR2(20 BYTE),
MARKS NUMBER,
INVOICENO CHAR(1 BYTE)
)
select * from student_details
alter table student_details
add (constraint pk primary key(rollno),
constraint uk unique(invoiceno),
constraint ck check(marks>0))
modify name not null modify city default 'pune'
select * from all_constraints
where table_name='STUDENT_DETAILS'
insert into student_details(rollno,name,marks,invoiceno)
values(2,'Raj',100,'z')
alter table student_details
disable constraint ck
alter table student_details
modify name null modify city default null
insert into student_details(rollno,name,marks,invoiceno)
values(2,'Raj',100,'z')
alter table student_details
enable novalidate primary key
even I am using novalidate clause still getting error like below
ORA-02437: cannot validate (SCOTT.PK) - primary key violated
|
|
|
Re: getting error on enabling constraints even using novalidate clause [message #603966 is a reply to message #603965] |
Sat, 21 December 2013 03:53 |
|
Littlefoot
Messages: 21808 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
This is, I presume, because of the fact that primary key constraint wants to use a unique index on the primary key column(s). A workaround is to a) create a non-unique index, b) create a primary key. Something like this:
SQL> CREATE TABLE SCOTT.STUDENT_DETAILS
2 (
3 ROLLNO NUMBER,
4 NAME VARCHAR2 (50 BYTE),
5 CITY VARCHAR2 (20 BYTE),
6 MARKS NUMBER,
7 INVOICENO CHAR (1 BYTE)
8 );
Table created.
SQL> CREATE INDEX i1_pk
2 ON student_details (rollno);
Index created.
SQL> ALTER TABLE student_details
2 ADD CONSTRAINT pk PRIMARY KEY(rollno);
Table altered.
SQL> ALTER TABLE student_details DISABLE CONSTRAINT pk;
Table altered.
SQL> INSERT INTO student_details (rollno,
2 name,
3 marks,
4 invoiceno)
5 VALUES (2,
6 'Raj',
7 100,
8 'z');
1 row created.
SQL>
SQL> INSERT INTO student_details (rollno,
2 name,
3 marks,
4 invoiceno)
5 VALUES (2,
6 'Raj',
7 100,
8 'z');
1 row created.
SQL> ALTER TABLE student_details ENABLE NOVALIDATE PRIMARY KEY;
Table altered.
SQL>
|
|
|
Re: getting error on enabling constraints even using novalidate clause [message #603968 is a reply to message #603965] |
Sat, 21 December 2013 04:44 |
|
thank you littlefoot
the way you have suggested is work around so what is exact issue as I have done every thing right
so why I am getting that error
second when I try to enable unique constraint with novalidate clause I am still a getting a same kind of error
for the below command
alter table student_details
enable novalidate unique(invoiceno)
and error like "ORA-02299: cannot validate (SCOTT.UK) - duplicate keys found"
|
|
|
|
|
Re: getting error on enabling constraints even using novalidate clause [message #603973 is a reply to message #603965] |
Sat, 21 December 2013 09:44 |
martijn
Messages: 286 Registered: December 2006 Location: Netherlands
|
Senior Member |
|
|
Hi Evivek,
Have a good look at the inserts:
evivek wrote on Sat, 21 December 2013 10:19[size=2]...
...
insert into student_details(rollno,name,marks,invoiceno)
values(2,'Raj',100,'z')
...
...
insert into student_details(rollno,name,marks,invoiceno)
values(2,'Raj',100,'z')
...
...
ORA-02437: cannot validate (SCOTT.PK) - primary key violated
You are entering the exact same information twice.
You have created a primary key on rollno.
This means rollno must be:
- not null
- unique
You did enter the same number for rollno twice. So this is not unique.
In other words: the primary key is violated
HTH
|
|
|
|
|
|
|
Re: getting error on enabling constraints even using novalidate clause [message #604028 is a reply to message #603965] |
Mon, 23 December 2013 03:25 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
evivek wrote on Sat, 21 December 2013 14:49even I am using novalidate clause still getting error like below
ORA-02437: cannot validate (SCOTT.PK) - primary key violated
When you create a primary key, implicitly an unique index is created. And disabling the constraint will drop this unique index.
However, Oracle behaves differently when there already exists an index(non-unique). As Littlefoot demonstrated, now this non-unique index would be used and thus duplicates can be entered.
So, the correct way is :
SQL> drop TABLE STUDENT_DETAILS;
Table dropped
SQL> CREATE TABLE STUDENT_DETAILS
2 (
3 ROLLNO NUMBER,
4 NAME VARCHAR2(50 BYTE),
5 CITY VARCHAR2(20 BYTE),
6 MARKS NUMBER,
7 INVOICENO CHAR(1 BYTE)
8 );
Table created
SQL> alter table student_details
2 add (
3 constraint uk unique(invoiceno),
4 constraint ck check(marks>0))
5 modify name not null modify city default 'pune';
Table altered
SQL> create index tbl_pk ON student_details(rollno);
Index created
SQL> alter table student_details add constraint pk primary key (rollno) disable keep index;
Table altered
SQL> insert into student_details(rollno,name,marks,invoiceno)
2 values(2,'Raj',100,'z');
1 row inserted
SQL> alter table student_details
2 disable constraint uk;
Table altered
SQL> alter table student_details
2 disable constraint ck;
Table altered
SQL> alter table student_details
2 modify name null modify city default NULL;
Table altered
SQL> insert into student_details(rollno,name,marks,invoiceno)
2 values(2,'Raj',100,'z');
1 row inserted
SQL> SELECT * FROM student_details;
ROLLNO NAME CITY MARKS INVOICENO
---------- -------------------------------------------------- -------------------- ---------- ---------
2 Raj pune 100 z
2 Raj 100 z
A similar question was posted recently here
|
|
|
Goto Forum:
Current Time: Fri Apr 26 07:57:07 CDT 2024
|