- IBM DB2 9.7 Advanced Application Developer Cookbook
- Sanjay Kumar Mohankumar Saraswatipura
- 301字
- 2021-08-20 15:42:18
Changing column names online using the ALTER TABLE operation
To rename a column in earlier versions of DB2, we used to recreate the table with a new column name and then insert the data from the earlier table on to a newly created table. The catch here is that while renaming the table, the source table should not have any references such as views, indexes, MQTs, functions, triggers, and constraints. This makes an application developer depend on a database administrator while changing the database object, based on the business requirement. In DB2 9.7, renaming a column is made extremely easy with just a single command inside the application code.
Getting ready
You need to have the ALTER
privilege on the table that needs to be altered.
How to do it...
You can rename an existing column in the table to a new name without losing the data, privileges, and LBAC policies.
The DB2 command syntax to rename the column is as follows:
ALTER TABLE <SCHEMAS>.<TABLENAME> RENAME COLUMN <COLUMN> TO <NEW COLUMN >
For example:
ALTER TABLE DBUSER.DEPARTMENT RENAME COLUMN LOC TO LOCATION
After renaming the column, the application can start accessing the table without a table REORG
requirement.

How it works…
When an ALTER TABLE RENAME COLUMN
command runs on the system, DB2 will rename the column in the table and invalidate the dependent objects (if any) such as views, functions, procedures, materialized query tables (MQT), and so on. Invalidated objects would get validated when the dependent objects are being accessed within the application or outside the application by a user. This automatic revalidation of invalid database objects depends on the value of the database configuration parameter, auto_reval
.
See also
Refer to the Using the CREATE WITH ERROR support recipe for more details on automatic revalidation of invalid database objects, discussed in this chapter.
- DBA攻堅指南:左手Oracle,右手MySQL
- Java應(yīng)用與實戰(zhàn)
- 在最好的年紀學(xué)Python:小學(xué)生趣味編程
- OpenCV實例精解
- 構(gòu)建移動網(wǎng)站與APP:HTML 5移動開發(fā)入門與實戰(zhàn)(跨平臺移動開發(fā)叢書)
- 青少年美育趣味課堂:XMind思維導(dǎo)圖制作
- Expert Android Programming
- Python面向?qū)ο缶幊蹋簶?gòu)建游戲和GUI
- Flutter跨平臺開發(fā)入門與實戰(zhàn)
- NetBeans IDE 8 Cookbook
- Python網(wǎng)絡(luò)爬蟲技術(shù)與應(yīng)用
- Mastering Adobe Captivate 7
- R語言數(shù)據(jù)挖掘:實用項目解析
- H5頁面設(shè)計與制作(全彩慕課版·第2版)
- 情境微課開發(fā)(第2版)