I need to ensure that the values in a column from a table are unique as part of a larger process.
I'm aware of the UNIQUE constraint, but I'm wondering if there is a better way to do the check.
I'm running the queries using psycopg2 so adding that tag on the off chance there's something in there that can help with this.
If the column is unique I can add a constraint. If the column is not unique adding the constraint will return an error.
If there is already a constraint of the same name a useful error is returned. in this case would prefer to just check for the existing constraint.
If the column is the primary key, the unique constraint can be added without error but in this case it would be preferable to just recognize that the column must be unique based on the primary key.
Code examples of this below.
DROP TABLE IF EXISTS unique_test;
CREATE TABLE unique_test (
pkey INT PRIMARY KEY,
unique_yes CHAR(1),
unique_no CHAR(1)
);
INSERT INTO unique_test (pkey, unique_yes, unique_no)
VALUES(1, 'a', 'a'),
(2, 'b', 'a');
CREATE UNIQUE INDEX CONCURRENTLY u_test_1 ON unique_test (unique_yes);
ALTER TABLE unique_test
ADD CONSTRAINT unique_target_1
UNIQUE USING INDEX u_test_1;
-- the above runs no problem
-- check what happens when column is not unique
CREATE UNIQUE INDEX CONCURRENTLY u_test_2 ON unique_test (unique_no);
ALTER TABLE unique_test
ADD CONSTRAINT unique_target_2
UNIQUE USING INDEX u_test_2;
-- returns:
-- SQL Error [23505]: ERROR: could not create unique index "u_test_2"
-- Detail: Key (unique_no)=(a) is duplicated.
CREATE UNIQUE INDEX CONCURRENTLY u_test_1 ON unique_test (unique_yes);
ALTER TABLE unique_test
ADD CONSTRAINT unique_target_1
UNIQUE USING INDEX u_test_1;
-- returns
-- SQL Error [42P07]: ERROR: relation "unique_target_1" already exists
-- test what happens if adding constrint to primary key column
CREATE UNIQUE INDEX CONCURRENTLY u_test_pkey ON unique_test (pkey);
ALTER TABLE unique_test
ADD CONSTRAINT unique_target_pkey
UNIQUE USING INDEX u_test_pkey;
-- this runs no problem but is inefficient.
If all you want to do is verify that values are unique, then use a query:
select unique_no, count(*)
from unique_test
group by unique_no
having count(*) > 1;
If it needs to be boolean output:
select not exists (
select unique_no, count(*)
from unique_test
group by unique_no
having count(*) > 1
);
If you just want a flag, you can use:
select count(*) <> count(distinct uniq_no) as duplicate_flag
from unique_test;
DELETE FROM
zoo x
USING zoo y
WHERE
x.animal_id < y.animal_id
AND x.animal = y.animal;
I think this is simpler, https://kb.objectrocket.com/postgresql/delete-duplicate-rows-in-postgresql-762 for reference
Related
In oracle, I want to add this unique constraint to a table with some records that contracted this unique constraint. for adding this unique constraint without validating previous data on the table I added ENABLE NOVALIDATE at the end of the statement but it has an error:
ORA-02158: invalid CREATE INDEX option
Is there any way to add this unique index without validating previous records in Table?
create unique index UK_SAME_THREAD ON T_THREADPARTICIPANT
(case when C_OPPOSITE_USER_ID is not null then C_OPPOSITE_USER_ID else null end,
case when C_OPPOSITE_USER_ID is not null then F_PARTICIPANT else null end,
case when C_OPPOSITE_USER_ID is not null then C_CONTACT_TYPE else null end)
ENABLE NOVALIDATE;
Workaround is to a) create index (non-unique), b) create unique constraint that doesn't validate existing values.
Table that contains duplicate ID values:
SQL> select * From test;
ID
----------
1
1
2
This is what you tried to do:
SQL> create unique index i1 on test (id) enable novalidate;
create unique index i1 on test (id) enable novalidate
*
ERROR at line 1:
ORA-02158: invalid CREATE INDEX option
Let's just alter table and add unique constraint (that won't work either):
SQL> alter table test add constraint uk_id unique (id) enable novalidate;
alter table test add constraint uk_id unique (id) enable novalidate
*
ERROR at line 1:
ORA-02299: cannot validate (SCOTT.UK_ID) - duplicate keys found
So: create index first ...
SQL> create index i1_test_id on test (id);
Index created.
... and alter the table next:
SQL> alter table test add constraint uk_id unique (id) enable novalidate;
Table altered.
SQL>
Does it work?
SQL> insert into test (id) values (2);
insert into test (id) values (2)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UK_ID) violated
SQL> insert into test (id) values (3);
1 row created.
SQL>
I guess it does.
How can I set a UNIQUE constraint in Azure SQL database that allows NULL values in the column?
The below workaround from SQL Server doesn't seem to work in Azure SQL:
CREATE UNIQUE NONCLUSTERED INDEX [IX_Email] ON [dbo].[Users]([Email] ASC)
WHERE ([Email] IS NOT NULL);
The error, which is in the comments is telling you the problem:
Violation of UNIQUE KEY Constraint "UQ_Users_5120...". Cannot insert duplicate key in object 'dbo.Users'. The duplicate key value is ().
Firstly, notice that the error states "UNIQUE KEY Constraint", emphasis mine. Not Index, Constraint. Also note that name of said constraint: UQ_Users_5120... That isn't the name of the object you created (IX_Email).
You can replicate this problem with the following:
CREATE TABLE dbo.SomeTable (SomeColumn varchar(10) NULL);
GO
--Create a UNIQUE Constraint
ALTER TABLE dbo.SomeTable ADD CONSTRAINT UQ_SomeColumn UNIQUE (SomeColumn);
GO
--Create a filtered unique index
CREATE UNIQUE NONCLUSTERED INDEX UX_SomeColumn_NotNull ON dbo.SomeTable(SomeColumn)
WHERE SomeColumn IS NOT NULL;
GO
--Initial Insert
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES('asdfasd'),
('asdasd'),
(NULL);
GO
--Insert another dupe, non NULL value. Fails
--This violates constraint 'UQ_SomeColumn'
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES('asdfasd');
GO
--Insert another dupe, NULL value. Fails
--This violates constraint 'UQ_SomeColumn'
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES(NULL);
GO
You can fix this my dropping your UNIQUE CONSTRAINT. We don't have the full name, but for the above example it would be the following:
ALTER TABLE dbo.SomeTable DROP CONSTRAINT UQ_SomeColumn;
GO
And then we can test again:
--Insert another dupe, non NULL value. Fails
--Cannot insert duplicate key row in object 'dbo.SomeTable' with unique index 'UX_SomeColumn_NotNull'. The duplicate key value is (asdfasd).
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES('asdfasd');
--Insert another dupe, NULL value. Success
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES(NULL);
GO
Notice as well, that the error for the duplicate value is completely different now. It mentions a unique index, not a unique key constraint, and has the name of the unique filtered index created, not something else.
I have a SQL table with a column called [applied], only one row from all rows can be applied ( have the value of 1) all other rows should have the value 0
Is there a check constraint that i can write to force such a case?
If you use null instead of 0, it will be much easier.
Have a CHECK constraint to make sure the (non-null) value = 1. Also have a UNIQUE constraint to only allow a single value 1.
create table testtable (
id int primary key,
applied int,
constraint applied_unique unique (applied),
constraint applied_eq_1 check (applied = 1)
);
Core ANSI SQL, i.e. expected to work with any database.
Most databases support filtered indexes:
create unique index unq_t_applied on t(applied) where applied = 1;
To know exactly how to write trigger that will help you an info of a database you use is needed.
You wil need a trigger where this will be your test control:
SELECT COUNT(APPLIED)
FROM TEST
WHERE APPLIED = 1
If it is > 0 then do not allow insert else allow.
While this can be done with triggers and constraints, they probably require an index. Instead, consider a join table.
create table things_applied (
id smallint primary key default 1,
thing_id bigint references things(id) not null,
check(id = 1)
);
Because the primary key is unique, there can only ever be one row.
The first is activated with an insert.
insert into things_applied (thing_id) values (1);
Change it by updating the row.
update things_applied set thing_id = 2;
To deactivate completely, delete the row.
delete things_applied;
To find the active row, join with the table.
select t.*
from things t
join things_applied ta on ta.thing_id = t.id
To check if it's active at all, count the rows.
select count(id) as active
from things_applied
Try it.
I have a table that I create independently, the primary key is set with the serial type and a sequence applied to the table, but when I try to insert a value a NULL CONSTRAINT error is thrown and the return looks like null was passed, am I missing something in the INSERT statement?
SQL for table generation:
DROP TABLE IF EXISTS public."Team" CASCADE;
CREATE TABLE public."Team" (
"IdTeam" serial PRIMARY KEY,
name text NOT null,
CONSTRAINT "pKeyTeamUnique" UNIQUE ("IdTeam")
);
ALTER TABLE public."Team" OWNER TO postgres;
DROP SEQUENCE IF EXISTS public."Team_IdTeam_seq" CASCADE;
CREATE SEQUENCE public."Team_IdTeam_seq"
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public."Team_IdTeam_seq" OWNER TO postgres;
ALTER SEQUENCE public."Team_IdTeam_seq" OWNED BY public."Team"."IdTeam";
SQL for insert :
INSERT INTO public."Team" (name) values ('Manchester Untited');
The returning error:
ERROR: null value in column "IdTeam" violates not-null constraint
DETAIL: Failing row contains (null, Manchester Untited).
SQL state: 23502
I am baffled. Why are you trying to define your own sequence when the column is already defined as serial?
Second, a primary key constraint is already unique. There is no need for a separate unique constraint.
Third, quoting identifiers just makes the code harder to write and to read.
You can just do:
DROP TABLE IF EXISTS public.Team CASCADE;
CREATE TABLE public.Team (
IdTeam serial PRIMARY KEY,
name text NOT null
);
INSERT INTO public.Team (name)
VALUES ('Manchester Untited');
Dropping the sequence causes the default definition for the IdTeam column to be dropped. After recreating the sequence you will have to recreate the default definition.
I'd like to add a NOT NULL column to a Redshift table that has records, an IDENTITY field, and that other tables have foreign keys to.
In PostgreSQL, you can add the column as NULL, fill it in, then ALTER it to be NOT NULL.
In Redshift, the best I've found so far is:
ALTER TABLE my_table ADD COLUMN new_column INTEGER;
-- Fill that column
CREATE TABLE my_table2 (
id INTEGER IDENTITY NOT NULL SORTKEY,
(... all the fields ... )
new_column INTEGER NOT NULL,
PRIMARY KEY(id)
) DISTSTYLE all;
UNLOAD ('select * from my_table')
to 's3://blah' credentials '<aws-auth-args>' ;
COPY my_table2
from 's3://blah' credentials '<aws-auth-args>'
EXPLICIT_IDS;
DROP table my_table;
ALTER TABLE my_table2 RENAME TO my_table;
-- For each table that had a foreign key to my_table:
ALTER TABLE another_table ADD FOREIGN KEY(my_table_id) REFERENCES my_table(id)
Is this the best way of achieving this?
You can achieve this w/o having to load to S3.
modify the existing table to create the desired column w/ a default value
update that column in some way (in my case it was copying from another column)
create a new table with the column w/o a default value
insert into the new table (you must list out the columns rather than using (*) since the order may be the same (say if you want the new column in position 2)
drop the old table
rename the table
alter table to give correct owner (if appropriate)
ex:
-- first add the column w/ a default value
alter table my_table_xyz
add visit_id bigint NOT NULL default 0; -- not null but default value
-- now populate the new column with whatever is appropriate (the key in my case)
update my_table_xyz
set visit_id = key;
-- now create the new table with the proper constraints
create table my_table_xzy_new
(
key bigint not null,
visit_id bigint NOT NULL, -- here it is not null and no default value
adt_id bigint not null
);
-- select all from old into new
insert into my_table_xyz_new
select key, visit_id, adt_id
from my_table_xyz;
-- remove the orig table
DROP table my_table_xzy_events;
-- rename the newly created table to the desired table
alter table my_table_xyz_new rename to my_table_xyz;
-- adjust any views, foreign keys or permissions as required