我在已经存在的表中添加了一个新列
alter table Employee
add Emp_dept varchar(50)
尝试将列插入到表中,但输出不是我预期的
insert into Employee(Emp_dept)
values ('Accounting'),
('Accounting'),
('Technical'),
('Technical'),
('Managing'),
('Managing');
答案 0 :(得分:2)
假设你开始使用表格:
Employee
+----+-------+
| ID | Name |
+----+-------+
| 1 | Joe |
| 2 | Bob |
| 3 | Mary |
+----+-------+
然后应用ALTER TABLE,你可能获得了:
Employee
+----+-------+----------+
| ID | Name | Emp_dept |
+----+-------+----------+
| 1 | Joe | NULL |
| 2 | Bob | NULL |
| 3 | Mary | NULL |
+----+-------+----------+
如果你随后执行了插入操作,并且它完全有效,我希望你会有类似的东西:
Employee
+----+-------+------------+
| ID | Name | Emp_dept |
+----+-------+------------+
| 1 | Joe | NULL |
| 2 | Bob | NULL |
| 3 | Mary | NULL |
| 4 | NULL | Accounting |
| 5 | NULL | Accounting |
| 6 | NULL | Technical |
| 7 | NULL | Technical |
| 8 | NULL | Managing |
| 9 | NULL | Managing |
+----+-------+------------+
我假设ID
是自动增量列,而Name
是NULL
能够。
要解决此问题,您可以尝试:
DELETE FROM Employee WHERE Emp_dept IS NOT NULL
其次是:
ALTER TABLE Employee DROP COLUMN Emp_dept
这应该会让您返回到添加列之前的位置,这似乎是您想要的。