How can I populate my database's all tables with random data?

馋奶兔 提交于 2019-12-12 10:55:16

问题


Is there such a software out there? It doesn't matter if the data itself make sense. I am just worried about the fields to get populated. Basically, it will read the table definitions and generate some data accordingly. It would also be great if it asks for how many rows to insert per table, whether the default values will be left blank or get populated, how to treat the varchars(to the full extent or up to a given,specified number of characters). Ideally free :) but commercial product suggestions are also highly welcome. Thank you all.


回答1:


I see there is still no answer that has been chosen as the best. Besides the solutions already mentioned in this thread, I can suggest using ApexSQL Generate – a SQL data generator tool, which has the ability to quickly generate millions of rows, using various data sources, with a variety of data generators.

It features a fully functional free trial, so you can download it and test to see if it will get your job done.




回答2:


Hey SQL Data Generator provides realistic test data you can download this from below website http://www.red-gate.com/products/sql-development/sql-data-generator/




回答3:


Try using Visual studio for this as shown in this article

http://www.mssqltips.com/sqlservertip/2190/generating-sql-server-test-data-with-visual-studio-2010/




回答4:


There is a huge number of tools for test data generation for SQL Server. So you don't have to do this manually by building your own logic somehow. Please check the Test Data Generator section at http://www.kodyaz.com/articles/tools-sql-server-tool-list.aspx




回答5:


For mysql databases you can use filldb.info.

It automatically generate data and fill your database tables with this dummy data




回答6:


In case someone is looking for a simple way to do this without extra software.

We just need a function that returns a random number:

-- Create the RANDBETWEEN function
-- Usage: SELECT dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID())))
CREATE FUNCTION dbo.RANDBETWEEN(@minval TINYINT, @maxval TINYINT, @random NUMERIC(18,10))
RETURNS TINYINT
AS
BEGIN
  RETURN (SELECT CAST(((@maxval + 1) - @minval) * @random + @minval AS TINYINT))
END
GO

And then you can run a query like this:

-- Insert 1 million records into the Person table
INSERT INTO Person (FirstName,LastName,CityId)
SELECT TOP 1000000
    CASE
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 0 THEN 'John'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 1 THEN 'Jack'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 2 THEN 'Bill'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 3 THEN 'Mary'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 4 THEN 'Kate'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 5 THEN 'Matt'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 6 THEN 'Rachel'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 7 THEN 'Tom'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 8 THEN 'Ann'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 9 THEN 'Andrew'
    ELSE 'Bob' END AS FirstName,
    CASE
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 0 THEN 'Smith'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 1 THEN 'Morgan'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 2 THEN 'Simpson'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 3 THEN 'Walker'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 4 THEN 'Bauer'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 5 THEN 'Taylor'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 6 THEN 'Morris'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 7 THEN 'Elliot'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 8 THEN 'White'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 9 THEN 'Davis'
    ELSE 'Brown' END AS LastName,
    dbo.RANDBETWEEN(1,15,RAND(CHECKSUM(NEWID()))) as CityId
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
GO

Result:

Adjust the query accordingly to match your table/column names.




回答7:


I created a project with java in netbeans IDE This project allow you to populate your tables with random data. It has some issues so I'm working on to solve it.



来源:https://stackoverflow.com/questions/15608438/how-can-i-populate-my-databases-all-tables-with-random-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!