Created
November 2, 2023 23:30
-
-
Save FYHenry/b7d2012dac9017ec6665fd5a33d51085 to your computer and use it in GitHub Desktop.
Usage of sequence functions in PostgreSQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP OWNED BY optical; | |
CREATE TABLE producttype (id int, | |
active boolean, | |
CONSTRAINT pk_producttype_id PRIMARY KEY (id)); | |
CREATE TABLE lang (id int, | |
name varchar NOT NULL, | |
CONSTRAINT pk_lang_id PRIMARY KEY (id), | |
CONSTRAINT un_lang_name UNIQUE (name)); | |
CREATE TABLE producttypei18n (id int, | |
label varchar, | |
lang varchar,-- CHECK (lang IN ('FRENCH', 'ENGLISH')), | |
producttype_id int, | |
CONSTRAINT pk_producttypei18n_id PRIMARY KEY (id), | |
CONSTRAINT fk_producttype_id FOREIGN KEY (producttype_id) | |
REFERENCES producttype (id), | |
CONSTRAINT fk_lang_name FOREIGN KEY (lang) | |
REFERENCES lang (name)); | |
CREATE SEQUENCE seq_producttype_id MINVALUE 0 MAXVALUE 1023 INCREMENT BY 1; | |
CREATE SEQUENCE seq_lang_name MINVALUE 0 MAXVALUE 1023 INCREMENT BY 1; | |
CREATE SEQUENCE seq_producttypei18n_id MINVALUE 0 MAXVALUE 1023 INCREMENT BY 1; | |
INSERT INTO lang (id, name) VALUES (nextval('seq_lang_name'), 'FRENCH'), | |
(nextval('seq_lang_name'), 'ENGLISH'); | |
INSERT INTO producttype (id, active) VALUES | |
(nextval('seq_producttype_id'), false); | |
INSERT INTO producttypei18n (id, label, lang, producttype_id) VALUES | |
(nextval('seq_producttypei18n_id'), 'Banane', 'FRENCH', currval('seq_producttype_id')), | |
(nextval('seq_producttypei18n_id'), 'Banana', 'ENGLISH', currval('seq_producttype_id')); | |
INSERT INTO producttype (id, active) VALUES | |
(nextval('seq_producttype_id'), true); | |
INSERT INTO producttypei18n (id, label, lang, producttype_id) VALUES | |
(nextval('seq_producttypei18n_id'), 'Pomme', 'FRENCH', currval('seq_producttype_id')), | |
(nextval('seq_producttypei18n_id'), 'Apple', 'ENGLISH', currval('seq_producttype_id')); | |
SELECT * FROM producttype; | |
SELECT * FROM producttypei18n; | |
SELECT i.id AS pti_id, i.label, i.lang, p.id AS pt_id, p.active FROM producttypei18n i, | |
producttype p; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment