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

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?

Two solutions.
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;
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;

Why don't you use UPDATE statement:
UPDATE tablename SET column=value <WHERE ...>
WHERE is optional. For instance in T-SQL for table:
I can update column NewTestColumn by this statement:
UPDATE [dbo].[Table] SET [NewTestColumn] = 'Some value'

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'

I think below SQL useful to you
update table_name set newly_added_column_name = value;

update table_name
set new_column=value

Update table_name set column_name = value where 'condition';

suppose emp is the table and Comm is the new column then fire the below query .
update emp set Comm=5000

For Microsoft SQL (T-SQL):
UPDATE TABLE_NAME SET COLUMN_NAME=10;
here 10 means it will set all values by default to 10

Related

update and concatenate columns in PostgreSQL

I want to update and concatenate 3 columns in a new a attribute I've already created in my table. However when I execute my code, it puts attributes that before had values in null.
My code example:
update saber2012_1
set estu_fecha_nacimiento = Concat(estu_nacimiento_dia,'/',estu_nacimiento_mes,'/',estu_nacimiento_anno);
In this picture all values that is in null before these had values.
when I make this update
ALTER TABLE saber2012_uno ADD COLUMN punt_c_naturales int;
update saber2012_uno set punt_c_naturales = round((PUNT_BIOLOGIA::numeric + PUNT_FISICA::numeric + PUNT_QUIMICA::numeric) / 3,0);
alter table saber2012_uno DROP COLUMN PUNT_BIOLOGIA;
alter table saber2012_uno DROP COLUMN PUNT_FISICA;
alter table saber2012_uno DROP COLUMN PUNT_QUIMICA;
all is well. but I do not know because previous update is incorrect.
Perhaps you want concat_ws():
update saber2012_1
set estu_fecha_nacimiento = concat_ws('/', estu_nacimiento_dia, estu_nacimiento_mes, estu_nacimiento_anno);
This will ignore any of the arguments that are NULL.
However, your update will not change any other columns in the table. Perhaps the results are just coming back in a different order, when you query after the update.

Data Type that automatically stores the DateTime of the last transaction done, SQL Server 2008

I need to add a new column to an existing table, so that whenever a new row is added, or an existing row is edited, this column will be filled with the exact date and time of the transaction. I tried using TimeStamp but apparently TimeStamp it has nothing to do with Date time
It's just a binary representation of a consecutive number - it's only good for making sure a row hasn't change since it's been read. (Quoted from How to convert SQL Server's timestamp column to datetime format
Any help is highly appreciated
It sounds like you need to create a trigger to populate/update this new column. See the following: How to: Create trigger for auto update modified date with SQL Server 2008
You have to create a trigger on INSERT and UPDATE for the table and in the trigger you can use
UPDATE myTable
SET myColumn = GETDATE()
Your trigger should look somethign like
CREATE TRIGGER trigger_updateTimestamp
ON myTable
AFTER INSERT, UPDATE
AS
UPDATE myTable
SET myColumn = GETDATE()
WHERE ID = 'xyz'
try
ALTER TABLE myTable ADD CONSTRAINT [DF_myTable_myColumn] DEFAULT (getdate()) FOR [myColumn]
GO
... and a good practice is to have an extra column for edited date, and update using a trigger
CREATE TRIGGER trigger_updateMyTable
ON myTable
AFTER UPDATE
AS
IF EXISTS (SELECT * FROM INSERTED)
BEGIN
IF ΝΟΤ EXISTS (SELECT * FROM DELETED)
BEGIN
UPDATE myTable
SET myEditColumn = GETDATE()
WHERE ID IN (
SELECT
ID
FROM INSERTED
)
END
END

SQL - how can I copy a column and its data to a new column in the same table?

I know i want to use an update statement but im having trouble with the structure of the query
The first of the following 2 SQL statements will create the new column in the table, and the second, update statement will populate the new column from the old column.
alter table Table1 add newColumn char(32);
update table1 set newColumn=oldColumn;
commit;
You can't create a new column using an update, you have to do that first. Then it's just as simple as:
update TheTable set NewColumn = OldColumn
UPDATE table
SET column2 = column1
update table_name set column_to_be_changed = existing column

How to insert one column into other column within the same table in SQL Server

I need to insert one column's data into another column within the same table.
Can anybody tell me how to write this?
Thanks
UPDATE table
SET col_2 = col_1
If you want to copy data from one column to another on the same table:
UPDATE table_name SET
destination_column_name=orig_column_name
WHERE condition_if_necessary
IF you want to add a new column and copy the original data to that column:
ALTER TABLE table_name
ADD new_column_name column_type NULL
UPDATE table_name SET
destination_column_name=orig_column_name
WHERE condition_if_necessary
If you want the column to be non-nullable, then you can set it to a default value before doing the update.
begin transaction
alter table Song add SortArtist nvarchar(128) not null default N''
go
update Song set SortArtist = Artist
commit transaction
alter table [dbo].[GetPermission]
add username1 varchar(100) ----------------ading new column username1
update GetPermission set username1=username

Fill entire SQL table column

I just added a new column in my DB which I need to propagate with a specific text value (Confirmed). Is there a simple way to apply to all my data entries so I don't have to go through all my rows and type the value in?
Thanks
you run the statement:
UPDATE whateveryourtableis SET whateveryourcolumnis = 'whatever';
Yould could make the desired value the new column's DEFAULT e.g.
ALTER TABLE MyTable ADD
my_new_column VARCHAR(20) DEFAULT 'Confirmed' NOT NULL;
Yes there is:
UPDATE [table]
SET [column] = 'Confirmed'