github.com/matrixorigin/matrixone@v1.2.0/test/distributed/cases/ddl/alter_table_rename_column.sql (about)

     1  -- @suit
     2  -- @case
     3  -- @desc: alter table rename column
     4  -- @label:bvt
     5  drop database if exists test;
     6  create database test;
     7  use test;
     8  
     9  -- rename column name: the same column name in the table
    10  drop table if exists samecolumn01;
    11  create table samecolumn01 (col1 int, col2 char);
    12  alter table samecolumn01 rename column col1 to newColumn;
    13  alter table samecolumn01 rename column col2 to newcolumn;
    14  show create table samecolumn01;
    15  show columns from samecolumn01;
    16  drop table samecolumn01;
    17  
    18  -- rename column in empty table
    19  drop table if exists rename01;
    20  create table rename01 (col1 int, col2 decimal);
    21  alter table rename01 rename column col1 to col1New;
    22  show create table rename01;
    23  show columns from rename01;
    24  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'name01' and COLUMN_NAME not like '__mo%';
    25  drop table rename01;
    26  
    27  
    28  -- rename the column name to the same as before
    29  drop table if exists rename02;
    30  create table rename02 (`colcolcol1` int, `colcolcol2` binary);
    31  insert into rename02 values (1, '2');
    32  insert into rename02 values (2, 'g');
    33  alter table rename02 rename column `colcolcol1` to `colcolcol1`;
    34  show create table rename02;
    35  insert into rename02 (colcolcol1, colcolcol2) values (3, '7');
    36  delete from rename02 where colcolcol1 = 1;
    37  update rename02 set colcolcol2 = '&' where colcolcol1 = 2;
    38  select * from rename02;
    39  show columns from rename02;
    40  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'rename02' and COLUMN_NAME not like '__mo%';
    41  drop table rename02;
    42  
    43  
    44  -- rename column then update and delete
    45  drop table if exists rename03;
    46  create table rename03(col1 int, col2 int, col3 varchar(20));
    47  insert into rename03 values (1,2,'cfewquier');
    48  insert into rename03 values (2,3,'329382');
    49  insert into rename03 values (3, 10, null);
    50  select * from rename03;
    51  alter table rename03 rename column col1 to col1New;
    52  alter table rename03 rename column col3 to col3New;
    53  show create table rename03;
    54  insert into rename03 (col1, col2, col3) values (3,4,'121131312');
    55  insert into rename03 (col1New, col2, col3New) values (3,4,'121131312');
    56  select * from rename03;
    57  update rename03 set col1New = 100 where col1New = 1;
    58  update rename03 set col3New = '2798u3d3frew' where col3New = '6';
    59  delete from rename03 where col3New is null;
    60  select * from rename03;
    61  show columns from rename03;
    62  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'rename03' and COLUMN_NAME not like '__mo%';
    63  drop table rename03;
    64  
    65  
    66  -- alter table rename column multi times
    67  drop table if exists rename04;
    68  create table rename04(a int,b int);
    69  insert into rename04 values(1,1);
    70  alter table rename04 rename column a to newA;
    71  show create table rename04;
    72  update rename04 set newA = 100 where b = 1;
    73  select * from rename04;
    74  alter table rename04 rename column newA to newnewA;
    75  show create table rename04;
    76  insert into rename04 values (1, 3);
    77  insert into rename04 values (1289,232);
    78  update rename04 set a = 10000 where b = 1;
    79  update rename04 set newnewA = 10000 where b = 1;
    80  select * from rename04;
    81  delet from rename04 where newnewa = 10000;
    82  select * from rename04;
    83  show columns from rename04;
    84  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'rename04' and COLUMN_NAME not like '__mo%';
    85  drop table rename04;
    86  
    87  
    88  -- error: abnormal column name
    89  drop table if exists rename06;
    90  create table rename06(col1 int);
    91  insert into rename06 values(1),(2);
    92  alter table rename06 rename column col1 to '';
    93  alter table rename06 rename column col1 to ' ';
    94  alter table rename06 rename column col1 to 数据库系统;
    95  alter table rename06 rename column col1 to 7327323467dhhjfkrnfe;
    96  alter table rename06 rename column col1 to **&&^^%%^&**;
    97  drop table rename06;
    98  
    99  
   100  -- rename column with ``
   101  drop table if exists rename06;
   102  create table rename06(col1 int);
   103  insert into rename06 values(1),(2);
   104  alter table rename06 rename column col1 to `数据库系统`;
   105  alter table rename06 rename column col1 to `数据操作,数据收集7327323467dhhjfkrnfe`;
   106  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'rename06' and COLUMN_NAME not like '__mo%';
   107  show columns from rename06;
   108  drop table rename06;
   109  
   110  -- rename internal table column: have no privilege
   111  alter table system.statement_info rename column role_id to role_idNew;
   112  alter table mo_catalog.mo_database rename column dat_type to newdat_type;
   113  alter table mysql.procs_priv rename column grantor to newGrantor;
   114  
   115  
   116  -- rename primary key column
   117  drop table if exists primary01;
   118  create table primary01 (col1 int primary key , col2 decimal);
   119  insert into primary01 values (2389324, 32784329.4309403);
   120  insert into primary01 values (3287, 89384038);
   121  alter table primary01 rename column col1 to col1New;
   122  show create table primary01;
   123  insert into primary01 values (-2839, 8239802839.00000000);
   124  insert into primary01 (col1New, col2) values (3287, 3293892.3232);
   125  delete from primary01 where col1New = -2839;
   126  update primary01 set col1 = 2873892 where col1New = 2389324;
   127  update primary01 set col1New = 2873892 where col1New = 2389324;
   128  select * from primary01;
   129  show columns from primary01;
   130  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'primary01' and COLUMN_NAME not like '__mo%';
   131  drop table primary01;
   132  
   133  
   134  -- rename foreign key column
   135  drop table if exists foreign01;
   136  create table foreign01(col1 int primary key,
   137                         col2 varchar(20),
   138                         col3 int,
   139                         col4 bigint);
   140  drop table if exists foreign02;
   141  create table foreign02(col1 int,
   142                         col2 int,
   143                         col3 int primary key,
   144                         constraint `c1` foreign key(col1) references foreign01(col1));
   145  show create table foreign01;
   146  show create table foreign02;
   147  insert into foreign01 values(1,'sfhuwe',1,1);
   148  insert into foreign01 values(2,'37829901k3d',2,2);
   149  insert into foreign02 values(1,1,1);
   150  insert into foreign02 values(2,2,2);
   151  select * from foreign01;
   152  select * from foreign02;
   153  alter table foreign01 rename column col1 to col1New;
   154  alter table foreign02 rename column col1 to `Colnewcolumn`;
   155  show create table foreign01;
   156  show create table foreign02;
   157  alter table foreign01 change col2 col2New varchar(100);
   158  alter table foreign02 change col2 col2new double after col3;
   159  insert into foreign01 values(3,'bcguwgheinwqneku678',2,2);
   160  insert into foreign02 values(6,6,6);
   161  delete from foreign01 where col2New = 'sfhuwe';
   162  delete from foreign02 where col2New = 2;
   163  update foreign01 set col2 = 'database ewueh ' where col1 = 1;
   164  update foreign01 set col1new = 9283923 where col1new = 1;
   165  select * from foreign01;
   166  select * from foreign02;
   167  show create table foreign01;
   168  show create table foreign02;
   169  show columns from foreign01;
   170  show columns from foreign02;
   171  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'foreign01' and COLUMN_NAME not like '__mo%';
   172  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'foreign02' and COLUMN_NAME not like '__mo%';
   173  drop table foreign02;
   174  drop table foreign01;
   175  
   176  
   177  -- unique key
   178  drop table if exists index01;
   179  CREATE TABLE index01(a INTEGER not null , b CHAR(10), c date, d decimal(7,2), UNIQUE KEY(a, b));
   180  show create table index01;
   181  insert into index01 values(1, 'ab', '1980-12-17', 800);
   182  insert into index01 values(2, 'ac', '1981-02-20', 1600);
   183  insert into index01 values(3, 'ad', '1981-02-22', 500);
   184  select * from index01;
   185  alter table index01 rename column b to bNew;
   186  show create table index01;
   187  show index from index01;
   188  insert into index01 (a, b, c, d) values (5, 'bh', '1999-01-01', 3000);
   189  insert into index01 (a, bnew, c, d) values (5, 'bh', '1999-01-01', 3000);
   190  select * from index01;
   191  delete from index01 where b = 'ab';
   192  delete from index01 where bneW = 'ab';
   193  select * from index01;
   194  update index01 set c = '2022-12-12' where bNew = 'ac';
   195  select * from index01;
   196  show columns from index01;
   197  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'index01' and COLUMN_NAME not like '__mo%';
   198  drop table index01;
   199  
   200  
   201  -- index
   202  drop table if exists index02;
   203  CREATE TABLE index02(a INTEGER PRIMARY KEY, b CHAR(10), c date, d decimal(7,2), INDEX(a, b), KEY(c));
   204  insert into index02 values(1, 'ab', '1980-12-17', 800);
   205  insert into index02 values(2, 'ac', '1981-02-20', 1600);
   206  insert into index02 values(3, 'ad', '1981-02-22', 500);
   207  select * from index02;
   208  alter table index02 rename column b to bNewNew;
   209  show create table index02;
   210  insert into index02 values (4, 'ab', '2000-10-10', 10000);
   211  insert into index02 values (5, 'gh', '1999-12-31', 20000);
   212  delete from index02 where bnewnew = 'ab';
   213  update index02 set bnewnew = 'database' where bnewnEW = 'ad';
   214  select * from index02;
   215  show index from index02;
   216  show columns from index02;
   217  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'index02' and COLUMN_NAME not like '__mo%';
   218  select * from index02;
   219  
   220  
   221  -- rename cluster by table column
   222  drop table if exists cluster01;
   223  create table cluster01(a tinyint primary key, b smallint signed, c int unsigned,d bigint not null);
   224  insert into cluster01 (a, b, c, d) values (1, 255, 438, 7328832832);
   225  alter table cluster01 rename column a to `NewA`;
   226  alter table cluster01 rename column `newa` to `NewAAAAAAAA`;
   227  show create table cluster01;
   228  insert into cluster01 (a, b, c, d) values (-32, 32832, 8329, 893434);
   229  insert into cluster01 (NewAAAAAAAA, b, c, d) values (-32, 32, 8329, 893434);
   230  select * from cluster01;
   231  show columns from cluster01;
   232  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'cluster01' and COLUMN_NAME not like '__mo%';
   233  drop table cluster01;
   234  
   235  
   236  -- rename temporary table column
   237  drop table if exists temporary01;
   238  create table temporary01 (col1 int, col2 decimal);
   239  insert into temporary01 (col1, col2) values (3728937, 37283.3232);
   240  alter table temporary01 rename column col1 to `UUUYGGBBJBJ`;
   241  insert into temporary01 (col1, col2) values (-32893, -89232);
   242  insert into temporary01 (`UUUYGGBBJBJ`, col2) values (-32893, -89232);
   243  select * from temporary01;
   244  show columns from temporary01;
   245  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'temporary01' and COLUMN_NAME not like '__mo%';
   246  drop table temporary01;
   247  
   248  
   249  -- rename external table column
   250  drop table if exists ex_table_2_1;
   251  create external table ex_table_2_1(num_col1 tinyint,num_col2 smallint,num_col3 int,num_col4 bigint,num_col5 tinyint unsigned,num_col6 smallint unsigned,num_col7 int unsigned,num_col8 bigint unsigned ,num_col9 float(5,3),num_col10 double(6,5),num_col11 decimal(38,19))infile{"filepath"='$resources/external_table_file/ex_table_2_1.csv'} fields terminated by ',' enclosed by '\"' lines terminated by '\n';
   252  alter table ex_table_2_1 rename column num_col1 to newnum_col1;
   253  alter table ex_table_2_1 rename column num_col2 to `shurhenwjkrferveg`;
   254  show create table ex_table_2_1;
   255  select * from ex_table_2_1;
   256  
   257  
   258  -- creating table, creating view, renaming the columns, view the view
   259  drop table if exists view01;
   260  drop table if exists view02;
   261  drop view if exists v0;
   262  create table view01 (a int);
   263  insert into view01 values (1),(2);
   264  create table view02 (a int);
   265  insert into view02 values (1);
   266  alter table view01 rename column a to `cwhuenwjfdwcweffcfwef`;
   267  alter table view02 rename column a to `cwhuenwjfdwcweffcfwef`;
   268  show columns from view01;
   269  show columns from view02;
   270  show create table view01;
   271  show create table view02;
   272  create view v0 as select view01.a, view02.a as b from view01 left join view02 using(a);
   273  create view v0 as select view01.cwhuenwjfdwcweffcfwef, view02.cwhuenwjfdwcweffcfwef as b from view01 left join view02 using(cwhuenwjfdwcweffcfwef);
   274  show create view v0;
   275  drop table view01;
   276  drop table view02;
   277  
   278  -- permission
   279  drop role if exists role_r1;
   280  drop user if exists role_u1;
   281  create role role_r1;
   282  create user role_u1 identified by '111' default role role_r1;
   283  drop table if exists rename01;
   284  create table rename01(col1 int);
   285  insert into rename01 values(1);
   286  insert into rename01 values(2);
   287  grant create database on account * to role_r1;
   288  grant show databases on account * to role_r1;
   289  grant connect on account * to role_r1;
   290  grant select on table * to role_r1;
   291  grant show tables on database * to role_r1;
   292  
   293  -- @session:id=2&user=sys:role_u1:role_r1&password=111
   294  use test;
   295  alter table rename01 rename column col1 to newCol1;
   296  -- @session
   297  grant alter table on database * to role_r1;
   298  
   299  -- @session:id=2&user=sys:role_u1:role_r1&password=111
   300  use test;
   301  alter table rename01 rename column col1 to newRename;
   302  alter table rename01 rename column newRename to `newNewRename`;
   303  show create table rename01;
   304  show columns from rename01;
   305  -- @session
   306  drop table rename01;
   307  drop role role_r1;
   308  drop user role_u1;
   309  -- rename column
   310  
   311  -- prepare
   312  drop table if exists prepare01;
   313  create table prepare01(col1 int, col2 char);
   314  insert into prepare01 values (1,'a'),(2,'b'),(3,'c');
   315  prepare s1 from 'alter table prepare01 rename column col1 to col1dheuwhvcer';
   316  execute s1;
   317  show create table prepare01;
   318  prepare s2 from 'alter table prepare01 rename column col1dheuwhvcer to col1';
   319  execute s2;
   320  show create table prepare01;
   321  show columns from prepare01;
   322  update prepare01 set col1 = 2147483647 where col2 = 'c';
   323  delete from prepare01 where col2 = 'b';
   324  insert into prepare01 values (42342, '3');
   325  select * from prepare01;
   326  drop table prepare01;
   327  
   328  -- begin, alter table rename, commit, then select
   329  drop table if exists table03;
   330  begin;
   331  create table table03(col1 int, col2 char);
   332  alter table table03 rename to NewCol1;
   333  commit;
   334  select * from NewCol1;
   335  select col1 from NewCol1;
   336  drop table NewCol1;
   337  drop database test;
   338