The MySQL Book
Notes on how to use the database
How to install and connect to the database via Docker
// (1) Download the latest version of MySQl from the Docker hub
debugme@desktop ~> docker pull mysql/mysql-server:latest
// (2) Start up a container based on the downloaded MySQL image
debugme@desktop ~> docker run --name=mysqlbox -d mysql/mysql-server:latest
// (3) Find out what the root password is for MySQL
debugme@desktop ~> docker logs mysqlbox 2>&1 | grep GENERATED
// (4) Log into MySQL on the container (Use root password when prompted)
debugme@desktop ~> docker exec -it mysqlbox mysql -u root -p
// (5) Change root password to one of your choice (e.g. 'password')
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
// (6) Exit the container
mysql> \q
// (7) Log into MySQL on the container (Use your new password when prompted)
debugme@desktop ~> docker exec -it mysqlbox mysql -u root -p
// (8) How to login with a shell onto the MySQL docker container and then connect to mysql
debugme@desktop ~> docker exec -it sh
sh-4.2# mysql -u root -p
// (9) How to install an editor in the MySQL docker container
debugme@desktop ~> docker exec -it sh
sh-4.2# yum install vimHow to clear mysql screen from within a tmux pane
How to see what databases are installed on your database server
How to create a database
How to delete a database
How to specify the active database
How to find out what the active database is
How to see what tables are in your database
How to see what columns are in a table
How to get a description of a table
How to delete a table
How to create a table
How to insert rows into a table
How to get data from a table
How to see what warnings you get when running a command
How to see what errors you get when running a command
How to make a column required
How to disallow null as a value for a column
How to create an auto-incrementing primary key on a table
How to update a column value
How to use string functions
How to filter selections
How to use aggregate functions
How can I think about group by?
(1) If you group by one or more columns, then you can use those column names directly in the select clause, but you cannot refer to any other columns in that select clause, unless you use them in conjunction with an aggregation function.
(2) When you group by a column with possible duplicate values, imagine subtables are being created that allow you to perform aggregation functions on each subtable.
select author_lname, author_fname, pages from books group by author_lname, author_fname;
Using pages in the select clause violates the rule we just explained so this query is invalid 😖
select author_lname, author_fname, sum(pages) from books group by author_lname, author_fname;
Using pages within the sum aggregation function follows the rule so this query is valid 🥳
How to use different datatypes
How to source a file from within the mysql shell
Different date and time functions
How to model a 1:N relationship using primary and foreign keys
How select records from both tables where join condition is met (i.e. inner join)
How to select records from left table where join condition is met (i.e. left join)
How to select records from right table where join condition is met (i.e. right join)
How to automatically delete record in child table when matching record in parent table is deleted
How to create a composite primary key
Last updated