我有一個需要對我的表實施的復合唯一約束,它包含這些列 N、DATE_、ID_OPERATION 的年份部分。我嘗試撰寫的代碼不正確
ALTER TABLE my_table
ADD CONSTRAINT UNIQUE_EXTRAIT UNIQUE (N, EXTRACT(year from DATE_), ID_OPERATION)
有可能嗎?
uj5u.com熱心網友回復:
約束不起作用(據我所知),但唯一索引會起作用。
SQL> create table test as select ename, hiredate from emp where deptno = 10;
Table created.
SQL> select * from test order by ename;
ENAME HIREDATE
---------- ----------
CLARK 09.06.1981
KING 17.11.1981
MILLER 23.01.1982
這行不通:
SQL> alter table test add constraint uk_test
2 unique(ename, extract(year from hiredate));
unique(ename, extract(year from hiredate))
*
ERROR at line 2:
ORA-00904: : invalid identifier
但這將:
SQL> create unique index ui1_test on test (ename, extract (year from hiredate));
Index created.
測驗:
SQL> insert into test values ('CLARK', date '1981-01-01');
insert into test values ('CLARK', date '1981-01-01')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UI1_TEST) violated
SQL> insert into test values ('CLARK', date '1985-01-01');
1 row created.
SQL>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/355163.html
