int
, float
, decimal
, etc.char
, varchar
, etc.date
, datetime
, time
, year
, etc.BIT
(1 or 0)Structured Query Language — SQL
retrieving data
SELECT -- columns to retrieve from DB
-- separate columns with commas
-- * means all columns
FROM -- tables to retrieve from DB, separate tables with commas
WHERE -- conditions while retrieving data
ORDER BY -- sort data by specified columns
-- ASC: ascending default
-- DESC: descending
-- Operators in WHERE
= -- equal to
<> -- not equal to
> < >= <=
IS NULL
IS NOT NULL
BETWEEN a AND b -- greater than a and less than b (inclusive)
LIKE -- search for pattern often use with wildcards %
AND / OR -- combine conditions
JOIN -- combine records from multiple tables
-- based on table relationships
SELECT columns -- resolve name conflicts: prefix with table names
FROM left_table
JOIN right_table
ON left_table.fk = right_table.pk;
SELECT tracks.name AS track_name -- aliasing
FROM tracks;
add
INSERT INTO table_name(col1, col2, ...)
VALUES (val1, val2, ...);
update
UPDATE table_name
SET col = val
WHERE condition; -- specify which records to update
-- without where clause, all records will be updated
delete
DELETE FROM table_name
WHERE condition;
-- without where clause, all records will be deleted
views
virtual tables created from SQL queries
reason to use
CREATE OR REPLACE VIEW view_name AS
SELECT cols FROM tab;
DROP VIEW view_name;
aggregate functions
count()
count number of rowsMIN()
/ MAX()
find column's min / max valueAVG()
calculate column's average valueSUM
sum column's total valuesRAND()
generate random number between 0 and 1
ORDER BY
to SELECT
records in random orderCHAR_LENGTH()
return length of given stringCONCAT()
concatenate stringsSELECT COUNT(*)
FROM albums
WHERE artist_id = 8;
SELECT MIN(milliseconds), MAX(milliseconds)
FROM tracks;
SELECT AVG(milliseconds)
FROM tracks
WHERE album_id = 3;
SELECT SUM(milliseconds)
FROM tracks
WHERE genre_id = 3;
SELECT *
FROM tracks
ORDER BY RAND();
SELECT name, CHAR_LENGTH(name) AS name_length
FROM tracks;
SELECT CONCAT(name, ' was composed by ', composer)
FROM tracks;
GROUP BY
group results by data in specified column
-- show artists and number of albums for each artist
SELECT album_id, name, COUNT()*
FROM albums
JOIN artists
ON album.artist_id = artists.artist_id
GROUP BY artists.artist_id;
-- find shortest track for each album
SELECT album_id, MIN(milliseconds)
FROM tracks
GROUP BY album_id;
PHP: Hypertext Preprocessor
open-source server-side scripting language
created for web development
syntax
// all PHP code goes between opening and closing tags
<?php
// comment
# comment
/*comment*/
# every line must end with ;
# variable name start with letter or underscore, case-sensitive
$variable = "hello world!";
echo "print" . $variable;
# superglobals (built-in)
$_SERVER // array with server and environment information
$_GET // array of HTTP GET variables
$_POST // array of HTTP POST variables
echo $_SERVER['HTTP_HOST'];
echo $_SERVER['HTTP_USER_AGENT'];
echo $_GET['track_id'];
echo $_POST['track_name'];
?>
HTTP POST
Method
<form action="" method="POST">
Username:
<input type="text" name="username">
<button type="submit">Submit</button>
</form>
<?php
echo $_POST['username'];
?>
HTTP GET
Method: submits all form data through URL
<form action="" method="GET">
Username:
<input type="text" name="username">
<button type="submit">Submit</button>
</form>
<?php
echo $_GET['username'];
?>