Rampriya Rajendran
Rampriya Rajendran

Reputation: 191

In SQL, How to add values after add a new column in the existing table?

I created a table and inserted 3 rows. Then I added a new column using alter. How can I add values to the column without using any null values?

Upvotes: 16

Views: 229778

Answers (9)

manasi
manasi

Reputation: 1

simply use update query for inserting values into new column .

syntax: update databasename.tablename set columnname=value where columnname=value;

eg. update company.emp set emp_no=8728483892 where ename="manasi";

Upvotes: -1

Vrushabh
Vrushabh

Reputation: 9

Update  table_name set column_name = value where 'condition'; 

Upvotes: 0

vinay kumar
vinay kumar

Reputation: 9

For Microsoft SQL (T-SQL):

UPDATE TABLE_NAME SET COLUMN_NAME=10;

here 10 means it will set all values by default to 10

Upvotes: -1

sachin yadav
sachin yadav

Reputation: 1

suppose emp is the table and Comm is the new column then fire the below query .

update emp set Comm=5000 

Upvotes: -1

Nikhil Kulkarni
Nikhil Kulkarni

Reputation: 31

Suppose you have a Employee table with these columns Employee_ID, Emp_Name,Emp_Email initially. Later you decide to add Emp_Department column to this table. To enter values to this column, you can use the following query :

Update *Table_Name* set *NewlyAddedColumnName*=Value where *Columname(primary key column)*=value

Example update TblEmployee set Emp_Department='Marketing' where Emp_ID='101'

Upvotes: 3

Venkatesh Panabaka
Venkatesh Panabaka

Reputation: 2154

I think below SQL useful to you

update table_name set newly_added_column_name = value;

Upvotes: 1

david sam
david sam

Reputation: 531

   update table_name
   set new_column=value

Upvotes: 0

Klaudiusz bryjamus
Klaudiusz bryjamus

Reputation: 326

Why don't you use UPDATE statement:

UPDATE tablename SET column=value <WHERE ...>

WHERE is optional. For instance in T-SQL for table:

enter image description here

I can update column NewTestColumn by this statement:

UPDATE [dbo].[Table] SET [NewTestColumn] = 'Some value'

Upvotes: 3

GolezTrol
GolezTrol

Reputation: 116110

Two solutions.

  1. Provide a default value for the column. This value will be used initially for all existing rows. The exact syntax depends on your database, but will will usually look like ..

this:

ALTER TABLE YourTable
ADD YourNewColumn INT NOT NULL
DEFAULT 10 
WITH VALUES;
  1. Add the column with null values first. Then update all rows to enter the values you want.

Like so:

ALTER TABLE YourTable
ADD YourNewColumn INT NULL;

UPDATE YourTable SET YourNewColumn = 10; -- Or some more complex expression

Then, if you need to, alter the column to make it not null:

ALTER TABLE YourTable ALTER COLUMN YourNewColumn NOT NULL;

Upvotes: 34

Related Questions