问题
I've been working with sql databases just for a little time but I'm impressed of the powerful possibilities that I discovered so far.
Now I want to do something that sounds so easy but I was not able to find an easy way to solve it:
Just create a new table with let's say 500000 rows and get them numbered automatically (ID).
Using PHP it's probably a simple loop but I wonder if it is really necesary to use PHP.
I am almost sure there is a simple solution using a mysql command or even with phpMyAdmin.
回答1:
Join any two tables without condition and the resulting number of rows will be multiplied. Increase the id on every select from that result using a local variable.
This creates about 1M rows (2^20);
set @i = 0;
drop TEMPORARY table if exists dummyids;
create TEMPORARY table dummyids
select @i := @i + 1 as id
from (select true union all select true) t0
join (select true union all select true) t1
join (select true union all select true) t2
join (select true union all select true) t3
join (select true union all select true) t4
join (select true union all select true) t5
join (select true union all select true) t6
join (select true union all select true) t7
join (select true union all select true) t8
join (select true union all select true) t9
join (select true union all select true) t10
join (select true union all select true) t11
join (select true union all select true) t12
join (select true union all select true) t13
join (select true union all select true) t14
join (select true union all select true) t15
join (select true union all select true) t16
join (select true union all select true) t17
join (select true union all select true) t18
join (select true union all select true) t19
;
select * from dummyids;
回答2:
Yes, you can use only mysql
CREATE PROCEDURE insertProcedure()
BEGIN
DECLARE i int DEFAULT 0;
WHILE i <= 500000 DO
INSERT INTO table_name (id, col1, col2) VALUES (i, null, null);
SET i = i + 1;
END WHILE;
END
Then cal it like:
CALL insertProcedure();
Of course, you need to adapt the insert line to your case (table_name, number of columns, values).
If id
is set to autoincrement, then you can replace the first i
with null
and let mysql use it's calculated value for it.
Note: to test it, use a much lower limit (say 500). Inserting 0.5 million rows will take some time, especially on a normal PC
来源:https://stackoverflow.com/questions/33719622/mysql-is-ther-an-easy-way-to-create-a-table-with-500000-dummy-zero-empty-rows