How to Run HW4 (psql Tutorial) Dr. Chris Mayfield Department of Computer Science James Madison University Feb 13, 2020
How to test HW4 Write/debug each query individually ◮ Follow the lines to join tables Paste into hw4.sql (from pgAdmin) ◮ Don’t forget the ’;’ at the end! ◮ Don’t change any existing lines! Run as a script , compare using meld ◮ Or WinMerge on Windows ◮ Or meld or opendiff on Mac ◮ Don’t compare without a tool! Feb 13, 2020 How to Run HW4 (psql Tutorial) 2 of 10
SQL style guide In general: ◮ Write one clause per line, indent any sub-clauses ◮ Use JOIN syntax when possible (instead of commas) ◮ ALL CAPS for keywords, all lowercase for names For example: SELECT m.title, count(i.info) FROM movie AS m JOIN movie_info AS i ON m.info_id = i.id WHERE m.kind_id = 1 AND m.year = 2014 GROUP BY m.id HAVING count(i.info) > 1 ORDER BY m.title LIMIT 100; Feb 13, 2020 How to Run HW4 (psql Tutorial) 3 of 10
Warm-up exercise -- \echo QUERY #1 \echo -- For the movies named Star Wars, what kind of -- movie was it, and what year was it released? -- -- Schema: kind varchar(15), year integer -- Order: year, kind Similar query: Display all info for “The Wizard of Oz” (1939) ◮ Schema: type info ◮ Example: budget ✩ 2,777,000 Feb 13, 2020 How to Run HW4 (psql Tutorial) 4 of 10
Command line tutorials YouTube videos ◮ https://w3.cs.jmu.edu/cs101/unit06/cmdline.html CS 149 lab — the basics; browsing files ◮ https://w3.cs.jmu.edu/spragunr/CS139 S16/activities/unix tutorial/ CS 101 lab — operating system details ◮ https://w3.cs.jmu.edu/cs101/unit04/Lab04-CmdLine.html Windows users: install PuTTY and WinSCP (connect to student.cs.jmu.edu ) Feb 13, 2020 How to Run HW4 (psql Tutorial) 5 of 10
Example command: less What does less mean? ◮ Back in the day, everything was command line ◮ The program more shows one screen at a time ◮ The program less is way better than more :) Keyboard shortcuts ◮ Use up/down arrows and page up/down ◮ g or G : go to first / last line of text ◮ / or ? : search forward / backward ◮ Press ’h’ for help ◮ Press ’q’ to quit Feb 13, 2020 How to Run HW4 (psql Tutorial) 6 of 10
PostgreSQL interactive terminal psql -h data.cs.jmu.edu -U username dbname ◮ Username is JMU e-ID, password is student number ◮ Your db account is separate from your JMU account! Type SQL interactively -- show the first 10 results SELECT * FROM movie LIMIT 10; Working remotely? ◮ ssh -L 5432:data.cs.jmu.edu:5432 stu.cs.jmu.edu ◮ psql -h localhost ... Feb 13, 2020 How to Run HW4 (psql Tutorial) 7 of 10
Basic psql commands General ◮ \ ? : help on psql commands ◮ \ h : help on SQL syntax ◮ \ i : execute commands from file ◮ \ q : quit psql (or Ctrl-D) Browsing ◮ \ d : list tables, views, etc ◮ \ d NAME : describe table/etc Important ◮ Ctrl-C cancels the current query ◮ Tab completion is your friend! Feb 13, 2020 How to Run HW4 (psql Tutorial) 8 of 10
Using psql on HW4 psql -q -h data.cs.jmu.edu postgres < hw4.sql 2>&1 | tee hw4.txt = quiet -q = hostname -h = redirect stdin from file < 2>&1 = redirect stderr to stdout = pipeline stdout to stdin | tee = echo to stdout and file meld hw4-sol.txt hw4.txt & meld = visual diff and merge = background process & Feb 13, 2020 How to Run HW4 (psql Tutorial) 9 of 10
What keys do I press? How do I exit? ◮ less movies.csv ◮ q ◮ psql -h data.cs.jmu.edu ◮ \ q or Ctrl-D ◮ ssh student.cs.jmu.edu ◮ exit or Ctrl-D What are the most important keys? ◮ Tab — command completion ◮ Up/down — command history Feb 13, 2020 How to Run HW4 (psql Tutorial) 10 of 10
Recommend
More recommend