简体   繁体   中英

sqlite3 in c++ create statements and foreign keys

I am trying out sqlite3 create statements and foreign keys in c++ and I came up with 4 requirements.

1) a employer can create a job position
2) a employer can view the form reject or shortlist the applicant for interview
3) a applicant may apply for a job position
4) a applicant must fill up their details and submit the form if they are keen to apply for a position

This is what I wrote

employer Table

/*SQL statement for employer table*/
 char *sql;
 sql = "CREATE TABLE EMPLOYER("  \
     "ID INT PRIMARY KEY     NOT NULL," \
     "NAME           TEXT    NOT NULL," \
     "ROLE           TEXT    NOT NULL );";

jobposition table

/*SQL statement for jobposition table */
char *sql;
sql = "CREATE TABLE JOBPOSITION("  \
     "ID INT PRIMARY KEY     NOT NULL," \
     "JOBPOSITION    TEXT    NOT NULL," \
     "OUTCOME        TEXT    NOT NULL );";

form table

/*SQL statement for form table*/
char *sql;
sql = "CREATE TABLE FORM ("  \
      "ID INT PRIMARY KEY     NOT NULL," \
     "FOREIGN KEY (ID) REFERENCES APPLICANT(ID)",
     "FOREIGN KEY (ID) REFERENCES JOBPOSITION(ID)",
      FOREIGN KEY (ID) REFERENCES EMPLOYER(ID)" );";

applicant table

 /*SQL statement for applicant table*/
 char *sql;
 sql = "CREATE TABLE APPLICANT ("  \
     "ID INT PRIMARY KEY     NOT NULL," \
     "APPLICANT_NAME    TEXT    NOT NULL," \
     "APPLICANT_ADDRESS    TEXT    NOT NULL," );";

I am not really sure if this is correct as I just started learning database and need some help with it.

I don't know what you mean by "correct", so I am going to show you how I would do it:

CREATE TABLE users (
    id            INT     NOT NULL PRIMARY_KEY AUTO_INCREMENT,
    name          TEXT    NOT NULL,
    address       TEXT    NOT NULL,
    role          TEXT    NOT NULL,    /* employer, applicant etc. */
);

CREATE TABLE job_positions (
    id             INT    NOT NULL PRIMARY_KEY AUTO_INCREMENT,
    creator_id     INT    NOT NULL FOREIGN_KEY REFERENCES users(id),
    description    TEXT   NOT NULL,
    outcome        TEXT   NOT NULL,
);

CREATE TABLE forms (
    id            INT    NOT NULL PRIMARY_KEY AUTO_INCREMENT,
    position_id   INT    NOT NULL FOREIGN_KEY REFERENCES job_positions(id)
    creator_id    INT    NOT NULL FOREIGN_KEY REFERENCES users(id)  
);

this way users have 1-N relationship with job_positions and forms; and job_positions have 1-N relationship with forms. In other words, job_positions belong to users; and forms belong to both users and job positions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM