【问题标题】:Running database creation script with constraints运行带约束的数据库创建脚本
【发布时间】:2022-01-11 06:54:02
【问题描述】:

我创建了一个表架构,但我不知道在这种情况下我应该如何运行脚本,因为我对需要创建其他表的每个表都有约束,是否有任何方法可以在创建后添加约束或在脚本中保持正确的表架构相等的其他方法?

我使用 PostgreSQL 作为数据库。

CREATE TABLE IF NOT EXISTS store (
    id INTEGER NOT NULL,
    nome VARCHAR(255) NOT NULL,
    document VARCHAR(80) NOT NULL,
    store_product INTEGER NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (store_product) REFERENCES product (id)
);

CREATE TABLE IF NOT EXISTS product (
    id INTEGER NOT NULL,
    nome VARCHAR(255) NOT NULL,
    price NUMERIC(15,2) NOT NULL,
    store_id INTEGER NOT NULL,
    inventory_id INTEGER NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (store_id) REFERENCES store (id),
    FOREIGN KEY (inventory_id) REFERENCES inventory (id)
);

CREATE TABLE IF NOT EXISTS inventory (
    id INTEGER NOT NULL PRIMARY KEY,
    amount INTEGER NOT NULL,
    product_id INTEGER NOT NULL,
    FOREIGN KEY (product_id) REFERENCES product (id)
);

【问题讨论】:

    标签: sql database postgresql performance postgresql-9.5


    【解决方案1】:

    首先创建没有外键约束的表,然后使用ALTER 将其更改为外键,这是一种解决方法

     CREATE TABLE IF NOT EXISTS store (
        id INTEGER NOT NULL,
        nome VARCHAR(255) NOT NULL,
        document VARCHAR(80) NOT NULL,
        store_product INTEGER NOT NULL,
        PRIMARY KEY (id),
    );
    
      CREATE TABLE IF NOT EXISTS product (
        id INTEGER NOT NULL,
        nome VARCHAR(255) NOT NULL,
        price NUMERIC(15,2) NOT NULL,
        store_id INTEGER NOT NULL,
        inventory_id INTEGER NOT NULL,
        PRIMARY KEY (id)
        
    );
    
    CREATE TABLE IF NOT EXISTS inventory (
        id INTEGER NOT NULL PRIMARY KEY,
        amount INTEGER NOT NULL,
        product_id INTEGER NOT NULL,
        
     );
    
    
       Alter table store
       ADD Constraint fk
       FOREIGN KEY (store_product) REFERENCES 
       product (id);
       Alter table inventory
       ADD Constraint fk1
       FOREIGN KEY (product_id) REFERENCES 
       product (id);
       Alter table product
       ADD Constraint fk2
        FOREIGN KEY (store_id) REFERENCES store (id),
       FOREIGN KEY (inventory_id) REFERENCES 
        inventory (id);
       
    

    【讨论】:

    • 效果很好,感谢您的回答和您的时间!!
    • 欢迎您:)
    【解决方案2】:

    外键约束有两个问题:

    1。添加约束

    当存在循环链接表子集的 FK 时,您可以先创建表,然后再添加约束。

    例如:

    CREATE TABLE store (
        id INTEGER NOT NULL,
        nome VARCHAR(255) NOT NULL,
        document VARCHAR(80) NOT NULL,
        store_product INTEGER NOT NULL,
        PRIMARY KEY (id)
    );
    
    CREATE TABLE product (
        id INTEGER NOT NULL,
        nome VARCHAR(255) NOT NULL,
        price NUMERIC(15,2) NOT NULL,
        store_id INTEGER NOT NULL,
        inventory_id INTEGER NOT NULL,
        PRIMARY KEY (id)
    );
    
    CREATE TABLE inventory (
        id INTEGER NOT NULL PRIMARY KEY,
        amount INTEGER NOT NULL,
        product_id INTEGER NOT NULL
    );
    

    然后:

    alter table store add constraint fk1 
    FOREIGN KEY (store_product) REFERENCES product (id) 
    deferrable initially deferred;
    
    alter table product add constraint fk2 
    FOREIGN KEY (store_id) REFERENCES store (id);
    
    alter table product add constraint fk3 
    FOREIGN KEY (inventory_id) REFERENCES inventory (id);
    
    alter table inventory add constraint fk4 
    FOREIGN KEY (product_id) REFERENCES product (id);
    
    2。插入数据

    插入相互依赖的数据时,您需要先决定要在哪个表中插入哪一行。这就是为什么上面的例子在第一个约束中包含DEFERRABLE INITIALLY DEFERRED

    这样就可以按顺序插入了:

    1. 开始交易。
    2. 插入store -- fk1 尚未验证。
    3. 插入inventory。验证 fk4
    4. 插入product。验证 fk2fk3
    5. 提交事务。此时fk1 将最终得到验证。

    【讨论】:

      猜你喜欢
      • 2018-09-30
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2011-02-16
      • 2014-12-05
      • 2017-12-07
      • 2021-11-16
      • 2015-12-10
      相关资源
      最近更新 更多