CODE: Dropping Databases
CODE: Dropping Databases
mysql-ctl cli;
show databases;
A specific example:
For Example:
Remember to be careful with this command! Once you drop a database, it's gone!
A specific example:
DROP TABLE cats; ---- Be careful with this command!
The "formula":
1. INSERT INTO table_name(column_name) VALUES (data);
For example:
1. INSERT INTO cats(name, age) VALUES ('Jetson', 7);
show tables;
MySQL Warnings Code
DESC cats;
SHOW WARNINGS;
SHOW WARNINGS;
DESC cats2;
SHOW WARNINGS;
SHOW WARNINGS;
DESC cats3;
DESC unique_cats;
Adding in AUTO_INCREMENT:
1. CREATE TABLE unique_cats2 (
2. cat_id INT NOT NULL AUTO_INCREMENT,
3. name VARCHAR(100),
4. age INT,
5. PRIMARY KEY (cat_id)
6. );
A test INSERT:
1. INSERT INTO employees(first_name, last_name, age) VALUES
2. ('Dora', 'Smith', 58);
DESC cats;
Select by name:
Another update:
Exit – Ctrl + C
12.
13. source first_file.sql
14.
15. DESC cats;
16.
17.
18.
19. INSERT INTO cats(name, age)
20. VALUES('Charlie', 17);
21.
22. INSERT INTO cats(name, age)
23. VALUES('Connie', 10);
24.
25. SELECT * FROM cats;
26.
27. source testing/insert.sql
CODE: Loading Our Book Data
1. First create your book_data.sql file with the following code:
1. DROP DATABASE IF EXISTS book_shop;
2. CREATE DATABASE book_shop;
3. USE book_shop;
4.
5. CREATE TABLE books
6. (
7. book_id INT NOT NULL AUTO_INCREMENT,
8. title VARCHAR(100),
9. author_fname VARCHAR(100),
10. author_lname VARCHAR(100),
11. released_year INT,
12. stock_quantity INT,
13. pages INT,
14. PRIMARY KEY(book_id)
15. );
16.
17. INSERT INTO books (title, author_fname, author_lname, released_year, stock_quantity, pages)
18. VALUES
19. ('The Namesake', 'Jhumpa', 'Lahiri', 2003, 32, 291),
20. ('Norse Mythology', 'Neil', 'Gaiman',2016, 43, 304),
21. ('American Gods', 'Neil', 'Gaiman', 2001, 12, 465),
22. ('Interpreter of Maladies', 'Jhumpa', 'Lahiri', 1996, 97, 198),
23. ('A Hologram for the King: A Novel', 'Dave', 'Eggers', 2012, 154, 352),
24. ('The Circle', 'Dave', 'Eggers', 2013, 26, 504),
25. ('The Amazing Adventures of Kavalier & Clay', 'Michael', 'Chabon', 2000, 68, 634),
26. ('Just Kids', 'Patti', 'Smith', 2010, 55, 304),
27. ('A Heartbreaking Work of Staggering Genius', 'Dave', 'Eggers', 2001, 104, 437),
28. ('Coraline', 'Neil', 'Gaiman', 2003, 100, 208),
29. ('What We Talk About When We Talk About Love: Stories', 'Raymond', 'Carver', 1981, 23, 176),
30. ("Where I'm Calling From: Selected Stories", 'Raymond', 'Carver', 1989, 12, 526),
31. ('White Noise', 'Don', 'DeLillo', 1985, 49, 320),
32. ('Cannery Row', 'John', 'Steinbeck', 1945, 95, 181),
33. ('Oblivion: Stories', 'David', 'Foster Wallace', 2004, 172, 329),
34. ('Consider the Lobster', 'David', 'Foster Wallace', 2005, 92, 343);
Notes:
- Use cmd + / (mac) or ctrl + / (pc) to comment out SQL in c9.
- The REPLACE() function, as well as the other string functions, only change the query output,
they don't affect the actual data in the database.
Resource: sql-format.com
SELECT REPLACE(CONCAT('I', ' ', 'like', ' ', 'cats'), ' ', '-');
1. SELECT
2. author_lname AS forwards,
3. REVERSE(author_lname) AS backwards
4. FROM books;
1. SELECT
2. UPPER
3. (
4. CONCAT(author_fname, ' ', author_lname)
5. ) AS 'full name in caps'
6. FROM books;
1. SELECT
2. CONCAT(title, ' was released in ', released_year) AS blurb
3. FROM books;
1. SELECT
2. title,
3. CHAR_LENGTH(title) AS 'character count'
4. FROM books;
1. SELECT
2. CONCAT(SUBSTRING(title, 1, 10), '...') AS 'short title',
3. CONCAT(author_lname, ',', author_fname) AS author,
4. CONCAT(stock_quantity, ' in stock') AS quantity
5. FROM books;
CODE: DECIMAL
1. CREATE TABLE items(price DECIMAL(5,2));
2.
3. INSERT INTO items(price) VALUES(7);
4.
5. INSERT INTO items(price) VALUES(7987654);
6.
7. INSERT INTO items(price) VALUES(34.88);
8.
9. INSERT INTO items(price) VALUES(298.9999);
10.
11. INSERT INTO items(price) VALUES(1.9999);
12.
13. SELECT * FROM items;
Please note, as of MySQL 8.0.17, the && operator is deprecated and support for it will be
removed in a future MySQL version. Applications should be adjusted to use the standard
SQL AND operator.
If you're using MySQL 5.7 or older, which most of you are if you're using GoormIDE, then
you don't have to worry about this right now. But, in newer versions of MySQL (8.0.17 and
newer) you will need to replace && with AND .
- source
CODE: Logical OR
1. SELECT
2. title,
3. author_lname,
4. released_year
5. FROM books
6. WHERE author_lname='Eggers' || released_year > 2010;
7.
8.
9. SELECT 40 <= 100 || -2 > 0;
10. -- true
11.
12. SELECT 10 > 5 || 5 = 5;
13. -- true
14.
15. SELECT 'a' = 5 || 3000 > 2000;
16. -- true
17.
18. SELECT title,
19. author_lname,
20. released_year,
21. stock_quantity
22. FROM books
23. WHERE author_lname = 'Eggers'
24. || released_year > 2010
25. OR stock_quantity > 100;
Please note, as of MySQL 8.0.17, the || operator is deprecated and support for it will be
removed in a future MySQL version. Applications should be adjusted to use the standard
SQL OR operator.
If you're using MySQL 5.7 or older, which most of you are if you're using GoormIDE, then
you don't have to worry about this right now. But, in newer versions of MySQL (8.0.17 and
newer) you will need to replace || with OR .
- source
CODE: Between
1. SELECT title, released_year FROM books WHERE released_year >= 2004 && released_year <= 2015;
2.
3. SELECT title, released_year FROM books
4. WHERE released_year BETWEEN 2004 AND 2015;
5.
6. SELECT title, released_year FROM books
7. WHERE released_year NOT BETWEEN 2004 AND 2015;
8.
9. SELECT CAST('2017-05-02' AS DATETIME);
10.
11. show databases;
12.
13. use new_testing_db;
14.
15. SELECT name, birthdt FROM people WHERE birthdt BETWEEN '1980-01-01' AND '2000-01-01';
16.
17. SELECT
18. name,
19. birthdt
20. FROM people
21. WHERE
22. birthdt BETWEEN CAST('1980-01-01' AS DATETIME)
23. AND CAST('2000-01-01' AS DATETIME);
1. SELECT
2. first_name,
3. last_name,
4. SUM(amount) AS total_spent
5. FROM customers
6. JOIN orders
7. ON customers.id = orders.customer_id
8. GROUP BY orders.customer_id
9. ORDER BY total_spent DESC;
-- LEFT JOINS
1. SELECT
2. first_name,
3. last_name,
4. IFNULL(SUM(amount), 0) AS total_spent
5. FROM customers
6. LEFT JOIN orders
7. ON customers.id = orders.customer_id
8. GROUP BY customers.id
9. ORDER BY total_spent;
1. SELECT
2. IFNULL(first_name,'MISSING') AS first,
3. IFNULL(last_name,'USER') as last,
4. order_date,
5. amount,
6. SUM(amount)
7. FROM customers
8. RIGHT JOIN orders
9. ON customers.id = orders.customer_id
10. GROUP BY first_name, last_name;
-- ALT SOLUTION
-- PROBLEM 2
-- PROBLEM 3
1. SELECT
2. first_name,
3. IFNULL(title, 'MISSING'),
4. IFNULL(grade, 0)
5. FROM students
6. LEFT JOIN papers
7. ON students.id = papers.student_id;
-- PROBLEM 4
1. SELECT
2. first_name,
3. IFNULL(AVG(grade), 0) AS average
4. FROM students
5. LEFT JOIN papers
6. ON students.id = papers.student_id
7. GROUP BY students.id
8. ORDER BY average DESC;
-- PROBLEM 5
1. SELECT first_name,
2. Ifnull(Avg(grade), 0) AS average,
3. CASE
4. WHEN Avg(grade) IS NULL THEN 'FAILING'
5. WHEN Avg(grade) >= 75 THEN 'PASSING'
6. ELSE 'FAILING'
7. end AS passing_status
8. FROM students
9. LEFT JOIN papers
10. ON students.id = papers.student_id
11. GROUP BY students.id
12. ORDER BY average DESC;
CODE: Creating Our Tables
-- CREATING THE REVIEWERS TABLE
1. SELECT
2. title,
3. rating
4. FROM series
5. JOIN reviews
6. ON series.id = reviews.series_id;
1. SELECT
2. title,
3. AVG(rating) as avg_rating
4. FROM series
5. JOIN reviews
6. ON series.id = reviews.series_id
7. GROUP BY series.id
8. ORDER BY avg_rating;
1. SELECT
2. first_name,
3. last_name,
4. rating
5. FROM reviewers
6. INNER JOIN reviews
7. ON reviewers.id = reviews.reviewer_id;
1. SELECT
2. first_name,
3. last_name,
4. rating
5. FROM reviews
6. INNER JOIN reviewers
7. ON reviewers.id = reviews.reviewer_id;
1. SELECT
2. title,
3. rating,
4. CONCAT(first_name,' ', last_name) AS reviewer
5. FROM reviewers
6. INNER JOIN reviews
7. ON reviewers.id = reviews.reviewer_id
8. INNER JOIN series
9. ON series.id = reviews.series_id
10. ORDER BY title;