Unique index or Unique constraint
Hi allI am a student of Oracle SQL Expert Certification. At one point in the book, speaking of the clues, the author says that a unique index is not the same a unique constraint. However, it does not explain why they are two different things.
Could someone clarify the difference between the two, please?
Thank you very much
Valerio
A constraint has a different meaning for an index. It gives the optimizer more information and allows you to have foreign keys on the column, then that is not a unique index.
for example:
SQL> create table t1 (col1 number, col2 varchar2(20), constraint t1_uq unique (col1));
Table created.
SQL> create table t2 (col1 number, col2 varchar2(20));
Table created.
SQL> create unique index t2_idx on t2 (col1);
Index created.
SQL> create table t3 (col1 number, col2 number, col3 varchar2(20), constraint t3_fk
2 foreign key (col2) references t1 (col1));
Table created.
SQL> create table t4 (col1 number, col2 number, col3 varchar2(20), constraint t4_fk
2 foreign key (col2) references t2 (col1));
foreign key (col2) references t2 (col1))
*
ERROR at line 2:
ORA-02270: no matching unique or primary key for this column-list
It's like saying ' what is the difference between a Chair and a Chair? They both allow you to sit! »
Tags: Database
Similar Questions
-
Index naming standards does not and unique constraints
I found yet another question of naming standards
I have tried to create a unique constraint as a normal index via the unique constraints and indexes in Properties of Table with the assumption that it will use the design properties of models. This isn't.
As noted in the image of the model above, my Unique constraint model is {table} _ {columns} lancement_remarques
and my model of Index idx _ {columns} {table}. Yet, when I try to create the Unique constraint, it creates the naming convention in names like CUSTOMER_ADDRESS__UK (2 lines of underscore?) as shown below.
My assumption is that he needed a column, so I added the column and also applied the rules naming - nothing.
Is my wrong assumption on these constraints being appointed through the models? If this is not the case, what naming rules is in this context?
The same behavior is evident when you try to create index.
I get not all comments on my posts. Are these bugs or just user error?
Hello
Thank you to report the problem, I logged a bug. You can get 'Rules of naming' works properly if you press the button 'Apply' before that.
I get not all comments on my posts.
I don't think that anyone (or any job) has been ignored on this forum. Depending on the complexity of the problem and our current commitment (other urgent tasks, holidays, time zone), you can get answer almost immediately or after a certain time.
It is useful for us, if you mark your messages as replied if you get the right answer.
Philippe
-
Indexes and UNIQUE constraints
For a TEST table (col1, col2, col3,...), I put the UNIQUE constraint as follows:
ALTER table TEST add constraint test_uniq UNIQUE col1, col2, col3);
It is said that when the UNIQUE constraint is created, ORACLE also creates an index on (col1, col2, col3) automatically. But sometimes, I use only one of the columns in the WHERE clause as col1 = xxx, or col2 = yyy.
Create separate indexes, one for col1 and col2? I think that even if the creation of the UNIQUE constraint also created an index. But this index is a composite index that is for the dosage of col1, col2 and col3 simultaneously (is that correct)? If so, create an index for col1 and col2 will be necessary?
Thank you for helping.
ScottIf there is an index on (col1, col2, col3) and ask about col1, the index will be used.
If you have questions about col2, it can (index skip scan) or maybe not.
If you ask about col3 then it will not be used.
So if you think that there will be queries with col3 in where clause and they can benefit from an index, you can create it.
-
Name of the PK, unique constraints and to support the index
I want to have my primary key constraint and index support share of the same name, IE:
Without definition explicit a unique index using the relational model, the generated DDL added 'X' for the name of the index:CREATE UNIQUE INDEX MYTABLE_PK ON MYTABLE(...); ALTER TABLE MYTABLE ADD CONSTRAINT MYTABLE_PK PRIMARY KEY(...);
I tried to add an index unique explicit to the table, but he changed the name (adds "v1") when I leave the table editor:CREATE UNIQUE INDEX MYTABLE_PKX ON MYTABLE(...); ALTER TABLE MYTABLE ADD CONSTRAINT MYTABLE_PK PRIMARY KEY(...);
I have the same problem with index & unique constraints.CREATE UNIQUE INDEX MYTABLE_PKv1 ON MYTABLE(...); ALTER TABLE MYTABLE ADD CONSTRAINT MYTABLE_PK PRIMARY KEY(...);
Issues related to the:
Can I generate DDL with name matching? "v1" thing seems unlikely, he doesn't want not myself save the definition as I want.
Where are the transformations of naming for this location? I found tools, preferences, and Data Modeling/naming Standards/Templates but this nugget does nto seem to be there.
Thank you.Hi Mike,.
The Production data release model 3.1 has been updated to allow constraints unique and primary and their indexes share the same name.
David
-
a unique index or unique constraint on the issue of view Matt
10.2.0.3
I have an OLTP table and a matte view to fast refresh of the table in the warehouse. I have unique indexes on the matte view just as I have on the OLTP table. Of course, it's a bad idea because the updating Oracle on mattress views mechanism does not apply to the dml in the same order that it occurred on the side of OLTP? Should I get rid of all the unique indexes on views mattress in my warehouse and create regular index because of their unique nature will just happen because the side OLTP has a unique index? What will be the impact on the performance of the queries? Here's the alert log...
Journal of owp2 alerts
=======================
ORA-12012: error on auto work 1595
ORA-12008: error path refresh materialized view
ORA-00001: unique constraint (SMS_AR. IU02_ROUTE_REF_MRKR) violated
ORA-06512: at "SYS." DBMS_SNAPSHOT", line 510
ORA-06512: at line 1
ORA-00001: unique constraint (SMS_AR. IU02_ROUTE_REF_MRKR) violated
ORA-00610: internal error Code
ORA-12012: error on auto work 260282
ORA-30439: updating of the ' ORA-30439: updating of the 'SMS_AR MV_ROUTE_REF_MRKR' failed due to the ORA-12008: error in the path of refresh materialized view
ORA-00001: unique constraint (SMS_AR. IU02_ROUTE_REF_MRKR) violated
ORA-06512: at "SYS." DBMS_SNAPSHOT", line 2254
ORA-06512: at "SYS." DBMS_SNAPSHOT", line 2460
ORA-06512: at "SYS." DBMS_SNAPSHOT", line 2429
ORA-06512: at "SMS_AR.PA_PIES_WAREHOUSE", line 44
ORA-06512: at line 2
ORA-20000: index 'SMS_AR '. "' I01_MV_PIES_INV_REFMKR ' or the partition of this index is unusableMark Reichman wrote:
I think that this problem is resolved... Unless someone has something else to add. I have not tried yet... But it seems to be valid. I did a test and a unique constraint can be delayed in fact creates a non-unique index. So I need to remove my unique index on my matte view and create unique constraints can be delayed.Or...
forget the unqiue part and simply change the indexes not unique because the main table has a unique index and guarantees uniqueness for me and the matte view will simply copy whats in the main table.
Mark,
the solution seems reasonable. Just a note: If you use a "reportable" unique constraint Oracle ignores any attempt to perform a direct-path insert of access and still stations conventional insert generating undo and many more again.
As long as you do only a 'rapid' refresh, it should not matter, but in case you deal with refreshs full large MVs, this could make a difference when running not atomic refreshs (who can take advantage of the direct-path inserts / DML etc. at the same time)...
Then you can consider using only non-unique index rather than the constraint may be delayed if performance can matter and given the fact that you should never see duplicates in the MV because of the constraint on the base table.
Furthermore, you can use a non-unique index to apply a not reportable unique/primary key constraint as well, it is supported. You just need to create the index yourself before you set the constraint or using explicit syntax "CREATE INDEX" of the constraint clause.
For more information, I wrote a note on this problem "may be deferred" some time ago:
http://Oracle-Randolf.blogspot.com/2008/07/Deferrable-constraints-and-direct-path.html
Kind regards
RandolfOracle related blog stuff:
http://Oracle-Randolf.blogspot.com/SQLTools ++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676 /.
http://sourceforge.NET/projects/SQLT-pp/ -
Unique constraint on the values in both directions
I'm looking to create a unique constraint that works in two ways. Say I got a constraint unique in columns 1 and 2. I want it to be impossible for the two lines below the two exist at the same time. Is there a way to do this? I googled around for a while now and I found nothing that works so far.
Header 1 Header 2 Dog CAT CAT Dog Hello
You can create an index based on a single function:, like this:
CREATE UNIQUE INDEX table_x_header_1_header_2
ON table_x (LESS (header_1, header_2)
More LARGE (header_1, header_2)
);
How will you use these values? You might be better to simply create a regular old unique constraint, but also have a CHECK constraint to ensure that header_1<= header_2. ="" that="" way,="" when="" you="" want="" to="" search="" for="" the="" combination="" ('cat',="" 'dog')="" you="" won't="" have="" to="" search="" for="" ('dog',="" 'cat')="">=>
-
Customized for a Unique constraint processing scripts
Hallo,
How can I create the Unique constraint (not a Unique Index) transformation scripts:
var key_column = 'KEY'; // get tables tables = model.getTableSet().toArray(); for (var t = 0; t < tables.length;t++){ table = tables[t]; table_name = table.getName(); abbr = table.getAbbreviation()+"_"; key_col = table.getElementByName(key_column); if (key_col!=null){ uniq = table.createIndex(); // it is right? table.setDirty(true); uniq.setName('UNIQUE'); // does not matter uniq.add(id3_col); uniq.setUK(true); // a method does not exist table.setUK(uniq);// a method does not exist } }
regarding
Oleg
Hello Oleg,
lines 14 and 15 are false. The use of the persistence method is:
uniq.setIndexState ("Unique constraint");
Other options here - you can spot if you look at the XML file representing the table with PK and UK constraints and regular index - "primary constraint, «Unique Index ordinary', «Clear index»»
Philippe
-
Case-insensitive unique constraint
Hi guys,.
Is it possible to create a unique constraint that is case-insensitive
I mean if there is a unique constraint on the name of col
then
'James' and 'JAMES' must not
Kind regards
PAPICreate a unique index to the basic function of...
create unique index uk_name on emp(upper(ename));
-
Unique constraint violation error
Hi all
I have a procedure called - FHM_DASHBOARD_PROC which inserts data into a table called FHM_DASHBOARD_F retrieve records in multiple tables. However, for a particular type of record, these data are not inserted because of the Unique constraint violation
the procedure is:
and the table where data integration iscreate or replace PROCEDURE FHM_DASHBOARD_PROC AS DB_METRICS_CNT1Z number; --V_PODNAME varchar2(10); V_KI_CODE_DB_STATSZ varchar2(50); V_ERRORSTRING varchar2(100); --CURSOR PODNAME_CUR IS SELECT PODNAME,SHORTNAME FROM CRMODDEV.POD_DATA WHERE PODSTATUS_ID=1 AND PODTYPE_ID=1 ORDER BY PODNAME; -- DB STATS BEGIN -- OPEN PODNAME_CUR; -- LOOP -- FETCH PODNAME_CUR INTO V_PODNAME,V_POD_SHORTNAME ; -- EXIT WHEN PODNAME_CUR%NOTFOUND; BEGIN SELECT COUNT(*) INTO DB_METRICS_CNT1Z FROM FHM_DB_METRICS_F A, FHM_DB_D B where A.DBNAME=B.DBNAME and PODNAME=V_PODNAME AND DB_DATE=TRUNC(SYSDATE-1); DBMS_OUTPUT.PUT_LINE('DB_METRICS_CNT1Z :'|| DB_METRICS_CNT1Z); IF DB_METRICS_CNT1Z >0 THEN DBMS_OUTPUT.PUT_LINE('DB STATS'); INSERT INTO FHM_DASHBOARD_F(PODNAME,DASH_DATE,KI_CODE,KI_VALUE,KI_STATUS) (SELECT PODNAME, DASH_DATE AS CU_DATE, KI.KI_CODE, NVL(PF.KI_VALUE,0), CASE WHEN PF.KI_VALUE = ki.warning_threshold then 2 when PF.KI_VALUE=0 then 0 ELSE 1 END AS ALERT_STATUS FROM (SELECT PODNAME,DB_DATE AS DASH_DATE,decode(a.stats_last_status,'SUCCEEDED',1,'FAILED',2,'STOPPED',2,NULL,0) KI_VALUE from FHM_DB_METRICS_F a,fhm_db_d b where a.dbname=b.dbname and podname='XYZ' and db_date=TRUNC(SYSDATE-1) and dbtype='OLTP')PF, FHM_KEY_INDICATOR_D KI where PF.PODNAME=KI.POD_NAME AND KI.TIER_CODE=3 AND KI.KI_NAME='DB_STATS' AND (PF.PODNAME,TRUNC(PF.DASH_DATE),KI.KI_CODE) NOT IN (SELECT PODNAME,DASH_DATE,KI_CODE FROM FHM_DASHBOARD_F)); COMMIT; ELSE SELECT KI_CODE INTO V_KI_CODE_DB_STATSZ FROM FHM_KEY_INDICATOR_D WHERE POD_NAME=V_PODNAME AND KI_NAME='DB_STATS'; DBMS_OUTPUT.PUT_LINE('V_KI_CODE_DB_STATSZ :'||V_KI_CODE_DB_STATSZ); INSERT INTO FHM_DASHBOARD_F(PODNAME,DASH_DATE,KI_CODE,KI_VALUE,KI_STATUS) VALUES(V_PODNAME,TRUNC(SYSDATE-1),V_KI_CODE_DB_STATSZ,0,0); COMMIT; END IF; EXCEPTION WHEN OTHERS THEN V_ERRORSTRING :='INSERT INTO FHM_DASHBOARD_F_ERROR_LOG(POD_NAME,KI_NAME,ERRORNO,ERRORMESSAGE,DATETIME) VALUES ('''||V_PODNAME||''',''DB_STATS'','''||SQLCODE||''','''||SQLERRM||''',SYSDATE)'; EXECUTE IMMEDIATE V_ERRORSTRING; COMMIT; END; --END LOOP; --CLOSE PODNAME_CUR; END; END FHM_DASHBOARD_PROC;
the constraint primary key is FHM_DASHBOARD_F_DATE_PK and is on 3 columns of the table, DASH_DATE, PODNAME, KI_CODECREATE TABLE "CRMODDEV"."FHM_DASHBOARD_F" ( "PODNAME" VARCHAR2(25 BYTE) NOT NULL ENABLE, "DASH_DATE" DATE, "KI_CODE" NUMBER NOT NULL ENABLE, "KI_VALUE" NUMBER, "KI_STATUS" NUMBER, CONSTRAINT "FHM_DASHBOARD_F_DATE_PK" PRIMARY KEY ("DASH_DATE", "PODNAME", "KI_CODE") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 NOLOGGING COMPUTE STATISTICS STORAGE(INITIAL 4194304 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "CRMODDEV_IDX" ENABLE, CONSTRAINT "FHM_DASHBOARD_F_KI_CODE_FK" FOREIGN KEY ("KI_CODE") REFERENCES "CRMODDEV"."FHM_KEY_INDICATOR_D" ("KI_CODE") ENABLE ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS NOLOGGING STORAGE ( INITIAL 3145728 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT ) TABLESPACE "CRMODDEV_TBL" ENABLE ROW MOVEMENT ;
And it's the query used in the procedure to insert the data into the table
It gives * record * 2 suite(SELECT PODNAME, DASH_DATE AS CU_DATE, KI.KI_CODE, NVL(PF.KI_VALUE,0), CASE WHEN PF.KI_VALUE = ki.warning_threshold then 2 when PF.KI_VALUE=0 then 0 ELSE 1 END AS ALERT_STATUS From (Select Podname,Db_Date As Dash_Date,Decode(A.Stats_Last_Status,'SUCCEEDED',1,'FAILED',2,'STOPPED',2,Null,0) Ki_Value From -- Added Distinct FHM_DB_METRICS_F a,fhm_db_d b where a.dbname=b.dbname and podname in ('XYZ') and db_date = TRUNC(SYSDATE-2) and dbtype='OLTP')PF, Fhm_Key_Indicator_D Ki Where Pf.Podname=Ki.Pod_Name And Ki.Tier_Code=3 And Ki.Ki_Name='DB_STATS' And (Pf.Podname,Trunc(Pf.Dash_Date),Ki.Ki_Code) Not In (Select Podname,Dash_Date,Ki_Code From Fhm_Dashboard_F));
---------------------------------------
XYZ JANUARY 20 12 2521 1 1
XYZ JANUARY 20 12 2521 1 1
It gives the Unique constraint violation error when inserting. Then, I changed in the above insertion code by adding a distinct clause. Once the query gives only a SINGLE record accordingly. However, this record is not be inserted into the table and give the same error.
Now the question is how should I insert that record into the table with success?
If the message is too long, however, I gave you the real structure of the object or procedure and error.
Thank you in advance.When you have 5 columns in the game adding THAT SEPARATE is n ot solution that you can still get the same error once.
Check the target table if the data exists before inserting... If this is not the case, check the structure of the table for a unique constraint created on other columns.
select *from
where DASH_DATE=date '2012-01-20' and PODNAME='XYZ' and KI_CODE=2521; -
But UNIQUE constraint with a condition?
Hello!
Is it possible to define a unique constraint in a table that would apply only to a condition (according to the content of the line only)?
For example:
The constraint would be UNIQUIE (a, b), but only when x is > 10.CREATE TABLE example ( pk number, a number, b number, c VARCHAR2(10), x number );
So if x is not greater than 10, then the same values for a and b nd should be allowed.
Maybe with a view?
Like this?
Kind regardscreate view example_view ( unique (a,b)) AS select a,b from example where x > 10 with read only; -- ??? needed?
DavidYou can create an index based on a function. This takes advantage of the fact that Oracle does not store the NULL entries in an index b-tree
CREATE UNIQUE INDEX idx_example ON example( (CASE WHEN x > 10 THEN a ELSE NULL END), (CASE WHEN x > 10 THEN b ELSE NULL END) );
Justin
-
Unique constraint on data type Date
Hello
I have table below.
CREATE TABLE unique_date_check
(employee_id NUMBER,
amount NUMBER,
pay_date DATE default sysdate,
UNIQUE CONSTRAINT Uq_pay_date (pay_date));
The table should accept only a single amount per employee per pay_date.
I use sysdate as pay_date. Because sysdate has timestamp inside, check the unique constraint on both the date and time stamp.
I need uniqueness only on the part of date (i.e., dd/mm/yyyy), but not on the parking meter.
Should I use triggers for this or there at - it another way to meet my needs?
Thanks in advance.
NadineYou cannot declare a unique constraint based on a function, but you can create a unique index based on a function:
CREATE TABLE unique_date_check (employee_id NUMBER , amount NUMBER , pay_date DATE DEFAULT sysdate); create unique index uq_pay_date on unique_date_check(employee_id,trunc(pay_date));
Note that I added employee_id in the index, to match your requirements.
Triggers should not be at the top of your list of techniques to solve problems, particularly the requirements of validation that involve other lines.
-
FK to a unique constraint in cachegroup
How to create a unique index of cachegroup FK.
Example of
table one
column a1, a2, a3, a4, a5
PK a1
single a2, a3
table b
columns b1, b2, b3, a2, a3
PK b1
How to implement FK on table b as: (a2, a3) FOREIGN KEY refers to a (a2, a3).TimesTen a foreign key can only referecne a PRIMARY KEY, a unique constraint/index. In addition, when you use cache groups, you can set FKs between groups of different cache tables.
Chris
-
I just got an error of constraint unique violation when I tried to insert a row in a table.
But even after that I deleted the PK constraint on the table in question, I still get the same error message when I try again the insert:
SQL error: ORA-00001: unique constraint (GILLNET_TAG. DELETED_AUTH_PK) violated
00001 00000 - "forced single (s.%s) violated."
* Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you can see
This message if there is an entry double at a different level.
* Action: Either remove the single restriction or not to insert the key.
Can someone tell me why? The removal of the constraint simply not registered with SQL Developer?
Thank you.Because you have only removed the constraint, not the corresponding unique index.
But... Why are you doing it? The unique constraint is there for a reason.
-
Unique constraint with conditions?
Hi all!
I have the following scenario... I need column UNIQUE Serial_number forced but only when the STATUS column = 'Y '. Serial_number can be non-unique, where the STATE has another value or is set to null. So, I couldn't en with this:
SERIAL_NUMBER | STATUS
--------------------------------------
123456789 N
123456789 N
123456789 IS
but a new line with 123456789 and 'Y' should not be allowed.
Is this possible with a UNIQUE constraint, or what I have to set in a trigger? I was playing with the UNIQUE constraint for a while, but neither DECODE, WHERE clauses seem to be accepted...
Thanks in advance!Try this:
create unique index a1 on tab(case when STATUS = 'Y' then SERIAL_NUMBER else null end);
Amiel
-
version: 11.1.0.7
I try an insert as below, with the following error
The weird part is that I am unable to find the constraint in the views of data as ALL_CONSTRAINTS or ALL_CONS_COLUMNS or DBA_CONSTRAINTS or DBA_CONS_COLUMNS (under any owner also) dictionary.INSERT INTO <dest_schema>.<dest_table_name> (col1,col2,col3) SELECT col1,col2,col3 FROM <src_schema>.<src_table_name>@dblink_name; ORA-00001: unique constraint (<dest_schema>.<constraint_name>) violated
I checked the same on the source database to find nothing.
I am currently out of thoughts on how to get hurt this problem. Please suggest.There is a unique index with constraint name? Because unique constraints are enforced by creating a unique index if no suitable index does already exist, a single index without a unique constraint can sometimes cause some confusion in the error message.
Justin
Maybe you are looking for
-
How do you get a restored iPhone library playlists?
I had to do a restore on my iPhone 6 s. It downloaded all my photos, but I don't know how to recover all my music. I had 2 put playlists in place, but there is no music in one of the playlists on my phone. Any guidance would be appreciated. Thank
-
Dear friends, My bike E restarts frequently. It is having this problem since last (I think after update of KitKat v4.4.4). Sometimes it restarts more than five times in an hour. More often it is restarted when I get the screen locking pin (previously
-
No material listed in Labview 2010 Linux DAQ
Hello I have a problem with DAQmx Base 3.4.0 under Linux: mxbaseconfig shows my NOR-6009 device to the USB port. I can also create tasks corresponding to the nor-6009 in there. When I try to create a task in Labview 2010, one of the constant I/O are
-
I have an Agilent E5052A that use Windows 2000, and I'm saving the screenshot in the c: local in scope. When I try to copy this image in my PC (via GPIB) the file passes through without problem but I can't open the file, please see the attached files
-
Hey I install SP2 you when the system is on the rise and running so he came a message that is! 0xc01a001d! 28333/89662 (register / Machine / component /DerivedData...) Reception recognizing answers David muller