Files
sql/bibliotheque/bibliotheque.sql
2025-09-30 07:40:56 +02:00

269 lines
8.9 KiB
SQL

-- Lookup tables
drop table if exists langues;
drop table if exists pays;
drop table if exists statuts;
drop table if exists auteurs;
drop table if exists editeurs;
drop table if exists series;
drop table if exists relations;
drop table if exists oeuvres;
drop table if exists participe;
drop table if exists editions;
drop table if exists exemplaires;
drop table if exists genres;
drop table if exists collections;
drop table if exists incorpore;
drop table if exists adherents;
drop table if exists adresses;
drop table if exists adherent_adresse;
drop table if exists commande_historique;
drop table if exists commande_ligne;
drop table if exists commande;
drop table if exists commande_statut;
drop table if exists livraison_methode;
-- ************************************************************
pragma integrity_check;
pragma foreign_key_check;
-- ************************************************************
CREATE TABLE statuts (
statutId INTEGER PRIMARY KEY,
statut TEXT NOT NULL
);
CREATE TABLE pays (
code TEXT PRIMARY KEY CHECK (LENGTH(code) = 2),
nom TEXT NOT NULL
);
insert into pays values
('FR', 'France'), ('GB', 'Royaume-Uni'), ('US', 'États-Unis'), ('TR', 'Türkiye');
CREATE TABLE langues (
code TEXT PRIMARY KEY CHECK (LENGTH(code) = 2 or LENGTH(code) = 5),
nom TEXT NOT NULL
);
insert into langues values
('fr', 'français'), ('tr', 'turc'),
('en', 'anglais'), ('en-gb', 'anglais britannique'), ('en-us', 'anglais américain');
CREATE TABLE genres (
genreId INTEGER PRIMARY KEY,
nom TEXT NOT NULL
);
insert into genres (nom) values
('space opera'), ('horreur'), ('fiction historique');
CREATE TABLE auteurs (
auteurId INTEGER PRIMARY KEY,
nom TEXT NOT NULL,
[references] TEXT DEFAULT NULL
);
insert into auteurs values
(3076,'Graham Masterton','https://www.noosfere.org/livres/auteur.asp?numauteur=19'),
(8222,'Stephen King','https://www.noosfere.org/livres/auteur.asp?NumAuteur=97'),
(9256,'Pierre Bordage','https://www.noosfere.org/livres/auteur.asp?NumAuteur=28'),
(9912,'Elif Şafak', 'https://www.babelio.com/auteur/Elif-Shafak/22574');
CREATE TABLE editeurs (
editeurId INTEGER PRIMARY KEY,
nom TEXT NOT NULL
);
insert into editeurs (nom) values
('L''Atalante'), ('J''Ai Lu'), ('10/18'), ('Viking'), ('Penguin Books'), ('Le Livre de Poche');
CREATE TABLE collections (
collectionId INTEGER PRIMARY KEY,
nom TEXT NOT NULL
);
insert into collections (nom) values
('Bibliothèque de l''évasion'), ('Science-Fiction');
CREATE TABLE series (
serieId INTEGER PRIMARY KEY,
nom TEXT NOT NULL
);
insert into series values
(1, 'Les Guerriers du silence');
-- ************************************************************
CREATE TABLE oeuvres (
oeuvreId INTEGER PRIMARY KEY,
titre TEXT,
langueCode TEXT REFERENCES langues (langue_code),
publicationDate DATE,
genreId integer references genres (genreId),
serie_id integer references series (serie_id)
);
insert into oeuvres values
(1, 'Les Guerriers du silence','fr', 1993, 1, 1),
(2, 'Terra Mater', 'fr', 1994, 1, 1),
(3, 'La Citadelle Hyponéros', 'fr', 1995, 1, 1),
(4, 'The Forty Rules of Love', 'en', 2009, 3, NULL),
(5, 'Salem''s Lot', 'en', 1975, 2, NULL),
(6, 'The Shining', 'en', 1977, 2, NULL),
(7, 'The Stand', 'en', 1978, 2, NULL),
(8, 'The Long Walk', 'en', 1975, 2, NULL),
(9, 'Charlie', 'en', 1980, 2, NULL),
(10, 'IT', 'en', 1986, 2, NULL),
(11, 'The Stand: The Complete & Uncut Edition', 'en', 1990, 2, NULL),
(12, 'The Tommyknockers', 'en', 1987,2,NULL),
(13, 'Pet Sematary', 'en', 1983, 2,NULL),
(14, 'Chritine', 'en', 1983, 2,NULL),
(15, 'Firestarter', 'en', 1980, 2,NULL);
create table participe (
oeuvre_id INTEGER NOT NULL REFERENCES oeuvres (oeuvre_id),
auteur_id INTEGER NOT NULL REFERENCES auteurs (auteur_id),
fonction TEXT,
alias TEXT,
CONSTRAINT pk_oeuvreauteur PRIMARY KEY (oeuvre_id, auteur_id)
);
insert into participe values
(1, 8222, 'auteur', NULL), (2, 8222, 'auteur', NULL), (3, 8222, 'auteur', NULL),
(4, 9912, 'auteur', NULL),
(5, 8222, 'auteur', NULL),(6, 8222, 'auteur', NULL),(7, 8222, 'auteur', NULL),
(8, 8222, 'auteur', 'Richard Bachman'),(9, 8222, 'auteur', NULL),(10, 8222, 'auteur', NULL),
(11, 8222, 'auteur', NULL),(12, 8222, 'auteur', NULL),(13, 8222, 'auteur', NULL),
(14, 8222, 'auteur', NULL), (15, 8222, 'auteur', NULL);
CREATE TABLE editions (
editionId INTEGER PRIMARY KEY,
editeurId INTEGER REFERENCES editeurs (editeurId),
isbn13 TEXT,
langueCode TEXT REFERENCES langues (langueCode),
publication_date DATE,
num_pages INTEGER,
titre TEXT,
informations TEXT
);
insert into editions values
(1, 1, '978-2-9051-5869-7', 'fr', '1993-04-01', 704, 'Les Guerriers du silence', NULL),
(2, 1, '978-2-2903-1901-7', 'fr', '1994-05-01', 672, 'Terra Mater', NULL),
(3, 1, '978-2-2903-3137-8', 'fr', '1995-06-01', 720, 'La Citadelle Hyponéros', NULL),
(4, 4, '978-0-1410-4718-8', 'en', '2010-02-18', 354, 'The Forty Rules of Love', NULL),
(5, 3, '978-2-2640-5406-7', 'fr', '2011-08-18', 480, 'Soufi, mon amour', '{ "traducteur": "Dominique Letellier" }'),
(6, 2, '978-2-277-22326-3', 'fr', '1988-01-01', 576, 'Le Fléau', '{ "traducteur": "Richard Matas", "couverture": "Matthieu Blanchin", "catégorie": "6", "collection": "Épouvante", "num catalogue": 2326 }'),
(7, 2, '978-2-277-23311-0', 'fr', '1992-09-01', 512, 'Le Fléau - 1', '{ "traducteur": "Jean-Pierre Quijano", "couverture": "Matthieu Blanchin", "catégorie": "6", "collection": "Épouvante", "num catalogue": 3311 }'),
(8, 2, '978-2-277-23312-9', 'fr', '1992-09-01', 512, 'Le Fléau - 2', '{ "traducteur": "Jean-Pierre Quijano", "couverture": "Matthieu Blanchin", "catégorie": "6", "collection": "Épouvante", "num catalogue": 3312 }'),
(9, 2, '978-2-277-23313-7', 'fr', '1992-09-01', 512, 'Le Fléau - 3', '{ "traducteur": "Jean-Pierre Quijano", "couverture": "Matthieu Blanchin", "catégorie": "6", "collection": "Épouvante", "num catalogue": 3313 }'),
(10, 6, '978-2-253-15141-8', 'fr', '2003-08-01', 768, 'Le Fléau - 1', '{ "traducteur": "Jean-Pierre Quijano", "couverture": "Philippe Brault", "num catalogue": 15141 }'),
(11, 6, '978-2-253-15142-4', 'fr', '2003-08-01', 800, 'Le Fléau - 2', '{ "traducteur": "Jean-Pierre Quijano", "couverture": "Philippe Brault", "num catalogue": 15142 }')
;
CREATE TABLE incorpore (
oeuvreId INTEGER NOT NULL REFERENCES oeuvres (oeuvreId),
editionId INTEGER NOT NULL REFERENCES editions (editionId),
CONSTRAINT pk_oeuvreauteur PRIMARY KEY (oeuvreId, editionId)
);
insert into incorpore values
(1, 1), (2, 2), (3, 3), (4, 4), (4, 5),
(7, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 11);
CREATE TABLE exemplaires (
exemplaireId INTEGER PRIMARY KEY,
editionId INTEGER REFERENCES editions (edition_id),
date_achat DATE,
prix_achat NUMERIC,
etat TEXT
);
insert into exemplaires values
(1, 1, '1993-10-30', 22.71, 'bon'),
(2, 2, '1995-02-15', 22.71, 'bon'),
(3, 3, '1996-03-22', 22.71, 'bon'),
(4, 4, '2009-12-22', 15.45, 'bon'),
(5, 5, '2012-09-05', 8.69, 'bon'),
(6, 7, '1993-01-17', 7.50, 'bon'),
(7, 10, '2014-11-05', 8.00, 'bon'),
(8, 11, '2014-09-05', 8.00, 'bon');
-- ************************************************************
CREATE TABLE adherents (
adherentId INTEGER PRIMARY KEY,
prenom TEXT NOT NULL,
nom TEXT NOT NULL,
email TEXT,
naissance DATE,
statutId INTEGER REFERENCES statuts (statutId)
);
CREATE TABLE adresses (
adresseId INTEGER PRIMARY KEY,
numero TEXT,
voie TEXT,
ville TEXT,
pays_id INTEGER REFERENCES pays (pays_id)
);
CREATE TABLE adherent_adresse (
adherentId INTEGER NOT NULL,
adresseId INTEGER NOT NULL,
statutId INTEGER,
CONSTRAINT pk_adherent_adresse PRIMARY KEY (adherentId, adresseId),
CONSTRAINT fk_aa_adherent FOREIGN KEY (adherentId) REFERENCES adherents (adherentId),
CONSTRAINT fk_aa_adresse FOREIGN KEY (adresseId) REFERENCES adresses (adresseId),
CONSTRAINT fk_aa_statut FOREIGN KEY (statutId) REFERENCES statuts (statutId)
);
-- ************************************************************
CREATE TABLE livraison_methode (
methodeId INTEGER PRIMARY KEY,
methode_name TEXT,
cout NUMERIC
);
CREATE TABLE commande (
commandeId INTEGER PRIMARY KEY,
commande_date DATETIME,
date_echeance DATE,
adherentId INTEGER REFERENCES adherents (adherentId),
livraison_methode_id INTEGER REFERENCES livraison_methode (methodeId),
dest_adresseId INTEGER REFERENCES adresses (adresseId)
);
CREATE TABLE commande_statut (
statutId INTEGER PRIMARY KEY,
statut_value TEXT
);
CREATE TABLE commande_ligne (
ligne_id INTEGER PRIMARY KEY,
commandeId INTEGER REFERENCES commande (commandeId),
exemplaire_id INTEGER REFERENCES exemplaires (exemplaire_id),
cout NUMERIC,
date_retour DATETIME
);
CREATE TABLE commande_historique (
historique_id INTEGER PRIMARY KEY,
commandeId INTEGER REFERENCES commande (commandeId),
statutId INTEGER REFERENCES commande_statut (statutId),
statut_date DATETIME
);
-- ************************************************************