MySQL Flashcards
Working with Command Line Client (if the MySQL workbench doesn’t work)
show databases;
use world;
show tables;
SELECT * FROM City LIMIT 10;
Clients + Servers (store files and databases) located on the same network
LAN (local area network)
WAN (wide area network) means 2+ networks are in the same geographical area
Primary key
- Cannot be Null
- One per table
- Unique identifier of row
Unique key
- Can have one Null
Datatypes
String - CHAR, VARCHAR
Integer - INT
Float - FLOAT
Date and time - DATE
Select current date and time / date / time
SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();
SELECT SQRT(25);
Create a default database
USE sakila;
SELECT *
FROM actor;
Display the column names only (trick)
SELECT *
FROM sakila.actor
WHERE 1 = 2;
Note, 1 is never equal to 2, so it won’t show any data except for the column names
Rounding etc
SELECT amount,
ROUND(amount) Amnt,
ROUND(amount, 1) Amnt1,
FLOOR(amount) FloorAmnt,
CEILING(amount) CeilingAmnt
FROM sakila.payment;
Floor rounds down to the nearest integer.
Ceiling rounds up to the nearest integer.
Concatination
SELECT CONCAT(first_name, ‘ ‘, last_name) AS FullName
FROM sakila.actor;
SELECT CONCAT(LEFT(first_name,1), ‘ ‘, LEFT(last_name,1)) AS FirstInitial
FROM sakila.actor;
Length of the string
SELECT LENGTH(CONCAT(first_name, ‘ ‘, last_name) AS length
FROM sakila.actor;
Selecting date format
SELECT DATE_FORMAT(last_update, ‘%m/%d/%Y’
FROM sakila.actor;
Selecting only unique rows
SELECT DISTINCT first_name
FROM sakila.actor;
Insert row/rows
INSERT INTO sakila.my_table (first_name, last_name, last_update)
VALUES (‘Pinal’, ‘Dave’, ‘2023-04-15’),
(‘Pinal2’, ‘Dave2’, ‘2023-04-16’);
Update (change) row/rows
UPDATE sakila.actor
SET first_name = ‘Pinal’,
last_name = ‘Dave’
WHERE actor_id IN (3,5,6);