Postgresql constraint : conditional check - postgresql

I have a table with 3 column : id1,id2,status
I want to have a constraint that match this example:
I can have only one (val_X,val_Y,True)
I can have many (val_X,,val_Y,False)
It must refuse a new row with same values (val_x,val_y) and true, and accept it with false.

I got the answer:
create unique index on table_name (id1,id2, status)
where status = true;
I will have only one (val_x,val_y,True).

Related

How to use existing index to create conditional constraint

I have following index:
op.create_index(
op.f("ix_order_company_id_email_lower"),
"order",
["company_id", sa.text("lower(email)")],
unique=False,
postgresql_concurrently=True
)
Now I want to create a check constraint that is going to create sort of a 'unique' constraint for only specific type of orders.
op.execute(
"""ALTER TABLE order
ADD CONSTRAINT check_order_company_id_email_lower_type
CHECK (type = 'AH01')
NOT VALID"""
)
How can I add additional check to only apply to records in ix_order_company_id_email_lower?
EDIT: Basically this type of order can only be submitted once per email in a specific company.
You need a partial unique index:
CREATE INDEX ON order (company_id, lower(email))
WHERE type = 'AH01';

How can I insert a a row with first ID in non empty table?

I have a table with autoincrement id. This table is non empty.
I need to update my table for insert a new row with id 1.
How can I move my entire table one row down ?
My table :
Name : rem_taux
Column : rtx_id | rtx_code | rtx_taux | rtx_date
Thanks.
three steps:
update rem_taux set rtx_id = rtx_id + 1; to move rows down
alter sequence restart with next_val (or just select nextval)
insert you row with not default rtx_id value
like this:
INSERT INTO rem_taux (rtx_id, rtx_code, rtx_taux, rtx_date)
VALUES (1, <some>, <some>, <some>)
this is with assumption you dont have FK or other dependant structure

SQL update statements updates wrong fields

I have the following code in Postgres
select op.url from identity.legal_entity le
join identity.profile op on le.legal_entity_id =op.legal_entity_id
where op.global_id = '8wyvr9wkd7kpg1n0q4klhkc4g'
which returns 1 row.
Then I try to update the url field with the following:
update identity.profile
set url = 'htpp:sam'
where identity.profile.url in (
select op.url from identity.legal_entity le
join identity.profile op on le.legal_entity_id =op.legal_entity_id
where global_id = '8wyvr9wkd7kpg1n0q4klhkc4g'
);
But the above ends up updating more than 1 row, actually all of the rows of the identity table.
I would assume since the first postgres statement returns one row, only one row at most can be updated, but I am getting the wrong effect where all of the rows are being updated. Why ?? Please help a nubie fix the above update statement.
Instead of using profile.url to identify the row you want to update, use the primary key. That is what it is there for.
So if the primary key column is called id, the statement could be modified to:
UPDATE identity.profile
SET ...
WHERE identity.profile.id IN (SELECT op.id FROM ...);
But you can do this much simpler in PostgreSQL with
UPDATE identity.profile op
SET url = 'htpp:sam'
FROM identity.legal_entity le
WHERE le.legal_entity_id = op.legal_entity_id
AND le.global_id = '8wyvr9wkd7kpg1n0q4klhkc4g';

Update columns based on subquery containing "select unnest" clause

I am attempting to update a boolean column in one table based upon the values in a second.
UPDATE channels
SET contains_photos = TRUE
WHERE id IN (SELECT unnest(ancestors)
FROM channel_tree WHERE id = 11329);
The channel_tree.ancestors column contains an array of channel IDs. The above is failing with the following error:
ERROR: cannot TRUNCATE "channel_tree" because it is being used by active queries in this session
The overriding goal is to set the contains_photos column to true for all ancestors of a given channel. Any one know how best to alleviate this error, or even an alternative solution?
No idea why your error says TRUNCATE. It sounds like you have a trigger or rule that is doing a truncate that we can't see.
Here's some alternative ways of doing that same query:
UPDATE channels
SET contains_photos = TRUE
WHERE id = ANY (SELECT ancestors
FROM channel_tree WHERE id = 11329);
Or with a join:
UPDATE channels
SET contains_photos = TRUE
FROM channel_tree
WHERE channels.id = ANY (channel_tree.ancestors)
AND channel_tree.id = 11329;

How to set an object to null

How to set an object to null.
Ex:
my object samp contains three fileds
samp.field1,samp.field2.sampfield3
If i set samp:= null;
im getting errors is there a way to set the object value to null.
An sql database does not know about objects, it deals with rows in table.
To remove a row use DELETE :
e.g. :
DELETE FROM samp WHERE id = 12345;
DELETE FROM samp WHERE field1 = 'Delete Me';
The first example is typical to remove individual rows uing their primary key (id in this case)
The second example will remove a group of rows which have a speciic value for a field.