Update MySQLNotes.md

master
Matt Huntington 9 years ago committed by GitHub
parent ef98c86ddc
commit adf9279ea4

@ -10,57 +10,57 @@
```sql
-- CREATE DB/TABLES
SHOW DATABASES;
CREATE DATABASE our_database_name;
USE our_database_name;
CREATE TABLE people (first_name VARCHAR(20), age INT);
DESCRIBE people;
SHOW DATABASES; -- show all sub databases
CREATE DATABASE our_database_name; -- create a sub database
USE our_database_name; -- use that sub database
CREATE TABLE people (first_name VARCHAR(20), age INT); -- craete a table with a text first_name column and an integer age column
DESCRIBE people; -- look at the table's schema
-- CRUD
INSERT INTO people (first_name, age) VALUES ('Matt' , 34);
SELECT age FROM people;
SELECT * FROM people;
UPDATE people SET weight = 300 WHERE first_name = 'Bill';
DELETE FROM people WHERE first_name = "Bill";
INSERT INTO people (first_name, age) VALUES ('Matt' , 34); -- insert a row into people table
SELECT age FROM people; -- find all people, display just the age column
SELECT * FROM people; -- find all people, display all columns
UPDATE people SET weight = 300 WHERE first_name = 'Bill'; -- set weight to 300 for all rows that have a first_name column set to Bill
DELETE FROM people WHERE first_name = "Bill"; -- delete all rows that have a first_name column set to Bill
-- OPERATORS
SELECT * FROM people WHERE age != 63;
SELECT * FROM people WHERE age < 63;
SELECT * FROM people WHERE age > 63;
SELECT * FROM people WHERE age >= 63;
SELECT * FROM people WHERE age <= 63;
SELECT * FROM people WHERE first_name LIKE "%Charlie%";
SELECT * FROM people WHERE first_name NOT LIKE "%Charlie%";
SELECT * FROM people WHERE age IS NULL;
SELECT * FROM people WHERE age IS NOT NULL;
SELECT * FROM people WHERE age != 63; -- find all rows that have an age that doesn't equal 63
SELECT * FROM people WHERE age < 63; -- find all rows that have an age less than 63
SELECT * FROM people WHERE age > 63; -- find all rows that have an age greater than 63
SELECT * FROM people WHERE age >= 63; -- find all rows that have an age greater or equal to than 63
SELECT * FROM people WHERE age <= 63; -- find all rows that have an age less than or equal to than 63
SELECT * FROM people WHERE first_name LIKE "%Charlie%"; -- find all rows that have a first_name that contains the value Charlie
SELECT * FROM people WHERE first_name NOT LIKE "%Charlie%"; -- find all rows that have a first_name that does not contain the value Charlie
SELECT * FROM people WHERE age IS NULL; -- find all rows that do not have any value for the age column
SELECT * FROM people WHERE age IS NOT NULL; -- find all rows that have any value for the age column
-- AND/OR
SELECT * FROM people WHERE first_name = 'Matt' AND age = 43;
SELECT * FROM people WHERE first_name = 'Matt' OR age = 49;
SELECT * FROM people WHERE first_name = 'Matt' AND age = 43; -- find all rows that have a first name set to Matt AND also have an age set to 43
SELECT * FROM people WHERE first_name = 'Matt' OR age = 49; -- find all rows that either have a first_name set to Matt OR have an age set to 49
-- ORDER
SELECT * FROM people ORDER BY age DESC;
SELECT * FROM people ORDER BY first_name DESC;
SELECT * FROM people ORDER BY age ASC LIMIT 2;
SELECT * FROM people ORDER BY age ASC LIMIT 2 OFFSET 1;
SELECT * FROM people ORDER BY age DESC, first_name ASC;
SELECT * FROM people ORDER BY age DESC; -- find all rows, but sort them in order of decreasing age
SELECT * FROM people ORDER BY first_name DESC; -- find all rows, but sort them in order of decreasing first_name values (reverse alphabetal)
SELECT * FROM people ORDER BY age ASC LIMIT 2; -- limit the results to 2
SELECT * FROM people ORDER BY age ASC LIMIT 2 OFFSET 3; -- limit the results to 2, but don't show the first 3 records
SELECT * FROM people ORDER BY age DESC, first_name ASC; -- order by age descending, for any rows that have the same age value, order those rows by first_name ascending
-- ALTER TABLE
ALTER TABLE people ADD COLUMN weight FLOAT;
ALTER TABLE people DROP COLUMN height;
ALTER TABLE people MODIFY COLUMN height FLOAT;
ALTER TABLE people ADD COLUMN weight FLOAT; -- add a weight column that is a FLOAT (decimal number)
ALTER TABLE people DROP COLUMN height; -- drop the height column
ALTER TABLE people MODIFY COLUMN height FLOAT; -- change the height column to be a FLOAT (decimal number)
ALTER TABLE people ADD COLUMN height FLOAT AFTER first_name;
ALTER TABLE people MODIFY COLUMN height FLOAT AFTER age;
ALTER TABLE people ADD COLUMN height FLOAT AFTER first_name; -- add a heigh column after the first_name column
ALTER TABLE people MODIFY COLUMN height FLOAT AFTER age; -- move an existing height column so that it is after the age column
ALTER TABLE people ADD COLUMN id INT FIRST;
ALTER TABLE people MODIFY COLUMN height FLOAT FIRST;
ALTER TABLE people ADD COLUMN id INT FIRST; -- add an id column in the first position
ALTER TABLE people MODIFY COLUMN height FLOAT FIRST; -- move an existing height column to the first position
ALTER TABLE people ADD COLUMN dob DATETIME;
ALTER TABLE people CHANGE dob date_of_birth DATETIME;
ALTER TABLE people ADD COLUMN dob DATETIME; -- create a dob column which is a date
ALTER TABLE people CHANGE dob date_of_birth DATETIME; -- change the name of the dob column to date_of_birth
ALTER TABLE people ADD COLUMN id INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
ALTER TABLE people ADD COLUMN id INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST; -- create an id column that increments with each new row created
-- AGGREGATION
SELECT COUNT(*), age FROM people GROUP BY age;

Loading…
Cancel
Save