Skip to content

Instantly share code, notes, and snippets.

@abranhe
Created July 18, 2020 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abranhe/444a77f3325af1c833481481571bc002 to your computer and use it in GitHub Desktop.
Save abranhe/444a77f3325af1c833481481571bc002 to your computer and use it in GitHub Desktop.
Example SQL Relationship
CREATE TABLE books (
id serial,
title varchar(100) NOT NULL,
author varchar(100) NOT NULL,
published_date timestamp NOT NULL,
isbn char(12),
PRIMARY KEY (id),
UNIQUE (isbn)
);
CREATE TABLE reviews (
id serial,
book_id integer NOT NULL,
reviewer_name varchar(255),
content varchar(255),
rating integer,
published_date timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (book_id) REFERENCES books(id)
);
CREATE TABLE books (
id serial,
title varchar(100) NOT NULL,
author varchar(100) NOT NULL,
published_date timestamp NOT NULL,
isbn char(12),
PRIMARY KEY (id),
UNIQUE (isbn)
);
CREATE TABLE reviews (
id serial,
book_id integer REFERENCES books(id),
reviewer_name varchar(255),
content varchar(255),
rating integer,
published_date timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment