使用模拟数据填充联结表

时间:2017-04-04 07:43:02

标签: sql sql-server ssms

我想填写这两个表的联结表:

Create table [dbo].[Event]
(
    [id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
    [naam] varchar(30) NOT NULL,
    [periode] date NOT NULL,
    [maxbezoeker] int NOT NULL,
    [locatie] varchar(50) NOT NULL
)

Create table [dbo].[Medewerker]
(
    [id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
    [wachtwoord] varchar(100) NOT NULL,
    [rechten] varchar(100) NOT NULL,
    [gebruikersnaam] varchar(30) UNIQUE NOT NULL
)

接合表:

Create table [dbo].[Event_Medewerker]
(
    [event_id] int FOREIGN KEY REFERENCES [dbo].[Event](id) NOT NULL,
    [medewerker_id] int FOREIGN KEY REFERENCES [dbo].[Medewerker](id) NOT NULL
)

Event表和Medewerker表当前有1000个模拟条目。我想链接它们之间的数据,但除了生成1000个随机数之外,我无法想到我将如何做到这一点。但是,如果我这样做,我觉得我最终会得到一大堆未链接的条目。有没有更好的方法用模拟数据填充连接表?

A" Medewerker"是一名员工和"事件"在这种情况下将是一个音乐节。我希望每个活动都与Medewerkers相关联。

我使用SQL Server 2014 Management Studio和mockaroo.com来生成数据。

4 个答案:

答案 0 :(得分:2)

因此,您可以根据每个表中的顺序ID值简单地加入您的2个表,因为这是模拟数据。然后,您可以使用连接的结果插入到链接表中。这是一个简单的示例,您可以单独运行以进行测试:

CREATE TABLE #event ( id INT );

CREATE TABLE #medwerker ( id INT );

CREATE TABLE #event_medwerker
    (
      event_id INT ,
      medwerker_id INT
    );

INSERT  INTO #event
        ( id )
VALUES  ( 1 ), ( 2 ), ( 3 );

SELECT * FROM #event;

INSERT  INTO #medwerker
        ( id )
VALUES  ( 1 ), ( 2 ), ( 3 );

SELECT * FROM #medwerker;

INSERT  INTO #event_medwerker
        ( event_id ,
          medwerker_id
        )
        SELECT  e.id ,
                m.id
        FROM    #event e
                INNER JOIN #medwerker m ON m.id = e.id;

SELECT * FROM #event_medwerker;

DROP TABLE #event_medwerker;
DROP TABLE #event;
DROP TABLE #medwerker;

答案 1 :(得分:0)

“Medewerker”是一名员工,本案中的“事件”将是一个音乐节。我希望每个活动都与Medewerkers相关联。

在这种情况下,您可以使用此查询

DECLARE @Event AS TABLE(id int)

DECLARE @Medewerker AS TABLE(id int)

DECLARE @Event_Medewerker AS TABLE (EventId int, MedewerkerId int)

INSERT INTO @Event
VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9),(10)

INSERT INTO @Medewerker
VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9),(10)

INSERT INTO  @Event_Medewerker (EventId, MedewerkerId)
SELECT e.id AS EventID, md.id AS MedewerkerId
FROM 

(SELECT * FROM @Event t) e 

CROSS APPLY

(
SELECT TOP (ABS(Checksum(NewID()) % 10) + 2) * FROM @Medewerker t ORDER BY newid()  -- random 2 - 12 item bat ki o bang @Medewerker
) md

ORDER BY e.id, md.id

SELECT * FROM @Event_Medewerker em

答案 2 :(得分:0)

我会使用临时表来填充所有可能的组合,包括随机排序编号。然后迭代所有事件,每次确定随机数量并使用随机排序编号从临时表中插入以确定随机数量。

这样的事情:

BEGIN
    -- temp table
    DECLARE @dummy_table table(Event_id int, Medewerker_id int, Order_nr int)

    -- insert all possible combinations with a random order nr
    insert into @dummy_table(Event_id, Medewerker_id, Order_nr)
    select
        ROW_NUMBER() OVER(PARTITION BY e.id ORDER BY newid() ASC),  -- use newid() to randomize order
        e.id,
        m.id
    from
        Event e
        join Medewerker m -- cross join

    -- cursor for all events
    DECLARE cur_evts CURSOR FOR SELECT Id FROM Event; 
    DECLARE @Event_id int
    DECLARE @random_amount int

    OPEN cur_evts;
    FETCH NEXT FROM cur_evts INTO @Event_id;

    -- iterate all events
    WHILE @@FETCH_STATUS = 0  
    BEGIN
        -- random amount between 50..500
        SET @random_amount = 50 + (450 * RAND(convert(varbinary, newid())))

        insert into Event_Medewerker(event_id, medewerker_id)
        select
            event_id,
            medewerker_id
        from
            @dummy_table
        where
            Event_id = @Event_id
            and order_nr < @random_amount

        FETCH NEXT FROM cur_evts INTO @Event_id;
    END
    CLOSE cur_evts;
    DEALLOCATE cur_evts;
END

答案 3 :(得分:0)

包含数据的示例模式

IF OBJECT_ID('dbo.Event') IS NOT NULL
Drop table [Event]
Create table [dbo].[Event]
(
    [id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
    [naam] varchar(30) NOT NULL,
    [periode] date NOT NULL,
    [maxbezoeker] int NOT NULL,
    [locatie] varchar(50) NOT NULL
)

IF OBJECT_ID('dbo.Medewerker') IS NOT NULL
Drop table [Medewerker]
Create table [dbo].[Medewerker]
(
    [id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
    [wachtwoord] varchar(100) NOT NULL,
    [rechten] varchar(100) NOT NULL,
    [gebruikersnaam] varchar(30) UNIQUE NOT NULL
)


 IF OBJECT_ID('dbo.Event_Medewerker') IS NOT NULL
  Drop table [Event_Medewerker]
Create table [dbo].[Event_Medewerker]
(
    [event_id] int FOREIGN KEY REFERENCES [dbo].[Event](id) NOT NULL,
    [medewerker_id] int FOREIGN KEY REFERENCES [dbo].[Medewerker](id) NOT NULL
)

INSERT INTO [Event]
(
 naam
,periode
,maxbezoeker
,locatie
)
SELECT 'Harry','2017-04-04',77,'dsghvgj'Union All
SELECT 'Harry','2017-04-04',77,'dsghvgj'Union All
SELECT 'Harry','2017-04-04',77,'dsghvgj'Union All
SELECT 'Harry','2017-04-04',77,'dsghvgj'

INSERT INTO [Medewerker]
(
wachtwoord
,rechten
,gebruikersnaam
)

SELECT 'Gills','three month','ggg' Union All
SELECT 'Gills','three month','ANbc' Union All
SELECT 'Gills','three month','ANbaac' Union All
SELECT 'Gills','three month','ooooANbc' 


SELECT * FROM [Event]
SELECT * FROM [Medewerker]
SELECT * FROM [Event_Medewerker] 

将数据插入桥表代码

DECLARE @Event INT
    ,@Medewerker INT
    ,@RecordCount INT
    ,@minid INT = 1
    ,@maxid INT

SELECT @RecordCount = COUNT(1) FROM [Event] inner join [Medewerker] on [Event].id=[Medewerker].id

SET @maxid = @RecordCount

    WHILE (@minid <= @maxid)
    BEGIN
        SELECT @Event =  id FROM [Event]
        WHERE NOT EXISTS (
                SELECT 1
                FROM [Event_Medewerker] Em
                WHERE em.event_id = [Event].id
                )
            AND id = @minid

        SELECT @Medewerker =  id FROM [Medewerker]
        WHERE NOT EXISTS (
                SELECT 1
                FROM [Event_Medewerker] Em
                WHERE em.event_id = [Medewerker].id
                )
            AND id = @minid

        INSERT INTO [Event_Medewerker]
         (
            event_id
            ,medewerker_id
            )
        SELECT @Event,@Medewerker

        SET @minid = @minid + 1
    END