Skip to content

Instantly share code, notes, and snippets.

@white-collar
Created February 27, 2020 15:05
Show Gist options
  • Save white-collar/14cc1b841c2dbf35b2121afaff0ab681 to your computer and use it in GitHub Desktop.
Save white-collar/14cc1b841c2dbf35b2121afaff0ab681 to your computer and use it in GitHub Desktop.
postgres join
CREATE TABLE Products
(
Id SERIAL PRIMARY KEY,
ProductName VARCHAR(30) NOT NULL,
Company VARCHAR(20) NOT NULL,
ProductCount INTEGER DEFAULT 0,
Price NUMERIC NOT NULL
);
CREATE TABLE Customers
(
Id SERIAL PRIMARY KEY,
FirstName VARCHAR(30) NOT NULL
);
CREATE TABLE Orders
(
Id SERIAL PRIMARY KEY,
ProductId INTEGER NOT NULL REFERENCES Products(Id) ON DELETE CASCADE,
CustomerId INTEGER NOT NULL REFERENCES Customers(Id) ON DELETE CASCADE,
CreatedAt DATE NOT NULL,
ProductCount INTEGER DEFAULT 1,
Price NUMERIC NOT NULL
);
SELECT Orders.CreatedAt, Orders.ProductCount, Products.ProductName
FROM Orders
JOIN Products ON Products.Id = Orders.ProductId;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment