Insert new columns with ID creation into existing table

Asked

Viewed 230 times

1

I have a table called products, this table already has the fields necessary for its use, but now I need to include 4 more (four) columns in it, columns that will be the description of some ID s I have, the table structure is currently like this:

IdProduto | IdDepto | IdSubDepto | Idmarca | IdModelo | Codigo | DescProd | Tamanho | largura

Now I need to insert the description of each ID s in this table keeping the existing fields, it should look like this:

IdProduto | IdDepto | DescDepto | IdSubDepto | DescSubDepto | Idmarca | Descmarca | IdModelo | DescModelo | Codigo | DescProd | Tamanho | largura

The columns to be inserted are: Descdepto, Descsubdepto, Descmarca and Descmodelo

I couldn’t envision a possibility that could help me solve this problem.

  • Are these new columns to keep a track of the last description? make a view with a Jay picking the description wouldn’t solve?

  • Hello @rray, the new columns should keep the description of the ID, I never made a view, but I will give a search, thanks for the tip.

1 answer

2


You can use a alter table, as mysql command:

ALTER TABLE nomedatabela ADD nomedacoluna VARCHAR(255);

And if you want to insert it after a certain column, you can use as an example this other command:

ALTER TABLE nomedatabela ADD nomedacoluna VARCHAR(255) AFTER colunaanterior;
  • Hello @Striter Alfa, thanks for the tip, but how does it look if the tables to be inserted have values these commands solve?

  • If the columns are nullables, the table will win the new columns and the new values will be null in cadas one of the results. If you do not want the columns to be null, you need to set a default value when creating the columns, and then change with the correct ones. As already mentioned, it is easier to bring the description using Inner Join with other tables rather than duplicate value if it is the same as another table.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.