You are on page 1of 3

Basic commands:

CREATE DATABASE dataBaseName;


CREATE TABLE or CREATE TABLE
( (
ColumnName1 COLUMN1TYPE PRIMARY KEY, ColumnName1
COLUMN1TYPE,
ColumnName2 COLUMN2TYPE, ColumnName2 COLUMN2TYPE,
ColumnName3 COLUMN3TYPE NOT NULL ColumnName3 COLUMN3TYPE
NOT NULL
) )
CONSTRAINT
PK_ColumnName1
PRIMARY KEY
(ColumnName1)
);

ALTER TABLE -> Add/remove column; change column data type; change column
constraints
example: ALTER TABLE
tableName
ADD CONSTRAINT
FK_columnName
FOREIGN KEY
(columnName)
REFERENCES
table2Name
(column2Name)

DROP TABLE tableName; -> deletes table tableName

-- -> comment line

USE dataBaseName; -> means you want to query through dataBaseName database

SELECT columnName FROM tableName -> return columnName date from table
INSERT INTO tableName(columnName1 , columnName2 ) VALUES (value1, value2)
UPDATE tableName SET columnName1 = value WHERE condition(for example id=1)
DELETE FROM tableName WHERE condition(for example id=1)

SELECT * FROM tableName -> returns all columns from table


SELECT columnName as Word FROM tableName -> alias columnName as Word
SELECT a.first_name as FirstName FROM actor a; -> alias table actor as a

SELECT DISTINCT columnName FROM tableName -> returns only unique data from
column columnName

Boolean Operators:

AND a AND b
OR a OR b
BETWEEN BETWEEN a AND b
LIKE firstName LIKE "as%" -> first name starts with as
firstName LIKE "%as%" -> first name contains letters as
in it
firstName LIKE "%as" -> first name ends with as
IN firstName IN ('Igor','Brance') -> first name is Igor or
Brance
IS firstName IS NULL -> checks if firstname is NULL
IS NOT firstName IS NOT NULL

Shaping resuts:

ORDER BY
SET FUNCTIONS:
COUNT -> SELECT COUNT(columnName) FROM tableName -> counts the
number of rows in column columnName
MAX -> SELECT MAX(columnName) FROM tableName -> selects MAX
number from column columnName
MIN -> SELECT MIN(columnName) FROM tableName -> selects MIN
number from column columnName
AVG -> SELECT AVG(columnName) FROM tableName -> selects AVG of
all numbers from column columnName
SUM -> SELECT SUM(columnName) FROM tableName -> selects SUM of
all numbers from column columnName
GROUP BY -> groups additional columns to the SET FUNCTIONS
example:
SELECT COUNT(a.first_name), a.first_name
FROM actor a
GROUP BY a.first_name;
HAVING -> works like WHERE against SELECT
example:
SELECT COUNT(a.first_name), a.first_name
FROM actor a
GROUP BY a.first_name
HAVING a.first_name
LIKE 'A%'; - this will present all the
names which starts with A, and give count how many of each names are there in table

Matching Different Data Tables with JOINs

CROSS JOIN -> returns All rows from both tables, No where clause
example: SELECT
a.first_name, c.first_name
FROM
actor a, customer c;

INNER JOIN ->


example: SELECT
a.first_name, c.first_name
FROM
actor a
INNER JOIN
customer c
ON
a.actor_id = c.customer_id;

OUTER JOIN is eqaul to INNER JOIN + it works with null values


LEFT JOIN -> returns data from table 1 with null values from table
2
RIGHT JOIN -> returns data from table 2 with null values from table
1
FULL JOIN -> is not supported by MySQL. returns data from table 1
with null values from table 2 & returns data from table 2 with null values from
table 1

SELF JOIN -> join table with it self


NATURAL JOIN -> combines columns with identical names from multiple
tables

USING KEYWORD:
SELECT s.StudentName, sc.ClassId = SELECT s.StudentName, sc.ClassId
From Students s From Students s
LEFT JOIN LEFT JOIN
StudentClass sc StudentClass sc
USING (StudentID); ON s.StudentID = sc.StudentID;

UNION KEYWORD:
Combines results from 2 or more SELECT statements. All Select statements must
return same number of columns

SCALAR FUNCTIONS:
STRING FUNCTIONS: MANIPULATES WITH STRINGS
NUMERIC FUNCTIONS: MANIPULATES WITH NUMBERS(MATH FUNCIONS)
DATE/TIME FUNCTIONS: MANIPULATES WITH DATE/TIME

CONTROL FLOW FUNCTIONS:


CASE()
IF(condition, resultIfConditionTrue, resultIfConditionIsFalse)
IFNULL(num1, num2) - if num1 is null, returns num2. else returns num1
NULLIF(num1, num2) - returns null if num1=num2

CREATE PROCEDURE -> same like creating methods in C#


example:
DELIMITER //
CREATE PROCEDURE CustomersWithIdX( X INT)
BEGIN
SELECT c.first_name, c.last_name
FROM customer c
WHERE c.customer_id=X;
END//
DELIMITER ;

EXECUTE PROCEDURE CustomersWithIdX -> CALL CustomersWithIdX(5);


DELETE PROCEDURE CustomersWithIdX -> DROP CustomersWithIdX;

CREATE FUNCTION -> The same as PROCEDURE, but can not modify any data, i.e. can
only return values

You might also like