t1
col1 col2
nishi 5000
lucky 6700
akash 7000
i want that a query that when i insert 7000 it will show me data already present and data will not insert. if data is not present it will insert.
Answers were Sorted based on User's Feedback
The best thing would be to create a trigger which checks and not allows duplicate data .
Create or replace trigger trig1
before insert or update on t1
for each row
declare
a number (10);
begin
select count(*) into a from t1 where col2 = :new.col2;
if a >= 1 then
raise_application_error(-20123,'data already present and data will not insert');
elsif a = 0 then
dbms_output.put_line('inserted');
end if;
end;
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / ravi gali
hi u can use DUP_VAL_ON_INDEX exception
SQL> create table test(id number primary key);
Table created.
SQL> set serverout on
SQL>
begin
insert into test values(1);
exception
when DUP_VAL_ON_INDEX then
raise_application_error('duplicate value entered on the row ');
when others then
dbms_output.put_line('if u entered wrong data');
end;
Is This Answer Correct ? | 1 Yes | 2 No |
How do you bind variables in oracle?
What is the effect of setting the value "CHOOSE" for OPTIMIZER_GOAL, parameter of the ALTER SESSION Command ?
List the parts of a database trigger.
How would you extract DDL of a table without using a GUI tool?
How to display employee records who gets more salary than the average salary in the department?
what are bitmap indexes? How does they work?
What is different bet native dynamic sql and Dbms_Sql?
If I have a select statment which retrives 2 rows, & that rows have the same data in all the fields except the last field and I want to merge the 2 rows to be in 1 row with concatenating the last field which have the different data.... eg: the 1st row has these fields: A-B-C the second row has: A-B-X ........ i want to merge the two row to be in one row like ----> A- B- C,X
What are various constraints used in SQL?
i have a table and it has constraints and i want to get "ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint type" this code must be in string type please help me if you want, i can give more explainings
What is an Integrity Constrains ?
There is a big table with "n" of rows and 40 + columns .It doesn't have primary key.How do you select the primary key. In other words how do you get the duplicate records.