Giter Club home page Giter Club logo

sql-demo's Introduction

SQL-Demo

0 - Running Postgres

  1. Starting the DB conection in terminal (likely already done): brew services start postgresql
  2. Opening postgres connection: psql postgres

1 - Creating a DB and table

  1. CREATE DATABASE fsa_demo
  • Show \l -> lists all DBs I've made
  • -q -> quits the connection to the DB
  • psql -d database_name will reconnect you OR psql and then \c databse_name
  1. Create a table
  • Show there are no tables in this yet by doing \d -> "did not find any relations" CREATE TABLE people(name VARCHAR(100)); // creates the table
  • Do \d to show your schema (which contains that database table)
  • Then do \d table_name (in this case, \d people )

2 - Adding Data

  1. Add a person to the people table
  • INSERT INTO people(name) VALUES ('Amy');
  • Show that it returns: SELECT * from people;
  • INSERT INTO people(name) VALUES ('Dan');
    • Show alias: SELECT name AS friend FROM people;
  1. Count rows of data
  • SELECT count(*) FROM people;
  • SELECT count(*) FROM people WHERE name = 'Dan'; // returns 1
  1. Drop table and make a new one
  • start over with: DROP TABLE people
  • Share this XKCD Comic
  • Create new table: CREATE TABLE people(id INTEGER PRIMARY KEY, name VARCHAR(100)); // mention primary key
  • INSERT INTO people(name) VALUES('Kevin'); // THIS WILL FAIL because I haven't added in an ID
  • Can put in any order as long as they match:
    • INSERT INTO people(name, id) VALUES('Kevin', 1);
  1. Serial - when you don't want to keep track of your unique IDs CREATE TABLE emails(id SERIAL PRIMARY KEY, email VARCHAR(100) UNIQUE, dept_id INTEGER); // Serial and Unique are both important keywords here INSERT INTO emails(email, dept_id) VALUES('billingemail.com', 2); INSERT INTO emails(email, dept_id) VALUES('developeremail.com', 3);

3 - Joining tables

  1. For JOINs, what relationship would we like to model? Let's say we have a company and want to join a departments table to our emails table
  • ` CREATE TABLE departments( id SERIAL PRIMARY KEY, dept_name VARCHAR(100));
  • INSERT INTO departments(dept_name) VALUES('HR');
  • "" "" departments(dept_name) VALUES('developers');
  • "" "" departments(dept_name) VALUES('billing');
  1. Create table with a reference to the departments table
  • CREATE TABLE employees(id SERIAL PRIMARY KEY, name VARCHAR(100), department_id INTEGER REFERENCES departments(id));
    • Why do we use the "REFERENCES"? It tells the DB that there is a relation, so it will only accept department IDs which correspond to an id in the departments table! It will fail otherwise
  1. Show INSERT fails if id doesn't match department table
  • INSERT INTO employees(name, department_id) VALUES('Bea Arthur', 7); // "violates key constraint" error At 31 minutes
  1. Successful JOIN
  • INSERT INTO employees(name, department_id) VALUES('Bea Arthur', 2); // 2 is an existing ID so this will work
  • Now add other employees:
  • INSERT INTO employees(name, department_id) VALUES('Rue McClanahan', 1);
  • INSERT INTO employees(name, department_id) VALUES('Betty White', 1);
  • SELECT * FROM employees JOIN departments ON departments.id = employees.department_id; // Will return just Bea Arthur
  1. Can go on to do left, right, full outer JOIN - most common is the inner join which is displaying the inner portion of the Venn Diagram

sql-demo's People

Contributors

karaatfullstack avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.