sql: add table definition

This commit is contained in:
King Kévin 2023-01-23 14:05:06 +01:00
parent 058c7a7b13
commit a00037aa93
1 changed files with 168 additions and 0 deletions

168
schema.sql Normal file
View File

@ -0,0 +1,168 @@
-- enable foreign key support in sqlite
--PRAGMA foreign_keys = ON;
-- part manufacturer
CREATE TABLE IF NOT EXISTS manufacturer (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
name TEXT NOT NULL UNIQUE, -- manufacturer (expanded version, without legal form)
nick TEXT, -- manufacturer nickname
partof INTEGER, -- if the manufacturer has been acquired or is part of another
homepage TEXT, -- URL to home page
search TEXT, -- URL to search page
FOREIGN KEY (partof) REFERENCES manufacturer (id)
);
-- part distributor
CREATE TABLE IF NOT EXISTS distributor (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
name TEXT NOT NULL UNIQUE,
homepage TEXT, -- URL to home page
product_page TEXT -- URL to product page (%s is replace by sku)
);
-- part package
CREATE TABLE IF NOT EXISTS package (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
name TEXT NOT NULL UNIQUE,
family TEXT -- if this package it part of a more general package family
);
-- the part itself
CREATE TABLE IF NOT EXISTS part (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
name TEXT NOT NULL UNIQUE,
description TEXT,
manufacturer INTEGER,
family INTEGER, -- if this part is part of a part family
datasheet TEXT, -- URL to datasheet
package INTEGER,
pincount INTEGER,
page TEXT, -- URL to product page
FOREIGN KEY (manufacturer) REFERENCES manufacturer (id),
FOREIGN KEY (family) REFERENCES part (id),
FOREIGN KEY (package) REFERENCES package (id)
);
-- CAD drawing type
CREATE TABLE IF NOT EXISTS cad (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
name TEXT NOT NULL UNIQUE,
extension TEXT
);
-- part drawing (symbol, footprint, ...)
CREATE TABLE IF NOT EXISTS drawing (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
cad INTEGER NOT NULL,
path TEXT NOT NULL,
option TEXT,
FOREIGN KEY (cad) REFERENCES cad (id)
);
-- a part at a distributor
CREATE TABLE IF NOT EXISTS distribution (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
part INTEGER NOT NULL, -- the part
distributor INTEGER NOT NULL, -- the part distributor
sku TEXT NOT NULL, -- distributor part number
FOREIGN KEY (part) REFERENCES part (id),
FOREIGN KEY (distributor) REFERENCES distributor (id)
);
-- part stock at a distributor
CREATE TABLE IF NOT EXISTS stock (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
distribution INTEGER NOT NULL, -- the part
stock INTEGER NOT NULL,
date DATETIME NOT NULL,
FOREIGN KEY (distribution) REFERENCES distribution (id)
);
-- price currency
CREATE TABLE IF NOT EXISTS currency (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
name TEXT NOT NULL
);
-- currency rate
CREATE TABLE IF NOT EXISTS rate (
origin INTEGER NOT NULL, -- from
destination INTEGER NOT NULL, -- to
rate REAL NOT NULL, -- 1 from = rate to
date DATETIME NOT NULL, -- date of the rate
FOREIGN KEY (origin) REFERENCES currency (id),
FOREIGN KEY (destination) REFERENCES currency (id)
);
-- part price at a distributor
CREATE TABLE IF NOT EXISTS price (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
distribution INTEGER NOT NULL, -- the part
quantity INTEGER NOT NULL, -- the minimum number of part for this price
price REAL NOT NULL, -- the unit price
currency INTEGER NOT NULL,
date DATETIME NOT NULL,
FOREIGN KEY (distribution) REFERENCES distribution (id),
FOREIGN KEY (currency) REFERENCES currency (id)
);
-- document types
CREATE TABLE IF NOT EXISTS document (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
type TEXT -- document type
);
-- part attachments
CREATE TABLE IF NOT EXISTS attachement (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
part INTEGER NOT NULL, -- the part
type INTEGER, -- attachment document type
path TEXT NOT NULL, -- the path to the attachment
priority INTEGER NOT NULL, -- in which place the attachment should be displayed
FOREIGN KEY (part) REFERENCES part (id),
FOREIGN KEY (type) REFERENCES document (id),
UNIQUE (part, priority)
);
-- property unit
CREATE TABLE IF NOT EXISTS unit (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
name TEXT NOT NULL UNIQUE,
symbol TEXT NOT NULL UNIQUE
);
-- part property
CREATE TABLE IF NOT EXISTS property (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
name TEXT NOT NULL, -- property name (min V, ...)
unit INTEGER,
FOREIGN KEY (unit) REFERENCES unit (id)
);
-- property value
CREATE TABLE IF NOT EXISTS poperty_value (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
part INTEGER NOT NULL, -- the part
property INTEGER NOT NULL,
value TEXT,
FOREIGN KEY (part) REFERENCES part (id),
FOREIGN KEY (property) REFERENCES property (id)
);
-- part location
CREATE TABLE IF NOT EXISTS location (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
name TEXT NOT NULL UNIQUE,
container TEXT -- container type
);
-- local stock
CREATE TABLE IF NOT EXISTS inventory (
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
part INTEGER NOT NULL,
location INTEGER,
quantity INTEGER NOT NULL,
date DATETIME NOT NULL, -- when the quantity changed
FOREIGN KEY (part) REFERENCES part (id),
FOREIGN KEY (location) REFERENCES location (id)
);