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

     1  -- @suit
     2  -- @case
     3  -- @desc: alter table add/drop column
     4  -- @label:bvt
     5  
     6  -- normal table adds primary key
     7  drop table if exists pri01;
     8  create table pri01 (col1 int, col2 decimal);
     9  insert into pri01 (col1, col2) values (1,2378.328839842);
    10  insert into pri01 values (234, -3923.2342342);
    11  select * from pri01;
    12  show create table pri01;
    13  alter table pri01 add constraint primary key(col1);
    14  insert into pri01 values (23423, 32432543.3242);
    15  insert into pri01 values (234, -3923.2342342);
    16  show columns from pri01;
    17  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri01' and COLUMN_NAME not like '__mo%';
    18  drop table pri01;
    19  
    20  
    21  -- failed to add a single primary key, duplicate values existed in the table
    22  drop table if exists pri02;
    23  create table pri02 (col1 char, col2 bigint unsigned);
    24  insert into pri02 (col1, col2) values ('a', 327349284903284032);
    25  insert into pri02 values ('*', 3289323423);
    26  insert into pri02 values ('*', 328932342342424);
    27  select * from pri02;
    28  alter table pri02 add constraint primary key (col1);
    29  show create table pri02;
    30  show columns from pri02;
    31  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri02' and COLUMN_NAME not like '__mo%';
    32  drop table pri02;
    33  
    34  
    35  -- failed to add a single primary key, duplicate values existed in the table
    36  drop table if exists pri03;
    37  create table pri03 (col1 char, col2 bigint unsigned);
    38  insert into pri03 (col1, col2) values ('a', 327349284903284032);
    39  insert into pri03 values ('*', 3289323423);
    40  select * from pri03;
    41  alter table pri03 add constraint primary key (col1);
    42  show create table pri03;
    43  show columns from pri03;
    44  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri03' and COLUMN_NAME not like '__mo%';
    45  drop table pri03;
    46  
    47  
    48  -- failed to add a single primary key, null fields existed in the table
    49  drop table if exists pri04;
    50  create table pri04 (col1 varchar(100), col2 float);
    51  insert into pri04 (col1, col2) values ('databaseDATABASE 数据库数据库系统', -32734928490.3284032);
    52  insert into pri04 values ('3782973804u2databasejnwfhui34数据库endfcioc', 3289323423);
    53  insert into pri04 values (null, 378270389824324);
    54  select * from pri04;
    55  alter table pri04 add constraint primary key (col1);
    56  show create table pri04;
    57  show columns from pri04;
    58  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri04' and COLUMN_NAME not like '__mo%';
    59  drop table pri04;
    60  
    61  
    62  -- failed to add a single primary key, null fields does not existed in the table
    63  drop table if exists pri05;
    64  create table pri05 (col1 date, col2 double);
    65  insert into pri05 (col1, col2) values ('1997-01-13', -32734928490.3284032);
    66  insert into pri05 values ('2023-08-18', 3289323423);
    67  select * from pri05;
    68  alter table pri05 add constraint primary key (col1);
    69  show create table pri05;
    70  show columns from pri05;
    71  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri05' and COLUMN_NAME not like '__mo%';
    72  drop table pri05;
    73  
    74  
    75  --  the column constraint in the table is 'default null'
    76  drop table if exists pri06;
    77  create table pri06 (col1 smallint default null, col2 double);
    78  insert into pri06 (col1, col2) values (100, -32734928490.3284032);
    79  insert into pri06 values (200, 3289323423);
    80  select * from pri06;
    81  alter table pri06 add constraint primary key (col1);
    82  show create table pri06;
    83  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri06' and COLUMN_NAME not like '__mo%';
    84  show columns from pri06;
    85  drop table pri06;
    86  
    87  
    88  --  modify the value of the column after add primary key
    89  drop table if exists pri07;
    90  create table pri07 (col1 decimal, col2 double);
    91  insert into pri07 (col1, col2) values (12.213231000021312, -32734928490.3284032);
    92  insert into pri07 values (32784234.4234243243243242, 3289323423);
    93  select * from pri07;
    94  alter table pri07 add constraint primary key (col1);
    95  show create table pri07;
    96  show columns from pri07;
    97  update pri07 set col1 = 1000000 where col2 = 3289323423;
    98  update pri07 set col1 = 12.213231000021312 where col2 = 3289323423;
    99  delete from pri07 where col1 = 12.213231000021312;
   100  select * from pri07;
   101  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri07' and COLUMN_NAME not like '__mo%';
   102  drop table pri07;
   103  
   104  
   105  -- abnormal test:Add more than one primary key to the same table
   106  drop table if exists pri08;
   107  create table pri08 (col1 binary, col2 int unsigned);
   108  insert into pri08 values ('ewfijew', 372984324);
   109  insert into pri08 values ('ew8u3ejkfcwev', 2147483647);
   110  select * from pri08;
   111  alter table pri08 add constraint primary key (col1);
   112  show create table pri08;
   113  show columns from pri08;
   114  alter table pri08 add constraint primary key (col2);
   115  show create table pri08;
   116  show columns from pri08;
   117  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri08' and COLUMN_NAME not like '__mo%';
   118  drop table pri08;
   119  
   120  
   121  -- add multiple columns of primary keys. The primary key column cannot be empty
   122  drop table if exists pri09;
   123  create table pri09 (col1 binary, col2 int unsigned);
   124  insert into pri09 values ('a', 372893243);
   125  insert into pri09 values (null, 2147483647);
   126  select * from pri09;
   127  alter table pri09 add constraint primary key (col1, col2);
   128  show create table pri09;
   129  show columns from pri09;
   130  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri09' and COLUMN_NAME not like '__mo%';
   131  drop table pri09;
   132  
   133  
   134  -- add multiple primary key columns to a common table
   135  drop table if exists pri10;
   136  create table pri10 (col1 int, col2 char(1));
   137  insert into pri10 (col1, col2) values (1, 'a');
   138  insert into pri10 values (-2, '*');
   139  select * from pri10;
   140  alter table pri10 add constraint primary key (col1, col2);
   141  show create table pri10;
   142  show columns from pri10;
   143  insert into pri10 (col1, col2) values (1, null);
   144  insert into pri10 values (-2, 'p');
   145  -- @pattern
   146  insert into pri10 (col1, col2) values (1, 'a');
   147  select * from pri10;
   148  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri10' and COLUMN_NAME not like '__mo%';
   149  drop table pri10;
   150  
   151  
   152  -- abnormal test: change a single primary key column to multiple primary key columns
   153  drop table if exists pri11;
   154  create table pri11 (col1 int primary key , col2 decimal, col3 char);
   155  insert into pri11 (col1, col2, col3) values (1, 3289034.3232, 'q');
   156  insert into pri11 values (2, 3829.3232, 'a');
   157  alter table pri11 add constraint primary key (col1, col2);
   158  show create table pri11;
   159  show columns from pri11;
   160  select * from pri11;
   161  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri11' and COLUMN_NAME not like '__mo%';
   162  drop table pri11;
   163  
   164  
   165  -- abnormal test:Add/drop primary keys for temporary tables
   166  drop table if exists temp01;
   167  create temporary table temp01 (col1 datetime, col2 blob);
   168  insert into temp01 values ('1997-01-13 00:00:00', '342ewfyuehcdeiuopwu4jo3lekwdfhiu48woi3jrdnefrbwui34f');
   169  insert into temp01 values ('2012-01-13 23:23:59', '63298ufh3jcweuiv4h32jhf432ouy4hu3enjwfnwje4n3bj24f34573');
   170  select * from temp01;
   171  alter table temp01 add constraint primary key (col2);
   172  select * from temp01;
   173  show create table temp01;
   174  show columns from temp01;
   175  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'temp01' and COLUMN_NAME not like '__mo%';
   176  drop table temp01;
   177  
   178  
   179  -- abnormal test:external table add/drop primary key
   180  drop table if exists ex_table_2_1;
   181  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';
   182  alter table ex_table_2_1 add constraint primary key (num_col1, num_col2);
   183  show create table ex_table_2_1;
   184  select * from ex_table_2_1;
   185  
   186  
   187  -- primary key deletion
   188  drop table if exists droppri01;
   189  create table droppri01 (col1 int primary key , col2 decimal);
   190  insert into droppri01 (col1, col2) values (1, 234324234.234242);
   191  insert into droppri01 values (32894324,4234294023.4324324234);
   192  alter table droppri01 drop primary key;
   193  show create table droppri01;
   194  show columns from droppri01;
   195  insert into droppri01 (col1, col2) values (1, 3489372843);
   196  truncate table droppri01;
   197  alter table droppri01 add constraint primary key (col2);
   198  show create table droppri01;
   199  alter table droppri01 drop primary key;
   200  show create table droppri01;
   201  show columns from droppri01;
   202  select * from droppri01;
   203  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'droppri01' and COLUMN_NAME not like '__mo%';
   204  drop table droppri01;
   205  
   206  
   207  -- Run the show create table error command to add or delete a primary key in one column and add a primary key in the other column
   208  drop table if exists pri01;
   209  create table pri01(col1 int, col2 decimal);
   210  alter table pri01 add constraint primary key(col1);
   211  show create table pri01;
   212  alter table pri01 drop primary key;
   213  show create table pri01;
   214  alter table pri01 add constraint primary key(col2);
   215  show create table pri01;
   216  show columns from pri01;
   217  select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'pri01' and COLUMN_NAME not like '__mo%';
   218  drop table pri01;
   219  
   220  
   221  -- multi-column primary key deletion
   222  drop table if exists droppri02;
   223  create table droppri02 (col1 int auto_increment, col2 decimal, col3 char, col4 varchar not null, col5 float, primary key (col1, col2, col3));
   224  show create table droppri02;
   225  show columns from droppri02;
   226  alter table droppri02 drop primary key;
   227  show create table droppri02;
   228  show columns from droppri02;
   229  drop table droppri02;
   230  
   231  
   232  -- prepare
   233  drop table if exists prepare01;
   234  create table prepare01(col1 int primary key , col2 char);
   235  insert into prepare01 values (1,'a'),(2,'b'),(3,'c');
   236  show create table prepare01;
   237  show columns from prepare01;
   238  prepare s1 from 'alter table prepare01 drop primary key';
   239  execute s1;
   240  show create table prepare01;
   241  show columns from prepare01;
   242  prepare s2 from 'alter table prepare01 add constraint primary key(col2) ';
   243  execute s2;
   244  show create table prepare01;
   245  show columns from prepare01;
   246  drop table prepare01;
   247  
   248  
   249  -- permission
   250  drop role if exists role_r1;
   251  drop user if exists role_u1;
   252  drop table if exists test01;
   253  create role role_r1;
   254  create user role_u1 identified by '111' default role role_r1;
   255  create table test01(col1 int, col2 varchar(10));
   256  insert into test01 values(1, 'ewuijernf');
   257  insert into test01 values(2, 'abscde');
   258  grant create database on account * to role_r1;
   259  grant show databases on account * to role_r1;
   260  grant connect on account * to role_r1;
   261  grant select on table * to role_r1;
   262  grant show tables on database * to role_r1;
   263  
   264  -- @session:id=2&user=sys:role_u1:role_r1&password=111
   265  use alter_table_add_drop_primary_key;
   266  alter table test01 add constraint primary key(col1);
   267  -- @session
   268  grant alter table on database * to role_r1;
   269  
   270  -- @session:id=2&user=sys:role_u1:role_r1&password=111
   271  use alter_table_add_drop_primary_key;
   272  alter table test01 add constraint primary key(col1);
   273  show create table test01;
   274  show columns from test01;
   275  alter table test01 drop primary key;
   276  show create table test01;
   277  show columns from test01;
   278  -- @session
   279  show create table test01;
   280  show columns from test01;
   281  drop table test01;
   282  drop role role_r1;
   283  drop user role_u1;
   284  
   285  
   286  -- mixed situation
   287  -- modify change rename column, add/drop primary key
   288  drop table if exists mix01;
   289  create table mix01 (col1 int, col2 decimal, col3 char, col4 varchar(100));
   290  insert into mix01 (col1, col2, col3, col4) values (1, 2, 'a', 'w3uir34jn2k48ujf4');
   291  insert into mix01 (col1, col2, col3, col4) values (2, 3, 'd', '3289u3ji2dff43');
   292  alter table mix01 modify col1 float after col3, change column col2 col2New double, rename column col3 to newCol3, add constraint primary key(col1);
   293  insert into mix01 (col1, col2, col3, col4) values (3, 'w', 37283.323, 'dswhjkfrewr');
   294  alter table mix01 add column col5 int after col1, rename column col2new to newnewCol2;
   295  select * from mix01;
   296  alter table mix01 rename column col2new to newnewCol2, drop primary key;
   297  show create table mix01;
   298  delete from mix01 where newnewcol2 = 2;
   299  update mix01 set newnewcol2 = 8290432.324 where newcol3 = 'd';
   300  select * from mix01;
   301  show create table mix01;
   302  show columns from mix01;
   303  drop table mix01;
   304  
   305  -- begin, alter table add/drop primary key column, commit, then select
   306  drop table if exists table01;
   307  begin;
   308  create table table01(col1 int, col2 decimal);
   309  insert into table01 values(100,200);
   310  insert into table01 values(200,300);
   311  alter table table01 add constraint primary key (col2);
   312  commit;
   313  select * from table01;
   314  select col1 from table01;
   315  drop table table01;
   316  
   317  drop table if exists table01;
   318  begin;
   319  create table table01(col1 int primary key, col2 decimal);
   320  insert into table01 values(100,200);
   321  insert into table01 values(200,300);
   322  alter table table01 drop primary key;
   323  commit;
   324  select * from table01;
   325  select col1 from table01;
   326  drop table table01;