Person Company City: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=Internal= * Querying Multiple Tables =Overview= <syntaxhighlight lang='sql'> </syntaxhighlight>") |
|||
(One intermediate revision by the same user not shown) | |||
Line 4: | Line 4: | ||
=Overview= | =Overview= | ||
<syntaxhighlight lang='sql'> | <syntaxhighlight lang='sql'> | ||
CREATE TABLE person (id integer, name text, company_id integer); | |||
ALTER TABLE person ADD PRIMARY KEY (id); | |||
CREATE TABLE company(id integer, name text, city_id integer); | |||
ALTER TABLE company ADD PRIMARY KEY (id); | |||
ALTER TABLE person ADD FOREIGN KEY (company_id) REFERENCES company(id); | |||
CREATE TABLE city(id integer, name text); | |||
ALTER TABLE city ADD PRIMARY KEY (id); | |||
ALTER TABLE company ADD FOREIGN KEY (city_id) REFERENCES city(id); | |||
INSERT INTO city (id, name) VALUES (100, 'San Francisco'); | |||
INSERT INTO city (id, name) VALUES (200, 'New York'); | |||
INSERT INTO city (id, name) VALUES (300, 'Chicago'); | |||
INSERT INTO company (id, name, city_id) VALUES (10, 'Moonphone', 100); | |||
INSERT INTO company (id, name, city_id) VALUES (20, 'Vortextime', 200); | |||
INSERT INTO company (id, name, city_id) VALUES (30, 'Bluestone', 300); | |||
INSERT INTO person (id, name, company_id) VALUES (1, 'Alice', 10); | |||
INSERT INTO person (id, name, company_id) VALUES (2, 'Bob', 20); | |||
INSERT INTO person (id, name, company_id) VALUES (3, 'Charlie', 30); | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 23:15, 24 May 2024
Internal
Overview
CREATE TABLE person (id integer, name text, company_id integer);
ALTER TABLE person ADD PRIMARY KEY (id);
CREATE TABLE company(id integer, name text, city_id integer);
ALTER TABLE company ADD PRIMARY KEY (id);
ALTER TABLE person ADD FOREIGN KEY (company_id) REFERENCES company(id);
CREATE TABLE city(id integer, name text);
ALTER TABLE city ADD PRIMARY KEY (id);
ALTER TABLE company ADD FOREIGN KEY (city_id) REFERENCES city(id);
INSERT INTO city (id, name) VALUES (100, 'San Francisco');
INSERT INTO city (id, name) VALUES (200, 'New York');
INSERT INTO city (id, name) VALUES (300, 'Chicago');
INSERT INTO company (id, name, city_id) VALUES (10, 'Moonphone', 100);
INSERT INTO company (id, name, city_id) VALUES (20, 'Vortextime', 200);
INSERT INTO company (id, name, city_id) VALUES (30, 'Bluestone', 300);
INSERT INTO person (id, name, company_id) VALUES (1, 'Alice', 10);
INSERT INTO person (id, name, company_id) VALUES (2, 'Bob', 20);
INSERT INTO person (id, name, company_id) VALUES (3, 'Charlie', 30);