dimanche 19 juin 2016

ALTER and UPDATE tables in SQL based on two criteria

I have two tables in a SQL database (using Google's Cloud SQL if that matters). The first table pre2016q1 has columns tag and adsh (and several others). I want to add columns, and then populate those columns with values from another table num2016q1, which also has the columns tag and adsh when the values are equal in both tables.

Note: These are large tables with > 2MM rows each. Edit: I'm asking before I run because it took a long time to get the DB in its current state, and I don't want to screw the whole thing up.

Question 1: Will the code below perform the operation described above?

Question 2: If there is no equivalent to tag and adsh will the records be left blank?

Question 3: Is having a column named value going to cause any issues or should I rename that beforehand?

Question 4: Does the size of the table matter at all in the time the operation will take? (I only ask because I need to do this operation 16 times).

ALTER TABLE pre2016q1
ADD
    version VARCHAR(20)
    ddate INTEGER
    qtrs INTEGER
    uom VARCHAR(20)
    value NUMERIC(28,4)
    footnote VARCHAR(512)

UPDATE 
    pre2016q1
SET 
    version.pre2016q1 = version.num2016q1
    ddate.pre2016q1 = ddate.num2016q1
    qtrs.pre2016q1 = qtrs.num2016q1
    uom.pre2016q1 = uom.num2016q1
    value.pre2016q1 = value.num2016q1
    footnote.pre2016q1 = footnote.num2016q1
WHERE
    adsh.pre2016q1 = adsh.num2016q1 AND tag.pre2016q1 = tag.num2016q1

Aucun commentaire:

Enregistrer un commentaire