如何正确地生成关于主键和外键的大型随机sql数据?

vs91vp4v  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(255)

最近,我有一项任务,就是生成基于这个e-r图的大型测试数据库:https://i.stack.imgur.com/i2kr9.png
我需要有超过300000行(加起来),很容易生成表,供应商和产品通过excel使用其随机函数,但我不知道如何正确地创建订单和orderitem表,因为我需要从多行(匹配ID)乘以每个单价和数量,以获得位于另一个表中的totalamount,当然每个pk和fk需要完全匹配。
我知道这是一个假问题,但任何小提示都会有帮助,无论我是否需要通过sql脚本、excel或任何其他方式直接创建。
提前谢谢!

cs7cruho

cs7cruho1#

:)查看示例northwind数据库。里面已经有那些table了。字段名和计数不匹配,但易于编辑。
您所需要的只是使用简单的插入来扩展行。ie:客户数量加倍:

insert into customers (FirstName, LastName, City, Country)
select FirstName+'2', LastName+'2', City, Country
from Customers;

30万行一点都不大,实际上很小。
ps:我以为您会将客户的id更改为int-identity列。在northwind中是字符数据。
编辑:代码我答应过。我知道这很难看:

-- Create a tally table
DECLARE @tally TABLE (n INT);

INSERT INTO @tally (n)
SELECT TOP(300000) ROW_NUMBER() OVER (ORDER BY t1.Object_ID)
            FROM Master.sys.All_Columns t1
            CROSS JOIN Master.sys.All_Columns t2;

-- 300K in total. But let's say we want 300K order items
-- Aprx. 1000 customers, each with 50 orders * 6 items per order 
create table Customers (Id int identity primary key, 
    FirstName varchar(15), 
    LastName varchar(15), 
    City varchar(15), 
    Country varchar(15), 
    Phone varchar(15) );

create table Orders (Id int identity primary key, 
    OrderDate datetime, 
    OrderNumber int, 
    CustomerId int foreign key references Customers(Id), 
    TotalAmount money null);

create table Suppliers (id int identity primary key, 
    CompanyName varchar(15), 
    ContactName varchar(15), 
    ContactTitle varchar(15), 
    City varchar(15), 
    Country varchar(15), 
    Phone varchar(15), 
    Fax varchar(15));

create table Products (Id int identity primary key, 
    ProductName varchar(50), 
    SupplierId int foreign key references Suppliers(Id), 
    UnitPrice money, 
    Package varchar(20),
    IsDiscontinued bit);

create table OrderItems (Id int identity primary key, 
    OrderId int foreign key references Orders(Id),
    ProductId int foreign key references Products(Id),
    UnitPrice money,
    Quantity int);

INSERT INTO Customers
(
    FirstName,
    LastName,
    City,
    Country,
    Phone
)
SELECT top 1000 'FirstName'+CAST(n AS VARCHAR(6)),
    'LastName'+CAST(n AS VARCHAR(6)),
    'City'+CAST(n%10 AS VARCHAR(6)),
    'Country'+CAST(n%100 AS VARCHAR(6)),
    'Phone'+cast(n as varchar(6))
from @tally;

insert into Orders (OrderDate, OrderNumber, CustomerId)
select t.d, t.n, c.Id
from customers c
cross apply (select top(50) n, dateadd(day, -n, getdate()) from @tally) t(n, d);

insert into Suppliers (CompanyName, 
    ContactName, 
    ContactTitle, 
    City, 
    Country, 
    Phone, 
    Fax)
SELECT top 10 'Company'+CAST(n AS VARCHAR(6)),
    'Contact'+CAST(n AS VARCHAR(6)),
    'Title'+CAST(n AS VARCHAR(6)),
    'City'+CAST(n%10 AS VARCHAR(6)),
    'Country'+CAST(n%100 AS VARCHAR(6)),
    'Phone'+cast(n as varchar(6)),
    'Fax'+cast(n as varchar(6))
from @tally;

with ts(n, sId) as (
select t.n, s.Id
from
(SELECT top(500) n from @tally) t, Suppliers s
)
insert into Products (ProductName, 
    SupplierId, 
    UnitPrice, 
    Package,
    IsDiscontinued)
SELECT top(5000) 'Product'+CAST(n AS VARCHAR(6)),
sId,
n * 10,
'Package'+CAST(n%5 AS VARCHAR(6)),
case when n%1500 = 0 then 1 else 0 end
from ts order by newid();

with pdata (oid, pid) aS (
select top(300*1000) 
 abs(cast(checksum(newid()) as bigint)) % 50000 + 1,
 abs(cast(checksum(newid()) as bigint)) % 5000 + 1
from @tally
order by newId())
insert into OrderItems
    (OrderId,
    ProductId,
    UnitPrice,
    Quantity)
select  d.oid, d.pid, p.UnitPrice, abs(cast(checksum(newid()) as bigint)) % 20 + 1 
from pData d inner join Products p on d.pid = p.id
order by d.oid, d.pid;

相关问题