2016-11-05 11 views
2

私のエンティティ内の行数を制限したい。 たとえば、テーブルファミリ(id、name)を作成しましたが、ファミリテーブルに5行以上挿入することはできません。 提案をいただいた方に感謝します。。エンティティ内の行数を制限する

答えて

-2

これを試してください。私はこれがあなたを助けることができると思います。凝集体またはデータ上の制約のために

CREATE TABLE family 
(id NUMBER PRIMARY KEY 
,name VARCHAR2(100) NOT NULL 
,rownum NUMBER NOT NULL 
,CONSTRAINT rownum_check CHECK (rownum = TRUNC(rownum) AND rownum BETWEEN 1 AND 5) 
,CONSTRAINT rownum_uk UNIQUE (rownum) 
); 
+0

'rownum'は、Oracleの予約語であります –

2

複数のテーブルからのヘルパーMATERIALIZED VIEWを作成することができます。

[email protected]_PDB_TCP> CREATE TABLE family (id NUMBER PRIMARY KEY, name VARCHAR2(255)); 

Table created. 

[email protected]_PDB_TCP> CREATE MATERIALIZED VIEW LOG ON family WITH ROWID INCLUDING NEW VALUES; 

Materialized view log created. 

CREATE MATERIALIZED VIEW family_count 
REFRESH FAST ON COMMIT 
AS SELECT count(*) family_count FROM family; 

Materialized view created. 

[email protected]_PDB_TCP> ALTER MATERIALIZED VIEW family_count ADD CONSTRAINT max_family_count CHECK (family_count <= 5); 

Materialized view altered. 

[email protected]_PDB_TCP> INSERT INTO family (id, name) values (1, 'Family 1'); 

1 row created. 

[email protected]_PDB_TCP> INSERT INTO family (id, name) values (2, 'Family 2'); 

1 row created. 

[email protected]_PDB_TCP> INSERT INTO family (id, name) values (3, 'Family 3'); 

1 row created. 

[email protected]_PDB_TCP> INSERT INTO family (id, name) values (4, 'Family 4'); 

1 row created. 

[email protected]_PDB_TCP> INSERT INTO family (id, name) values (5, 'Family 5'); 

1 row created. 

[email protected]_PDB_TCP> COMMIT; 

Commit complete. 

[email protected]_PDB_TCP> INSERT INTO family (id, name) values (6, 'Family 6'); 

1 row created. 

[email protected]_PDB_TCP> COMMIT; 
COMMIT 
* 
ERROR at line 1: 
ORA-12008: error in materialized view refresh path 
ORA-02290: check constraint (HUSQVIK.MAX_FAMILY_COUNT) violated 
関連する問題