CODE: Loading Our Book Data
1. First create TABLE with the following code:
DROP DATABASE IF EXISTS book_shop;
CREATE DATABASE book_shop;
USE book_shop;
CREATE TABLE books
(
book_id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(100),
author_fname VARCHAR(100),
author_lname VARCHAR(100),
released_year INT,
stock_quantity INT,
pages INT,
PRIMARY KEY(book_id)
);
INSERT INTO books (title, author_fname, author_lname, released_year, stock_quantity, pages)
VALUES
('The Namesake', 'Jhumpa', 'Lahiri', 2003, 32, 291),
('Norse Mythology', 'Neil', 'Gaiman',2016, 43, 304),
('American Gods', 'Neil', 'Gaiman', 2001, 12, 465),
('Interpreter of Maladies', 'Jhumpa', 'Lahiri', 1996, 97, 198),
('A Hologram for the King: A Novel', 'Dave', 'Eggers', 2012, 154, 352),
('The Circle', 'Dave', 'Eggers', 2013, 26, 504),
('The Amazing Adventures of Kavalier & Clay', 'Michael', 'Chabon', 2000, 68, 634),
('Just Kids', 'Patti', 'Smith', 2010, 55, 304),
('A Heartbreaking Work of Staggering Genius', 'Dave', 'Eggers', 2001, 104, 437),
('Coraline', 'Neil', 'Gaiman', 2003, 100, 208),
('What We Talk About When We Talk About Love: Stories', 'Raymond', 'Carver', 1981, 23, 176),
("Where I'm Calling From: Selected Stories", 'Raymond', 'Carver', 1989, 12, 526),
('White Noise', 'Don', 'DeLillo', 1985, 49, 320),
('Cannery Row', 'John', 'Steinbeck', 1945, 95, 181),
('Oblivion: Stories', 'David', 'Foster Wallace', 2004, 172, 329),
('Consider the Lobster', 'David', 'Foster Wallace', 2005, 92, 343);
2. Then source that file
source book_data.sql
3. Now check your work:
DESC books;
SELECT * FROM books;
=======================================================================================================================
CODE: Working With CONCAT
SELECT author_fname, author_lname FROM books;
CONCAT(x,y,z) // from slides
CONCAT(column, anotherColumn) // from slides
CONCAT(author_fname, author_lname)
CONCAT(column, 'text', anotherColumn, 'more text')
CONCAT(author_fname, ' ', author_lname)
CONCAT(author_fname, author_lname); // invalid syntax
SELECT CONCAT('Hello', 'World');
SELECT CONCAT('Hello', '...', 'World');
SELECT
CONCAT(author_fname, ' ', author_lname)
FROM books;
SELECT
CONCAT(author_fname, ' ', author_lname)
AS 'full name'
FROM books;
SELECT author_fname AS first, author_lname AS last,
CONCAT(author_fname, ' ', author_lname) AS full
FROM books;
SELECT author_fname AS first, author_lname AS last,
CONCAT(author_fname, ', ', author_lname) AS full
FROM books;
SELECT CONCAT(title, '-', author_fname, '-', author_lname) FROM books;
SELECT
CONCAT_WS(' - ', title, author_fname, author_lname)
FROM books;
99. Working with CONCAT
101. Introducing SUBSTRING
=======================================================================================================================
CODE: Introducing SUBSTRING
SELECT SUBSTRING('Hello World', 1, 4);
SELECT SUBSTRING('Hello World', 7);
SELECT SUBSTRING('Hello World', 3, 8);
SELECT SUBSTRING('Hello World', 3);
SELECT SUBSTRING('Hello World', -3);
SELECT SUBSTRING('Hello World', -7);
SELECT title FROM books;
SELECT SUBSTRING("Where I'm Calling From: Selected Stories", 1, 10);
SELECT SUBSTRING(title, 1, 10) FROM books;
SELECT SUBSTRING(title, 1, 10) AS 'short title' FROM books;
SELECT SUBSTR(title, 1, 10) AS 'short title' FROM books;
SELECT CONCAT
(
SUBSTRING(title, 1, 10),
'...'
)
FROM books;
SELECT CONCAT
(
SUBSTRING(title, 1, 10),
'...'
) AS 'short title'
FROM books;
Introducing SUBSTRING
Introducing REPLACE
=======================================================================================================================
CODE: Introducing REPLACE
SELECT REPLACE('Hello World', 'Hell', '%$#@');
SELECT REPLACE('Hello World', 'l', '7');
SELECT REPLACE('Hello World', 'o', '0');
SELECT REPLACE('HellO World', 'o', '*');
SELECT
REPLACE('cheese bread coffee milk', ' ', ' and ');
SELECT REPLACE(title, 'e ', '3') FROM books;
-- SELECT
-- CONCAT
-- (
-- SUBSTRING(title, 1, 10),
-- '...'
-- ) AS 'short title'
-- FROM books;
SELECT
SUBSTRING(REPLACE(title, 'e', '3'), 1, 10)
FROM books;
SELECT
SUBSTRING(REPLACE(title, 'e', '3'), 1, 10) AS 'weird string'
FROM books;
- 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.
=======================================================================================================================
CODE: Using REVERSE
SELECT REVERSE('Hello World');
SELECT REVERSE('meow meow');
SELECT REVERSE(author_fname) FROM books;
SELECT CONCAT('woof', REVERSE('woof'));
SELECT CONCAT(author_fname, REVERSE(author_fname)) FROM books;
=======================================================================================================================
CODE: Working with CHAR LENGTH
SELECT CHAR_LENGTH('Hello World');
SELECT author_lname, CHAR_LENGTH(author_lname) AS 'length' FROM books;
SELECT CONCAT(author_lname, ' is ', CHAR_LENGTH(author_lname), ' characters long') FROM books;
=======================================================================================================================
CODE: Changing Case with UPPER and LOWER
SELECT UPPER('Hello World');
SELECT LOWER('Hello World');
SELECT UPPER(title) FROM books;
SELECT CONCAT('MY FAVORITE BOOK IS ', UPPER(title)) FROM books;
SELECT CONCAT('MY FAVORITE BOOK IS ', LOWER(title)) FROM books;
Note about string functions
I have two important notes for you.
Firstly, before you move onto the next, please remember that order is important when dealing with combining/wrapping certain string functions.
For example:
This works:
SELECT UPPER(CONCAT(author_fname, ' ', author_lname)) AS "full name in caps"
FROM books;
While this does not:
SELECT CONCAT(UPPER(author_fname, ' ', author_lname)) AS "full name in caps"
FROM books;
UPPER only takes one argument and CONCAT takes two or more arguments, so they can't be switched in that way.
You could do it this way, however:
SELECT CONCAT(UPPER(author_fname), ' ', UPPER(author_lname)) AS "full name in caps"
FROM books;
=======================================================================================================================
CODE: String Function Challenges Solution
SELECT REVERSE(UPPER('Why does my cat look at me with such hatred?'));
SELECT UPPER(REVERSE('Why does my cat look at me with such hatred?'));
SELECT REPLACE(CONCAT('I', ' ', 'like', ' ', 'cats'), ' ', '-');
SELECT REPLACE(title, ' ', '->') AS title FROM books;
SELECT
author_lname AS forwards,
REVERSE(author_lname) AS backwards
FROM books;
SELECT
UPPER
(
CONCAT(author_fname, ' ', author_lname)
) AS 'full name in caps'
FROM books;
SELECT
CONCAT(title, ' was released in ', released_year) AS blurb
FROM books;
SELECT
title,
CHAR_LENGTH(title) AS 'character count'
FROM books;
SELECT
CONCAT(SUBSTRING(title, 1, 10), '...') AS 'short title',
CONCAT(author_lname, ',', author_fname) AS author,
CONCAT(stock_quantity, ' in stock') AS quantity
FROM books;
CODE: Seed Data: Adding A Couple New Books
INSERT INTO books
(title, author_fname, author_lname, released_year, stock_quantity, pages)
VALUES ('10% Happier', 'Dan', 'Harris', 2014, 29, 256),
('fake_book', 'Freida', 'Harris', 2001, 287, 428),
('Lincoln In The Bardo', 'George', 'Saunders', 2017, 1000, 367);
SELECT title FROM books;
Comments
Post a Comment