summaryrefslogtreecommitdiffstats
path: root/mysql-test/suite/innodb/t/innodb-alter-autoinc.test
blob: e01c44aa26e6bc8651914958800d59ce0ed7df3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
--source include/have_innodb.inc

CREATE TABLE t1 (a INT) ENGINE=InnoDB;
INSERT INTO t1 VALUES(347),(33101),(123),(45),(6);
# NULL -> NOT NULL only allowed INPLACE if strict sql_mode is on.
SET @old_sql_mode = @@sql_mode;
SET @@sql_mode = 'STRICT_TRANS_TABLES';
ALTER TABLE t1 ADD PRIMARY KEY(a);
SET @@sql_mode = @old_sql_mode;

# We cannot assign AUTO_INCREMENT values during online index creation.
--error ER_ALTER_OPERATION_NOT_SUPPORTED_REASON
ALTER TABLE t1 DROP PRIMARY KEY, ADD id INT AUTO_INCREMENT PRIMARY KEY,
	LOCK=NONE;

--error ER_WRONG_AUTO_KEY
ALTER TABLE t1 ADD id INT AUTO_INCREMENT;

--error ER_WRONG_AUTO_KEY
ALTER TABLE t1 ADD id INT AUTO_INCREMENT, ADD INDEX(a, id);

ALTER TABLE t1 ADD id INT NOT NULL, ADD INDEX(id, a);

SELECT * FROM t1;

# Test with a non-default increment and offset
SET AUTO_INCREMENT_INCREMENT = 5, AUTO_INCREMENT_OFFSET = 30;

ALTER TABLE t1 DROP PRIMARY KEY, ADD id INT AUTO_INCREMENT PRIMARY KEY,
	DROP COLUMN id, AUTO_INCREMENT = 42, ALGORITHM=COPY;

SHOW CREATE TABLE t1;

# The autoinc next value should increase. It is not rolled back.
BEGIN;
INSERT INTO t1 VALUES(7,0);
SELECT * FROM t1;
ROLLBACK;

SHOW CREATE TABLE t1;

# We cannot assign AUTO_INCREMENT values during online index creation.
--error ER_ALTER_OPERATION_NOT_SUPPORTED_REASON
ALTER TABLE t1 DROP PRIMARY KEY, ADD id INT AUTO_INCREMENT PRIMARY KEY,
	DROP COLUMN id, AUTO_INCREMENT = 42, LOCK=NONE;

ALTER TABLE t1 DROP PRIMARY KEY, ADD id INT AUTO_INCREMENT PRIMARY KEY,
	DROP COLUMN id, AUTO_INCREMENT = 42, ALGORITHM=INPLACE;

SELECT * FROM t1;
SHOW CREATE TABLE t1;

INSERT INTO t1 SET a=123;
INSERT INTO t1 VALUES(-123,-45);

ALTER TABLE t1 AUTO_INCREMENT = 75;

INSERT INTO t1 SET a=123;
SELECT * FROM t1;

SHOW CREATE TABLE t1;

DROP TABLE t1;

# ALGORITHM=INPLACE should deliver identical results to ALGORITHM=COPY.
CREATE TABLE t1 (a INT) ENGINE=InnoDB;
INSERT INTO t1 VALUES(347),(33101),(123),(45),(6);
ALTER TABLE t1 ADD PRIMARY KEY(a);

ALTER TABLE t1 ADD id INT NOT NULL, ADD INDEX(id, a);

SELECT * FROM t1;

ALTER TABLE t1 DROP PRIMARY KEY, ADD id INT AUTO_INCREMENT PRIMARY KEY,
	DROP COLUMN id, AUTO_INCREMENT = 42, ALGORITHM=INPLACE;

SHOW CREATE TABLE t1;

# The autoinc next value should increase. It is not rolled back.
BEGIN;
INSERT INTO t1 VALUES(7,0);
SELECT * FROM t1;
ROLLBACK;

SHOW CREATE TABLE t1;

ALTER TABLE t1 DROP PRIMARY KEY, ADD id INT AUTO_INCREMENT PRIMARY KEY,
	DROP COLUMN id, AUTO_INCREMENT = 42, ALGORITHM=COPY;

SELECT * FROM t1;

SHOW CREATE TABLE t1;

INSERT INTO t1 SET a=123;
INSERT INTO t1 VALUES(-123,-45);

ALTER TABLE t1 AUTO_INCREMENT = 75;

INSERT INTO t1 SET a=123;
SELECT * FROM t1;

SHOW CREATE TABLE t1;

DROP TABLE t1;