Friday, 5 June 2015

MySQL cheat sheet

http://havel.mojeservery.cz/wp-content/uploads/2015/10/mysql-ref-card.pdf https://www.fer.unizg.hr/_download/repository/mysql-cheat-sheet-v1.pdf
https://www.cheatography.com/guslong/cheat-sheets/essential-mysql/
http://cse.unl.edu/~sscott/ShowFiles/SQL/CheatSheet/SQLCheatSheet.html
http://www.mysqltutorial.org/mysql-cheat-sheet.aspx
https://gist.github.com/hofmannsven/9164408
http://williamqliu.github.io/2015/04/10/sql.html
https://support.rackspace.com/how-to/installing-mysql-server-on-centos/

MySQL Data Types

CHAR
String (0 - 255)
VARCHAR
String (0 - 255)
TINYTEXT
String (0 - 255)
TEXT
String (0 - 65535)
BLOB
String (0 - 65535)
MEDIUMTEXT
String (0 - 16777215)
MEDIUMBLOB
String (0 - 16777215)
LONGTEXT
String (0 - 429496­7295)
LONGBLOB
String (0 - 429496­7295)
TINYINT x
Integer (-128 to 127)
SMALLINT x
Integer (-32768 to 32767)
MEDIUMINT x
Integer (-8388608 to 8388607)
INT x
Integer (-2147­483648 to 214748­3647)
BIGINT x
Integer (-9223­372­036­854­775808 to 922337­203­685­477­5807)
FLOAT
Decimal (precise to 23 digits)
DOUBLE
Decimal (24 to 53 digits)
DECIMAL
"­DOU­BLE­" stored as string
DATE
YYYY-MM-DD
DATETIME
YYYY-MM-DD HH:MM:SS
TIMESTAMP
YYYYMM­DDH­HMMSS
TIME
HH:MM:SS
One of preset options
Selection of preset options
Integers (marked x) that are "­UNS­IGN­ED" have the same range of values but start from 0 (i.e., an UNSIGNED TINYINT can have any value from 0 to 255).

Select queries

select all columns
SELECT * FROM tbl;
select some columns
SELECT col1, col2 FROM tbl;
select only unique records
SELECT DISTINCT FROM tbl WHERE condition;
column alias with AS
SELECT col FROM tbl AS newname;
order results
SELECT * FROM tbl ORDER BY col [ASC | DESC];
group results
SELECT col1, SUM(col2) FROM tbl GROUP BY col1;

Creating and modifying

create a database
CREATE DATABASE db_name;
select a database
USE db_name;
list the databases on the server
SHOW DATABASES;
show a table's fields
DESCRIBE tbl;
create a new table
CREATE TABLE tbl (field1, field2);
insert data into a table
INSERT INTO tbl VALUES ("va­l1", "­val­2");
delete a row
DELETE * FROM tbl WHERE condition;
add a column from a table
ALTER TABLE tbl ADD COLUMN col;
remove a column from a table
ALTER TABLE tbl DROP COLUMN col;
make a column a primary key
ALTER TABLE tbl ADD PRIMARY KEY (col);
return only 1 row matching query
... LIMIT = 1
amend the values of a column
UPDATE table SET column­1="v­al1­" WHERE ...
clear all the values, leaving the table structure
TRUNCATE TABLE tbl;
delete the table
DROP TABLE tbl;
delete the database
DROP DATABASE db_name;

Matching data

matching data using LIKE
SELECT * FROM tbl1 WHERE col LIKE ‘%value%’
matching data using REGEX
SELECT * FROM tbl1 WHERE col RLIKE ‘regul­ar_­exp­res­sion’

Joins

INNER JOIN
returns only where match in both tables
OUTER JOIN
also returns non-ma­tching records from both tables
LEFT JOIN
also returns non-ma­tching records from left table
RIGHT JOIN
also returns non-ma­tching records in right table
JOIN syntax: 

SELECT * FROM tbl1 INNER JOIN tbl2 ON tbl1.id = tbl2.id;

String functions mySQL

Compare strings
STRCMP­("st­r1",­"­str­2")
Convert to lower case
LOWER(­"­str­")
Convert to upper case
UPPER(­"­str­")
Left trim
LTRIM(­"­str­")
Substring of a string
SUBSTR­ING­("st­r","i­nx1­"­,"in­x2")
Concat­enate
CONCAT­("st­r1",­"­str­2")

MySQL calcul­ation functions

Count rows
COUNT(col)
Average
AVG(col)
Minimum value
MIN(col)
Maximum value
MAX(col)
Sum of values
SUM(col)

Create table with auto-i­ncr­eme­nting primary key

CREATE TABLE table_name (
id INT AUTO_I­NCR­EMENT,
column VARCHA­R(2),
column VARCHA­R(32),
PRIMARY KEY (id)
);



MySQL Command-Line
WhatHowExample(s)
Running MySQLmysql -uusername -ppasswordmysql -ucusack2RO -pegbdf5s
Importingmysql -uusername -ppassword < filenamemysql -usomeDB -pblah < myNewDB.sql
Dumping
(Saving)
mysqldump -uusername -ppassword database [tables] > filenamemysqldump -ume -pblah myDB > My.sql
mysqldump -ume -pblah myDB table1
        table2 > my.sql
Common MySQL Column Types
PurposeData TypeExample
Integersint(M)int(5)
Floating-point (real) numbersfloat(M,D)float(12,3)
Double-precision floating-pointdouble(M,D)double(20,3)
Dates and timestimestamp(M)timestamp(8) (for YYYYMMDD)
timestamp(12) (for YYYYMMDDHHMMSS)
Fixed-length stringschar(M)char(10)
Variable-length stringsvarchar(M)varchar(20)
A large amount of textblobblob
Values chosen from a listenum('value1',value2',...)enum('apples','oranges','bananas')

M is maximum to display, and D is precision to the right of the decimal.
MySQL Mathematical Functions
WhatHow
Count rows per groupCOUNT(column | *)
Average value of groupAVG(column)
Minumum value of groupMIN(column)
Maximum value of groupMAX(column)
Sum values in a groupSUM(column)
Absolute valueabs(number)
Rounding numbersround(number)
Largest integer not greaterfloor(number)
Smallest integer not smallerceiling(number)
Square rootsqrt(number)
nth powerpow(base,exponent)
random number n, 0<n < 1rand()
sin (similar cos, etc.)sin(number)
MySQL String Functions
WhatHow
Compare stringsstrcmp(string1,string2)
Convert to lower caselower(string)
Convert to upper caseupper(string)
Left-trim whitespace (similar right)ltrim(string)
Substring of stringsubstring(string,index1,index2)
Encrypt passwordpassword(string)
Encode stringencode(string,key)
Decode stringdecode(string,key)
Get datecurdate()
Get timecurtime()
Extract day name from date stringdayname(string)
Extract day number from date stringdayofweek(string)
Extract month from date stringmonthname(string)
Basic MySQL Commands
WhatHowExample(s)
List all databasesSHOW DATABASES;SHOW DATABASES;
Create databaseCREATE DATABASE database;CREATE DATABASE PhoneDB;
Use a databaseUSE database;USE PhonDB;
List tables in the databaseSHOW TABLES;SHOW TABLES;
Show the structure of a tableDESCRIBE table;
SHOW COLUMNS FROM table;
DESCRIBE Animals;
SHOW COLUMNS FROM Animals;
Delete a database (Careful!)DROP DATABASE database;DROP DATABASE PhoneDB;
SQL Commands: Modifying
WhatHowExample(s)
Create tableCREATE TABLE table (
        column1 type [[NOT] NULL]
                [AUTO_INCREMENT],
        column2 type [[NOT] NULL]
                [AUTO_INCREMENT],
        ...
        other options,
        PRIMARY KEY (column(s))    );
CREATE TABLE Students (
        LastName varchar(30) NOT NULL,
        FirstName varchar(30) NOT NULL,
        StudentID int NOT NULL,
        Major varchar(20),
        Dorm varchar(20),
        PRIMARY KEY (StudentID)     );
Insert dataINSERT INTO table VALUES
        (list of values);
INSERT INTO table SET
        column1=value1,
        column2=value2,
        ...
        columnk=valuek;
INSERT INTO table (column1,column2,...)
        VALUES (value1,value2...);
INSERT INTO Students VALUES
        ('Smith','John',123456789,'Math','Selleck');
INSERT INTO Students SET
        FirstName='John',
        LastName='Smith',
        StudentID=123456789,
        Major='Math';
INSERT INTO Students
        (StudentID,FirstName,LastName)
        VALUES (123456789,'John','Smith');
Insert/SelectINSERT INTO table (column1,column2,...)
        SELECT statement;
        (See below)
INSERT INTO Students
        (StudentID,FirstName,LastName)
        SELECT StudentID,FirstName,LastName
        FROM OtherStudentTable;
        WHERE LastName like '%son';
Delete dataDELETE FROM table
        [WHERE condition(s)];



(Omit WHERE to delete all data)
DELETE FROM Students
        WHERE LastName='Smith';
DELETE FROM Students
        WHERE LastName like '%Smith%';
        AND FirstName='John';
DELETE FROM Students;
Updating DataUPDATE table SET
        column1=value1,
        column2=value2,
        ...
        columnk=valuek
        [WHERE condition(s)];
UPDATE Students SET
        LastName='Jones' WHERE
        StudentID=987654321;
UPDATE Students SET
        LastName='Jones', Major='Theatre'
        WHERE StudentID=987654321 OR
        (MAJOR='Art' AND FirstName='Pete');
Insert columnALTER TABLE table ADD COLUMN
        column type options;
ALTER TABLE Students ADD COLUMN
        Hometown varchar(20);
Delete columnALTER TABLE table
        DROP COLUMN column;
ALTER TABLE Students
        DROP COLUMN Dorm;
Delete table (Careful!)DROP TABLE [IF EXISTS] table;DROP TABLE Animals;
SQL Commands: Querying
WhatHowExample(s)
All columnsSELECT * FROM table;SELECT * FROM Students;
Some columnsSELECT column1,column2,... FROM table;SELECT LastName, FirstName FROM Students;
Some rows/
columns
SELECT column1,column2,...
        FROM table
        [WHERE condition(s)];
SELECT LastName,FirstName
        FROM Students
        WHERE StudentID LIKE '%123%';
No RepeatsSELECT [DISTINCT] column(s)
        FROM table;
SELECT DISTINCT LastName
        FROM Students;
OrderingSELECT column1,column2,...
        FROM table
        [ORDER BY column(s) [DESC]];
SELECT LastName,FirstName
        FROM Students
        ORDER BY LastName, FirstName DESC;
Column
Aliases
SELECT column1 [AS alias1],
        column2 [AS alias2], ...
        FROM table1;
SELECT LastName,FirstName AS First
        FROM Students;
GroupingSELECT column1,column2,...
        FROM table
        [GROUP BY column(s)];
SELECT LastName,COUNT(*)
        FROM Students
        GROUP BY LastName;
Group FilteringSELECT column1,column2,...
        FROM table
        [GROUP BY column(s)]
        [HAVING condition(s)];
SELECT LastName,COUNT(*)
        FROM Students
        GROUP BY LastName
        HAVING LastName like '%son';
JoinsSELECT column1,column2,...
        FROM table1,table2,...
        [WHERE condition(s)];
SELECT LastName,Points
        FROM Students,Assignments
        WHERE AssignmentID=12 AND
        Students.StudentID=Assignments.StudentID;
Table
Aliases
SELECT column1,column2,...
        FROM table1 [alias1],
        table2 [alias2],...
        [WHERE condition(s)];
SELECT LastName,Points
        FROM Students S,Assignments A
        WHERE S.StudentID=A.StudentID AND
        A.AssignmentID=12;
EverythingSELECT [DISTINCT]
        column1 [AS alias1],
        column2 [AS alias2], ...
        FROM table1 [alias1],
        table2 [alias2],...
        [WHERE condition(s)]
        [GROUP BY column(s)]
        [HAVING condition(s)]
        [ORDER BY column(s) [DESC]];
SELECT Points, COUNT(*) AS Cnt
        FROM Students S,Assignments A
        WHERE S.StudentID=A.StudentID AND
        A.AssignmentID=12
        GROUP BY Points
        HAVING Points > 10
        ORDER BY Cnt, Points DESC;


MySQL cheat sheet provides you with the most commonly used MySQL commands and statements that help you work with MySQL more effectively.

MySQL Commands

Access MySQL server from mysql client using a username and password (MySQL will prompt for a password):
Access a specific database using a username and password:
Exit

Working with Database

Create a database with a specified name if it does not exist in database server
Use database or change current database to another database you are working with
Drop a database with specified name permanently. All physical file associated with the database is no longer exists.
Show all available databases in the MySQL database server

Working with Table

Lists all tables in a current database.

Altering table structure

There are many actions that you can use with the ALTER TABLE statement as following:
Add a new column into a table
Drop an existing column in a table
Add index with a specific name to a table on a column.
Add primary key into a tables.
Remove primary key from a table.
Deleting table structure and data permanently.
Get information about the a table or a column.

Working with Index

Creating an index with the specified name on a table
Removing a specified index from table

Querying Data

Query all data from a table
Query specified data which is shown in the column list from a table
Query unique records
Query data with a filter using a WHERE clause.
Change the output of the column name using column alias.
Query data from multiple tables using inner joinleft join
Counting number of rows in a database table.
Sorting ascending or descending based on one or more columns using ORDER BY clause.
Group rows using GROUP BY clause.
Filter group of rows using both GROUP BY and HAVING clauses.

Modifying Data

Insert a new row into a table
Insert multiple rows into a table
Update data for all rows
Update data for a set of rows specified by a condition in WHERE clause.
Delete rows specified by a condition

Search

Search for data using LIKE operator:
Text search using a regular expression with RLIKE operator.

The following statement imports data from the  c:\tmp\discounts.csv file into the discountstable.
The field of the file is terminated by a comma indicated by  FIELD TERMINATED BY ',' and enclosed by double quotation marks specified by ENCLOSED BY '" ‘.
Each line of the CSV file is terminated by a newline character indicated by LINES TERMINATED BY '\n' .
Because the file has the first line that contains the column headings, which should not be imported into the table, therefore we ignore it by specifying  IGNORE 1 ROWS option.


Access monitor: mysql -u [username] -p; (will prompt for password)
Show all databases: show databases;
Access database: mysql -u [username] -p [database] (will prompt for password)
Create new database: create database [database];
Select database: use [database];
Show all tables: show tables;
Show table structure: describe [table];
Create new table with columns: CREATE TABLE [table] ([column] VARCHAR(120), [another-column] DATETIME);
Adding a column: ALTER TABLE [table] ADD COLUMN [column] VARCHAR(120);
Adding a column with an unique, auto-incrementing ID: ALTER TABLE [table] ADD COLUMN [column] int NOT NULL AUTO_INCREMENT PRIMARY KEY;
Inserting a record: INSERT INTO [table] ([column], [column]) VALUES ('[value]', [value]');
MySQL function for datetime input: NOW()
Selecting records: SELECT * FROM [table];
Explain records: EXPLAIN SELECT * FROM [table];
Selecting parts of records: SELECT [column], [another-column] FROM [table];
Counting records: SELECT COUNT([column]) FROM [table];
Counting and selecting grouped records: SELECT *, (SELECT COUNT([column]) FROM [table]) AS count FROM [table] GROUP BY [column];
Selecting specific records: SELECT * FROM [table] WHERE [column] = [value]; (Selectors: <>!=; combine multiple selectors with ANDOR)
Select records containing [value]SELECT * FROM [table] WHERE [column] LIKE '%[value]%';
Select records starting with [value]SELECT * FROM [table] WHERE [column] LIKE '[value]%';
Select records starting with val and ending with ueSELECT * FROM [table] WHERE [column] LIKE '[val_ue]';
Select a range: SELECT * FROM [table] WHERE [column] BETWEEN [value1] and [value2];
Select with custom order and only limit: SELECT * FROM [table] WHERE [column] ORDER BY [column] ASC LIMIT [value];(Order: DESCASC)
Updating records: UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];
Deleting records: DELETE FROM [table] WHERE [column] = [value];
Delete all records from a table (without dropping the table itself): DELETE FROM [table]; (This also resets the incrementing counter for auto generated columns like an id column.)
Removing table columns: ALTER TABLE [table] DROP COLUMN [column];
Deleting tables: DROP TABLE [table];
Deleting databases: DROP DATABASE [database];
Custom column output names: SELECT [column] AS [custom-column] FROM [table];
Export a database dump (more info here): mysqldump -u [username] -p [database] > db_backup.sql
Use --lock-tables=false option for locked tables (more info here).
Import a database dump (more info here): mysql -u [username] -p -h localhost [database] < db_backup.sql
Logout: exit;

Aggregate functions

Select but without duplicates: SELECT distinct name, email, acception FROM owners WHERE acception = 1 AND date >= 2015-01-01 00:00:00
Calculate total number of records: SELECT SUM([column]) FROM [table];
Count total number of [column] and group by [category-column]SELECT [category-column], SUM([column]) FROM [table] GROUP BY [category-column];
Get largest value in [column]SELECT MAX([column]) FROM [table];
Get smallest value: SELECT MIN([column]) FROM [table];
Get average value: SELECT AVG([column]) FROM [table];
Get rounded average value and group by [category-column]SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];

Multiple tables

Select from multiple tables: SELECT [table1].[column], [table1].[another-column], [table2].[column] FROM [table1], [table2];
Combine rows from different tables: SELECT * FROM [table1] INNER JOIN [table2] ON [table1].[column] = [table2].[column];
Combine rows from different tables but do not require the join condition: SELECT * FROM [table1] LEFT OUTER JOIN [table2] ON [table1].[column] = [table2].[column]; (The left table is the first table that appears in the statement.)
Rename column or table using an aliasSELECT [table1].[column] AS '[value]', [table2].[column] AS '[value]' FROM [table1], [table2];

Users functions

List all users: SELECT User,Host FROM mysql.user;
Create new user: CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
Grant ALL access to user for * tables: GRANT ALL ON database.* TO 'user'@'localhost';

Find out the IP Address of the Mysql Host

SHOW VARIABLES WHERE Variable_name = 'hostname'; (source)

MySQL Command From a Bash Shell in One Line

Use the following command for quickly execution of MySQL query from a Linux Bash Shell :
# mysql -u [user] -p[pass] -e "[mysql commands]"
Example :
# mysql -u root -pSeCrEt -e "show databases"

Run a MySQL Query From a Bash script using EOF

Use the following syntax in your Bash scripts for running MySQL commands :
mysql -u [user] -p[pass] << EOF
[mysql commands]
EOF
Example :
#!/bin/bash
mysql -u root -pSeCrEt << EOF
use mysql;
show tables;
EOF

Execute a MySQL Command Remotely

Use -h option to specify a MySQL server's IP address:
# mysql -h [ip] -u [user] -p[pass] -e "[mysql commands]"
Example :
# mysql -h 192.168.1.10 -u root -pSeCrEt -e "show databases"

Specify a Database to Use

Use -D option to specify the name of MySQL database :
# mysql -D [db name] -u [user] -p[pass] -e "[mysql commands]"
Example :
# mysql -D clients -u root -pSeCrEt -e "show tables"

Summary
SQL (Structured Query Language) is the standard language for communicating with relational database management systems. There’s a few variations including Microsoft’s SQL Server, MySQL, PostgreSQL, SQLite. Below instructions are with MySQL. We’ll setup a MySQL server, then use a client to connect to the server and query for data.

Installation

On a mac, use homebrew to install mysql using $brew install mysql to install the MySQL as a server. For a GUI client, you can download MySQL Workbench, which will allow you to visually see the data with a GUI (as opposed to only command line).

Bash Commands

Useful MySQL commands (Start, Stop Server)

$mysql.server start  # start the server
$mysql.server stop  # stop the server
$mysql.server status  # check if server is running
$mysql.server help  # get help commands

Run a text file (containing sql scripts) from command line

$mysql < myscript.sql

Switching from bash to shell

$mysql  # enters into the shell from bash
$mysql -u root -p  # enters into the shell as root user
mysql>  # This is what you'll see when in shell

Shell Commands

Create, Drop, and Use a Database

mysql> DROP DATABASE IF EXISTS my_database_name;
mysql> CREATE DATABASE my_database_name;
mysql> USE my_database_name;

Loading Data

Say we want to load the iris dataset. We do a curl to get the dataset, then run the below sql query.
curl http://mlr.cs.umass.edu/ml/machine-learning-databases/iris/iris.data > iris.csv
sepallengthsepalwidthpetallengthpetalwidthclass
5.13.51.40.2Iris-setosa
4.931.40.2Iris-setosa
4.73.21.30.2Iris-setosa
LOAD DATA LOCAL 

#EDIT THIS PATH:
INFILE "/Users/williamliu/Desktop/iris.csv"   

INTO TABLE sampdb.iris  #db sampdb, table iris

FIELDS
    TERMINATED BY ',' 
    #ENCLOSED BY '"' 

LINES
   TERMINATED BY '\n'
#IGNORE 1 LINES;

Create Table

create table iris(
sepallength float,
sepalwidth float,
petallength float,
petalwidth float,
class nvarchar(30)
);

Queries

Basic Format of SQL Queries

Assuming data (say mytable from mydatabase) looks like:
# last_name, first_name, suffix, city, state, birth, death
Adams, John, , Braintree, MA, 1735-10-30, 1826-07-04
Adams, John Quincy, , Braintree, MA, 1767-07-11, 1848-02-23
Arthur, Chester A., , Fairfield, VT, 1829-10-05, 1886-11-18
Buchanan, James, , Mercersburg, PA, 1791-04-23, 1868-06-01
Bush, George H.W., , Milton, MA, 1924-06-12, 
We can do the following query:
SELECT last_name, first_name, state
FROM mydatabase.mytable;
  • a database holds a lot of tables
  • a table holds lots of rows and columns of data
  • a record is a particular instance of a row and column

ROW NUMBER

To get a returning count of what the row number is, do this. Note that you can set variables and return them in the results.
SET @n=0;
SELECT @n := @n+1 AS 'row_num', sepallength FROM sampdb.iris;

COUNT

SELECT COUNT(*) FROM sampdb.president
SELECT COUNT(first_name), COUNT(death) FROM sampdb.president  # Returns 42 and 38
SELECT COUNT(DISTINCT state, first_name) FROM sampdb.president
  • Counts the not NULL values passed into it
  • This is an ‘aggregating’ function - takes a set of records or values and performs calculations on that set
  • For Distinct, we get 42 instead of 43 total presidents, means one duplicate state and first_name combo
  • Can also Subset and Count the data

Other Functions (AVG, SUM, MIN, MAX)

SELECT AVG(death) FROM sampdb.president
SELECT SUM(age) FROM sampdb.president
SELECT MIN(age) FROM sampdb.president
SELECT MAX(age) FROM sampdb.president

Functions (AS, DATEDIFF, CONCAT, etc.)

SELECT ... score * 2 AS doubled_score
SELECT ... DATEDIFF(death, birth)
SELECT ... CONCAT(first_name, ' ', last_name)
SELECT ... UNIQUE(last_name, middle_name, first_name)
  • Can create an alias with AS (required for subqueries)
  • Can run functions (e.g. CONCAT combines strings into one, DATEDIFF gets difference in days)
  • Can get unique values using UNIQUE

NULL Values and Functions

  • NULL values represent missing or unknown data (not the same as the value 0).
  • To test for NULL, do IS NULL or IS NOT NULL.
    • IS NULL checks if the value is null. For example, SELECT LastName, FirstName FROM Persons WHERE LastName IS NULL
    • IS NOT NULL checks if the value is not null. For example, SELECT LastName, FirstName FROM Persons WHERE LastName IS NOT NULL
  • There are functions ISNULL(), NVL(), IFNULL(), and COALESCE() that specify how we want to treat a NULL value
    • For MySQL, IFNULL() tells us how to treat a NULL value (in this case, replacing it with 0): SELECT UnitPrice*(UnitsInStock+IFNULL(UnitsOnOrder,0)) FROM Products

WHERE

SELECT COUNT(first_name) FROM sampdb.president WHERE last_name="Adams" 
  • Filters (before GROUP BY)
  • Can pass in lists (using IN) or tuples
    • Filter by list: … WHERE state IN ('MA', 'VA')
    • Filter by tuple: … WHERE (last_name, state) IN ('Adams', 'MA')

GROUP BY

SELECT state, COUNT(*) FROM sampdb.president GROUP BY state ORDER BY state
  • GROUP BY is a subset; divides records by unique tuples

GROUP BY using WHERE and HAVING

SELECT state, COUNT(*) FROM sampdb.president
   WHERE birth <= '1900-01-01'
   GROUP BY state
   HAVING COUNT(*) >=2
   ORDER BY COUNT(*);
Sample Output:
state, count(*)
   MA, 2
   VT, 2
   NC, 2
   NY, 4
   OH, 7
   VA, 8
  • WHERE filters before the GROUP BY
  • HAVING filters after the GROUP BY

Subqueries

  • Subqueries is a query inside another query
  • Usually this means an outer query and an inner query SELECT name FROM city WHERE pincode IN (SELECT pincode FROM pin WHERE zone = 'west')
    • outer query is SELECT name FROM city WHERE pincode IN ...
    • inner query is SELECT pincode FROM pin WHERE zone = 'west'
  • Returned table has to have the fields and row expected of it by the calling query
  • Alias (AS) is required for subqueries
  • Two varieties of subqueries (correlated and non-correlated)
    • Non-correlated subquery means that the inner query doesn’t depend on the outer queryand can run as a stand alone query
    • SELECT company FROM stock WHERE listed_on_exchange = (SELECT ric FROM market WHERE country='japan') - The inner query executes first, then the outer query. - non-correlated subqueries usually use IN or NOT IN
    • Correlated subquery means that the inner query depends on the outer query
    • SELECT student_id, score FROM sampdb.score AS scr WHERE event_id = 3 AND score > ( SELECT AVG(score) FROM sampdb.score WHERE event_id = scr.event_id GROUP BY event_id) ) - Requires use of alias (AS) - The outer query executes first, then the inner query; this is because the inner query depends on the output of the outer query - These queries are slower than non-correlated queries (think about doing a join instead) - correlated subqueries usually use EXISTS or NOT EXISTS

EXISTS

  • The EXISTS and NOT EXISTS conditions are used with a subquery (usually the correlated subquery). Condition is met if the subquery returns at least one row. It can be used withSELECT, INSERT, UPDATE, or DELETE statements.
  • SELECT * FROM mysuppliers WHERE NOT EXISTS (SELECT * FROM myorders WHERE mysuppliers.supplier_id = myorders.order_id);
  • Note that these are very inefficient since the sub-query is re-run for every single row so use only if no other solution

JOINS

Used to combine rows from two or more tables. Types of joins include:
  • INNER JOIN selects all rows from both tables that have a match
    • SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name;
  • LEFT JOIN selects all rows from the left table (table1) and matching rows on the right table (table2)
    • SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;
  • RIGHT JOIN selects all rows from the right table (table2) and matching rows on the left table (table1)
    • SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;
  • FULL JOIN selects all rows from the left table (table1) and all rows on the right table (table2)
    • SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;
  • UNION and UNION ALL combines the result of two or more SELECT statements (with each statement having the same number of columns, data types, and order). UNION selects distinct values only. UNION ALL allows duplicates.
    • SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2
    • SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM table2

Primary Key (PK) and Foreign Key (FK)

  • primary key constraint uniquiely identifies each record in a database table. These values must be unique, cannot contain NULL values, and each table can only have one primary key. Note that you can have multiple column values that make up the primary key (e.g. P_ID, LastName, SSN).
    • Add a primary key to a table with a single column (e.g. P_ID) as the constraint: ALTER TABLE MyTable ADD PRIMARY KEY (P_ID)
    • Add a primary key to a table with multiple columns (e.g. P_ID, LastName) as the constraint: ALTER TABLE MyTable ADD PRIMARY KEY(P_ID, LastName)
    • Remove a primary keyALTER TABLE MyTable DROP PRIMARY KEY
  • foreign key constraint in one table is just a primary key in another table.
    • Add a foreign key on a single column as the constraint: ALTER TABLE MyTable ADD FOREIGN KEY (P_ID) REFERENCES OtherTable(P_ID)
    • Add a foreign key on multiple columns (e.g. P_ID) as the constraint: ALTER TABLE MyTable ADD CONSTRAINT my_fk FOREIGN KEY (P_ID) REFERENCES OtherTable(P_ID)
    • Remove a foreign key with: ALTER TABLE MyTable DROP FOREIGN KEY my_fk

DROP and TRUNCATE

  • To delete an entire database, be extra sure you want to and can spell correctly, then do: DROP DATABASE MyDatabase. This deletes everything in the database.
  • To delete an entire table (including the table itself), be extra sure you want to and can spell correctly, then do: DROP TABLE MyTable
  • To delete the data inside an entire table, do: TRUNCATE TABLE MyTable

ALTER

  • To add, delete, or modify columns in an existing table, use ALTER.
  • To add a column in a table, use: ALTER TABLE MyTable MODIFY COLUMN MyColumn <datatype>
  • To delete a column in a table, use: ALTER TABLE MyTable DROP COLUMN MyColumn
  • To modify a column in a table, use: ALTER TABLE MyTable MODIFY COLUMN MyColumn <datatype>

INSERT

  • To insert data by field: INSERT INTO MyDatabase.MyTable SET ThisField = ThisData
  • To insert data by row: INSERT INTO MyDatabase.MyTable VALUES ('field1', 'field2', 'field3'

UPDATE

UPDATE MyDatabase.MyTable SET ThisField = ThisData

DELETE

DELETE FROM MyDatabase.MyTable  # Careful, no criteria deletes the entire database!

IF

IF(2>1, 'OK', 'NOT)

CASE

CASE
    WHEN 2 > 1
        THEN 'OK'
    WHEN 1==1
        THEN 'YEP'
    ELSE
        'NOT OK'
END
You can combine all of the above into something like this (Note: From SQL Server):
SELECT IVRName, 
SUM(CASE WHEN Disconnection in ('Answer') THEN 1 END) AS Answered, 
SUM(CASE WHEN Disconnection in ('Abandon') THEN 1 END) AS Abandoned
FROM LifeNetDW.dbo.QueueMetrics
WHERE CallTime > '2015-1-31' AND IVRName IS NOT NULL
GROUP BY IVRName

Other

Schemas

  • Command: SHOW DATABASES;
    • Shows all databases available on the server
  • Command: SHOW TABLES IN books;
    • Shows all tables available in a particular database
  • Command: DESCRIBE books.authors;
    • Shows columns, data types, keys, in a particular table
  • Command: SHOW CREATE TABLE books.authors\G;
    • Shows the command for creating a table

Indexing

  • Makes processing things much faster
  • An index is a pre-ordered sort of a particular field
  • Done and stored by the database server
  • Indexing has storage and performance costs
  • For every index on a table, the database must reorder the index for a new entry
  • Each index takes up space in storage and memory
  • Do ‘profiling’ to see where things are slow
  • To create an index, do: CREATE INDEX MyIndex on MyTable (colToIndex)
  • To create an index on a combination of volumns, do: CREATE INDEX MyIndex on MyTable(colToIndex1, colToIndex2, colToIndex3)
  • To drop an index, do: ALTER TABLE MyTable DROP INDEX MyIndex

Normalization (Nth normal form)

  • Principles of Modeling Data ‘norms’
  • Rules for making sure data is where we expect it and that it isn’t duplicated
  • First Normal Form (1NF)
    • Data should be atomic (i.e. values should not be combined)
    • E.g. George Washington shouldn’t be a value (because it combines first and last name)
    • No repeating columns
    • E.g. Columns like Author1, Author2, Author3
  • Second Normal Form (2NF)
    • No ‘partial dependencies’
    • Cannot determine a nonkey column value with only a portion of the primary key
    • Basically, unnecessary duplication
  • Third Normal Form (3NF)
    • Second Normal Form plus no nonkey column depends on the value of another nonkey column
    • Values are driven by the primary key fields; these values should be removed to a table of their own
sudo mysqldump -u <local_user> \
    --databases world \
    --single-transaction \
    --compress \
    --order-by-primary  \
    -p <local_password> | mysql -u <RDS_user_name> \
        --port=3306 \
        --host=hostname \
        -p <RDS_password>
#!/usr/bin/env bash

FILE_NAME="example_db-`date +%Y%m%d%H%M`.sql.gz"
SAVE_DIR="/backup"
S3BUCKET="mysqlbackupbucket"

# Get MYSQL_USER and MYSQL_PASSWORD
# export MYSQL_USER=dbuser; export MYSQL_PASSWORD=t0ps3r3!
source /home/backup/.env mysqldump -u ${MYSQL_USER} -p${MYSQL_PASSWORD} example_db | gzip > ${SAVE_DIR}/${FILE_NAME} if [ -e ${SAVE_DIR}/${FILE_NAME} ]; then # Upload to AWS aws s3 cp ${SAVE_DIR}/${FILE_NAME} s3://${S3BUCKET}/${FILE_NAME} # Test result of last command run if [ "$?" -ne "0" ]; then echo "Upload to AWS failed" exit 1 fi # If success, remove backup file rm ${SAVE_DIR}/${FILE_NAME} # Exit with no error exit 0 fi # Exit with error if we reach this point echo "Backup file not created" exit 1

http://sqlfiddle.com/#!9/f20c2/3
SELECT word, SUM(amount) 
FROM Data 
Group By word (with rollup)

No comments:

Post a Comment