在MySQL中创build一个“数字表”

我试图在mysql中生成一个连续数字的大表。 我只想要2列; 一个主键和一个范围为0-X的数字列,其中X非常大。 约。 64000行应该这样做。 我试过这个代码没有成功:

CREATE TABLE numbers ( number INT NOT NULL CONSTRAINT XPKnumbers PRIMARY KEY CLUSTERED (number) ) INSERT INTO numbers (number) VALUES (0) DECLARE @i INT SET @i = 20 WHILE 0 < @i BEGIN INSERT INTO numbers (number) SELECT number + (SELECT 1 + Max(number) FROM numbers) FROM numbers SET @i = @i - 1 END SELECT * FROM numbers 

我得到这个错误:

#1064 – 你的SQL语法错误; 检查与您的MySQL服务器版本相对应的手册,在“CONSTRAINT XPKnumbers PRIMARY KEY CLUSTERED(number)”附近使用正确的语法)在第3行INSERT INTO n'

任何人有任何build议,使这项工作?

你缺less分号,逗号,甚至在修正语法之后,从表中selectmax也不是一个好主意,每次只需要在循环中插入一行。

http://use-the-index-luke.com/blog/2011-07-30/mysql-row-generator中删除并使用生成器:;

 CREATE OR REPLACE VIEW generator_16 AS SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15; CREATE OR REPLACE VIEW generator_256 AS SELECT ( ( hi.n << 4 ) | lo.n ) AS n FROM generator_16 lo, generator_16 hi; CREATE OR REPLACE VIEW generator_4k AS SELECT ( ( hi.n << 8 ) | lo.n ) AS n FROM generator_256 lo, generator_16 hi; CREATE OR REPLACE VIEW generator_64k AS SELECT ( ( hi.n << 8 ) | lo.n ) AS n FROM generator_256 lo, generator_256 hi; CREATE OR REPLACE VIEW generator_1m AS SELECT ( ( hi.n << 16 ) | lo.n ) AS n FROM generator_64k lo, generator_16 hi; 

如果出于任何原因,你真的需要一个数字表格:

 INSERT INTO numbers(number) SELECT n FROM generator_64k WHERE n < 64000 

您在列和约束声明之间缺less逗号:

 CREATE TABLE numbers ( number INT NOT NULL, CONSTRAINT XPKnumbers PRIMARY KEY CLUSTERED (number) )