简体   繁体   中英

Did I set this relational database up correctly (python, sqlite3)

I'm trying to make a relational database that connects 3 different tables. The main program database contains the vendors, that links to invoices from each vendor, then the invoices links to items purchased in that invoice.

tempcur.execute("""CREATE TABLE program (
                     vendorid INTEGER PRIMARY KEY, 
                     vendor TEXT, 
                     phone TEXT, 
                     store INTEGER)""")

tempcur.execute("""CREATE TABLE dairystore (
                     invoice INTEGER REFERENCES program(vendorid), 
                     date VARCHAR)""")

tempcur.execute("""CREATE TABLE invoices(
                     item INTEGER REFERENCES dairystore(invoice), 
                     shipped VARCHAR, 
                     description TEXT, 
                     weight INTEGER, 
                     price INTEGER, 
                     amount INTEGER)""")

No, you have a mistake in dairystore table. It should be the following:

CREATE TABLE dairystore (
  invoice INTEGER  PRIMARY KEY,
  vendor INTEGER REFERENCES program(vendorid), 
  date VARCHAR
)

You're trying to reference to dairystore.invoice from invoices table, so dairystore.invoice must be the id of record. In your code dairystore.invoice can't be the id of record because it is reference to program.vendorid and its values will be equal to ids of records in program table.

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