MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
is_key_column_usage.inc
1 # suite/funcs_1/datadict/is_key_column_usage.inc
2 #
3 # Check the layout of information_schema.key_column_usage and the impact of
4 # CREATE/ALTER/DROP TABLE/VIEW/SCHEMA/COLUMN ... on it.
5 #
6 # Note:
7 # This test is not intended
8 # - to show information about the all time existing tables
9 # within the databases information_schema and mysql
10 # - for checking storage engine properties
11 # Therefore please do not alter $engine_type.
12 #
13 # Author:
14 # 2008-01-23 mleich WL#4203 Reorganize and fix the data dictionary tests of
15 # testsuite funcs_1
16 # Create this script based on older scripts and new code.
17 # Last Change:
18 # 2008-06-11 mleich Move t/is_key_column_usage.test to this file and
19 # create variants for embedded/non embedded server.
20 #
21 
22 let $engine_type = MEMORY;
23 
24 let $is_table = KEY_COLUMN_USAGE;
25 
26 # The table INFORMATION_SCHEMA.KEY_COLUMN_USAGE must exist
27 eval SHOW TABLES FROM information_schema LIKE '$is_table';
28 
29 --echo #######################################################################
30 --echo # Testcase 3.2.1.1: INFORMATION_SCHEMA tables can be queried via SELECT
31 --echo #######################################################################
32 # Ensure that every INFORMATION_SCHEMA table can be queried with a SELECT
33 # statement, just as if it were an ordinary user-defined table.
34 #
35 --source suite/funcs_1/datadict/is_table_query.inc
36 
37 
38 --echo #########################################################################
39 --echo # Testcase 3.2.7.1: INFORMATION_SCHEMA.KEY_COLUMN_USAGE layout
40 --echo #########################################################################
41 # Ensure that the INFORMATION_SCHEMA.KEY_COLUMN_USAGE table has the following
42 # columns, in the following order:
43 #
44 # CONSTRAINT_CATALOG (always shows NULL),
45 # CONSTRAINT_SCHEMA (shows the database, or schema, in which an accessible
46 # constraint, or index, resides),
47 # CONSTRAINT_NAME (shows the name of the accessible constraint),
48 # TABLE_CATALOG (always shows NULL),
49 # TABLE_SCHEMA (shows the database, or schema, in which the table constrained
50 # by that constraint resides),
51 # TABLE_NAME (shows the name of the table constrained by the constraint),
52 # COLUMN_NAME (shows the name of a column that is the index key, or part of
53 # the index key),
54 # ORDINAL_POSITION (shows the ordinal position of the column within the
55 # constraint index),
56 # POSITION_IN_UNIQUE_CONSTRAINT (shows, for a foreign key column, the ordinal
57 # position of the referenced column within the referenced unique index;
58 # otherwise NULL).
59 # added with 5.0.6:
60 # REFERENCED_TABLE_SCHEMA,
61 # REFERENCED_TABLE_NAME,
62 # REFERENCED_COLUMN_NAME
63 #
64 --source suite/funcs_1/datadict/datadict_bug_12777.inc
65 eval DESCRIBE information_schema.$is_table;
66 --source suite/funcs_1/datadict/datadict_bug_12777.inc
67 eval SHOW CREATE TABLE information_schema.$is_table;
68 --source suite/funcs_1/datadict/datadict_bug_12777.inc
69 eval SHOW COLUMNS FROM information_schema.$is_table;
70 
71 # Note: Retrieval of information within information_schema.columns about
72 # information_schema.key_column_usage is in is_columns_is.test.
73 
74 # Show that CONSTRAINT_CATALOG and TABLE_CATALOG are always NULL.
75 SELECT constraint_catalog, constraint_schema, constraint_name, table_catalog,
76  table_schema, table_name, column_name
77 FROM information_schema.key_column_usage
78 WHERE constraint_catalog IS NOT NULL OR table_catalog IS NOT NULL;
79 
80 
81 --echo ########################################################################################
82 --echo # Testcase 3.2.7.2 + 3.2.7.3: INFORMATION_SCHEMA.KEY_COLUMN_USAGE accessible information
83 --echo ########################################################################################
84 # 3.2.7.2: Ensure that the table shows the relevant information on every column, defined to
85 # be part of an index key, which is accessible to the current user or to PUBLIC.
86 # 3.2.7.3: Ensure that the table does not show any information on any indexed column that is
87 # not accessible to the current user or PUBLIC.
88 #
89 --disable_warnings
90 DROP DATABASE IF EXISTS db_datadict;
91 --enable_warnings
92 CREATE DATABASE db_datadict;
93 
94 --error 0,ER_CANNOT_USER
95 DROP USER 'testuser1'@'localhost';
96 CREATE USER 'testuser1'@'localhost';
97 --error 0,ER_CANNOT_USER
98 DROP USER 'testuser2'@'localhost';
99 CREATE USER 'testuser2'@'localhost';
100 
101 USE db_datadict;
102 
103 --replace_result $engine_type <engine_type>
104 eval
105 CREATE TABLE t1_1
106  (f1 INT NOT NULL, PRIMARY KEY(f1),
107  f2 INT, INDEX f2_ind(f2))
108 ENGINE = $engine_type;
109 GRANT SELECT ON t1_1 to 'testuser1'@'localhost';
110 
111 --replace_result $engine_type <engine_type>
112 eval
113 CREATE TABLE t1_2
114  (f1 INT NOT NULL, PRIMARY KEY(f1),
115  f2 INT, INDEX f2_ind(f2))
116 ENGINE = $engine_type;
117 GRANT SELECT ON t1_2 to 'testuser2'@'localhost';
118 #FIXME: add foreign keys
119 
120 let $select= SELECT * FROM information_schema.key_column_usage
121 WHERE table_name LIKE 't1_%'
122 ORDER BY constraint_catalog, constraint_schema, constraint_name,
123  table_catalog, table_schema, table_name, ordinal_position;
124 
125 # show view of user root
126 eval $select;
127 
128 --echo # Establish connection testuser1 (user=testuser1)
129 --replace_result $MASTER_MYPORT MYSQL_PORT $MASTER_MYSOCK MYSQL_SOCK
130 connect (testuser1, localhost, testuser1, , db_datadict);
131 eval $select;
132 
133 --echo # Establish connection testuser2 (user=testuser2)
134 --replace_result $MASTER_MYPORT MYSQL_PORT $MASTER_MYSOCK MYSQL_SOCK
135 connect (testuser2, localhost, testuser2, , db_datadict);
136 eval $select;
137 
138 # Cleanup
139 --echo # Switch to connection default and close connections testuser1, testuser2
140 connection default;
141 disconnect testuser1;
142 disconnect testuser2;
143 DROP USER 'testuser1'@'localhost';
144 DROP USER 'testuser2'@'localhost';
145 DROP TABLE t1_1;
146 DROP TABLE t1_2;
147 DROP DATABASE IF EXISTS db_datadict;
148 
149 
150 --echo ########################################################################################
151 --echo # Testcase 3.2.1.13+3.2.1.14+3.2.1.15: INFORMATION_SCHEMA.KEY_COLUMN_USAGE modifications
152 --echo ########################################################################################
153 # 3.2.1.13: Ensure that the creation of any new database object (e.g. table or
154 # column) automatically inserts all relevant information on that
155 # object into every appropriate INFORMATION_SCHEMA table.
156 # 3.2.1.14: Ensure that the alteration of any existing database object
157 # automatically updates all relevant information on that object in
158 # every appropriate INFORMATION_SCHEMA table.
159 # 3.2.1.15: Ensure that the dropping of any existing database object
160 # automatically deletes all relevant information on that object from
161 # every appropriate INFORMATION_SCHEMA table.
162 #
163 --disable_warnings
164 DROP DATABASE IF EXISTS db_datadict;
165 DROP TABLE IF EXISTS test.t1_my_table;
166 --enable_warnings
167 CREATE DATABASE db_datadict;
168 
169 SELECT table_name FROM information_schema.key_column_usage
170 WHERE table_name LIKE 't1_my_table%';
171 --replace_result $engine_type <engine_type>
172 eval
173 CREATE TABLE test.t1_my_table
174  (f1 CHAR(12), f2 TIMESTAMP, f4 BIGINT, PRIMARY KEY(f1,f2))
175 DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci
176 ENGINE = $engine_type;
177 # Settings used in CREATE TABLE must be visible
178 # in information_schema.key_column_usage.
179 --vertical_results
180 SELECT * FROM information_schema.key_column_usage
181 WHERE table_name = 't1_my_table';
182 --horizontal_results
183 #
184 # Check modification of TABLE_NAME
185 SELECT DISTINCT table_name FROM information_schema.key_column_usage
186 WHERE table_name LIKE 't1_my_table%';
187 RENAME TABLE test.t1_my_table TO test.t1_my_tablex;
188 SELECT DISTINCT table_name FROM information_schema.key_column_usage
189 WHERE table_name LIKE 't1_my_table%';
190 #
191 # Check modification of TABLE_SCHEMA
192 SELECT DISTINCT table_schema,table_name FROM information_schema.key_column_usage
193 WHERE table_name = 't1_my_tablex';
194 RENAME TABLE test.t1_my_tablex TO db_datadict.t1_my_tablex;
195 SELECT DISTINCT table_schema,table_name FROM information_schema.key_column_usage
196 WHERE table_name = 't1_my_tablex';
197 #
198 # Check modification of COLUMN_NAME
199 SELECT DISTINCT table_name, column_name FROM information_schema.key_column_usage
200 WHERE table_name = 't1_my_tablex'
201 ORDER BY table_name, column_name;
202 ALTER TABLE db_datadict.t1_my_tablex CHANGE COLUMN f1 first_col CHAR(12);
203 SELECT DISTINCT table_name, column_name FROM information_schema.key_column_usage
204 WHERE table_name = 't1_my_tablex'
205 ORDER BY table_name, column_name;
206 #
207 # Note: The size of the column list and the not very selective qualification
208 # is intended. I want to see that the schema names are equal and
209 # all records about 't1_my_tablex'.
210 let $my_select = SELECT constraint_schema, constraint_name, table_schema,
211 table_name, column_name, ordinal_position
212 FROM information_schema.key_column_usage
213 WHERE table_name = 't1_my_tablex'
214 ORDER BY constraint_schema, constraint_name, table_schema,
215  table_name, ordinal_position;
216 #
217 # Check ADD INDEX being not UNIQUE (does not show up in key_column_usage)
218 eval $my_select;
219 CREATE INDEX f2 ON db_datadict.t1_my_tablex(f2);
220 eval $my_select;
221 DROP INDEX f2 ON db_datadict.t1_my_tablex;
222 #
223 # Check ADD UNIQUE INDEX without name explicit assigned
224 eval $my_select;
225 ALTER TABLE db_datadict.t1_my_tablex ADD UNIQUE (f2);
226 eval $my_select;
227 DROP INDEX f2 ON db_datadict.t1_my_tablex;
228 #
229 # Check ADD UNIQUE INDEX with name explicit assigned
230 eval $my_select;
231 ALTER TABLE db_datadict.t1_my_tablex ADD UNIQUE my_idx (f2);
232 eval $my_select;
233 DROP INDEX my_idx ON db_datadict.t1_my_tablex;
234 eval $my_select;
235 ALTER TABLE db_datadict.t1_my_tablex ADD UNIQUE my_idx (f4,first_col);
236 eval $my_select;
237 #
238 # Check DROP COLUMN
239 eval $my_select;
240 ALTER TABLE db_datadict.t1_my_tablex
241 DROP COLUMN first_col;
242 eval $my_select;
243 #
244 # Check impact of DROP TABLE
245 SELECT table_name, column_name
246 FROM information_schema.key_column_usage
247 WHERE table_name = 't1_my_tablex'
248 ORDER BY table_name, column_name;
249 DROP TABLE db_datadict.t1_my_tablex;
250 SELECT table_name, column_name
251 FROM information_schema.key_column_usage
252 WHERE table_name = 't1_my_tablex';
253 #
254 # No UNIQUE CONSTRAINT -> no entry in key_column_usage
255 SELECT table_name FROM information_schema.key_column_usage
256 WHERE table_name = 't1_my_tablex';
257 --replace_result $engine_type <engine_type>
258 eval
259 CREATE TABLE db_datadict.t1_my_tablex
260 ENGINE = $engine_type AS
261 SELECT 1 AS f1;
262 SELECT table_name FROM information_schema.key_column_usage
263 WHERE table_name = 't1_my_tablex';
264 # UNIQUE CONSTRAINT -> entry in key_column_usage
265 ALTER TABLE db_datadict.t1_my_tablex ADD PRIMARY KEY(f1);
266 SELECT table_name FROM information_schema.key_column_usage
267 WHERE table_name = 't1_my_tablex';
268 #
269 # Check impact of DROP SCHEMA
270 SELECT table_name FROM information_schema.key_column_usage
271 WHERE table_name = 't1_my_tablex';
272 DROP DATABASE db_datadict;
273 SELECT table_name FROM information_schema.key_column_usage
274 WHERE table_name = 't1_my_tablex';
275 #
276 
277 
278 --echo ########################################################################
279 --echo # Testcases 3.2.1.3-3.2.1.5 + 3.2.1.8-3.2.1.12: INSERT/UPDATE/DELETE and
280 --echo # DDL on INFORMATION_SCHEMA table are not supported
281 --echo ########################################################################
282 # 3.2.1.3: Ensure that no user may execute an INSERT statement on any
283 # INFORMATION_SCHEMA table.
284 # 3.2.1.4: Ensure that no user may execute an UPDATE statement on any
285 # INFORMATION_SCHEMA table.
286 # 3.2.1.5: Ensure that no user may execute a DELETE statement on any
287 # INFORMATION_SCHEMA table.
288 # 3.2.1.8: Ensure that no user may create an index on an
289 # INFORMATION_SCHEMA table.
290 # 3.2.1.9: Ensure that no user may alter the definition of an
291 # INFORMATION_SCHEMA table.
292 # 3.2.1.10: Ensure that no user may drop an INFORMATION_SCHEMA table.
293 # 3.2.1.11: Ensure that no user may move an INFORMATION_SCHEMA table to any
294 # other database.
295 # 3.2.1.12: Ensure that no user may directly add to, alter, or delete any data
296 # in an INFORMATION_SCHEMA table.
297 #
298 --disable_warnings
299 DROP DATABASE IF EXISTS db_datadict;
300 DROP TABLE IF EXISTS db_datadict.t1;
301 --enable_warnings
302 CREATE DATABASE db_datadict;
303 --replace_result $engine_type <engine_type>
304 eval
305 CREATE TABLE db_datadict.t1 (f1 BIGINT)
306 ENGINE = $engine_type;
307 
308 # Note(mleich):
309 # 1. We can get here different error messages.
310 # 2. We do not want to unify the individual messages to the far to unspecific
311 # 'Got one of the listed errors'.
312 let $my_error_message =
313 ##### The previous statement must fail ######
314 # Server type | expected error name | expected error message
315 # --------------------------------------------------------------------------------------------------------------------
316 # not embedded | ER_DBACCESS_DENIED_ERROR | ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema'
317 # embedded | ER_NON_INSERTABLE_TABLE | ERROR HY000: The target table schemata of the INSERT is not insertable-into
318 # | or similar | or similar
319 ;
320 
321 --disable_abort_on_error
322 INSERT INTO information_schema.key_column_usage
323  (constraint_schema, constraint_name, table_name)
324 VALUES ( 'mysql', 'primary', 'db');
325 if (!$mysql_errno)
326 {
327  --echo $my_error_message
328  exit;
329 }
330 --error ER_DBACCESS_DENIED_ERROR
331 INSERT INTO information_schema.key_column_usage
332 SELECT * FROM information_schema.key_column_usage;
333 
334 --error ER_DBACCESS_DENIED_ERROR
335 UPDATE information_schema.key_column_usage
336 SET table_name = 'db1' WHERE constraint_name = 'primary';
337 
338 --error ER_DBACCESS_DENIED_ERROR
339 DELETE FROM information_schema.key_column_usage WHERE table_name = 't1';
340 --error ER_DBACCESS_DENIED_ERROR
341 TRUNCATE information_schema.key_column_usage;
342 
343 --error ER_DBACCESS_DENIED_ERROR
344 CREATE INDEX i3 ON information_schema.key_column_usage(table_name);
345 
346 --error ER_DBACCESS_DENIED_ERROR
347 ALTER TABLE information_schema.key_column_usage ADD f1 INT;
348 
349 --error ER_DBACCESS_DENIED_ERROR
350 DROP TABLE information_schema.key_column_usage;
351 
352 --error ER_DBACCESS_DENIED_ERROR
353 ALTER TABLE information_schema.key_column_usage
354 RENAME db_datadict.key_column_usage;
355 --error ER_DBACCESS_DENIED_ERROR
356 ALTER TABLE information_schema.key_column_usage
357 RENAME information_schema.xkey_column_usage;
358 --enable_abort_on_error
359 
360 # Cleanup
361 DROP TABLE db_datadict.t1;
362 DROP DATABASE db_datadict;