rbi-learning / Today-I-Learned

1 stars 0 forks source link

Today I Learned 08/13 #134

Open paulapereira1 opened 4 years ago

paulapereira1 commented 4 years ago

Day 13 Lesson

Basics & Refreshers

SQL Lesson When deploying something to Heroku —> Heroku is a hosting/deployment platform (i.e. you can push your code to display in the internet) for small and mid-sized projects

  1. heroku create unique-name
  2. git push heroku master //this pushes master branch to heroic in order to deploy project NOTE: this is a server, and when servers restart your data does not get saved; that’s why we are working in a more permanent solution, which is storing our information in a database… Unlike servers (APIs), databases don’t get restarted in ways that wipe your data

To have access to express, we use the package manager, typically Yarn:

  1. yarn init -y // -y tells it to answer yes to all questions Note: package.json holds all the third party dependencies that we need for our project, like express
  2. yarn add express
    • app.use are middle bits of functionalities that we enable our express server to do
    • .gitignore file is where you place everything you want your git to ignore (generally we don’t want to commit our node_module folders because they are huge, so we’d put them into .gitignore)

In order to run a javascript file (much like we do with npx serve) use node index.js Then, use Postman or your web browser to run http://localhost:3000/ Note: much like on real we click play and stop to run our commands, we need to do this manually by killing (control+C) and restarting (yarn start) our server to see the changes

Setting up connection to SQL database (in Heroku)

  1. Make a new directory named pg_demo i.e. mkdir pg_demo and cd into it
  2. git init // this will make a new repository
  3. git remote add heroic https://git.heroku.com/rbi=postgres-demo.git // connect your git to heroku
  4. heroku login // note: if signing in through the browser fails (i.e. no trust message or “waiting for login…” message), control+c and write heroic login -i to sign in through your local terminal ** This step is important because it will only work if you’re logged into Heroku ***
  5. heroku pg:psql//this enables a direct connection to the database which is shared in Heroku
  6. To exit this use ` or control+d

SQL Queries: Select —> allows you to “select all” i.e. to pull the table data that is shared use `select from bootcamp_people order by id;`

  1. Select Recipe
    Select ____ //the column or columns to be searched
    From ___; //the table the data is in - all sql statements must have a `;` at the end to complete the statement 
  2. You can filter the data by adding a “WHERE” clause to the end of the recipe statement:
    Select ___
    From ___
    Where____;// “where” filters out other information and will return only the match that is requested (i.e. id = 2)
    • In sequel, ‘’ are used for whole string character values (i.e. movie title ‘The Kid’)
  3. We can sort the information by using ORDER BY; we can select a category (i.e. ORDER BY duration;) and use commands like DESC (i.e. ORDER BY duration DESC;) to sort by descending order; the default is ascending order
    • We can also use comparison operators (i.e, >, <,=, <=, >=, <> or != which are used as “do not equal to”)to set parameters (i.e. WHERE duration > 100;)
  4. Adding more filters with “AND” allows us to segment the data further, but the WHERE and AND conditions must be met i.e.
    SELECT title
    FROM movies
    WHERE id = 1 
    AND genre = ‘Comedy’;

    —> will return movies with the id of 1 that are also comedies ; the WHERE is first located, and those items are then searched for the AND

  5. “OR” in the other hand will allow us to do the same but what is returned may either contained what is signified by the WHERE or the OR i.e.
    SELECT title
    FROM movies
    WHERE id = 1 
    OR genre = ‘Comedy’; // will return movies that either have the id of 1 OR the genre of comedy
  6. To add a new item to a SQL table we need an INSERT statement…here’s the recipe:
    INSERT INTO ____ (_____) // The first blank indicates the table we want to add the data to and the second blank the column(s) we want to add it to 
    VALUES (_____); // In this blank we place the actual data values that will be added 
    • Empty cells are considered to have a NULL value
  7. To update values in a table use the UPDATE statement:
    UPDATE ____ // The table the data is in
    SET ____ = ____ // The first blank sets the column to be changed and the second blank is what you want the new value to be
    WHERE _____ // This pinpoints where the change will take place (i.e. id = 1;)

    i.e. Changing Pizza to Hamburger:

    UPDATE concessions
    SET  item = 'Hamburger'
    WHERE id = 8;
  8. DELETE statement can be used to remove e rows that match specific conditions DELETE Recipe:
    DELETE FROM movies WHERE id = 5; // where is very important because it identifies which rows to remove 
  9. To create a new database, use the CREATE DATABASE Recipe:
    CREATE DATABASE _____; —> this creates a new empty database // name of database goes in the blank
  10. To remove a database we can use the DROP DATABASE Recipe:
    DROP DATABASE ____; —> deleting a database is permanent // name of database goes in the blank
  11. Once the database is created, we can create a new table inside using the CREATE TABLE Recipe:
    CREATE TABLE ____ // name of table goes in the blank
    (
    Column_name 1 datatype,
    column_name 2 datatype,
    column_name 3 datatype,
    …
    );

    i.e. 1

    CREATE TABLE movies
    (
    Id int,
    Title varchar (50),
    Genre archer(100), 
    Duration int // int is used to tell the database that these columns will hold numbers 
    );

    i.e. 2

    Create table bootcamp people (
    id serial primary key,
    first_name varchar(128) not null, // varchar defines the max number of characters
    last_name varchar(128) not null, // not null tells it that people are not allowed to leave first_name as an empty value 
    age integer not null, 
    current_mood varchar(128),
    sql_confidence integer check (sql_confidence > 0), check (sql_confidence <= 10) 
    );
  12. To remove a table from a database, we can use the DROP TABLE RECIPE:
    Drop Table ____ ; // name of database goes in the blank
  13. To add, modify and remove columns with can use the… ALTER TABLE ADD RECIPE:
    ALTER TABLE ____ // the table to make changes to 
    ADD COLUMN ______ ____; // The first blank is the name of the new column and the second blank is the data type of the new column  
  14. To update a table, we need to issue separate update commands ALTER TABLE DROP:
    ALTER TABLE____ // name of table we want to target
    DROP COLUMN_____; // name of column we want to drop 
    • A datatype is serial means that the first entry will be 1 and it should auto-increment by 1 each time