Created
February 27, 2020 15:05
-
-
Save white-collar/14cc1b841c2dbf35b2121afaff0ab681 to your computer and use it in GitHub Desktop.
postgres join
This file contains 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
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