how do you restrict number of rows for a particular value
in a column.For example:there is a table called
fruits,having apples,bananas ,papayas.I dont want to have
more than 100 apples in that table ,so how can u restrict
number of rows for apple to hundred?
Answers were Sorted based on User's Feedback
Answer / gk
We can use Check constraint to restict the values of any
column.
Is This Answer Correct ? | 12 Yes | 3 No |
Answer / suresh babu
create check constraint is the best way to restrict the
limited number of entries,because the constraint won't take
a separate space,but Trigger required separate memory
space.perhaps the trigger got error,it would affect all
entries in table.
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / bikash khuntia
We have to create a trigger for that which is one of the
way for the solutuion as below:-
create or replace trigger trigger_name
before insert on table_name
for each row
DECLARE
v_count number;
begin
select count(apple) into v_count from table_name;
if v_count=100 then
raise_application_error('-20011','u cant insert more than
100 apples in the table');
end if;
end;
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / ashwin
You can create a sequence named seq_fruit as below .
create sequence seq_fruit
increment by 1
start with 1
maxvalue 100
minvalue 1
cycle
cache 10
if the current count ie currval of seq_fruit is 100 and
after this if u r inserting a new row in fruit table as
following
insert into fruit values(seq_fruit.nextval,30,40);
after the execution of this statement next value for apple
will be entered as 1 and not 101.
The cycle keyword will avoid the count of fruit column to go
beyond 100.
Is This Answer Correct ? | 0 Yes | 1 No |
Which are the most commonly used sql joins?
What is schema in sql?
1.when will you use week refcursor and when will you use strong ref cursor ? 2.what is the use of sql trace..how do you use it ? 3.can you please send all the sql plus commands...like set line 6000....
What is normalization? dec 2009
What is mutating error?
explain the difference between bool, tinyint and bit. : Sql dba
What are the types of keys?
what is column? : Sql dba
What are schema-level triggers?
What is rank () in sql?
What are the two virtual tables available at the time of database trigger execution?
What is sql used for?