DEV Community

Cover image for mysql 101
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

mysql 101

Hello fams, I started learning a relational database (Sequel Query Language) and I decided to make a post to document my continuous progress. I stem from a NoSQL background (MongoDb). My choice software is Mysql and the Operating system used is windows. All commands should be the same. I have included examples in the Basic Queries Section

minons

Table of Content
🎯 * Installation (windows)
🎯 * Setup (Windows)
🎯 * Path Variable setup
🎯 * Basic Queries
🎯 * Conclusion
🎯 * References


Let's get right to it. Click the link Download Link to navigate to MySQL and download the MSI

msi


I use Windows, and to run my SQL commands from my command prompt we need to add the download path (containing the binary) to our Environment variable. (Highlighted in red)
path


Go to system properties πŸ‘‰ Environmental Variables πŸ‘‰ System variable πŸ‘‰ Click on path πŸ‘‰ click on edit πŸ‘‰ click new πŸ‘‰ paste the copied path highlighted in red earlier πŸ‘‰ Ok.
Now we have successfully added this path to our environment variable.
system 5
system3

Check if MySQL is running

$ mysql --version

πŸ“Œ You should get

C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe  Ver 8.0.25 for Win64 on x86_64 (MySQL Community Server - GPL)
Enter fullscreen mode Exit fullscreen mode

With that out of the way let's login to our Mysql with the commands below


login to mysql from terminal (command prompt)
mysql -u root -p
mysql -u joseph -p
Enter fullscreen mode Exit fullscreen mode

πŸ”— Create a specific user for a DataBase
CREATE USER 'joseph'@'localhost' IDENTIFIED BY 'password';

πŸ”— To see selected users
SELECT user, host FROM mysql.user;

πŸ”— Give privileges i.e full access
GRANT ALL PRIVILEGES ON * . * TO 'user'@'localhost';
GRANT ALL PRIVILEGES ON * . * TO 'joseph'@'localhost';

πŸ”— Flush privileges after granting privileges
FLUSH PRIVILEGES;

πŸ”— Check privileges for certain users
SHOW GRANTS FOR 'user'@'localhost';
SHOW GRANTS FOR 'joseph'@'localhost';

πŸ”— Exit mysql terminal to windows terminal by:
exit;

Login into mysql terminal
mysql -u joseph -p
Enter fullscreen mode Exit fullscreen mode

πŸ”— Actions
SELECT * FROM users;
Note: Actions are mostly written in Capitals.( not compulsory just a convention).

πŸ”— Create DB
CREATE DATABASE ximple;

πŸ”— Show DB
SHOW DATABASES;

πŸ”— To use the actual db
USE
USE ximple;

πŸ”— ** Show tables**
SHOW TABLES;

πŸ”— *Create tableπŸ”— *
CREATE TABLE employees(
id INT AUTO_INCREMENT,
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(75),
password VARCHAR(255),
location VARCHAR(100),
dept VARCHAR(75),
is_admin TINYINT(1),
register_date DATETIME,
PRIMARY KEY(id)
);

NB: MAX LENGTH FOR VARCHAR is 255.

πŸ”— Delete table
DROP TABLE ;
DROP TABLE users;

πŸ”— Delete Database
DROP DATABLE
DROP DATABASE ximple;

πŸ”— Inserting single data
INSERT INTO bands (name)
VALUES ('Abayomi Joseph');

πŸ”— Insert
INSERT INTO users (first_name, last_name, email, password, location, dept, is_admin, register_date)
values ('James', 'Pork','james@gmail.com', '1234', 'Lagos', 'backend', 1, now());

πŸ”— Insert multiple values separated with commas
INSERT INTO users (first_name, last_name, email, password, location, dept, is_admin, register_date)
values ('John', 'Pork','john@gmail.com', '1234', 'Ibadan', 'frontend', 0, now())<mark>,</mark>('bale', 'mario','mario@gmail.com', '1234', 'Uyo', 'backend', 1, now());

πŸ”— Select values
SELECT first_name, last_name FROM users;

πŸ”— WHERE clause
SELECT* FROM users WHERE location = 'sagamu';

πŸ”— Multiple Conditions
SELECT* FROM users WHERE location = 'sagamu' AND dept = "graphics";

πŸ”— ALTER TABLE test
ADD another_column VARCHAR(255);

πŸ”— Bands table
CREATE TABLE bands(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

πŸ”— Albums table linked to bands
CREATE TABLE albums(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
release_year INT,
band_id INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(band_id) REFERENCES bands(id)
);

πŸ”— Inserting multiple data
INSERT INTO bands (name)
VALUES ('Abayomi Joseph'), ('the maxess'), ('blues');

πŸ”— Selecting with limits
SELECT * FROM bands LIMIT 2;

πŸ”— Selecting specific property
SELECT name FROM bands;

πŸ”— Renaming using the AS alias
SELECT id AS 'ID', name AS 'Band Name'
FROM bands;

πŸ”— Ordering and sorting data
SELECT * FROM bands ORDER BY name;

πŸ”— Ordering and sorting data Descending order
SELECT * FROM bands ORDER BY name DESC;

πŸ”— Ordering and sorting data ascending order
SELECT * FROM bands ORDER BY name ASC;

πŸ”— *Ordering and sorting data Descending order II *
SELECT * FROM bands ORDER BY name;

πŸ”— ** Multiple insert**
INSERT INTO albums(name, release_year, band_id)
VALUES('the beast', 1990, 1),
('power shell', 1986, 2),
('the shark', 2009, 3),
('boom',NULL, 3);

πŸ”— Querying data
SELECT * FROM albums;
SELECT name FROM albums;

πŸ”— Selecting distinct data without duplicate
SELECT DISTINCT name FROM albums;

πŸ”— Updating data
UPDATE albums
SET release_year= 1677
WHERE id = 1;

πŸ”—** Filtering data using the WHERE **
SELECT * FROM albums
WHERE release_year < 2000;

or

SELECT * FROM albums*
WHERE release_year > 2000;

πŸ”—** Selecting using the string filter**
SELECT * FROM albums
WHERE name LIKE '%er%';

The above example is saying filter any string which has 'er' inside
Enter fullscreen mode Exit fullscreen mode

πŸ”— The OR operator
SELECT * FROM albums
WHERE name LIKE '%er%' OR band_id=3;

πŸ”— The AND clause
SELECT * FROM albums
WHERE release_year=1986 AND band_id=1;

πŸ”— Filtering between two different values
SELECT *FROM albums
WHERE release_year BETWEEN 2000 AND 2009;

πŸ”— ** Filtering NULL SELECT FROM albums*
WHERE release_year IS NULL;

πŸ”— Delete clause
DELETE FROM albums
WHERE id = 4;

πŸ”— Join tables together
SELECT * FROM bands
JOIN albums ON bands.id = albums.band_id;

πŸ”—** Inner join work as above**
SELECT * FROM bands
INNER JOIN albums ON bands.id = albums.band_id;

πŸ”—** LEFT JOIN**
SELECT * FROM bands
LEFT JOIN albums ON bands.id = albums.band_id;

πŸ”— RIGHT JOIN
SELECT * FROM albums
RIGHT JOIN bands ON bands.id = albums.band_id;


AGGREGRATE FUNCTION

πŸ”— AVERAGE
SELECT AVG(release_year) FROM albums;

πŸ”— SUM
SELECT SUM(release_year) FROM albums;

πŸ”— COUNT BY GROUP
SELECT band_id, COUNT(band_id) FROM albums
GROUP BY band_id;

πŸ”— Complex grouping
SELECT b.name AS band_name, COUNT(a.id) AS num_albums
FROM bands AS b
LEFT JOIN albums AS a ON b.id = a.band_id
GROUP BY b.id;

πŸ”— HAVING
SELECT b.name AS band_name, COUNT(a.id) AS num_albums
FROM bands AS b
LEFT JOIN albums AS a ON b.id = a.band_id
WHERE b.name = 'blues'
GROUP BY b.id
HAVING num_albums = 1;


Conclusion:

This is a quick guide into using SQL if I miss any command or you have a better way of querying kindly drop it in the comment section. Thanks πŸ™ŒπŸ½ for reading

References:

πŸ„ Mike Dane Video
πŸ„ Traversy Media Video

GitHub logo drsimplegraffiti / drsimplegraffiti

Config files for my GitHub profile.

Calm Developer

Hi πŸ‘‹, I'm Abayomi.

A Software Engineer interested in Backend

Software Engineer

A

drsimplegraffiti

drsimplegraffiti

drsimplegraffi1

Languages and Tools:

aws bash docker express javascript linux mongodb mysql nginx nodejs postgresql rabbitMQ redis typescript

drsimplegraffiti

Β drsimplegraffiti

drsimplegraffiti

Dev.to Post

πŸ”— Improve your Github Profile

πŸ”— Download Browser Page as PDF

πŸ”— Send mails using NodeJS

πŸ”— Chat App using Socket.io

πŸ”— Expose a local web server to the internet

πŸ”— Web scraping using Node Js

πŸ”— Bash Terminal Guide

πŸ”— Best Practices: Node JS Security

πŸ”— Postman Hacks

πŸ”— Time Zone: Nodejs

πŸ”— Conditional Rendering : REACT




Top comments (0)