2017-07-15 12 views
0

実際には私はうなずき、1週間この問題に固執しました。私はそれを説明しようとします。 私はUSERのテーブル、 と製品のテーブルを持っています すべての製品のすべてのユーザーのデータを保存したいと思います。 if_product_bought、num_of_items、およびallと同様です。データベース内のデータベースの作成方法(postgres)

私はデータベース内のデータベースを考えることができます。つまり、データベースという名前のユーザーの中に製品のコピーを作成して保存を開始します。

事前

答えて

1

で おかげで、これはどのように可能であるか、他のよりよい解決策がある場合は、PostgreSQLを使用するときは、実際にdatabase(またはtabletable)内databaseを作成しないんか他のSQL RDBMS

tablesJOINを使用します。通常、テーブルとテーブルitems_x_ordersが、usersitemsの上にあります。これは、すべてのRelational Modelいわゆるに基づいています

CREATE TABLE users 
(
    user_id INTEGER /* SERIAL */ NOT NULL PRIMARY KEY, 
    user_name text 
) ; 

CREATE TABLE items 
(
    item_id INTEGER /* SERIAL */ NOT NULL PRIMARY KEY, 
    item_description text NOT NULL, 
    item_unit text NOT NULL, 
    item_standard_price decimal(10,2) NOT NULL 
) ; 

CREATE TABLE orders 
(
    order_id INTEGER /* SERIAL */ NOT NULL PRIMARY KEY, 
    user_id INTEGER NOT NULL REFERENCES users(user_id), 
    order_date DATE NOT NULL DEFAULT now(), 
    other_data TEXT 
) ; 

CREATE TABLE items_x_orders 
(
    order_id INTEGER NOT NULL REFERENCES orders(order_id), 
    item_id INTEGER NOT NULL REFERENCES items(item_id), 

    -- You're supposed not to have the item more than once in an order 
    -- This makes the following the "natural key" for this table 
    PRIMARY KEY (order_id, item_id), 

    item_quantity DECIMAL(10,2) NOT NULL CHECK(item_quantity <> /* > */ 0), 
    item_percent_discount DECIMAL(5,2) NOT NULL DEFAULT 0.0, 
    other_data TEXT 
) ; 

は、これは非常に単純化したシナリオです。あなたが考えていたのは、、またはNoSQLデータベース(JSONまたはXMLの階層構造としてデータを保存する)で使用されているdocument modelです。

あなたのようなデータと、それらのテーブルを埋めます:

INSERT INTO users 
    (user_id, user_name) 
VALUES 
    (1, 'Alice Cooper') ; 

INSERT INTO items 
    (item_id, item_description, item_unit, item_standard_price) 
VALUES 
    (1, 'Oranges', 'kg', 0.75), 
    (2, 'Cookies', 'box', 1.25), 
    (3, 'Milk', '1l carton', 0.90) ; 

INSERT INTO orders 
    (order_id, user_id) 
VALUES 
    (100, 1) ; 

INSERT INTO items_x_orders 
    (order_id, item_id, item_quantity, item_percent_discount, other_data) 
VALUES 
    (100, 1, 2.5, 0.00, NULL), 
    (100, 2, 3.0, 0.00, 'I don''t want Oreo'), 
    (100, 3, 1.0, 0.05, 'Make it promo milk') ; 

そして、あなたは、次のいずれかのようなクエリを生成するどこJOIN関連するすべてのテーブル:

SELECT 
    user_name, item_description, item_quantity, item_unit, 
    item_standard_price, item_percent_discount, 
    CAST(item_quantity * (item_standard_price * (1-item_percent_discount/100.0)) AS DECIMAL(10,2)) AS items_price 
FROM 
    items_x_orders 
    JOIN orders USING (order_id) 
    JOIN items USING (item_id) 
    JOIN users USING (user_id) ; 

...と取得これらの結果:

 
user_name | item_description | item_quantity | item_unit | item_standard_price | item_percent_discount | items_price 
:----------- | :--------------- | ------------: | :-------- | ------------------: | --------------------: | ----------: 
Alice Cooper | Oranges   |   2.50 | kg  |    0.75 |     0.00 |  1.88 
Alice Cooper | Cookies   |   3.00 | box  |    1.25 |     0.00 |  3.75 
Alice Cooper | Milk    |   1.00 | 1l carton |    0.90 |     5.00 |  0.86 

すべてのコードとテストはdbfiddle here