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.
115 lines
4.4 KiB
115 lines
4.4 KiB
# Intro to SQL
|
|
|
|
## Video Link
|
|
|
|
[Intro to SQL](https://www.youtube.com/watch?v=nZfVXH4CBGg&list=PLdnONIhPScSQqXfMndCJRYWQl-0uApORf&index=22)
|
|
|
|
## Lesson Objectives tools
|
|
1. Connect to Postgres through CLI
|
|
1. Create a Database
|
|
1. Create a table
|
|
1. Insert into the table
|
|
1. Select from table
|
|
1. Update the table
|
|
1. 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:
|
|
|
|
```sql
|
|
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:
|
|
|
|
```sql
|
|
\l
|
|
```
|
|
|
|
## Create a Database
|
|
|
|
Like MongoDB, Postgres has "sub-databases":
|
|
|
|
```SQL
|
|
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:
|
|
|
|
1. int - whole number
|
|
1. decimal - float/decimal
|
|
1. bool - boolean
|
|
1. varchar(n) - small text
|
|
1. text - large text
|
|
1. 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'
|
|
|
|
```sql
|
|
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
|
|
|
|
```sql
|
|
INSERT INTO users ( name, age, email ) VALUES ( 'Matt', 36, 'matt.huntington@generalassemb.ly'); -- create a row
|
|
```
|
|
|
|
## Select from table
|
|
|
|
```sql
|
|
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
|
|
|
|
```sql
|
|
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
|
|
|
|
```sql
|
|
DELETE FROM users WHERE id = 1; -- delete all rows from the users table that have the id column set to 1
|
|
```
|