insert_select.test
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:9k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. #
  2. # Problem with INSERT ... SELECT
  3. #
  4. --disable_warnings
  5. drop table if exists t1,t2,t3;
  6. --enable_warnings
  7. create table t1 (bandID MEDIUMINT UNSIGNED NOT NULL PRIMARY KEY, payoutID SMALLINT UNSIGNED NOT NULL);
  8. insert into t1 (bandID,payoutID) VALUES (1,6),(2,6),(3,4),(4,9),(5,10),(6,1),(7,12),(8,12);
  9. create table t2 (payoutID SMALLINT UNSIGNED NOT NULL PRIMARY KEY);
  10. insert into t2 (payoutID) SELECT DISTINCT payoutID FROM t1;
  11. --error 1062
  12. insert into t2 (payoutID) SELECT payoutID+10 FROM t1;
  13. insert ignore into t2 (payoutID) SELECT payoutID+10 FROM t1;
  14. select * from t2;
  15. drop table t1,t2;
  16. #
  17. # bug in bulk insert optimization
  18. # test case by Fournier Jocelyn <joc@presence-pc.com>
  19. #
  20. CREATE TABLE `t1` (
  21.   `numeropost` bigint(20) unsigned NOT NULL default '0',
  22.   `icone` tinyint(4) unsigned NOT NULL default '0',
  23.   `numreponse` bigint(20) unsigned NOT NULL auto_increment,
  24.   `contenu` text NOT NULL,
  25.   `pseudo` varchar(50) NOT NULL default '',
  26.   `date` datetime NOT NULL default '0000-00-00 00:00:00',
  27.   `ip` bigint(11) NOT NULL default '0',
  28.   `signature` tinyint(1) unsigned NOT NULL default '0',
  29.   PRIMARY KEY  (`numeropost`,`numreponse`)
  30.   ,KEY `ip` (`ip`),
  31.   KEY `date` (`date`),
  32.   KEY `pseudo` (`pseudo`),
  33.   KEY `numreponse` (`numreponse`)
  34. ) ENGINE=MyISAM;
  35. CREATE TABLE `t2` (
  36.   `numeropost` bigint(20) unsigned NOT NULL default '0',
  37.   `icone` tinyint(4) unsigned NOT NULL default '0',
  38.   `numreponse` bigint(20) unsigned NOT NULL auto_increment,
  39.   `contenu` text NOT NULL,
  40.   `pseudo` varchar(50) NOT NULL default '',
  41.   `date` datetime NOT NULL default '0000-00-00 00:00:00',
  42.   `ip` bigint(11) NOT NULL default '0',
  43.   `signature` tinyint(1) unsigned NOT NULL default '0',
  44.   PRIMARY KEY  (`numeropost`,`numreponse`),
  45.   KEY `ip` (`ip`),
  46.   KEY `date` (`date`),
  47.   KEY `pseudo` (`pseudo`),
  48.   KEY `numreponse` (`numreponse`)
  49. ) ENGINE=MyISAM;
  50. INSERT INTO t2
  51. (numeropost,icone,numreponse,contenu,pseudo,date,ip,signature) VALUES
  52. (9,1,56,'test','joce','2001-07-25 13:50:53'
  53. ,3649052399,0);
  54. INSERT INTO t1 (numeropost,icone,contenu,pseudo,date,signature,ip)
  55. SELECT 1618,icone,contenu,pseudo,date,signature,ip FROM t2
  56. WHERE numeropost=9 ORDER BY numreponse ASC;
  57. show variables like '%bulk%';
  58. INSERT INTO t1 (numeropost,icone,contenu,pseudo,date,signature,ip)
  59. SELECT 1718,icone,contenu,pseudo,date,signature,ip FROM t2
  60. WHERE numeropost=9 ORDER BY numreponse ASC;
  61. DROP TABLE t1,t2;
  62. #
  63. # Test of insert ... select from same table
  64. #
  65. create table t1 (a int not null);
  66. create table t2 (a int not null);
  67. insert into t1 values (1);
  68. insert into t1 values (a+2);
  69. insert into t1 values (a+3);
  70. insert into t1 values (4),(a+5);
  71. insert into t1 select * from t1;
  72. select * from t1;
  73. insert into t1 select * from t1 as t2;
  74. select * from t1;
  75. insert into t2 select * from t1 as t2;
  76. select * from t1;
  77. insert into t1 select t2.a from t1,t2;
  78. select * from t1;
  79. --error 1066
  80. insert into t1 select * from t1,t1;
  81. drop table t1,t2;
  82. #
  83. # test replace ... select
  84. #
  85. create table t1 (a int not null primary key, b char(10));
  86. create table t2 (a int not null, b char(10));
  87. insert into t1 values (1,"t1:1"),(3,"t1:3");
  88. insert into t2 values (2,"t2:2"), (3,"t2:3");
  89. --error 1062
  90. insert into t1 select * from t2;
  91. select * from t1;
  92. # REPLACE .. SELECT is not yet supported by PS
  93. --disable_ps_protocol
  94. replace into t1 select * from t2;
  95. --enable_ps_protocol
  96. select * from t1;
  97. drop table t1,t2;
  98. #
  99. # Test that caused uninitialized memory access in auto_increment_key update
  100. #
  101. CREATE TABLE t1 ( USID INTEGER UNSIGNED, ServerID TINYINT UNSIGNED, State ENUM ('unknown', 'Access-Granted', 'Session-Active', 'Session-Closed' ) NOT NULL DEFAULT 'unknown', SessionID CHAR(32), User CHAR(32) NOT NULL DEFAULT '<UNKNOWN>', NASAddr INTEGER UNSIGNED, NASPort INTEGER UNSIGNED, NASPortType INTEGER UNSIGNED, ConnectSpeed INTEGER UNSIGNED, CarrierType CHAR(32), CallingStationID CHAR(32), CalledStationID CHAR(32), AssignedAddr INTEGER UNSIGNED, SessionTime INTEGER UNSIGNED, PacketsIn INTEGER UNSIGNED, OctetsIn INTEGER UNSIGNED, PacketsOut INTEGER UNSIGNED, OctetsOut INTEGER UNSIGNED, TerminateCause INTEGER UNSIGNED, UnauthTime TINYINT UNSIGNED, AccessRequestTime DATETIME, AcctStartTime DATETIME, AcctLastTime DATETIME, LastModification TIMESTAMP NOT NULL);
  102. CREATE TABLE t2 ( USID INTEGER UNSIGNED AUTO_INCREMENT, ServerID TINYINT UNSIGNED, State ENUM ('unknown', 'Access-Granted', 'Session-Active', 'Session-Closed' ) NOT NULL DEFAULT 'unknown', SessionID CHAR(32), User TEXT NOT NULL, NASAddr INTEGER UNSIGNED, NASPort INTEGER UNSIGNED, NASPortType INTEGER UNSIGNED, ConnectSpeed INTEGER UNSIGNED, CarrierType CHAR(32), CallingStationID CHAR(32), CalledStationID CHAR(32), AssignedAddr INTEGER UNSIGNED, SessionTime INTEGER UNSIGNED, PacketsIn INTEGER UNSIGNED, OctetsIn INTEGER UNSIGNED, PacketsOut INTEGER UNSIGNED, OctetsOut INTEGER UNSIGNED, TerminateCause INTEGER UNSIGNED, UnauthTime TINYINT UNSIGNED, AccessRequestTime DATETIME, AcctStartTime DATETIME, AcctLastTime DATETIME, LastModification TIMESTAMP NOT NULL, INDEX(USID,ServerID,NASAddr,SessionID), INDEX(AssignedAddr));
  103. INSERT INTO t1 VALUES (39,42,'Access-Granted','46','491721000045',2130706433,17690,NULL,NULL,'Localnet','491721000045','49172200000',754974766,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2003-07-18 00:11:21',NULL,NULL,20030718001121);
  104. INSERT INTO t2 SELECT USID, ServerID, State, SessionID, User, NASAddr, NASPort, NASPortType, ConnectSpeed, CarrierType, CallingStationID, CalledStationID, AssignedAddr, SessionTime, PacketsIn, OctetsIn, PacketsOut, OctetsOut, TerminateCause, UnauthTime, AccessRequestTime, AcctStartTime, AcctLastTime, LastModification from t1 LIMIT 1;
  105. drop table t1,t2;
  106. #
  107. # Another problem from Bug #2012
  108. #
  109. CREATE TABLE t1(
  110.  Month date NOT NULL,
  111.  Type tinyint(3) unsigned NOT NULL auto_increment,
  112.  Field int(10) unsigned NOT NULL,
  113.  Count int(10) unsigned NOT NULL,
  114.  UNIQUE KEY Month (Month,Type,Field)
  115. );
  116.   
  117. insert into t1 Values
  118. (20030901, 1, 1, 100),
  119. (20030901, 1, 2, 100),
  120. (20030901, 2, 1, 100),
  121. (20030901, 2, 2, 100),
  122. (20030901, 3, 1, 100);
  123. select * from t1;
  124.   
  125. Select null, Field, Count From t1 Where Month=20030901 and Type=2;
  126.   
  127. create table t2(No int not null, Field int not null, Count int not null);
  128.   
  129. insert into t2 Select null, Field, Count From t1 Where Month=20030901 and Type=2;
  130.   
  131. select * from t2;
  132. drop table t1, t2;
  133. #
  134. # BUG#6034 - Error code 124:  Wrong medium type
  135. #
  136. CREATE TABLE t1 (
  137.   ID           int(11) NOT NULL auto_increment,
  138.   NO           int(11) NOT NULL default '0',
  139.   SEQ          int(11) NOT NULL default '0',
  140.   PRIMARY KEY  (ID),
  141.   KEY t1$NO    (SEQ,NO)
  142. ) ENGINE=MyISAM;
  143. INSERT INTO t1 (SEQ, NO) SELECT "1" AS SEQ, IF(MAX(NO) IS NULL, 0, MAX(NO)) + 1 AS NO FROM t1 WHERE (SEQ = 1);
  144. select SQL_BUFFER_RESULT * from t1 WHERE (SEQ = 1);
  145. drop table t1;
  146. #
  147. # Bug#10886 - Have to restore default values after update ON DUPLICATE KEY
  148. #
  149. create table t1 (f1 int);
  150. create table t2 (ff1 int unique, ff2 int default 1);
  151. insert into t1 values (1),(1),(2);
  152. insert into t2(ff1) select f1 from t1 on duplicate key update ff2=ff2+1;
  153. select * from t2;
  154. drop table t1, t2;
  155. #
  156. # BUGS #9728 - 'Decreased functionality in "on duplicate key update"' 
  157. #      #8147 - 'a column proclaimed ambigous in INSERT ... SELECT .. ON
  158. #      DUPLICATE'
  159. #
  160. create table t1 (a int unique);
  161. create table t2 (a int, b int);
  162. create table t3 (c int, d int);
  163. insert into t1 values (1),(2);
  164. insert into t2 values (1,2);
  165. insert into t3 values (1,6),(3,7);
  166. select * from t1;
  167. insert into t1 select a from t2 on duplicate key update a= t1.a + t2.b;
  168. select * from t1;
  169. insert into t1 select a+1 from t2 on duplicate key update t1.a= t1.a + t2.b+1;
  170. select * from t1;
  171. insert into t1 select t3.c from t3 on duplicate key update a= a + t3.d;
  172. select * from t1;
  173. insert into t1 select t2.a from t2 group by t2.a on duplicate key update a= a + 10;
  174. #Some error cases
  175. --error 1052
  176. insert into t1 select t2.a from t2 on duplicate key update a= a + t2.b;
  177. --error 1109
  178. insert into t1 select t2.a from t2 on duplicate key update t2.a= a + t2.b;
  179. --error 1109
  180. insert into t1 select t2.a from t2 group by t2.a on duplicate key update a= t1.a + t2.b;
  181. drop table t1,t2,t3;
  182. #
  183. # Bug #12695 Item_func_isnull::update_used_tables() did not update 
  184. #            const_item_cache
  185. create table t1(f1 varchar(5) key);
  186. insert into t1(f1) select if(max(f1) is null, '2000',max(f1)+1) from t1;
  187. insert into t1(f1) select if(max(f1) is null, '2000',max(f1)+1) from t1;
  188. insert into t1(f1) select if(max(f1) is null, '2000',max(f1)+1) from t1;
  189. select * from t1;
  190. drop table t1;
  191. #
  192. # Bug #13392 values() fails with 'ambiguous' or returns NULL 
  193. #            with ON DUPLICATE and SELECT
  194. create table t1(x int, y int);
  195. create table t2(x int, z int);
  196. insert into t1(x,y) select x,z from t2 on duplicate key update x=values(x);
  197. --error 1054
  198. insert into t1(x,y) select x,z from t2 on duplicate key update x=values(z);
  199. --error 1109
  200. insert into t1(x,y) select x,z from t2 on duplicate key update x=values(t2.x);
  201. drop table t1,t2; 
  202. # End of 4.1 tests