L
L
Ler Den2018-04-06 01:13:25
PostgreSQL
Ler Den, 2018-04-06 01:13:25

How to copy fields from one table to another?

PostgreSQL 10. There are two tables connected with each other. The recording table actually has other columns, otherwise it may seem that the tables are the same.

create table video (
  "id" serial primary key,
  "videoId" varchar(25) not null,
  "recordingId" serial REFERENCES recording (id),
  "default" varchar(100),
  "medium" varchar(100),
  "high" varchar(100),  
  unique ("videoId", "recordingId")
);

create table recording (
  "id" serial primary key,
  "videoId" varchar(15) not null,
  "default" varchar(60),
  "medium" varchar(60),
  "high" varchar(60)
);

There are more records in the recording table than in video, i.e. I need to update not all lines in recording, but only those that are related to acc. rows from the video table. I do it like this:
UPDATE recording
    SET recording."videoId" = video."videoId"
    SET recording.default = video.default
    SET recording.medium = video.medium
    SET recording.high = video.high
    FROM video
    WHERE recording.id = video."recordingId"

The update doesn't happen. Maybe the reason is that one table has varchar(60) and the other has varchar(100)?
And another question about how to save on the size of the database. If one field is varchar(100) and the other is varchar(60), but both have 50 characters of data, which field will take up less space? Would it be more economical to allocate varchar(60) or would they both take up as much as they should for 50 characters?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan., 2018-04-06
@givemoneybiatch

Try this query:
UPDATE recording
SET recording.videoId. = video.videoId
SET recording.default = video.default
SET recording.medium = video.medium
SET recording.high = video.high
FROM video
JOIN recording
ON recording.id = video.recordingId
You can make a test query before running update, like this :
Select
recording.videoId, video.videoId
recording.default, video.default
recording.medium, video.medium
recording.high, video.high
FROM video
JOIN recording
ON recording.id = video.recordingId
The query will show which records were selected for the update.
About varchar: varchar stores as many characters as actually transferred, and char as many as specified in the field description.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question