Advertisement
Guest User

Martian Database

a guest
Feb 23rd, 2020
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.58 KB | None | 0 0
  1. /*
  2. --- README ----
  3. Data used in "SQL Join Explained" from the SQL Tutorial Series,
  4. by socratica (https://www.youtube.com/channel/UCW6TXMZ5Pq6yL6_k5NZ2e0Q).
  5.  
  6. Videos here:
  7. https://www.youtube.com/watch?v=9yeOJ0ZMUYw
  8. https://www.youtube.com/watch?v=Jh_pvk48jHA
  9.  
  10. Since I'm working with the command line interface for postgres
  11. (psql), I'm assuming you do the same.
  12.  
  13. So first of all, create a database named "martiandb" and connect to
  14. it. After that, you can execute this script to create the relevant
  15. tables and populate the data. So, fire up psql and at the psql
  16. prompt type the following:
  17.  
  18. CREATE DATABASE martiandb;
  19. \c martiandb
  20. \ir martian.sql
  21.  
  22. \c and \ir are special commands used in psql (check \? for reference).
  23. \c connects to the db, \ir executes a SQL script with a relative
  24. path: make sure you are executing psql in the same folder of the
  25. script.
  26.  */
  27.  
  28. -- TABLES USED
  29. CREATE TABLE base (
  30.     base_id SERIAL PRIMARY KEY,
  31.     base_name VARCHAR(100),
  32.     founded TIMESTAMP
  33. );
  34.  
  35. CREATE TABLE martian (
  36.     martian_id SERIAL PRIMARY KEY,
  37.     first_name VARCHAR(100),
  38.     last_name VARCHAR(100),
  39.     base_id INT REFERENCES base(base_id),
  40.     super_id INT
  41. );
  42.  
  43. CREATE TABLE visitor (
  44.     visitor_id SERIAL PRIMARY KEY,
  45.     host_id INT REFERENCES martian(martian_id),
  46.     first_name VARCHAR(100),
  47.     last_name VARCHAR(100)
  48. );
  49.  
  50. CREATE TABLE supply (
  51.     supply_id SERIAL PRIMARY KEY,
  52.     name VARCHAR(100),
  53.     description VARCHAR(200),
  54.     quantity INT
  55. );
  56.  
  57. CREATE TABLE inventory (
  58.     base_id INT REFERENCES base(base_id),
  59.     supply_id INT REFERENCES supply(supply_id),
  60.     quantity INT,
  61.     PRIMARY KEY(base_id, supply_id)
  62. );
  63.  
  64. --DATA
  65. INSERT INTO base
  66.     (base_name, founded)
  67. VALUES
  68.     ('Tharsisland', '2037-06-03'),
  69.     ('Valles Marineris 2.0', '2040-12-01'),
  70.     ('Gale Cratertown', '2041-08-15'),
  71.     ('New New New York', '2042-02-10'),
  72.     ('Olympus Mons Spa & Casino', NULL);
  73.  
  74. INSERT INTO martian
  75.     (first_name, last_name, base_id, super_id)
  76. VALUES
  77.     ('Ray', 'Bradbury', 1, NULL),
  78.     ('John', 'Black', 4, 10),
  79.     ('Samuel', 'Hinkston', 4, 2),
  80.     ('Jeff', 'Spender', 1, 9),
  81.     ('Sam', 'Parkhill', 2 ,12),
  82.     ('Emma', 'Parkhill', 3, 8),
  83.     ('Melissa', 'Lewis', 1, 1),
  84.     ('Mark', 'Watney', 3, NULL),
  85.     ('Beth', 'Johanssen', 1, 1),
  86.     ('Chris', 'Beck', 4, NULL),
  87.     ('Nathaniel', 'York', 4, 2),
  88.     ('Elon', 'Musk', 2, NULL),
  89.     ('John', 'Carter', NULL, 8);
  90.  
  91.  
  92. INSERT INTO visitor
  93.     (host_id, first_name, last_name)
  94. VALUES
  95.     (7, 'George', 'Ambrose'),
  96.     (1, 'Kris', 'Cardenas'),
  97.     (9, 'Priscilla', 'Lane'),
  98.     (11, 'Jane', 'Thorton'),
  99.     (NULL, 'Doug', 'Stavenger'),
  100.     (NULL, 'Jamie', 'Waterman'),
  101.     (8, 'Martin', 'Humphries');
  102.  
  103. INSERT INTO supply
  104.     (name, description, quantity)
  105. VALUES
  106. ('Solar Panel', 'Standard 1x1 meter cell', 912),
  107. ('Water Filter', 'This takes the things out of your water so it''s drinkable',6),
  108. ('Duct Tape', 'A 10 meter roll of duct tape for ALL your repairs', 951),
  109. ('Ketchup', 'It''s ketchup...', 206),
  110. ('Battery Cell', 'Standard 1000 kAh battery cell for power grid (heavy item)', 17),
  111. ('USB 6.0 Cable', 'Carbon fiber coated / 15 TBps spool', 42),
  112. ('Fuzzy Duster', 'It gets dusty around here! Be prepared', 19),
  113. ('Mars Bars', 'The ORIGINAL nutrient bar made with the finest bioengeneered ingrendients', 3801),
  114. ('Air Filter', 'Removes 99% of all Martian dust from your ventilation unit', 23),
  115. ('Famous Ray''s Frozen Pizza ', 'This Martian favorite is covered in all your favorite toppings. 1 flavor only.', 823);
  116.  
  117. INSERT INTO inventory
  118.     (base_id, supply_id, quantity)
  119. VALUES
  120.     (1, 1, 8),
  121.     (1, 3, 5),
  122.     (1, 5, 1),
  123.     (1, 6, 2),
  124.     (1, 8, 12),
  125.     (1, 9, 1),
  126.     (2, 4, 5),
  127.     (2, 8, 62),
  128.     (2, 10, 37),
  129.     (3, 2, 11),
  130.     (3, 7, 2),
  131.     (4, 10, 91);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement