简体   繁体   English

SQL 将值添加到现有表中的新列?

[英]SQL adding values to a new column in an existing table?

for example i created an employees table with columns named emp_id,emp_name,emp_job etc. and i wanted to add a manager number column ie.例如,我创建了一个员工表,其中包含名为 emp_id、emp_name、emp_job 等的列,我想添加一个经理编号列,即。 'mgr_no', so i made this column with the alter statement with default null values. 'mgr_no',所以我用默认空值的alter语句制作了这个列。

Now my question is i already have 50 entries in the table and i want to enter 50 entries for mgr_no column which at the moment is empty or null,do i have to use the update command 50 times to fill the column, or is there a way to add values without running the update command 50 times.现在我的问题是表中已经有 50 个条目,我想为 mgr_no 列输入 50 个条目,该列目前为空或空,我是否必须使用更新命令 50 次来填充该列,或者是否有在不运行更新命令 50 次的情况下添加值的方法。

PS: iam using oracle 11g as it is bounded by my teacher. PS:我使用的是 oracle 11g,因为它受我老师的限制。

It depends on whether you want to update all (or many) rows to have the same new mgr_no value, or to 50 different values.这取决于您是要将所有(或许多)行更新为具有相同的mgr_no值,还是更新为 50 个不同的值。

If you need 50 different values, then yes, you have to run 50 UPDATE s.如果您需要 50 个不同的值,那么是的,您必须运行 50 个UPDATE That's pretty normal though and not a problem.不过这很正常,也不是问题。

Otherwise, if you can "group" the new values: The UPDATE statement by default already updates all rows that match the filter.否则,如果您可以“分组”新值:默认情况下, UPDATE语句已更新与过滤器匹配的所有行。 So just writing UPDATE employees SET mgr_no = 123 will set all 50 of your employees to have manager 123. You actually have to use a WHERE to limit this behavior.因此,只需编写UPDATE employees SET mgr_no = 123所有 50 名员工设置为经理 123。您实际上必须使用WHERE限制这种行为。 If you want to set the manager to 123 only for employees with job accounting , you'd use UPDATE employees SET mgr_no = 123 WHERE emp_job = 'accounting' .如果您只想将经理设置为 123 为有工作accounting员工,您可以使用UPDATE employees SET mgr_no = 123 WHERE emp_job = 'accounting'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM