There are two tables:

CREATE TABLE classes ( class_id integer NOT NULL, grade integer, letter text, PRIMARY KEY (class_id), UNIQUE (grade, letter) ) WITH ( OIDS=FALSE ); ALTER TABLE public.classes OWNER TO "user"; CREATE TABLE mainTable ( id SERIAL NOT NULL, class_id integer, CONSTRAINT readers_class_id_fkey FOREIGN KEY(class_id) REFERENCES public.reader_class(class_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) WITH(OIDS= FALSE); ALTER TABLE public.readers OWNER TO "user"; 

mainTable has a mainTable foreign key.

I am running the update request:

 UPDATE readers SET class_id = 1 WHERE id = 1 

The program gets a pair of grade and letter values ​​that are guaranteed to match the value of classes.class_id .

How can I make a request so that when specifying in it the grade and letter , class_id in the request receive the corresponding value?

Obviously, it was possible to avoid such a need by preserving in memory the entire table classes . But in this case, this implies considerable time costs, which I cannot afford.

  • It worked! You can translate in response. - InfernumDeus

1 answer 1

 UPDATE readers SET class_id = (select class_id from classes where grade=X and letter=Y) where id=1 
  • Fine. Only it is better to immediately instead of "and" write "and". - InfernumDeus