You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
4.4 KiB
4.4 KiB
Intro to SQL
Video Link
Lesson Objectives tools
- Connect to Postgres through CLI
- Create a Database
- Create a table
- Insert into the table
- Select from table
- Update the table
- Delete from table
Connect to Postgres through CLI
Since we used homebrew to install, use this command to start up the postgres enviornment (this is like running mongod when we were using MongoDB).
brew services start postgresql
You can use this command to stop the service if you would like or you can just leave it running in the background at all times (it will disconnect when you shut down your machine).
brew services stop postgresql
Lets start up the postgres shell:
psql -l (list all subdatabses)
psql db_name (start psql shell, using the sub database db_name)
psql (start psql shell connecting to your username)
Once inside the psql app, you can list the sub databases like this:
\l
Create a Database
Like MongoDB, Postgres has "sub-databases":
CREATE DATABASE foo; -- create the sub database foo
DROP DATABASE foo; -- drop it
CREATE DATABASE test_db;
\connect test_db; -- connect to the test_db sub database
Data types
Postgres has the following data types:
- int - whole number
- decimal - float/decimal
- bool - boolean
- varchar(n) - small text
- text - large text
- timestamp - date
Create a table
- Instead of collections, we have tables, which are just like a spreadsheet, or grid. Rows are entries, and columns are properties of each row.
- Unlike MongoDB, you have to tell Postgres, what data type each column is. It's very 'strict'
CREATE TABLE foo ( name varchar(20) ); -- create a table called 'foo' with one column called 'name' which is a small text column
\dt -- describe your tables
DROP TABLE foo; -- drop a table
CREATE TABLE users ( id serial, name varchar(20), age int, email varchar(32) ); -- 'test' table has an id column, which is just a number, and a name column
\d users; -- describe the columns of the test sub database
Insert into the table
INSERT INTO users ( name, age, email ) VALUES ( 'Matt', 36, 'matt.huntington@generalassemb.ly'); -- create a row
Select from table
SELECT name FROM users; -- select all rows from the users table. display only the name column
SELECT * FROM users; -- select all rows from the users table. display only the all columns
SELECT * FROM users WHERE name = 'Matt'; -- select all rows from the user table where the name column is set to 'Matt'
SELECT * FROM users WHERE name LIKE '%Matt%'; -- select all rows from the user table where the name column contains 'Matt'
SELECT * FROM users WHERE name = 'Matt' AND email = 'matt.huntington@generalassemb.ly'; -- select all rows from the user table where the name column is set to 'Matt' AND the email column is set to matt.huntington@generalassemb.ly
SELECT * FROM users WHERE name = 'Matt' OR email = 'matt.huntington@generalassemb.ly'; -- select all rows from the user table where either the name column is set to 'Matt' OR the email column is set to matt.huntington@generalassemb.ly
SELECT * FROM users WHERE age = 36; -- select all rows from the user table where the age column is set to 36
SELECT * FROM users WHERE age != 16; -- select all rows from the user table where the age column is not set to 16
SELECT * FROM users WHERE age > 26; -- select all rows from the user table where the age column is greater than 26
SELECT * FROM users WHERE age < 46; -- select all rows from the user table where the age column is less than 26
SELECT * FROM users WHERE age <= 36; -- select all rows from the user table where the age column is less than or equal to 36
SELECT * FROM users WHERE age >= 36; -- select all rows from the user table where the age column is greater than or equal to 36
SELECT * FROM users WHERE age IS NULL; -- select all rows from the user table where the age column has no value
SELECT * FROM users WHERE age IS NOT NULL; -- select all rows from the user table where the age column has any value
Update the table
UPDATE users SET name = 'Matthew' WHERE id = 1; -- update the users table. Set the name column to 'Matthew' for every row that has the id column set to 1
Delete from table
DELETE FROM users WHERE id = 1; -- delete all rows from the users table that have the id column set to 1