Foreign keys referring other foreign keys in PostgreSQL

前端 未结 2 1578
南笙
南笙 2021-01-24 23:56

In PostgreSQL I have a database, which I intend to make the following table declaration:

CREATE TABLE canvas_user (
    id INTEGER,
    login_id VARCHAR(50) UNIQ         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-25 00:54

    Make the foreign key from the correction table to the submission table a compound key that is unique. Also, review the design of the submission table; num is a serial type and should be the unique primary key. You can add a unique constraint to the columns num, user_id, and assignment_id

    CREATE TABLE canvas_user (
        id INTEGER,
        login_id VARCHAR(50) UNIQUE NOT NULL,
        email VARCHAR(355) UNIQUE NOT NULL,
        name_given VARCHAR(30),
        name_family VARCHAR(30),
        name_full VARCHAR(50),
        role canvas_role,
        last_login TIMESTAMP,
        PRIMARY KEY (id)
    );
    
    
    CREATE TABLE problem (
        id SERIAL,
        title VARCHAR(50),
        author VARCHAR(50),
        path TEXT,
        compiler VARCHAR(20),
        PRIMARY KEY (id)
    );
    
    
    CREATE TABLE assignment (
        id INTEGER,
        title TEXT NOT NULL,
        points_possible INTEGER NOT NULL,
        problem_id INTEGER,
        PRIMARY KEY (id),
        FOREIGN KEY (problem_id) REFERENCES problem(id)
    );
    
    
    CREATE TABLE submission (
        num SERIAL,
        user_id INTEGER,
        assignment_id INTEGER,
        timestamp TIMESTAMP,
        path TEXT,
        lti_info TEXT[],
        PRIMARY KEY(num, user_id, assignment_id),
        FOREIGN KEY (user_id) REFERENCES canvas_user(id),
        FOREIGN KEY (assignment_id) REFERENCES assignment(id)
    );
    
    
    CREATE TABLE correction (
        num INTEGER,
        user_id INTEGER,
        assignment_id INTEGER,
        timestamp TIMESTAMP,
        path TEXT,
        execution_time interval,
        PRIMARY KEY(num, user_id, assignment_id),
        FOREIGN KEY (num, user_id,assignment_id ) REFERENCES submission(num, user_id, assignment_id)
    
    );
    

提交回复
热议问题