MySQL 有没有办法将一列添加到mysql中的现有视图中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7437857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to add a column into an existing view in mysql?
提问by
For a table I can do like this:
对于一张桌子,我可以这样做:
ALTER TABLE table_name
ADD column_name datatype
but this does not apply to an existing view. I wonder if there is a way to solve this?
但这不适用于现有视图。我想知道有没有办法解决这个问题?
采纳答案by David
Use the Alter View statement to edit a view. Simply use the existing SQL Statement in the current view, and add the column to the end.
使用 Alter View 语句编辑视图。只需使用当前视图中现有的 SQL 语句,并将列添加到末尾。
http://dev.mysql.com/doc/refman/5.0/en/alter-view.html
http://dev.mysql.com/doc/refman/5.0/en/alter-view.html
More detailed explanation than the actual docs can be found here:
可以在此处找到比实际文档更详细的解释:
http://www.roseindia.net/mysql/mysql5/views.shtml
http://www.roseindia.net/mysql/mysql5/views.shtml
Edit - added
编辑 - 添加
A view can only display data from an existing table. You would have to add the column to the table and then modify the view to show it as well.
视图只能显示现有表中的数据。您必须将列添加到表中,然后修改视图以显示它。
Think of it this way: A view is just a way of looking at existing data in tables. Tables are holders of real data.
可以这样想:视图只是查看表中现有数据的一种方式。表是真实数据的持有者。
The only exception to the pattern above that I can think of is that you can have a column on a view that is filled with the results of a calculation such as addition or string contention. For example, consider a table with EmployeeId, FirstName and LastName columns...
我能想到的上述模式的唯一例外是,您可以在视图上有一列填充计算结果,例如加法或字符串争用。例如,考虑一个包含 EmployeeId、FirstName 和 LastName 列的表......
You could have a view that looks like this:
你可以有一个看起来像这样的视图:
Create View FullNames AS
Select EmployeeId, firstname + ' ' + lastname AS FullName from Employees
In that case, I'm sort ofadding a column that doesn't exist in a table - FullName. It's a computed value based on table data. However, it's still based on data stored in teh DB somewhere.
在这种情况下,我是那种将不表格中存在一列-全名。它是基于表数据的计算值。但是,它仍然基于存储在某个数据库中的数据。