Is there an order to how unique constraint violations occur in oracle sql? - sql

Given that we have a table like this,
table name: my_table;
columns: id, a, b, c;
primary key: id;
unique constraints: a, (b, c);
I want to know whether there is an ordering to how the unique constraints would fail.
With this setting, I do the following insert
insert into my_table values (1,2,3,4);
And then I do the same insert once more. Can I be sure that this second insert will fail because of the primary key violation, and not because of the unique key constraints on the other columns?
And then, if I try to do this insert into my_table values (2,2,3,4);, is there a way for me to make sure which unique constraint would fail first (the one on column a or the one on columns (b,c))?

Related

UNIQUE Constraint not working? (Postgresql)

I altered my postgresql database table to have a unique constraint like this:
ALTER TABLE my_table
ADD CONSTRAINT unique_values UNIQUE (value_one, value_two, value_three);
I want to not be able to post duplicate values for value_one, value_two, or value_three. So there should only ever be ONE unique value in these columns in the entire database.
But I'm still able to INSERT duplicate values in the value_one, value_two, and value_three columns.
What am I doing wrong?
Edit:
I have deleted all the duplicate data in the database, but get an error whenever I try to add a unique constraint to individual columns in the table.
Another thing, if I say:
ALTER TABLE my_table
ADD CONSTRAINT unique_values UNIQUE (value_one, value_two, value_three);
I do not get an error. But if I say:
ALTER TABLE my_table
ADD CONSTRAINT unique_values UNIQUE (value_one);
Like with just one value, I get this error:
could not create unique index "unique_values"
If you define a unique constraint over three columns, that means that you cannot insert the same three values a second time. But each of these columns can contain duplicate values. For example, these three rows would satisfy the uniqueness constraint:
(1, 2, 3)
(1, 2, 4)
(1, 5, 3)
but you would get an error if you inserted one of these triples a second time.
If you want each column to be unique, you have to define three constraints, one for each column.

ADD combination constraint into a dabase table

I want to make a combination of columns in a table unique which they can not have any duplicates value.
For example, I have a table of Person which contain a name, age, address and phone_number.
I want that the combination of (name, age, phone_number) must be unique.
The table person contain this data:
"Name1"|22|"adresse1"|000000
So the combination (''Name1'' and 22 and 000000) it has to be unique but if only one of them changed for example ("name2",22,000000) it will be permitted to insert data to the table.
How can I manage that with SQL please?
If you want a column or combination of columns to be unique, you can implement a unique constraint or index (they are functionally equivalent for this purpose):
create unique index unq_t_col1_col2_col3 on t(col1, col2, col3);
And for a constraint:
alter table t add constraint unq_t_col1_col2_col3
unique (col1, col2, col3);
Any attempt to insert values into the columns that already exists will result in an error, as will any attempt to update the columns with values in a different row.

Can I make a unique constraint in Oracle on a combination of 2 columns from 2 different tables?

Column A is the PK of table A. Column B is a column in table B. Column C is a FK is table B that references column A in table A.
Can I define a constraint that says that that a column B AND column C have to be unique? As in, I don't want any repeats of the same combination of values from A and B.
Here's one possibility i'm thinking about:
Create a unique ID column.
unique_id varchar2 GENERATED ALWAYS AS (B || '-' || FK that references column A) VIRTUAL
set it as unique
CONSTRAINT unique_id UNIQUE
If i go with this solution i'm confused about one thing. The docs say: "Virtual columns are not supported for index-organized, external, object, cluster, or temporary tables." Obviously, because they don't get stored like other columns. Would this be a problem if I wanted to make my tables clustered?
Yes, you can create a UNIQUE constraint on two columns. For example:
create table table_a (
a number(6) primary key not null
);
create table table_b (
b number(6) not null,
c number(6) not null,
constraint uq1 unique (b, c),
constraint fk1 foreign key (c) references table_a (a)
);
Then, if you try to insert it will fail for duplicates. For example:
insert into table_a (a) values (1);
insert into table_a (a) values (2);
insert into table_b (b, c) values (10, 1);
insert into table_b (b, c) values (10, 1); -- fails!
insert into table_b (b, c) values (10, 2);
See running example at db<>fiddle.

Create constraint for control insert in table

There are two tables - orders and a list of services. In the first there is a bool field that the order is approved, if it is true then you can’t insert / delete values in the second table. With the UPDATE of the first table and the DELETE of the second, it is clear.
INSERT make as
INSERT INTO b (a_id, b_value)
SELECT *
FROM (VALUES (1, 'AA1-BB1'),(1, 'AA1-BB2'),(1, 'AA1-BB3')) va
WHERE (SELECT NOT confirm FROM a WHERE a_id = 2);
https://dbfiddle.uk/?rdbms=postgres_12&fiddle=7b0086967c1c38b0c80ca5624ebe92e9
How to forbid to insert without triggers and stored procedures? Is it possible to compose somehow complex constraint or a foreign key for checking conditions at the DBMS level?
The most recent version of Postgres supports generated columns. So, you can do:
alter table b add confirm boolean generated always as (false) stored;
Then create a unique key in a:
alter table a add constraint unq_a_confirm_id unique (confirm, id);
And finally the foreign key relationship:
alter table b add constraint fk_b_a_id_confirm
foreign key (confirm, a_id) references a(confirm, id);
Now, only confirmed = false ids can be used. Note that this will prevent updates to a that would invalidate the foreign key constraint.

How to prevent duplicate rows in insert query using Oracle?

I have table A in Oracle that has a primary key (id). I need to insert data into this table.
How do I prevent duplicate rows?
If the id column is marked as the PK you will not be able to insert a duplicate key, the server will throw an exception.
If you will insert duplicate data with different key - that's a logic that you need to deal with (like putting a unique constraint on the actual data) or do a check before you insert.
If you mean that you have rows that are identical (apart from the primary key) and you want to know how to delete them then do:
select col2, col3, ...coln, min(id) from A
group by col2, col3, ...coln
(I.e. select on all columns except the id.)
To get the unique instances do
delete from A where id not in
(select min(id) from A
group by col2, col3, ...coln) as x
to delete all rows except the unique instances (i.e. the duplicates).
First you would create a unique id on this table that has a sequence. Then when you insert into that table, in the insert statement you can use:
tableName_sn.nextval
in a statement like:
inserti into tableName (id) values (tableName_sn.nextval)
to get the next unique key in the sequence. Then if you use this key you will guarantee that it is unique. However, the caveat to that is if someone just entered a key not using the nextval function you will get primary key violations.