How to use oracle check constraints to limit number of registration?

后端 未结 3 1143
礼貌的吻别
礼貌的吻别 2021-01-28 14:57

I\'ve a user table with unique user_id. User can register using there id. Now I want to limit the max. registration per user using CHECK constraints. so I use this:



        
3条回答
  •  心在旅途
    2021-01-28 15:39

    You cannot use constraints in this case but you may create an updatable view with check option:

    drop table t;
    drop view v;
    create table t (counter number);
    create view v as select * from t where (select count(*) from t) <= 2 with check option;
    insert into v values(1); -- OK
    insert into v values(2); -- OK
    insert into v values(3); -- OK
    insert into v values(4); -- ERROR
    

    You may adapt this example for your case.

    An updatable view is view which can be treated as an ordinary table. You can create such a view with CHECK OPTION in this case Oracle will prevent you from executing DML on this view which don't relevant to the WHERE clause for this view.

提交回复
热议问题