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.

5.6 KiB

MySQL Bootcamp

Lesson Objectives

  1. What is a DB?
  2. What are the different ways to store data?
  3. What are the advantages of a database?
  4. What does SQL Stand for? What is it?
  5. Diagram MySQL structure
-- CREATE DB/TABLES
SHOW DATABASES; -- show all sub databases
CREATE DATABASE our_database_name; -- create a sub database
USE our_database_name; -- use that sub database
CREATE TABLE people (first_name VARCHAR(20), age INT); -- craete a table with a text first_name column and an integer age column
DESCRIBE people; -- look at the table's schema

-- CRUD
INSERT INTO people (first_name, age) VALUES ('Matt' , 34); -- insert a row into people table
SELECT age FROM people; -- find all people, display just the age column
SELECT * FROM people; --  find all people, display all columns
UPDATE people SET weight = 300 WHERE first_name = 'Bill'; -- set weight to 300 for all rows that have a first_name column set to Bill
DELETE FROM people WHERE first_name = "Bill"; -- delete all rows that have a first_name column set to Bill

-- OPERATORS
SELECT * FROM people WHERE age != 63; -- find all rows that have an age that doesn't equal 63
SELECT * FROM people WHERE age < 63; -- find all rows that have an age less than 63
SELECT * FROM people WHERE age > 63; -- find all rows that have an age greater than 63
SELECT * FROM people WHERE age >= 63; -- find all rows that have an age greater or equal to than 63
SELECT * FROM people WHERE age <= 63; -- find all rows that have an age less than or equal to than 63
SELECT * FROM people WHERE first_name LIKE "%Charlie%"; -- find all rows that have a first_name that contains the value Charlie
SELECT * FROM people WHERE first_name NOT LIKE "%Charlie%"; -- find all rows that have a first_name that does not contain the value Charlie
SELECT * FROM people WHERE age IS NULL; -- find all rows that do not have any value for the age column
SELECT * FROM people WHERE age IS NOT NULL; -- find all rows that have any value for the age column


-- AND/OR
SELECT * FROM people WHERE first_name = 'Matt' AND age = 43; -- find all rows that have a first name set to Matt AND also have an age set to 43
SELECT * FROM people WHERE first_name = 'Matt' OR age = 49; -- find all rows that either have a first_name set to Matt OR have an age set to 49

-- ORDER
SELECT * FROM people ORDER BY age DESC; -- find all rows, but sort them in order of decreasing age
SELECT * FROM people ORDER BY first_name DESC; -- find all rows, but sort them in order of decreasing first_name values (reverse alphabetal)
SELECT * FROM people ORDER BY age ASC LIMIT 2; -- limit the results to 2
SELECT * FROM people ORDER BY age ASC LIMIT 2 OFFSET 3; -- limit the results to 2, but don't show the first 3 records
SELECT * FROM people ORDER BY age DESC, first_name ASC; -- order by age descending, for any rows that have the same age value, order those rows by first_name ascending

-- ALTER TABLE
ALTER TABLE people ADD COLUMN weight FLOAT; -- add a weight column that is a FLOAT (decimal number)
ALTER TABLE people DROP COLUMN height; -- drop the height column
ALTER TABLE people MODIFY COLUMN height FLOAT; -- change the height column to be a FLOAT (decimal number)

ALTER TABLE people ADD COLUMN height FLOAT AFTER first_name; -- add a heigh column after the first_name column
ALTER TABLE people MODIFY COLUMN height FLOAT AFTER age; -- move an existing height column so that it is after the age column

ALTER TABLE people ADD COLUMN id INT FIRST; -- add an id column in the first position
ALTER TABLE people MODIFY COLUMN height FLOAT FIRST; -- move an existing height column to the first position

ALTER TABLE people ADD COLUMN dob DATETIME; -- create a dob column which is a date
ALTER TABLE people CHANGE dob date_of_birth DATETIME; -- change the name of the dob column to date_of_birth

ALTER TABLE people ADD COLUMN id INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST; -- create an id column that increments with each new row created

-- AGGREGATION
SELECT COUNT(*), age FROM people GROUP BY age; -- separate the rows into groups, based on having equivalent ages. Count how many rows are in each group
SELECT SUM(salary), age FROM people GROUP BY age; -- group by age, display the SUM of all salaries in the group and the age for that group
SELECT AVG(salary), age FROM people GROUP BY age; -- group by age, show average salary for everyone in the group
SELECT MIN(salary), age FROM people GROUP BY age; -- show mininimum salary for the group
SELECT MAX(salary), age FROM people GROUP BY age; -- show max salary for the group
SELECT GROUP_CONCAT(first_name), age FROM people GROUP BY age; -- create a list of all the first_names in the group
SELECT GROUP_CONCAT(first_name), age, height FROM people GROUP BY age, height; -- create a group based on people having the same age and the same height

-- JOINS
SELECT * from people JOIN companies; -- combine each row of the people table with each row of the companies table
SELECT * FROM people JOIN companies ON people.employer_id = companies.id; -- combine the people table with the companies table and show only the rows where the employer_id of the people table matches the id of the companies table
SELECT * FROM people RIGHT JOIN companies ON people.employer_id = companies.id; -- do the same as above, but show any rows from the companies table that might have been left off, due to there not being any people that have an employer_id that matches the companies id
SELECT * FROM people LEFT JOIN companies ON people.employer_id = companies.id; -- do the same as above, but show any rows from the people table that might have been left off, due to there not being any companies that have an id that matches the person's employer_id