Adding data to a newly added column
-
05-10-2020 - |
题
I added an extra column to my table and now need to populate it against the data already there.
As a dummy example, I have a table already named customers with CustomerID
, FirstName
, LastName
. I've now added a column called MobilePhone
and want to populate the final column to match the data already there.
CustomerID FirstName LastName MobilePhone
83276432 Joe Bloggs
48273452 Ava Avery
26432346 Jack Jakes
How do I populate the MobilePhone column for Joe, Ava and Jack?
I've tried
INSERT INTO CUSTOMERS
(MobilePhone)
WHERE FirstName = 'Joe', 'Ava', 'Jack'
VALUES (07874384243, 07436453264, 07632531627);
解决方案
Well you can't insert new values into the newly added column in such way.
You can update the newly added column as shown below.
SQL> alter table cust add mobile varchar2(10);
Table altered.
SQL> select * from cust;
ID NAME MOBILE
---------- -------------------- ----------
1 joe
2 jane
SQL> update cust set mobile='9878765346' where id=1;
1 row updated.
SQL> commit;
Commit complete.
SQL> select * from cust;
ID NAME MOBILE
---------- -------------------- ----------
1 joe 9878765346
2 jane
If you want to update all records at once, here are some methods you can use one appropriate for you-ORAFAQ:8 Bulk Update Methods Compared
不隶属于 dba.stackexchange