skip to Main Content

I find it difficult to fill a new column that I just added to an already existing table in SQL

I tried the Insert command, to fill in the table afresh, but couldn’t see it true because the table has 1445 rows

2

Answers


  1. You could specify a default value when you add the column:

    ALTER TALE mytable ADD COLUMN new_column VARCHAR(10) DEFAULT 'my_value'
    

    Alternatively, once you added the column, you could use an update statement:

    UPDATE mytable
    SET    new_column = 'my_value'
    
    Login or Signup to reply.
  2. Just like the answer above you can use ALTER TABLE to add a new column

     ALTER TABLE dummy
     ADD temp_column VARCHAR(50);
    

    Then you can use the update and add a where statement to be more specific

     UPDATE dummy
    -> SET temp_column = 'DEFAULT'
    -> where id > 10;
    

    Alternatively you could try using Five, you can import your database in it as an SQL dump, it actually allows you to do way more with new columns, like copy data from an existing field in the table or fill in the values from a query

    here is an image of Five prompting you to fill in values for a new column

    there are other things as well like you can write queries directly in Five, reuse the queries and the tables can be generated with simple point and click.

    Disclaimer: I work for Five

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search