MySQL
MySQL 101
Command Line
We can use the mysql
utility for basic authentication and interaction with the MSQL database
Login Locally
Remote Login
Queries must end with ;
Create a Database
Show all Databases and Use A Databases
Create a Table
A data type defines what kind of value a column may hold. Can consist from numbers, strings, date, time.
Show all Tables
Show Table Structure
With the DESCRIBE
keyword we can see the table structure
SQL Statements
INSERT
Used to add new records a table
SELECT
Allows us get data from a tables
Retrieves the user table where their id = 1
DROP
Remove tables and databases from the server
ALTER
Change table name and fields
Add new Column
Rename Column
UPDATE
Update table properties
Query Results
We can control the output of our results and sort them with the following keywords;
ORDER BY
It's also possible to change the order from ascending to descending
LIMIT
Limits the number of items returned
WHERE
Filte for specific data based on a condition
for example
WHERE name = rod
WHERE id < 5
LIKE
Used to match a certain pattern
The %
acts as wildcard and will match all characters after it.
The _
symbol is used to match exactly one character.
SQL Operators
Allows us to use mutiple conditions at once
AND
Takes two condition and return true or false based on the outcome.
OR
Takes two expressions and returns true when at least one is true
NOT
Toggle between a boolean value
Symbol Operators
AND, NOT and OR can be represented as &&, II and !
Basic SQL queries
Union SQL query;
Retrieves a table containing a row with the user's information (where their id = 5) and all usernames and credit card numbers from accounts.
If we have a query that gets an item based in the $id
:
We can change it to:
Since this will always be true, we should get back all the items in the products table!
We can also use this in a UNION:
Last updated
Was this helpful?