云数据库 GaussDB-ALTER TABLE:修改列示例

时间:2025-01-26 10:48:21

修改列示例

  • 修改列名
    --建表。gaussdb=# CREATE TABLE test_alt2(c1 INT,c2 INT);--修改列名。gaussdb=# ALTER TABLE test_alt2 RENAME c1 TO id;gaussdb=# ALTER TABLE test_alt2 RENAME COLUMN c2 to areaid; --查看。gaussdb=# \d test_alt2   Table "public.test_alt2" Column |  Type   | Modifiers --------+---------+----------- id     | integer |  areaid | integer | 
  • 增加列
    --表test_alt2增加列。gaussdb=# ALTER TABLE IF EXISTS test_alt2 ADD COLUMN name VARCHAR(20);--查看。gaussdb=# \d test_alt2           Table "public.test_alt2"  Column  |         Type          | Modifiers ----------+-----------------------+----------- id       | integer               |  areaid   | integer               |  name     | character varying(20) |
  • 增加自增列
    --表local_autoinc增加AUTO_INCREMENT自增列(根据实际情况修改DATANODE名字,SELECT node_name FROM pgxc_node WHERE node_type = 'D';)。gaussdb=# CREATE TABLE local_autoinc(col1 int)DISTRIBUTE BY LIST(col1)(    SLICE s1 VALUES (1) DATANODE datanode1,    SLICE s2 VALUES (2) DATANODE datanode2);--数据分布到DN1上。gaussdb=# INSERT INTO local_autoinc(col1) VALUES(1);--添加一个本地自增列,每个DN从1开始自增。gaussdb=# ALTER TABLE local_autoinc ADD COLUMN col int AUTO_INCREMENT;gaussdb=# SELECT col,col1 FROM local_autoinc ORDER BY 2,1; col | col1-----+------   1 |    1(1 row)--将所有DN的下一个自增值设为10。gaussdb=# ALTER TABLE local_autoinc AUTO_INCREMENT = 10;--数据分布到DN1上,NULL触发自增,自增值为10。gaussdb=# INSERT INTO local_autoinc(col, col1) VALUES(NULL,1);--数据分布到DN2上,0触发自增,自增值为10。gaussdb=# INSERT INTO local_autoinc(col, col1) VALUES(0,2);gaussdb=# SELECT col,col1 FROM local_autoinc ORDER BY 2,1; col | col1-----+------   1 |    1  10 |    1  10 |    2(3 rows)
  • 修改列的数据类型
    --修改test_alt2表中name字段的类型。gaussdb=# ALTER TABLE test_alt2 MODIFY name VARCHAR(50);--查看。gaussdb=# \d test_alt2          Table "public.test_alt2" Column |         Type          | Modifiers --------+-----------------------+----------- id     | integer               |  areaid | integer               |  name   | character varying(50) | --在线修改test_alt2表中name字段的类型。gaussdb=# ALTER TABLE ONLINE test_alt2 MODIFY name VARCHAR(60);--查看。gaussdb=# \d test_alt2          Table "public.test_alt2" Column |         Type          | Modifiers --------+-----------------------+----------- id     | integer               |  areaid | integer               |  name   | character varying(60) | 
    --修改test_alt2表中name字段的类型。gaussdb=# ALTER TABLE test_alt2 ALTER COLUMN name TYPE VARCHAR(25);--查看。gaussdb=# \d test_alt2          Table "public.test_alt2" Column |         Type          | Modifiers --------+-----------------------+----------- id     | integer               |  areaid | integer               |  name   | character varying(25) | --在线修改test_alt2表中name字段的类型。gaussdb=# ALTER TABLE ONLINE test_alt2 ALTER COLUMN name TYPE VARCHAR(35);--查看。gaussdb=# \d test_alt2          Table "public.test_alt2" Column |         Type          | Modifiers --------+-----------------------+----------- id     | integer               |  areaid | integer               |  name   | character varying(35) | 
  • 删除列
    --删除test_alt2中areaid字段。gaussdb=# ALTER TABLE test_alt2 DROP COLUMN areaid;--查看。gaussdb=# \d test_alt2          Table "public.test_alt2" Column |         Type          | Modifiers --------+-----------------------+----------- id     | integer               |  name   | character varying(25) | 
  • 修改列的存储模式
    --查看表详细信息。gaussdb=# \d+ test_alt2                              Table "public.test_alt2" Column |         Type          | Modifiers | Storage  | Stats target | Description --------+-----------------------+-----------+----------+--------------+------------- id     | integer               |           | plain    |              |  name   | character varying(35) |           | extended |              | Has OIDs: noDistribute By: HASH(id)Location Nodes: ALL DATANODESOptions: orientation=row, compression=no, storage_type=USTORE, segment=off--修改test_alt2表中name字段的存储模式。gaussdb=# ALTER TABLE test_alt2 ALTER COLUMN name SET STORAGE PLAIN;--查看。gaussdb=# \d+ test_alt2                             Table "public.test_alt2" Column |         Type          | Modifiers | Storage | Stats target | Description --------+-----------------------+-----------+---------+--------------+------------- id     | integer               |           | plain   |              |  name   | character varying(35) |           | plain   |              | Has OIDs: noDistribute By: HASH(id)Location Nodes: ALL DATANODESOptions: orientation=row, compression=no, storage_type=USTORE, segment=off--删除。gaussdb=# DROP TABLE test_alt2;
support.huaweicloud.com/distributed-devg-v8-gaussdb/gaussdb-12-0509.html