How do i Update data in table from other table with condition - sql

i have 2 tables and both has one column as a primary and foreign key. i have to update one table column which is empty but the primary table has the values which needs to be updated here. how do i update that particular column by referencing those columns of primary table in foreign-key table?
Table 1 - Primary -Column list
SI.No (PrimaryKey)
UpdateTime
StudentDetail
table 2 - Foreign - Column list
SI.No(ForeignKey)
UpdateTime
BatchCode
Table 2 of updateTime is empty for some students due to some reason. I need to get the update time from table 1 of those empty students and update it to table 2. how do i do this?? using postgress i am.

In Postgres, you can use a FROM clause to reference another table:
update table2 t2
set updatetime = t1.updatetime
from table1 t1
where t1.sl_no = t2.sl_no and t2.updatetime is null;

Related

Prevent row insertion if foreign_key is not null in the referenced row

Simple table with primary key as the first column and foreign key referencing the same table as the second column.
1 | null
2 | 1
I want to prevent insertion of a row whose fk would reference a row whose fk is not null. E.g. insertion of the row below should be prevented:
3 | 2
How can it be achieved? I'v tried the following
ALTER TABLE t
ADD CONSTRAINT t_check
CHECK (
fk not in (select t.pk from t1 t where t.pk = fk and t.fk is not null)
);
but got
ERROR: cannot use subquery in check constraint
as expected.

Update the Foreign key column by checking updates in other table Primary Key Column

I have to update the E_ID and other columns of table User which is FK and being referenced to M_ID column of Master table which is PK based on the LastModifiedOn column in master table. I'm trying to do using below update query but it's picking the first value of M_ID from master table i.e 1 and update all the values of user table E_ID to 1. Thanks
update User
set E_Id = m.M_ID
from master m
where cast(m.LastModifiedOn as date) = cast(getdate() as date)
Master Table:
User Table:

postgres sql returns no row

I have 3 followings tables:
CREATE TABLE public.a
(
id bigint NOT NULL DEFAULT nextval('a_id_seq'::regclass),
CONSTRAINT a_pkey PRIMARY KEY (id)
)
CREATE TABLE public.b
(
id bigint NOT NULL DEFAULT nextval('b_id_seq'::regclass),
fkid bigint,
CONSTRAINT b_pkey PRIMARY KEY (id),
CONSTRAINT b_fkid_fkey FOREIGN KEY (fkid)
REFERENCES public.a (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
CREATE TABLE public.history
(
id bigint NOT NULL DEFAULT nextval('history_id_seq'::regclass),
CONSTRAINT history_pkey PRIMARY KEY (id)
)
As you can see, the table A has 1 to many relationship with table B. The history table keeps track of logical time inside the app. The following query:
WITH main_q AS (
SELECT
a.id
FROM a
LEFT JOIN b ON a.id = b.fkid
)
SELECT main_q.id,
max(history.id) as as_of
FROM main_q,history
GROUP BY
main_q.id
Returns no rows, even if tables A contains rows, because History table is empty. However, as soon as I add at least 1 row in history table, the query works.
Could someone please explain how I should modify my query so that it returns rows even if history is empty (as long as table A contains rows) ? I would expect to receive a NULL value in as_of column or 0. I tried COALESCE(max(history.id,0)) but this doesn't work either, well, because no rows in history
EDIT Example:
Table A content:
id
1
2
Table B content:
id, fkid
1, 2
2, 2
Table History content:
<empty>
The issue is the query below
SELECT main_q.id,
max(history.id) as as_of
FROM main_q,history
GROUP BY
main_q.id
Why not try a left join, you may have nothing to join, you can try something below
SELECT main_q.id,
max(history.id) as as_of
FROM main_q left join history on 1=1
GROUP BY
main_q.id
If I am not wrong, you are performing a cartesian product between main_q and history, because you haven't a predicate to join these 2 tables.
Likely, when you are performing this cartesian product, if the second table (history) is empty, the cartesian product returns nothing. As soon as you have 1 record in history, you will get data from Main_q. However, if you have more than 1 record in history, you will start to get duplicate data because of the cartesian product.

Query for column key when foreign key is absent in another table postgresql

We have a PostgreSQL database which has a table with a foreign key reference to the primary key of another table like below
Table A
a_key
b_key
when_
Table B
b_key
There was a bug in our code where we removed rows from Table B but did not remove the entries in Table A that were associated with those rows. I am trying to right a query to find all of the primary keys from Table A which have a "b_key" value that does not exist in Table B, I also added a time restriction to the query. My query is below but it is not returning any results. Can anyone see an issue with the query? Is it not done correctly?
select a_key
from A left join B b on a.b_key = b.b_key
where b.b_key is null and A.when_ < '2017-03-13 00:00:00.0'::timestamp
try this first
SELECT
a_key
FROM A
where not exists (
select b_key from B where B.b_key = A.b_key)

INSERTING a single field into a table, referenced from another table

I need to insert a field in a that references an id field in another table.
The id field it is to going is next to the field 'test' (column - codedescription, table typecategory) and coming from an id field next to the word 'assessment' (column categorydescription, table typecategory)
INSERT INTO codetype
(typecategoryid)
Where codedescription='test'
SELECT id FROM typecategory WHERE categorydescription='Assessment Types'
There are plenty of examples of inserting entire columns but nobody has written how to insert a single field from another table.
table - codetype
id bigserial primary key
codedescription varchar
typecategoryid bigint foreign key to typecatogory on the ID column
Table - typecategory
ID big serial primary key
categorydescription varchar
If the column already exists and there are are already records in the rest of the columns in the table, then you need an UPDATE statement, not an INSERT.
Looks like this post might help you: Update a column of a table with a column of another table in PostgreSQL
maybe
UPDATE codetype c
SET c.typecategoryid = t.id
FROM typecategory t
WHERE c.codedescription = 'test' and t.categorydescription='Assessment Types'