Mysql
Mysql
MySQL
1. MySQL is a widely used relational database management system (RDBMS).
2. MySQL is free and open-source.
3. MySQL is ideal for both small and large applications.
4. MySQL is very fast, reliable, scalable, and easy to use
5. MySQL is cross-platform
6. MySQL is compliant with the ANSI SQL standard
7. MySQL was first released in 1995
8. MySQL is developed, distributed, and supported by Oracle Corporation
9. MySQL is named after co-founder Monty Widenius's daughter: My
Uses of MySQL
1. Huge websites like Facebook, Twitter, Airbnb, Booking.com, Uber, GitHub, YouTube, etc.
2. Content Management Systems like WordPress, Drupal, Joomla!, Contao, etc.
3. A very large number of web developers around the world
RDBMS
1. RDBMS stands for Relational Database Management System.
2. RDBMS is a program used to maintain a relational database.
3. RDBMS is the basis for all modern database systems such as MySQL, Microsoft SQL Server, Oracle, and Microsoft Access.
4. RDBMS uses SQL queries to access the data in the database.
Relational Database
A relational database defines database relationships in the form of tables. The tables are related to each other - based on data
common to each.
SQL
1. SQL is the standard language for dealing with Relational Databases.
2. SQL is used to insert, search, update, and delete database records.
3. SQL keywords are NOT case sensitive i.e. select is the same as SELECT
SQL Commands
SELECT -> extracts data from a database
UPDATE -> updates data in a database
DELETE -> deletes data from a database
INSERT INTO -> inserts new data into a database
CREATE DATABASE -> creates a new database
ALTER DATABASE -> modifies a database
CREATE TABLE -> creates a new table
ALTER TABLE -> modifies a table
DROP TABLE -> deletes a table
CREATE INDEX -> creates an index (search key)
DROP INDEX -> deletes an index
To delete the data inside a table, but not the table itself:
TRUNCATE TABLE table_name;
INSERT INTO
The INSERT INTO statement is used to insert new records in a table.
WHERE
used to filter records
Operators in Where
'=' -> Equal
'>' -> Greater than
'<' -> Less than
'>=' -> Greater than or equal
'<=' -> Less than or equal
'<>' -> Not equal. Note: In some versions of SQL this operator may be written as '!='
'BETWEEN' -> Between a certain range
'LIKE' -> Search for a pattern
'IN' -> To specify multiple possible values for a column
AND Syntax: "If all the conditions separated by AND are TRUE"
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
ORDER BY
used to sort the result-set in ascending or descending order
Order By
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
NULL Values
NULL value is a field with no value
A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank
during record creation!
Is Null
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
Is Not Null
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
UPDATE
used to modify the existing records in a table
Update record
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
LIKE
used in a WHERE clause to search for a specified pattern in a column
There are two wildcards often used in conjunction with the LIKE operator:
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length
WHERE CustomerName LIKE 'a_%_%' Finds any values that starts with "a" and are at least 3 characters in length
WHERE CustomerName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
Like
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Not Like
SELECT column1, column2, ...
FROM table_name
WHERE columnN NOT LIKE pattern;
IN
To specify multiple values in a WHERE clause
It's a shorthand for multiple OR conditions
In:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Not In:
SELECT column_name(s)
FROM table_name
WHERE column_name NOT IN (value1, value2, ...);
OR
In:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
Not In:
SELECT column_name(s)
FROM table_name
WHERE column_name NOT IN (SELECT STATEMENT);
BETWEEN
It selects values within a given range
The values can be numbers, text, or dates
begin and end values are included
Between
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Not Between
SELECT column_name(s)
FROM table_name
WHERE column_name NOT BETWEEN value1 AND value2;
Between with IN
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2 And column_name NOT IN (value3,value4,...);
Between Text
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN textvalue1 AND textvalue2;
Between Dates
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN date1 AND date2;
Aliases
used to give a table, or a column in a table, a temporary name
used to make column names more readable
only exists for the duration of that query
created with the AS keyword
Alisas Column
SELECT column_name AS alias_name
FROM table_name;
Alisas Table
SELECT column_name(s)
FROM table_name AS alias_name;
Joins
used to combine rows from two or more tables, based on a related column between them
Types of Joins
INNER JOIN: Returns records that have matching values in both tables
LEFT JOIN: Returns all records from the left table, and the matched records from the right table
RIGHT JOIN: Returns all records from the right table, and the matched records from the left table
CROSS JOIN: Returns all records from both tables
LEFT JOIN: "Returns all records from the left table1, and the
matching records (if any) from the right table2"
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
RIGHT JOIN: "Returns all records from the right table2, and the
matching records (if any) from the left table1"
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
UNION
used to combine the result-set of two or more SELECT statements
Every SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in every SELECT statement must also be in the same order
Union:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
ORDER BY columan_name; //Optional
Union All:
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2
ORDER BY columan_name; //Optional
GROUP BY
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one
or more columns.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
HAVING
The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
EXISTS
Used to test for the existence of any record in a subquery
Returns TRUE if the subquery returns one or more records
SELECT column_name(s)
FROM table_name
WHERE EXISTS (SELECT column_name FROM table_name WHERE condition);
Any:
Returns a boolean value as a result
ANY means that the condition will be true if the operation is true for any of the values in the range.
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY (SELECT column_name FROM table_name WHERE condition);
All:
Returns a boolean value as a result
Returns TRUE if ALL of the subquery values meet the condition
CASE
The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So,
once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.
If there is no ELSE part and no conditions are true, it returns NULL.
MySQL Operators
Arithmetic Operators
'+' -> Add
'-' -> Subtract
'*' -> Multiply
'/' -> Divide
'%' -> Modulo
Bitwise Operators
'&' Bitwise AND
'|' Bitwise OR
'^' Bitwise exclusive OR
Comparison Operators
'=' -> Equal to
'>' -> Greater than
'<' -> Less than
'>=' -> Greater than or equal to
'<=' -> Less than or equal to
'<>' -> Not equal to
Compound Operators
'+=' -> Add equals
'-=' -> Subtract equals
'*=' -> Multiply equals
'/=' -> Divide equals
'%=' -> Modulo equals
'&=' -> Bitwise AND equals
'^-=' -> Bitwise exclusive equals
'|*=' -> Bitwise OR equals
Logical Operators
'ALL' -> TRUE if all of the subquery values meet the condition
'AND' -> TRUE if all the conditions separated by AND is TRUE
'ANY' -> TRUE if any of the subquery values meet the condition
'BETWEEN' -> TRUE if the operand is within the range of comparisons
'EXISTS' -> TRUE if the subquery returns one or more records
'IN' -> TRUE if the operand is equal to one of a list of expressions
'LIKE' -> TRUE if the operand matches a pattern
'NOT' -> Displays a record if the condition(s) is NOT TRUE
'OR' -> TRUE if any of the conditions separated by OR is TRUE
'SOME' -> TRUE if any of the subquery values meet the condition
ALTER TABLE
used to add, delete, or modify columns in an existing table
And used to add and drop various constraints on an existing table
Syntax:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
CREATE INDEX
CREATE INDEX
CREATE INDEX index_name
ON table_name (column1, column2, ...);
DROP INDEX
ALTER TABLE table_name
DROP INDEX index_name;
AUTO INCREMENT
Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table.
Often this is the primary key field that we would like to be created automatically every time a new record is inserted.
CREATE VIEW
A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
Creating a View
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Updating a View
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Dropping a View
DROP VIEW view_name;