Alibaba Cloud Big Data Actual Record 4: Adding columns in the production environment

How to add columns in production environment

Add column by code

When adding columns, pay attention to the engine type . You can check the engine type according to the following operations.
If you have permission to view configuration-related content, you can also view it in the configuration. (Currently I don’t have this permission, and I don’t know what’s in it~~)
image.png

Why pay attention to the engine type? Because different engines mean different configurations, and the corresponding syntax is also different. For example, the operation of adding columns in the mysql engine and the maxcomputer engine is different. The following two documents are available for reference. Cloud native data warehouse AnalyticDB MySQL
version- Add columns
Cloud-native big data computing service MaxCompute-Add columns

The specific syntax and examples are as follows:

-- MaxComputer新增列
alter table <table_name> add columns (<col_name1> <type1> comment ['<col_comment>'][, <col_name2> <type2> comment '<col_comment>'...]);
-- 例子:新增1列
alter table customer add columns (customer_name STRING comment '用户名');
-- 例子:新增2列
alter table customer add columns (customer_name STRING , age BIGINT);

-- ADB MySQL新增列
ALTER TABLE db_name.table_name ADD [COLUMN] column_name data_type;
-- 例子:新增1列
ALTER TABLE adb_demo.customer ADD COLUMN customer_name varchar comment '用户名';
-- 例子:新增2列
ALTER TABLE adb_demo.customer ADD COLUMN customer_name varchar ,ADD COLUMN age varchar;

A few notes:

  • If you are using the standard mode, you need to add new ones to the development table and production table separately. If you have synchronized to an external table, you need to add new ones to the external table, and then update the synchronized file to associate the newly added columns.
  • The character type needs to be consistent, especially for MaxComputer, which has strict character restrictions. If the field is of date type and the data is of datetime type, an error will be reported. Once the form is released to the production environment, the character type cannot be modified. At this time, it can only be rebuilt. The form is up. Regarding the data type of the field read from the source table, you can view it from the data map .

Added via table management

In addition to adding directly by running code, you can also open the corresponding form through table management and add columns through visualization.
Let’s take the standard mode as an example. The operation process is as follows: Find the table
in the table management and open it. After adding fields, submit it to the development environment first, and then submit it to the production environment to complete the addition of columns. Isn't it easy?
image.png

Of course, the premise is that you must have Alterpermissions. For example, when I was operating an ADB MySQL table, I found that the public account for operation only had selectand dropaccount for some forms, and had no Alterpermissions.
If you do not have permission to add columns, you cannot add columns through code or visualization.

There are several solutions:

Add column via DMS

Let me talk about some operations of DMS. There are three horizontal bars on the left side of the icon. Move the cursor to this place and a floating window will pop up. Put the cursor on all functions , and a lot of related functions will appear for selection. Adding a column is an ordinary data change operation, so find it (direct search).
image.png
When I operate, I only need to fill in the reason, business background, number of affected rows, and change the SQL. I don’t know if it is uniform.
When selecting the execution method, you can choose to execute automatically after the approval is passed . This is an automatic approval by the system and does not require manual review. It can be executed after submission.

Similarly, if you do not have relevant database permissions, you need to apply first, find the permission ticket in all functions , and then fill it in according to the prompts.

A few questions to supplement MaxCompute:

In MaxCompute , columns can be added, but columns cannot be deleted, and the data type and partition field of columns cannot be modified.
If the column must be modified, the table needs to be deleted and rebuilt, and the data reloaded.
If you must delete a column, you can create a new table (select the columns required by the old table) -> delete the old table -> modify the new table name to the old table name, for example:

create table new_table_name as select c1,c2,c3 from old_table_name;
drop table old_table_name; 
alter table new_table_name rename to old_table_name;

MaxCompute also does not support the self-increasing column function. If the amount of data is relatively small, you can also use the window function ROW_NUMBER to implement it.

For more frequently asked questions, refer to Frequently Asked Questions about DDL Operations .

Guess you like

Origin blog.csdn.net/qq_45476428/article/details/127701882