sql >> データベース >  >> RDS >> Mysql

SQL結果テーブル、2番目のテーブルで一致SETタイプ

    ジャンクション/インターセクトテーブルの使用例。

    create table subscription_plans
    (
        id int not null auto_increment primary key, -- common practice
        name varchar(40) not null,
        description varchar(255) not null,
        price decimal(12,2) not null
        -- additional indexes:
    );
    
    create table pricing_offers
    (
        id int not null auto_increment primary key, -- common practice
        name varchar(40) not null,
        description varchar(255) not null
        -- additional indexes:
    );
    
    create table so_junction
    (   -- intersects mapping subscription_plans and pricing_offers
        id int not null auto_increment primary key, -- common practice
        subId int not null,
        offerId int not null,
    
        -- row cannot be inserted/updated if subId does not exist in parent table
        -- the fk name is completely made up
        -- parent row cannot be deleted and thus orphaning children
        CONSTRAINT fk_soj_subplans 
            FOREIGN KEY (subId)
            REFERENCES subscription_plans(id),
    
        -- row cannot be inserted/updated if offerId does not exist in parent table
        -- the fk name is completely made up
        -- parent row cannot be deleted and thus orphaning children
        CONSTRAINT fk_soj_priceoffer 
            FOREIGN KEY (offerId)
            REFERENCES pricing_offers(id),
    
        -- the below allows for only ONE combo of subId,offerId
        CONSTRAINT soj_unique_ids unique (subId,offerId)
        -- additional indexes:
    );
    
    insert into subscription_plans (name,description,price) values ('plan_A','description',9.99);
    insert into subscription_plans (name,description,price) values ('plan_B','description',19.99);
    insert into subscription_plans (name,description,price) values ('plan_C','description',29.99);
    select * from subscription_plans;
    
    insert into pricing_offers (name,description) values ('free donuts','you get free donuts, limit 3');
    insert into pricing_offers (name,description) values ('extra sauce','extra sauce');
    insert into pricing_offers (name,description) values ('poney ride','Free ride on Wilbur');
    insert into pricing_offers (name,description) values ('bus fare -50%','domestic less 50');
    
    select * from pricing_offers;
    
    insert so_junction(subId,offerId) values (1,1); -- free donuts to plans
    insert so_junction(subId,offerId) values (1,2),(2,2),(3,2); -- extra sauce to plans
    insert so_junction(subId,offerId) values (3,3); -- wilbur
    insert so_junction(subId,offerId) values (1,4),(2,4),(3,4); -- bus to plans
    select * from so_junction;
    
    -- try to add another of like above to so_junction
    -- Error Code 1062: Duplicate entry
    
    -- show joins of all
    select s.*,p.*
    from subscription_plans s
    join so_junction so
    on so.subId=s.id
    join pricing_offers p
    on p.id=so.offerId
    order by s.name,p.name
    
    -- show extra sauce intersects
    select s.*,p.*
    from subscription_plans s
    join so_junction so
    on so.subId=s.id
    join pricing_offers p
    on p.id=so.offerId
    where p.name='extra sauce'
    order by s.name,p.name
    

    基本的に、ジャンクションテーブルに挿入したり、ジャンクションテーブルから削除したりします(この例では、実際に更新することはできません)。

    インデックスのない遅くて扱いにくいセットを台無しにすることなく、クリーンで高速な結合

    もう誰もウィルバー・ザ・ポニーに乗ることはできませんか?次に

    delete from so_junction
    where offerId in (select id from pricing_offers where name='poney ride')
    

    質問があるかどうか尋ねてください。

    そして頑張ってください!




    1. Linq to SqlでSqlGeographyを使用することは可能ですか?

    2. PostgreSQLでのSin()のしくみ

    3. トランザクションログファットのトリミング

    4. postgreSQLmysqloracleの違い