Git Product home page Git Product logo

u3_lab_sql_practice's Introduction

SQL Practice

Getting Started

  • Fork and Clone

Creating Our Database

$ createdb library

Note that this is a command-line utility that ships with Postgres, as an alternate to using the SQL command CREATE DATABASE library; inside psql.

That means you should run this command from your Bash prompt -- not from inside psql.

Inspecting The Schema

Look critically at each line of the provided schema.sql file. Here's how one row breaks down...

id SERIAL PRIMARY KEY

  • id: column name, how we will refer to this column
  • SERIAL: the data type (similar to integer or string). It's a special datatype for unique identifier columns, which the db auto-increments.
  • PRIMARY KEY: a special constraint which indicates a unique identifier for each row

Take a few minutes to research the other rows.

Load The Schema

Load the schema into your database from the command line...

$ psql -d library < schema.sql

This command is also run from your Bash prompt -- not inside psql

Loading A Seed File

We've provided a sql file that adds sample data into our library database.

Load that in so we can practice interacting with our data. Make sure to also look at its contents and see how authors and books are related.

$ psql -d library < seed.sql

Performing CRUD actions with SQL

CRUD stands for the most basic interactions we want to have with any database: Create, Read, Update and Destroy.

The most common SQL commands correspond to these 4 actions...

  • INSERT -> Create a row
  • SELECT -> Read / get information for rows
  • UPDATE -> Update a row
  • DELETE -> Destroy a row

First, enter into the library DB...

$ psql
$ \c library

INSERT

INSERT adds a row to a table...

INSERT INTO authors(name, nationality, birth_year) VALUES ('Adam Bray', 'United States of America', 1985);

SELECT

SELECT returns rows from a table...

-- select all columns from all rows
SELECT * FROM authors;

-- select only some columns, from all rows
SELECT name, birth_year FROM authors;

-- select rows that meet certain criteria
SELECT * FROM authors WHERE name = 'James Baldwin';

UPDATE

UPDATE updates values for one or more rows...

UPDATE authors SET name = 'Adam B.', birth_year = 1986 WHERE name = 'Adam Bray';

DELETE

DELETE removes rows from a table...

DELETE FROM authors WHERE name = 'Adam B.';

End of You Do: Building Our Database


Exercises

There is one exercise:

Resources

u3_lab_sql_practice's People

Contributors

anpato avatar ahonore42 avatar adambray avatar amaseda avatar brunopgalvao avatar marveldylan avatar nobodyslackey avatar kdabug 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.