Creating a table with max number of rows (ORACLE)

孤者浪人 提交于 2019-12-13 19:15:01

问题


Is there any way to limit the maximum number of rows when I create a table in Oracle?


回答1:


If the table has a numeric key, you could add a check constraint that states that the key does not exceed a specific value:

ALTER TABLE turnip
ADD CONSTRAINT check_turnip_id
CHECK (turnip_id <= 50)
/



回答2:


You could create a statement trigger "BEFORE INSERT" and check there the nubmer of rows.

CREATE OR REPLACE TRIGGER BI_MY_TABLE
    BEFORE INSERT ON MY_TABLE
DECLARE
    CountRows NUMBER;
BEGIN
    SELECT COUNT(*)
    INTO CountRows
    FROM MY_TABLE;

    IF CountRows > 100 THEN
        RAISE_APPLICATION_ERROR(-20001, 'Only 100 records are allowed');    
    END IF;
END;
/

However, this trigger does not work properly in a multi-user environment.



来源:https://stackoverflow.com/questions/21047903/creating-a-table-with-max-number-of-rows-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!