Skip to content

Instantly share code, notes, and snippets.

@VitalikS
Created February 28, 2017 23:04
Show Gist options
  • Save VitalikS/b74b96f52f263e4916cbf646f3c84093 to your computer and use it in GitHub Desktop.
Save VitalikS/b74b96f52f263e4916cbf646f3c84093 to your computer and use it in GitHub Desktop.
Самостоятельная 28.02.17
CREATE TABLE Items (
id integer NOT NULL DEFAULT nextval('sequ_id_seq'::regclass),
name text,
price integer,
CONSTRAINT Items_pkey PRIMARY KEY (id)
);
CREATE TABLE Orders (
id integer NOT NULL DEFAULT nextval('sequ_id2_seq'::regclass),
quantity integer,
email varchar(40),
item_id integer NOT NULL,
CONSTRAINT Orders_pkey PRIMARY KEY (id),
CONSTRAINT Orders_items_fk FOREIGN KEY (item_id)
REFERENCES Items (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE
INITIALLY DEFERRED
);
CREATE TABLE Status (
id integer NOT NULL DEFAULT nextval('sequ_id3_seq'::regclass),
quantity integer,
item_id integer,
CONSTRAINT Status_pkey PRIMARY KEY (id)
);
INSERT INTO Items(name, price) VALUES('Gold', 100);
INSERT INTO Orders(quantity, email, item_id) VALUES(10, 'misha@misha.ru', 3);
INSERT INTO Status (quantity, item_id) VALUES (1, 1);
SELECT * FROM items
inner join orders AS o ON (
items.id = o.item_id);
SELECT * FROM items
inner join Status AS s ON (
items.id = s.item_id);
SELECT * FROM items
inner join orders AS o ON (
items.id = o.item_id)
inner join status AS s ON (
items.id = s.item_id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment