T-SQL:插入随机值链接

时间:2016-01-05 12:00:17

标签: sql sql-server tsql

早上好,

我创建了一个包含6个表的测试数据库,我希望在我的“主表”Car_Spec中将它们全部链接在一起。

我希望我的表'Car_Spec'根据'Model'字段中的内容自动填充'Manufacutre_ID'字段。

即。如果Model_ID是104(熊猫),那么我希望manufacturer_ID为102(菲亚特)。

我刚刚使用以下代码创建了数据库和表:

CREATE TABLE car_manufacture
(
    manufacture_ID int NOT NULL IDENTITY(100,1),
    manufacture_Name varchar(50) NOT NULL,
    manufacture_Country varchar(50),
    PRIMARY KEY(manufacture_ID)
)

CREATE TABLE car_model
(
    model_ID int NOT NULL IDENTITY(100,1),
    manufacture_ID INT,
    model_name varchar(50) NOT NULL,
    PRIMARY KEY (model_ID),
    FOREIGN KEY (manufacture_ID) REFERENCES car_manufacture (manufacture_ID),
)

CREATE TABLE car_drive
(
    drive_ID int NOT NULL,
    drive_side varchar(10)
    PRIMARY KEY (drive_ID)
)

CREATE TABLE car_litre
(
    litre_ID int NOT NULL IDENTITY(100,1),
    litre int,
    PRIMARY KEY (litre_ID)
)

CREATE TABLE car_fueltype
(
    fuel_ID INT NOT NULL,
    fuel VARCHAR(20),
    PRIMARY KEY (fuel_ID)
)

CREATE TABLE car_spec
(
    car_ID INT NOT NULL IDENTITY (100,1),
    manufacture_ID INT NOT NULL,
    model_ID INT NOT NULL,
    drive_ID INT NOT NULL DEFAULT '3',
    fuel_ID INT NOT NULL DEFAULT '4',
    age INT,
    miles INT,
    registration VARCHAR (10),
    previous_owners INT,
    PRIMARY KEY (car_ID),

    FOREIGN KEY (manufacture_ID) REFERENCES car_manufacture (manufacture_ID),
    FOREIGN KEY (model_ID) REFERENCES car_model(model_ID),
    FOREIGN KEY (drive_ID) REFERENCES car_drive (drive_ID),
    FOREIGN KEY (fuel_ID) REFERENCES car_fueltype(fuel_ID)
)

我使用以下代码填充了除最后一个表之外的所有表:

INSERT INTO car_manufacture (manufacture_Name, manufacture_Country) VALUES 

    ('Volkswagen',  'Germany'),
    ('Fiat',        'Italy'),
    ('Seat',        'Spain'),
    ('Ford',        'American')

INSERT INTO car_model (manufacture_ID,model_name)VALUES

    ((Select manufacture_ID from car_manufacture where manufacture_Name = 'Volkswagen'),'Polo'),
    ((Select manufacture_ID from car_manufacture where manufacture_Name = 'Volkswagen'),'Golf'),
    ((Select manufacture_ID from car_manufacture where manufacture_Name = 'Fiat'),'Panda'), 
    ((Select manufacture_ID from car_manufacture where manufacture_Name = 'Fiat'),'Punto'),
    ((Select manufacture_ID from car_manufacture where manufacture_Name = 'Seat'),'Ibiza'),
    ((Select manufacture_ID from car_manufacture where manufacture_Name = 'Seat'),'Leon'),
    ((Select manufacture_ID from car_manufacture where manufacture_Name = 'Fiat'),'500'),
    ((Select manufacture_ID from car_manufacture where manufacture_Name = 'Volkswagen'),'Passat')

INSERT INTO car_drive (drive_ID, drive_side)VALUES
    (1, 'Left'),
    (2, 'Right'),
    (3, 'Unknown')

INSERT INTO car_litre (litre) Values
    (1),
    (1.2),
    (1.4),
    (1.6),
    (1.8),
    (2)

INSERT INTO car_fueltype (fuel_id,fuel) values
(1,'Petrol)'), 
(2,'Diesel'),
(3,'Gas'), 
(4,'Unknown')

这是我被困的地方......

Insert into car_spec (age, car_ID, drive_ID, fuel_ID, manufacture_ID, miles, model_ID, previous_owners, registration)
Values

    (
        3, 
        (select TOP 1 model_ID from car_model ORDER BY NEWID()), 
        (select TOP 1 drive_ID from car_drive ORDER BY NEWID()),
        (select TOP 1 fuel from car_fueltype ORDER BY NEWID()),
        **THIS IS WHERE I WANT A STATEMENT 'Select manufacture_ID from car_manufacutre based on model_ID,', **

车牌ID =型号ID。

任何建议都会很棒,我考虑过使用变量,但是再一次,不确定它是如何工作的。我认为InnerJoin会解决这个问题,但我无法解决这个问题。

由于 萨姆

1 个答案:

答案 0 :(得分:0)

使用insert . . . select。然后您可以更好地控制数据。查询将如下所示:

Insert into car_spec(age, car_ID, drive_ID, fuel_ID, manufacture_ID, 
                     miles, model_ID, previous_owners, registration
                    )
    select t.age, t.model_id, t.drive_id, d.fuel_id, cm.manufacturer_id, 
           . . .
    from (select 3 as age, 
                 (select TOP 1 model_ID from car_model ORDER BY NEWID()) as model_id,
                 (select TOP 1 drive_ID from car_drive ORDER BY NEWID()) as drive_id,
                 (select TOP 1 fuel from car_fueltype ORDER BY NEWID()) as fuel_id
         ) t join
         car_model cm 
         on t.model_id = cm.model_id;