github.com/matrixorigin/matrixone@v1.2.0/test/distributed/cases/ddl/alter_table_change_column.sql (about) 1 drop database if exists db2; 2 create database db2; 3 use db2; 4 5 CREATE TABLE t1 (a INTEGER PRIMARY KEY, b CHAR(10)); 6 desc t1; 7 8 insert into t1 values(1, 'ab'); 9 insert into t1 values(2, 'ac'); 10 insert into t1 values(3, 'ad'); 11 12 select * from t1; 13 14 alter table t1 change a x VARCHAR(20); 15 desc t1; 16 select * from t1; 17 ---------------------------------------------------------------------------------- 18 drop table if exists t1; 19 CREATE TABLE t1 (a INTEGER PRIMARY KEY, b CHAR(10), c date); 20 desc t1; 21 22 insert into t1 values(1, 'ab', '1980-12-17'); 23 insert into t1 values(2, 'ac', '1981-02-20'); 24 insert into t1 values(3, 'ad', '1981-02-22'); 25 26 select * from t1; 27 28 alter table t1 change a x VARCHAR(20) after b; 29 desc t1; 30 select * from t1; 31 ---------------------------------------------------------------------------------- 32 drop table if exists t1; 33 CREATE TABLE t1 (a INTEGER PRIMARY KEY, b CHAR(10), c date); 34 desc t1; 35 36 insert into t1 values(1, 'ab', '1980-12-17'); 37 insert into t1 values(2, 'ac', '1981-02-20'); 38 insert into t1 values(3, 'ad', '1981-02-22'); 39 40 select * from t1; 41 42 alter table t1 change a x VARCHAR(20) after c; 43 desc t1; 44 select * from t1; 45 -------------------------------------------------------------------------------------- 46 drop table if exists t1; 47 CREATE TABLE t1 (a INTEGER, b CHAR(10), c date, PRIMARY KEY(a)); 48 desc t1; 49 50 insert into t1 values(1, 'ab', '1980-12-17'); 51 insert into t1 values(2, 'ac', '1981-02-20'); 52 insert into t1 values(3, 'ad', '1981-02-22'); 53 54 select * from t1; 55 56 alter table t1 change b x VARCHAR(20) first; 57 desc t1; 58 select * from t1; 59 60 ----------------------------------------------------------------------------------- 61 drop table if exists t1; 62 CREATE TABLE t1 (a INTEGER PRIMARY KEY, b CHAR(10), c date, d decimal(7,2)); 63 desc t1; 64 65 insert into t1 values(1, 'ab', '1980-12-17', 800); 66 insert into t1 values(2, 'ac', '1981-02-20', 1600); 67 insert into t1 values(3, 'ad', '1981-02-22', 500); 68 69 select * from t1; 70 71 alter table t1 change b x VARCHAR(20), change d y int unsigned; 72 73 desc t1; 74 select * from t1; 75 ---------------------------------------------------------------------------------------- 76 drop table if exists t1; 77 CREATE TABLE t1 (a INTEGER PRIMARY KEY, b CHAR(10), c date, d decimal(7,2)); 78 desc t1; 79 80 insert into t1 values(1, 'ab', '1980-12-17', 800); 81 insert into t1 values(2, 'ac', '1981-02-20', 1600); 82 insert into t1 values(3, 'ad', '1981-02-22', 500); 83 84 select * from t1; 85 --ERROR 1068 (42000): Multiple primary key defined 86 alter table t1 change a x VARCHAR(20) PRIMARY KEY; 87 --ERROR 1068 (42000): Multiple primary key defined 88 alter table t1 change b x VARCHAR(20) PRIMARY KEY; 89 --ERROR 1054 (42S22): Unknown column 'b' in 't1' 90 alter table t1 change b z VARCHAR(20) first, change d m int unsigned after b; 91 92 ALTER TABLE t1 CHANGE a a INT NOT NULL; 93 desc t1; 94 select * from t1; 95 alter table t1 change b z VARCHAR(20) first, change d m int unsigned after z; 96 97 desc t1; 98 select * from t1; 99 ---------------------------------------------------------------------------------------- 100 drop table if exists t1; 101 CREATE TABLE t1(a INTEGER, b CHAR(10), c datetime, d decimal(7,2), PRIMARY KEY(a, b)); 102 desc t1; 103 104 insert into t1 values(1, 'ab', '1980-12-17 11:34:45', 800); 105 insert into t1 values(2, 'ac', '1981-02-20 10:34:45', 1600); 106 insert into t1 values(3, 'ad', '1981-02-22 09:34:45', 500); 107 select * from t1; 108 109 alter table t1 change c x datetime default '2023-06-21 12:34:45' on update CURRENT_TIMESTAMP; 110 desc t1; 111 select * from t1; 112 113 alter table t1 change x y date; 114 desc t1; 115 select * from t1; 116 ---------------------------------------------------------------------------------------- 117 drop table if exists t1; 118 CREATE TABLE t1 (a INTEGER, b CHAR(10), c datetime PRIMARY KEY default '2023-06-21' on update CURRENT_TIMESTAMP); 119 desc t1; 120 121 insert into t1 values(1, 'ab', '1980-12-17'); 122 insert into t1 values(2, 'ac', '1981-02-20'); 123 insert into t1 values(3, 'ad', '1981-02-22'); 124 select * from t1; 125 126 alter table t1 change c x date first; 127 desc t1; 128 select * from t1; 129 130 alter table t1 change x y datetime default '2023-06-21'; 131 desc t1; 132 select * from t1; 133 ----------------------------------------------------------------------------------------- 134 drop table if exists t1; 135 CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(10), c date, d decimal(7,2), UNIQUE KEY(a, b)); 136 desc t1; 137 138 insert into t1 values(1, 'ab', '1980-12-17', 800); 139 insert into t1 values(2, 'ac', '1981-02-20', 1600); 140 insert into t1 values(3, 'ad', '1981-02-22', 500); 141 select * from t1; 142 143 alter table t1 change b x VARCHAR(20); 144 show index from t1; 145 desc t1; 146 select * from t1; 147 148 alter table t1 change x y VARCHAR(20) UNIQUE KEY; 149 show index from t1; 150 desc t1; 151 select * from t1; 152 153 alter table t1 change y z VARCHAR(20) UNIQUE KEY; 154 show index from t1; 155 desc t1; 156 select * from t1; 157 ------------------------------------------------------------------------------------------ 158 drop table if exists t1; 159 CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(10), c date, d decimal(7,2), UNIQUE KEY(a, b)); 160 desc t1; 161 162 insert into t1 values(1, 'ab', '1980-12-17', 800); 163 insert into t1 values(2, 'ac', '1981-02-20', 1600); 164 insert into t1 values(3, 'ad', '1981-02-22', 500); 165 select * from t1; 166 167 alter table t1 rename column a to x; 168 show index from t1; 169 desc t1; 170 select * from t1; 171 172 alter table t1 rename column b to y; 173 show index from t1; 174 desc t1; 175 select * from t1; 176 ------------------------------------------------------------------------------------------ 177 drop table if exists t1; 178 CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(10), c date, d decimal(7,2), INDEX(a, b), KEY(c)); 179 desc t1; 180 181 insert into t1 values(1, 'ab', '1980-12-17', 800); 182 insert into t1 values(2, 'ac', '1981-02-20', 1600); 183 insert into t1 values(3, 'ad', '1981-02-22', 500); 184 select * from t1; 185 186 alter table t1 change c x datetime; 187 show index from t1; 188 desc t1; 189 select * from t1; 190 191 --ERROR 1068 (42000): Multiple primary key defined 192 alter table t1 change x y VARCHAR(20) KEY; 193 194 alter table t1 change x y VARCHAR(20) UNIQUE KEY; 195 show index from t1; 196 desc t1; 197 select * from t1; 198 ----------------------------------------------------------------------------------------- 199 drop table if exists t1; 200 create table t1(a int unsigned, b varchar(15) NOT NULL, c date, d decimal(7,2), primary key(a)); 201 desc t1; 202 203 insert into t1 values (7369,'SMITH','1980-12-17',800); 204 insert into t1 values (7499,'ALLEN','1981-02-20',1600); 205 insert into t1 values (7521,'WARD','1981-02-22',1250); 206 insert into t1 values (7566,'JONES','1981-04-02',2975); 207 insert into t1 values (7654,'MARTIN','1981-09-28',1250); 208 select * from t1; 209 210 211 alter table t1 change a x int auto_increment; 212 desc t1; 213 select * from t1; 214 215 alter table t1 change d y decimal(6,2); 216 desc t1; 217 select * from t1; 218 ----------------------------------------------------------------------------------------- 219 drop table if exists t1; 220 CREATE TABLE t1(a INTEGER, b CHAR(10), c datetime, d decimal(7,2), PRIMARY KEY(a, b)); 221 desc t1; 222 223 insert into t1 values(1, 'ab', '1980-12-17 11:34:45', 800); 224 insert into t1 values(2, 'ac', '1981-02-20 10:34:45', 1600); 225 insert into t1 values(3, 'ad', '1981-02-22 09:34:45', 500); 226 select * from t1; 227 228 alter table t1 change a x decimal(7,2); 229 desc t1; 230 select * from t1; 231 232 alter table t1 rename column b to y; 233 desc t1; 234 select * from t1; 235 236 alter table t1 rename column c to z; 237 desc t1; 238 select * from t1; 239 ----------------------------------------------------------------------------------------- 240 drop table if exists dept; 241 create table dept( 242 deptno varchar(20), 243 dname varchar(15), 244 loc varchar(50), 245 primary key(deptno) 246 ); 247 248 INSERT INTO dept VALUES (10,'ACCOUNTING','NEW YORK'); 249 INSERT INTO dept VALUES (20,'RESEARCH','DALLAS'); 250 INSERT INTO dept VALUES (30,'SALES','CHICAGO'); 251 INSERT INTO dept VALUES (40,'OPERATIONS','BOSTON'); 252 253 drop table if exists emp; 254 create table emp( 255 empno int unsigned auto_increment, 256 ename varchar(15), 257 job varchar(10), 258 mgr int unsigned, 259 hiredate date, 260 sal decimal(7,2), 261 comm decimal(7,2), 262 deptno varchar(20), 263 primary key(empno), 264 constraint `c1` FOREIGN KEY (deptno) REFERENCES dept(deptno) 265 ); 266 267 INSERT INTO emp VALUES (7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20); 268 INSERT INTO emp VALUES (7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30); 269 INSERT INTO emp VALUES (7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30); 270 INSERT INTO emp VALUES (7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20); 271 INSERT INTO emp VALUES (7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30); 272 INSERT INTO emp VALUES (7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30); 273 INSERT INTO emp VALUES (7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10); 274 INSERT INTO emp VALUES (7788,'SCOTT','ANALYST',7566,'0087-07-13',3000,NULL,20); 275 INSERT INTO emp VALUES (7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10); 276 INSERT INTO emp VALUES (7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30); 277 INSERT INTO emp VALUES (7876,'ADAMS','CLERK',7788,'0087-07-13',1100,NULL,20); 278 INSERT INTO emp VALUES (7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30); 279 INSERT INTO emp VALUES (7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20); 280 INSERT INTO emp VALUES (7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10); 281 282 --ERROR 1832 (HY000): Cannot change column 'deptno': used in a foreign key constraint 'emp_ibfk_1' 283 alter table emp change deptno deptno char(20); 284 --ERROR 1832 (HY000): Cannot change column 'deptno': used in a foreign key constraint 'emp_ibfk_1' 285 alter table emp change deptno deptno int; 286 287 --ERROR 1832 (HY000): Cannot change column 'deptno': used in a foreign key constraint '' 288 alter table emp change deptno deptno varchar(10); 289 alter table emp change deptno deptno varchar(25); 290 desc emp; 291 select * from emp; 292 293 alter table emp change deptno deptId varchar(25); 294 desc emp; 295 select * from emp; 296 ------------------------------------------------------------------------------------------ 297 drop table if exists t1; 298 CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(10), c date, d decimal(7,2), UNIQUE KEY(a, b)); 299 desc t1; 300 301 insert into t1 values(1, 'ab', '1980-12-17', 800); 302 insert into t1 values(2, 'ac', '1981-02-20', 1600); 303 insert into t1 values(3, 'ad', '1981-02-22', 500); 304 select * from t1; 305 306 alter table t1 alter column a set default 100; 307 show index from t1; 308 desc t1; 309 select * from t1; 310 311 alter table t1 alter column a drop default; 312 show index from t1; 313 desc t1; 314 select * from t1; 315 316 alter table t1 alter column b set visible; 317 show index from t1; 318 desc t1; 319 select * from t1; 320 ------------------------------------------------------------------------------------------ 321 drop table if exists t1; 322 CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(10), c date default '1990-12-17', d decimal(7,2), UNIQUE KEY(a, b)); 323 desc t1; 324 325 insert into t1 values(1, 'ab', '1980-12-17', 800); 326 insert into t1 values(2, 'ac', '1981-02-20', 1600); 327 insert into t1 values(3, 'ad', '1981-02-22', 500); 328 select * from t1; 329 330 alter table t1 alter column c set default '2003-12-17'; 331 show index from t1; 332 desc t1; 333 select * from t1; 334 335 alter table t1 alter column c drop default; 336 show index from t1; 337 desc t1; 338 select * from t1; 339 340 alter table t1 alter column b set invisible; 341 show index from t1; 342 desc t1; 343 select * from t1; 344 ---------------------------------------------------------------------------------------- 345 drop table if exists t1; 346 CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(10), c date, d decimal(7,2), UNIQUE KEY(a, b)); 347 desc t1; 348 349 insert into t1 values(1, 'ab', '1980-12-17', 800); 350 insert into t1 values(2, 'ac', '1981-02-20', 1600); 351 insert into t1 values(3, 'ad', '1981-02-22', 500); 352 select * from t1; 353 354 alter table t1 order by a DESC, b ASC; 355 desc t1; 356 select * from t1; 357 358 alter table t1 order by a DESC, c; 359 desc t1; 360 select * from t1; 361 362 drop table t1; 363 ---------------------------------------------------------------------------------------- 364 drop table if exists t1; 365 CREATE TABLE t1(col1 int not null, col2 varchar(10)); 366 insert into t1 values (1, '137iu2'); 367 insert into t1 values (1, '73ujf34f'); 368 select * from t1; 369 370 alter table t1 change col1 col1 int primary key; 371 --ERROR 1062 (23000): Duplicate entry '1' for key 't1.PRIMARY' 372 desc t1; 373 374 alter table t1 change col2 col2 varchar(10) primary key; 375 desc t1; 376 377 insert into t1 values (1, 'cdsdsa'); 378 select * from t1; 379 drop table t1; 380 381 -- @suit 382 -- @case 383 -- @desc: alter table change column 384 -- @label:bvt 385 386 -- column name test: Chinese character 387 drop table if exists name01; 388 create table name01 (col1 int, col2 decimal); 389 insert into name01 values (1, 378292); 390 insert into name01 values (2, 3289.111111); 391 alter table name01 change column col1 `数据库管理系统数据库操作系统数据库系统数据库` int; 392 show create table name01; 393 drop table name01; 394 395 -- column name test: with `` and space 396 drop table if exists name02; 397 create table name02 (col1 int, col2 decimal); 398 insert into name02 values (1, 378292); 399 insert into name02 values (2, 3289.111111); 400 alter table name02 change column col1 `cwhueh 3u2j4kfker` int; 401 show create table name02; 402 drop table name02; 403 404 -- column name test: with `` and special character 405 drop table if exists name03; 406 create table name03 (col1 int, col2 decimal); 407 insert into name03 values (1, 378292); 408 insert into name03 values (2, 3289.111111); 409 alter table name03 change column col1 `RAVHJBWUIHNJCDW****&&*((()(*&^&^%^^&^*&` int; 410 show create table name03; 411 drop table name03; 412 413 414 -- empty table modify column 415 drop table if exists name04; 416 create table name04 (col1 int, col2 varchar(10)); 417 alter table name04 change column col1 col1New varchar(10); 418 show create table name04; 419 alter table name04 change column col2 col2New char(20); 420 show create table name04; 421 drop table name04; 422 423 424 -- Change the column name to the same as before 425 drop table if exists name05; 426 create table name05 (col1 int, col2 decimal); 427 insert into name05 values (1, 37829734); 428 insert into name05 values (2, 3289.111111); 429 select * from name05; 430 alter table name05 change column col1 col1 float; 431 show create table name05; 432 delete from name05 where col2 = 3289; 433 select * from name05; 434 alter table name05 change column col1 col1 int; 435 show create table name05; 436 drop table name05; 437 438 439 -- abnormal test: change column to bool 440 drop table if exists bool01; 441 create table bool01 (col1 int, col2 char); 442 insert into bool01 values (1, 'q'); 443 insert into bool01 values (2, 'a'); 444 insert into bool01 values (10, '3'); 445 select * from bool01; 446 alter table bool01 change col2 col2New bool after col1; 447 show create table bool01; 448 show columns from bool01; 449 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'bool01' and COLUMN_NAME not like '__mo%'; 450 drop table bool01; 451 452 453 -- cast char to varchar after col 454 drop table if exists char01; 455 create table char01 (col1 int, col2 char); 456 insert into char01 values (1, 'q'); 457 insert into char01 values (2, '*'); 458 insert into char01 values (10, '3'); 459 insert into char01 values (20, '数'); 460 select * from char01; 461 alter table char01 change col2 col2New varchar(20) after col1; 462 show create table char01; 463 insert into char01 values (100, '**(*(&(*UJHI'); 464 truncate table char01; 465 select * from char01; 466 show columns from char01; 467 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'char01' and COLUMN_NAME not like '__mo%'; 468 drop table char01; 469 470 471 -- cast varchar to char first 472 drop table if exists char02; 473 create table char02 (col1 int, col2 varchar(10)); 474 insert into char02 values (1, 'w43234rfq'); 475 insert into char02 values (2, 'a32f4'); 476 insert into char02 values (10, '3432t43r4f'); 477 select * from char02; 478 alter table char02 change col2 col2New char(20) first; 479 show create table char02; 480 insert into char02 values ('738fewhu&^YUH', 100); 481 select * from char02; 482 update char02 set col1 = 100 where col2New = 'w43234rfq'; 483 select * from char02; 484 delete from char02 where col2New = 'a32f4'; 485 select * from char02; 486 show columns from char02; 487 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'char02' and COLUMN_NAME not like '__mo%'; 488 drop table char02; 489 490 491 -- abnormal test: varchar length change to char length, and varchar length > char length 492 drop table if exists char03; 493 create table char03 (col1 int, col2 char(10)); 494 insert into char03 values (1, 'ahu323ew32'); 495 insert into char03 values (2, '367283r343'); 496 insert into char03 values (3, null); 497 select * from char03; 498 alter table char03 change col2 col2New varchar(5); 499 show create table char03; 500 show columns from char03; 501 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'char03' and COLUMN_NAME not like '__mo%'; 502 drop table char03; 503 504 505 -- abnormal test: null change to not null 506 drop table if exists char04; 507 create table char04 (col1 varchar(200), col2 char(10)); 508 insert into char04 values (null, 'ahu323ew32'); 509 insert into char04 values ('23789178942u1uj3ridjfh2d28u49u4ueji32jf2f32ef32894rjk32nv432f432f', '367283r343'); 510 insert into char04 values ('32jhbfchjecmwd%^&^(*&)UJHFRE%^T&YUHIJKNM', null); 511 select * from char04; 512 alter table char04 change col1 col1New varchar(100) not null; 513 alter table char04 change col2 col2New char(10) not null; 514 show create table char04; 515 insert into char04 values (4, 'eyuiwqewq3'); 516 select * from char04; 517 show columns from char04; 518 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'char04' and COLUMN_NAME not like '__mo%'; 519 drop table char04; 520 521 -- text, mediumtext, longtext 522 drop table if exists text01; 523 create table text01 (col1 text primary key , col2 mediumtext unique index, col3 longtext); 524 insert into text01 values ('37268434','32718hcuwh432fr234f34g4f34e4','&*&YHRE%^&*YUIHGT^&Y*UIJK'); 525 insert into text01 values ('jefwyq3uih2r321fr3', '38eu4jfc3w4e3dcewcevewcve', null); 526 insert into text01 values (null, '2789378u2uifj4234r23', '32r43frecdrfedwq'); 527 alter table text01 change col1 col1New text; 528 select * from text01; 529 show columns from text01; 530 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'text01' and COLUMN_NAME not like '__mo%'; 531 drop table text01; 532 533 534 -- continuously change column 535 drop table if exists alter03; 536 create table alter03 (col1 int, col2 binary, col3 decimal); 537 insert into alter03 values (1, 'e', 324214.2134123); 538 insert into alter03 values (2, '4', -242134.3231432); 539 select * from alter03; 540 alter table alter03 change col1 col1New decimal after col3, change col2 col2New varbinary(20); 541 show create table alter03; 542 insert into alter03 values ('32143124', 42432321.000, 132432.214234); 543 select * from alter03; 544 show columns from alter03; 545 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'alter03' and COLUMN_NAME not like '__mo%'; 546 drop table alter03; 547 548 549 -- change column auto_increment 550 drop table if exists alter04; 551 create table alter04 (col1 int not null default 100 primary key ); 552 insert into alter04 values (); 553 insert into alter04 values (101); 554 alter table alter04 change col1 col1New int auto_increment; 555 show create table alter04; 556 insert into alter04 values (); 557 insert into alter04 values (); 558 select * from alter04; 559 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'alter04' and COLUMN_NAME not like '__mo%'; 560 drop table alter04; 561 562 563 -- change auto_increment column 564 drop table if exists alter05; 565 create table alter05 (col1 int primary key auto_increment); 566 insert into alter05 values (); 567 insert into alter05 values (); 568 select * from alter05; 569 alter table alter05 change col1 col1New int unique key; 570 show create table alter05; 571 show columns from alter05; 572 select * from alter05; 573 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'alter05' and COLUMN_NAME not like '__mo%'; 574 drop table alter05; 575 576 577 -- change column to primary key column 578 drop table if exists primary01; 579 create table primary01 (col1 int, col2 text); 580 insert into primary01 values (1, 'wq432r43rf32y2493821ijfk2env3ui4y33i24'); 581 insert into primary01 values (2, '243ewfvefreverewfcwr'); 582 alter table primary01 change col1 col1New float primary key; 583 show create table primary01; 584 insert into primary01 values (1, '432r2f234day89ujfw42342'); 585 insert into primary01 values (2378.32423, '234242))())_'); 586 select * from primary01; 587 show columns from primary01; 588 update primary01 set col2 = 'whuihedjwqncew' where col1 = 1; 589 update primary01 set col2 = 'whuihedjwqncew' where col1New = 1; 590 select * from primary01; 591 delete from primary01 where col1 = 1; 592 delete from primary01 where col1New = 1; 593 select * from primary01; 594 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'primary01' and COLUMN_NAME not like '__mo%'; 595 drop table primary01; 596 597 598 -- change column to primary key, duplicate values exist in the table 599 drop table if exists primary02; 600 create table primary02(col1 int, col2 binary(10)); 601 insert into primary02 values (1, '32143'); 602 insert into primary02 values (2, '3e'); 603 select * from primary02; 604 alter table primary02 change col1 col1New int primary key; 605 show create table primary02; 606 show columns from primary02; 607 insert into primary02 (col1, col2) VALUES (3, 'ehuwu32'); 608 delete from primary02 where col1 = 1; 609 delete from primary02 where col1New = 1; 610 update primary02 set col1New = 100 where col1New = 1; 611 select * from primary02; 612 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'primary02' and COLUMN_NAME not like '__mo%'; 613 drop table primary02; 614 615 616 -- change column to primary key, no duplicate values in the table 617 drop table if exists primary03; 618 create table primary03(col1 int, col2 binary(10)); 619 insert into primary03 values (1, '32143'); 620 insert into primary03 values (2, '3e'); 621 alter table primary03 change col1 col1New int primary key; 622 show create table primary03; 623 insert into primary03 (col1New, col2) values (3, '*'); 624 insert into primary03 values (3, 'assad'); 625 update from primary03 set col2 = 'database' where col1New = 3; 626 select * from primary03; 627 show columns from primary03; 628 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'primary03' and COLUMN_NAME not like '__mo%'; 629 drop table primary03; 630 631 632 -- change column with primary key 633 drop table if exists primary04; 634 create table primary04(col1 int primary key ,col2 varbinary(20)); 635 insert into primary04 values (1, 'qfreqvreq'); 636 insert into primary04 values (2, '324543##'); 637 alter table primary04 change col1 col1New float; 638 alter table primary04 change col2 col2New varbinary(50); 639 show create table primary04; 640 insert into primary04 values (1, '324342__'); 641 insert into primary04 values (3, 'qw'); 642 delete from primary04 where col2 = 'qfreqvreq'; 643 delete from primary04 where col2New = 'qfreqvreq'; 644 update primary04 set col2New = 'ewhueifjnweknd3242e' where col1New = 1; 645 select * from primary04; 646 show columns from primary04; 647 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'primary04' and COLUMN_NAME not like '__mo%'; 648 drop table primary04; 649 650 651 -- primary key exist in the table, change another column to primary key 652 drop table if exists primary05; 653 create table primary05(col1 int primary key ,col2 varbinary(20)); 654 insert into primary05 values (1, 'qfreqvreq'); 655 insert into primary05 values (2, '324543##'); 656 alter table primary05 change col2 col2New binary(30) primary key; 657 show create table primary05; 658 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'primary05' and COLUMN_NAME not like '__mo%'; 659 drop table primary05; 660 661 662 -- multiple primary key defined 663 drop table if exists primary06; 664 create table primary06(col1 int primary key ,col2 varbinary(20)); 665 insert into primary06 values (1, 'qfreqvreq'); 666 insert into primary06 values (2, '324543##'); 667 alter table primary06 change col1 col1New int unsigned primary key; 668 show create table primary06; 669 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'primary06' and COLUMN_NAME not like '__mo%'; 670 drop table primary06; 671 672 673 -- abnormal test:joint primary key, change one of the primary column null 674 drop table if exists primary07; 675 create table primary07(col1 int ,col2 float, col3 decimal, primary key (col1, col2)); 676 insert into primary07 values (1, 213412.32143, 3214312.34243214242); 677 insert into primary07 values (2, -324.2342432423, -1243.42334234242); 678 alter table primary07 change col1 col1New double default null; 679 alter table primary07 change col2 col2New int default null; 680 show create table primary07; 681 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'primary07' and COLUMN_NAME not like '__mo%'; 682 drop table primary07; 683 684 685 -- joint primary key 686 drop table if exists primary08; 687 create table primary08(col1 int ,col2 float, col3 decimal, primary key (col1, col2)); 688 insert into primary08 values (1, 213412.32143, 3214312.34243214242); 689 insert into primary08 values (2, -324.2342432423, -1243.42334234242); 690 alter table primary08 change col1 col1New double not null; 691 alter table primary08 change col2 col2New decimal(28,10); 692 show create table primary08; 693 insert into primary08 values (3, 73829.273897298374823, 38298390.3242223); 694 update primary08 set col2 = 32432.3242424242 where col1 = 1; 695 update primary08 set col2New = 32432.3242424242 where col1New = 1; 696 delete from primary08 where col1 = 1; 697 delete from primary08 where col1New = 2; 698 select * from primary08; 699 drop table primary08; 700 701 702 -- column primary key change to default null 703 drop table if exists primary09; 704 create table primary09 (col1 int primary key, col2 decimal); 705 show create table primary09; 706 insert into primary09 values(1, 3412.324); 707 insert into primary09 values (-10, 323943.2343); 708 alter table primary09 change col1 col1New float default null; 709 drop table primary09 710 711 712 -- unique key 713 drop table if exists index01; 714 CREATE TABLE index01(a INTEGER not null , b CHAR(10), c date, d decimal(7,2), UNIQUE KEY(a, b)); 715 show create table index01; 716 insert into index01 values(1, 'ab', '1980-12-17', 800); 717 insert into index01 values(2, 'ac', '1981-02-20', 1600); 718 insert into index01 values(3, 'ad', '1981-02-22', 500); 719 select * from index01; 720 alter table index01 change b bNew VARCHAR(20); 721 show create table index01; 722 show index from index01; 723 alter table index01 change bNew bNewNew VARCHAR(20) UNIQUE KEY; 724 show index from index01; 725 show create table index01; 726 insert into index01 (a, b, c, d) values (5, 'bh', '1999-01-01', 3000); 727 insert into index01 (a, bnewNEW, c, d) values (5, 'bh', '1999-01-01', 3000); 728 select * from index01; 729 delete from index01 where b = 'ab'; 730 delete from index01 where bnewNew = 'ab'; 731 select * from index01; 732 update index01 set c = '2022-12-12' where bNewNeW = 'ac'; 733 select * from index01; 734 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'index01' and COLUMN_NAME not like '__mo%'; 735 drop table index01; 736 737 738 -- index 739 drop table if exists index02; 740 CREATE TABLE index02(a INTEGER PRIMARY KEY, b CHAR(10), c date, d decimal(7,2), INDEX(a, b), KEY(c)); 741 insert into index02 values(1, 'ab', '1980-12-17', 800); 742 insert into index02 values(2, 'ac', '1981-02-20', 1600); 743 insert into index02 values(3, 'ad', '1981-02-22', 500); 744 select * from index02; 745 alter table index02 change b bNewNew VARCHAR(20) KEY; 746 show create table index02; 747 alter table index02 change b bnewNew VARCHAR(20) UNIQUE KEY; 748 show index from index02; 749 show create table index02; 750 insert into index02 values (4, 'ab', '2000-10-10', 10000); 751 insert into index02 values (5, 'gh', '1999-12-31', 20000); 752 delete from index02 where bnewnew = 'ab'; 753 update index02 set bnewnew = 'database' where bnewnEW = 'ad'; 754 desc index02; 755 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'index02' and COLUMN_NAME not like '__mo%'; 756 select * from index02; 757 758 759 -- foreign key 760 drop table if exists foreign01; 761 create table foreign01(col1 int primary key, 762 col2 varchar(20), 763 col3 int, 764 col4 bigint); 765 drop table if exists foreign02; 766 create table foreign02(col1 int, 767 col2 int, 768 col3 int primary key, 769 constraint `c1` foreign key(col1) references foreign01(col1)); 770 show create table foreign01; 771 show create table foreign02; 772 insert into foreign01 values(1,'sfhuwe',1,1); 773 insert into foreign01 values(2,'37829901k3d',2,2); 774 insert into foreign02 values(1,1,1); 775 insert into foreign02 values(2,2,2); 776 select * from foreign01; 777 select * from foreign02; 778 alter table foreign01 change col1 col1New decimal; 779 alter table foreign02 change col1 col1New float after col3; 780 show create table foreign01; 781 show create table foreign02; 782 alter table foreign01 change col2 col2New varchar(100); 783 alter table foreign02 change col2 col2new double after col3; 784 insert into foreign01 values(3,'bcguwgheinwqneku678',2,2); 785 insert into foreign02 values(6,6,6); 786 delete from foreign01 where col2New = 'sfhuwe'; 787 delete from foreign02 where col2New = 2; 788 update foreign01 set col2 = 'database ewueh ' where col1 = 1; 789 update foreign01 set col2New = 'database ewueh ' where col1 = 1; 790 select * from foreign01; 791 select * from foreign02; 792 show create table foreign01; 793 show create table foreign02; 794 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'foreign01' and COLUMN_NAME not like '__mo%'; 795 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'foreign02' and COLUMN_NAME not like '__mo%'; 796 drop table foreign02; 797 drop table foreign01; 798 799 800 -- decimal:Conversion between high precision and low precision 801 drop table if exists decimal01; 802 create table decimal01 (col1 decimal,col2 decimal(38,10)); 803 insert into decimal01 values (23746723468723.242334243, 38208439024234.43242342423423423422342); 804 insert into decimal01 values (32487329847923424, -4324324324.3297932749028949373324242423424); 805 alter table decimal01 change column col1 col1New decimal(38,6); 806 alter table decimal01 change col2 col2New38782ufdhwg43o2uih4f32f4 decimal(38,9); 807 show create table decimal01; 808 select * from decimal01; 809 truncate table decimal01; 810 alter table decimal01 add column col3 decimal(19,0) first; 811 show create table decimal01; 812 insert into decimal01 values (37298342.123, -37829342.3244234, -283794324.2342); 813 select * from decimal01; 814 drop table decimal01; 815 816 817 -- abnormal test: change column from null to not null, null exist in the table 818 drop table if exists null01; 819 create table null01(col1 int default null, col2 binary(10)); 820 insert into null01 values (1, '32143'); 821 insert into null01 values (null, '3e'); 822 alter table null01 change col1 col1hwjefewv int not null; 823 show create table null01; 824 select * from null01; 825 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'null01' and COLUMN_NAME not like '__mo%'; 826 drop table null01; 827 828 829 -- change column from null to not null, null does not exist in the table 830 drop table if exists null02; 831 create table null02(col1 int default null, col2 varbinary(100)); 832 insert into null02 values (1, '32143'); 833 insert into null02 values (2, '3e'); 834 alter table null02 change col1 col1New int not null; 835 show create table null02; 836 insert into null02 values (null, '1'); 837 insert into null02 values (342, 'aesd'); 838 select * from null02; 839 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'null02' and COLUMN_NAME not like '__mo%'; 840 drop table null02; 841 842 843 -- cast int to float、double、decimal 844 drop table if exists cast01; 845 create table cast01 (col1 int, col2 smallint, col3 bigint unsigned, col4 tinyint unsigned); 846 insert into cast01 values (1, -32768, 12352314214243242, 0); 847 insert into cast01 values (329884234, 32767, 3828493, 21); 848 insert into cast01 values (-29302423, 32, 324242132321, 10); 849 insert into cast01 values (null, null, null, null); 850 select * from cast01; 851 alter table cast01 change col1 col1New float; 852 show create table cast01; 853 insert into cast01 values (3271.312432, null, 323254324321432, 100); 854 select * from cast01; 855 alter table cast01 change col2 col2New double first; 856 show create table cast01; 857 insert into cast01 values (3271834.2134, -3892843.214, 328943232, 255); 858 select * from cast01; 859 alter table cast01 change col3 col3New double; 860 show create table cast01; 861 insert into cast01 values (3271834.2134, -3892843.214, 328943232.3234, 255); 862 select * from cast01; 863 alter table cast01 change col4 col4New decimal(28,10) after col2New; 864 show create table cast01; 865 insert into cast01 values (3271834.2134, -3823243.4324, 328943232.3234, -32423.43243); 866 select * from cast01; 867 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'cast01' and COLUMN_NAME not like '__mo%'; 868 drop table cast01; 869 870 871 -- cast float、double、decimal to int 872 drop table if exists cast02; 873 create table cast02 (col1 float, col2 double, col3 decimal(30,5), col4 decimal(37,1)); 874 insert into cast02 values (1.321341, -32768.32142, 1235231421424.3214242134124324323, 12342.43243242121); 875 insert into cast02 values (329884234.3242, null, 3828493, 21); 876 insert into cast02 values (93024232.32324, -32.243142, 324242132321, null); 877 select * from cast02; 878 alter table cast02 change col1 col1New int unsigned; 879 show create table cast02; 880 insert into cast02 values (2724.327832, null, 32325432421432, 100.3322142142); 881 select * from cast02; 882 alter table cast02 change col2 col22361738278472874382 bigint; 883 show create table cast02; 884 insert into cast02 values (1000, 323421423421342, 328943232.321424, -255.321151234); 885 select * from cast02; 886 alter table cast02 change col3 col3vhejwh4i3uh4r bigint unsigned; 887 show create table cast02; 888 insert into cast02 values (32718, 100, 32894323237289, 234); 889 select * from cast02; 890 alter table cast02 change col4 col4cdhewjruekhwjf smallint first; 891 show create table cast02; 892 insert into cast02 values (234, 32718, 100, 32894323237289); 893 select * from cast02; 894 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'cast02' and COLUMN_NAME not like '__mo%'; 895 drop table cast02; 896 897 898 -- numeric type cast to char 899 drop table if exists cast03; 900 create table cast03 (col1 smallint unsigned, col2 float, col3 double, col4 decimal); 901 insert into cast03 values (1, 323242.34242, 23432.3242, 8329498352.32534242323432); 902 insert into cast03 values (200, -213443.321412, 32424.342424242, 0.382943424324234); 903 insert into cast03 (col1, col2, col3, col4) values (null, null, null, null); 904 alter table cast03 change col1 colNew char(50), change col2 col2New char(100), change col3 col3New varchar(50), change col4 Newdwhjvb32v varchar(15) first; 905 show create table cast03; 906 insert into cast03 values ('3243342', '3242f()', '4728947234342,', '457328990r3if943i4u9owiuo4ewfr3w4r3fre'); 907 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'cast03' and COLUMN_NAME not like '__mo%'; 908 select * from cast03; 909 drop table cast03; 910 911 912 -- converts a character type in numeric format to a numeric type 913 drop table if exists cast04; 914 create table cast04 (col1 char, col2 varchar, col3 text, col4 blob); 915 insert into cast04 values ('1', '-281321.21312', '328', '327482739.32413'); 916 insert into cast04 values ('0', '3412234321', '-332134324.2432423423423', '-1032412.4324'); 917 insert into cast04 values (null, null, null, null); 918 alter table cast04 change col1 col1hwejnejfwdewfr int unsigned; 919 alter table cast04 change col2 col237289738yiuhckehvjnkehifewjdhwenbvrferf decimal(34,4) after col4; 920 show create table cast04; 921 alter table cast04 change col3 col3njwkeyhuijre double, change col4 ewh3ui2ou3i2f4 float not null; 922 show create table cast04; 923 insert into cast04 values (); 924 select * from cast04; 925 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'cast04' and COLUMN_NAME not like '__mo%'; 926 drop table cast04; 927 928 929 -- column date change to column char and datetime 930 drop table if exists cast05; 931 create table cast05 (col1 date); 932 insert into cast05 values ('1997-01-13'); 933 insert into cast05 values ('2023-12-12'); 934 insert into cast05 values (null); 935 select * from cast05; 936 alter table cast05 change col1 ahgedbjwq varchar(100); 937 alter table cast05 change ahgedbjwq YUYHJB datetime first; 938 select * from cast05; 939 show create table cast05; 940 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'cast05' and COLUMN_NAME not like '__mo%'; 941 drop table cast05; 942 943 944 -- unsigned change to signed 945 drop table if exists abnormal02; 946 create table abnormal02 (col1 int unsigned not null, col2 smallint unsigned); 947 insert into abnormal02 values (2147483647, 255); 948 insert into abnormal02 values (3242334, 10); 949 select * from abnormal02; 950 alter table abnormal02 change col1 YYYU&*&*&&& int; 951 alter table abnormal02 change col2 ehwuh3YUUUHHHB smallint; 952 select * from abnormal02; 953 show create table abnormal02; 954 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'abnormal02' and COLUMN_NAME not like '__mo%'; 955 drop table abnormal02; 956 957 958 -- abnormal test:The data in the modified column exceeds the normal range of the column 959 drop table if exists abnormal01; 960 create table abnormal01 (col1 int, col2 bigint, col3 decimal); 961 insert into abnormal01 values (2147483647, 9223372036854775807, 3291843920.32783); 962 insert into abnormal01 values (-21843243, 832942343241999999, -2787343243.2343); 963 insert into abnormal01 values (null, null, null); 964 select * from abnormal01; 965 alter table abnormal01 change col2 col2COLLLHNNHHHH int; 966 alter table abnormal01 change col1 whu3hkjwedn&32783u2j smallint; 967 alter table abnormal01 change col3 decimal(10,0); 968 select * from abnormal01; 969 show create table abnormal01; 970 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'abnormal01' and COLUMN_NAME not like '__mo%'; 971 drop table abnormal01; 972 973 974 -- abnormal test:signed change to unsigned 975 drop table if exists abnormal02; 976 create table abnormal02 (col1 int not null, col2 smallint); 977 insert into abnormal02 values (-2147483647, 255); 978 insert into abnormal02 values (3242334, -10); 979 select * from abnormal02; 980 alter table abnormal02 change col1 col1New int unsigned; 981 alter table abnormal02 change col2 col2New smallint smallint unsigned; 982 select * from abnormal02; 983 show create table abnormal02; 984 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'abnormal02' and COLUMN_NAME not like '__mo%'; 985 drop table abnormal02; 986 987 988 -- column datetime change to varchar, abnormal test:blob column change to int 989 drop table if exists abnormal03; 990 create table abnormal03 (col1 datetime, col2 blob); 991 insert into abnormal03 values ('1997-01-13 00:00:00', '342ewfyuehcdeiuopwu4jo3lekwdfhiu48woi3jrdnefrbwui34f'); 992 insert into abnormal03 values ('2012-01-13 23:23:59', '63298ufh3jcweuiv4h32jhf432ouy4hu3enjwfnwje4n3bj24f34573'); 993 select * from abnormal03; 994 alter table abnormal03 change col1 col1time varchar(100); 995 alter table abnormal03 change col2 col2int int; 996 select * from abnormal03; 997 show create table abnormal03; 998 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'abnormal03' and COLUMN_NAME not like '__mo%'; 999 drop table abnormal03; 1000 1001 1002 -- abnormal test: column json、enum change to float,int 1003 drop table if exists abnormal04; 1004 create table abnormal04 (col1 json, col2 enum('a', 'b')); 1005 insert into abnormal04 values (('{"x": 17, "x": "red"}'),'a'); 1006 insert into abnormal04 values (('{"x": 17, "x": "red", "x": [3, 5, 7]}'), 'b'); 1007 select * from abnormal04; 1008 alter table abnormal04 change col1 col1NewCOl float; 1009 alter table abnormal04 change col2 col2NewCOL int; 1010 select * from abnormal04; 1011 show create table abnormal04; 1012 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'abnormal04' and COLUMN_NAME not like '__mo%'; 1013 drop table abnormal04; 1014 1015 1016 -- abnormal test: temporary table 1017 drop table if exists abnormal05; 1018 create temporary table abnormal05 (col1 datetime, col2 blob); 1019 insert into abnormal05 values ('1997-01-13 00:00:00', '342ewfyuehcdeiuopwu4jo3lekwdfhiu48woi3jrdnefrbwui34f'); 1020 insert into abnormal05 values ('2012-01-13 23:23:59', '63298ufh3jcweuiv4h32jhf432ouy4hu3enjwfnwje4n3bj24f34573'); 1021 select * from abnormal05; 1022 alter table abnormal05 change col1 col1_euwhnew float; 1023 alter table abnormal05 change col2 col2_njkwhew int; 1024 select * from abnormal05; 1025 show create table abnormal05; 1026 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'abnormal05' and COLUMN_NAME not like '__mo%'; 1027 drop table abnormal05; 1028 1029 1030 -- creating table, creating view, changeing the columns, view the view 1031 drop table if exists view01; 1032 drop table if exists view02; 1033 drop view v0; 1034 create table view01 (a int); 1035 insert into view01 values (1),(2); 1036 create table view02 (a int); 1037 insert into view02 values (1); 1038 create view v0 as select view01.a, view02.a as b from view01 left join view02 using(a); 1039 show create view v0; 1040 alter table view01 change a aCOL float; 1041 show create table view01; 1042 show create view v0; 1043 drop table view01; 1044 drop table view02; 1045 drop view v0; 1046 1047 1048 -- cluster by 1049 drop table if exists cluster01; 1050 create table cluster01(col1 int, col2 decimal) cluster by col1; 1051 insert into cluster01 values (1, 389234924); 1052 insert into cluster01 values (2, -2893428); 1053 alter table cluster01 change col2 col2worejnfenrororiri float; 1054 alter table cluster01 change col1 iwijwehfndatabasectm double after col2worejnfenrororiri; 1055 show create table cluster01; 1056 select * from cluster01; 1057 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'cluster01' and COLUMN_NAME not like '__mo%'; 1058 drop table cluster01; 1059 1060 1061 -- cast date, datetime, timestamp, time to int 1062 drop table if exists time01; 1063 create table time01 (col1 date, col2 datetime, col3 timestamp, col4 time); 1064 insert into time01 values ('2020-01-01', '2000-10-10 12:12:12', '1970-01-01 12:23:59.323000', '01:01:29'); 1065 insert into time01 values ('1997-01-13', null, '1989-01-01 23:23:59.100000', '23:23:59'); 1066 insert into time01 (col1, col2, col3, col4) values ('2030-12-31', '2031-09-09 01:01:01', '2013-12-12 10:10:10.125000', '10:12:12'); 1067 select * from time01; 1068 alter table time01 change col1 col1sbavhehfiwuejn int, change col2 col2chwjvhejkwbhjgeh int first, change col3 col3cvwheuhjhjk int after col1, change col4 col4ushebjfevce int; 1069 show create table time01; 1070 select * from time01; 1071 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'time01' and COLUMN_NAME not like '__mo%'; 1072 drop table time01; 1073 1074 1075 -- cast date, datetime, timestamp, time to decimal 1076 drop table if exists time02; 1077 create table time02 (col2 datetime, col3 timestamp, col4 time); 1078 insert into time02 values ('2000-10-10 12:12:12', '1970-01-01 12:23:59.323000', '01:01:29'); 1079 insert into time02 values ( null, '1889-01-01 23:23:59.125000', '23:23:59'); 1080 insert into time02 (col2, col3, col4) values ('2031-09-09 01:01:01', '2013-12-12 10:10:10.125000', '10:12:12'); 1081 select * from time02; 1082 alter table time02 change col2 decimal(20,10) first, change col3 decimal after col2, change col4 decimal(38,0); 1083 show create table time02; 1084 select * from time02; 1085 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'time02' and COLUMN_NAME not like '__mo%'; 1086 drop table time02; 1087 1088 1089 -- prepare 1090 drop table if exists prepare01; 1091 create table prepare01(col1 int, col2 char); 1092 insert into prepare01 values (1,'a'),(2,'b'),(3,'c'); 1093 prepare s1 from 'alter table prepare01 change col1 col1dheuwhvcer decimal primary key'; 1094 execute s1; 1095 show create table prepare01; 1096 prepare s2 from 'alter table prepare01 change col2 col2chuwhe varchar(100) not null'; 1097 execute s2; 1098 show create table prepare01; 1099 drop table prepare01; 1100 1101 1102 -- permission 1103 drop role if exists role_r1; 1104 drop user if exists role_u1; 1105 drop table if exists test01; 1106 create role role_r1; 1107 create user role_u1 identified by '111' default role role_r1; 1108 create table test01(col1 int); 1109 insert into test01 values(1); 1110 insert into test01 values(2); 1111 grant create database on account * to role_r1; 1112 grant show databases on account * to role_r1; 1113 grant connect on account * to role_r1; 1114 grant select on table * to role_r1; 1115 grant show tables on database * to role_r1; 1116 1117 -- @session:id=2&user=sys:role_u1:role_r1&password=111 1118 use alter_table_change_column; 1119 alter table test01 change col1 col1New int primary key; 1120 -- @session 1121 grant alter table on database * to role_r1; 1122 1123 -- @session:id=2&user=sys:role_u1:role_r1&password=111 1124 use alter_table_change_column; 1125 alter table test01 change col1 int primary key; 1126 show create table test01; 1127 -- @session 1128 show create table test01; 1129 drop table test01; 1130 drop role role_r1; 1131 drop user role_u1; 1132 1133 1134 --mixed situation :add/drop column and change column 1135 drop table if exists mix01; 1136 create table mix01 (col1 int not null , col2 decimal, col3 date, col4 varchar(100)); 1137 insert into mix01 values (1, 23849234.324, '2100-01-01', 'qy83uhfbh234y78y&*%^&%$$$E%^&Y*UIJNHBGVFTY^&Y*UJHBGVTF^&*U(OK'); 1138 insert into mix01 values (2, 773892.32748000000000, '1997-01-13', '38782yhbf3uhy4iendb32gefdc7y834uh2neujdr2h4f3v43'); 1139 insert into mix01 values (3, -82913942.3434, null, null); 1140 select * from mix01; 1141 alter table mix01 add column col1_2 binary after col1; 1142 show create table mix01; 1143 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'mix01' and COLUMN_NAME not like '__mo%'; 1144 alter table mix01 change column col1_2 col1_2New varbinary(10) first; 1145 show create table mix01; 1146 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'mix01' and COLUMN_NAME not like '__mo%'; 1147 truncate table mix01; 1148 alter table mix01 add column col5 int; 1149 show create table mix01; 1150 select table_name,COLUMN_NAME, data_type,is_nullable from information_schema.columns where table_name like 'mix01' and COLUMN_NAME not like '__mo%'; 1151 drop table mix01; 1152 1153 -- begin, alter table change column, commit, then select 1154 drop table if exists table01; 1155 begin; 1156 create table table01(col1 int, col2 decimal); 1157 insert into table01 values(100,200); 1158 alter table table01 change column col1 NewCol1 float; 1159 commit; 1160 select * from table01; 1161 select newcol1 from table01; 1162 drop table table01; 1163 1164 -- alter table modify column of varchar to enum 1165 drop table if exists t1; 1166 create table t1(name varchar(25)); 1167 insert into t1 values ('A'),('B'),('C'); 1168 select * from t1; 1169 alter table t1 modify column name enum('A','B'); 1170 alter table t1 modify column name enum('A','B','C'); 1171 alter table t1 modify column name enum('A','B','C','D'), add column age int; 1172 select * from t1; 1173 insert into t1 values('D', 29); 1174 show create table t1; 1175 desc t1; 1176 drop table t1; 1177 1178 drop database db2;