Database
It is defined as a collection of interrelated data stored together to serve multiple applications.
MySQL Elements
MySQL has certain elements that play an important role in querying a database.
Literals
Literals refer to a fixed data value
Data Types
Data types are means to identify the type of data.
NULL Values
If a column has no value, then it is said to be NULL
Comments
A comment is a text that is not executed.
MySQL Simple Calculations
You can perform simple calculations in MySQL, just by using the Select command, there's no need to select any particular database to perform these commands.
Addition
It will add two numbers
Subtraction
It will subtract the second number from first
Multiplication
It will give the product of supplied numbers
Division
It will divide the number
Accessing Database
These commands allow one to check all the databases and tables
Show command
It will show all the databases in the system
It will show all the tables in a selected database
Use command
It will start using the specified database i.e. now you can create tables in the selected database
Creating tables
These commands allow you to create the table in MySQL
Create table command
This query is used to create a table in the selected database
Insert command
It will add data into the selected table
Inserting NULL values
This query will add NULL value in the col3 of the selected table
Inserting Dates
It will add the following data into the selected column of the table
Select Command
A select query is used to fetch the data from the database
Selecting All Data
It will retrieve all the data of the selected table
Selecting Particular Rows
It will retrieve all the data of the row that will satisfy the condition
Selecting Particular Columns
It will retrieve data of selected columns that will satisfy the condition
DISTINCT Keyword
It will retrieve only distinct data i.e. duplicate data rows will get eliminated
ALL Keyword
It will retrieve all the data of the selected column
Column Aliases
It is used to give a temporary name to a table or a column in a table for the purpose of a particular query
Condition Based on a Range
It will only retrieve data of those columns whose values will fall between value1 and value2 (both inclusive)
Condition Based on a List
Condition Based on Pattern Match
Searching NULL
It returns data that contains a NULL value in them
SQL Constraints
SQL constraints are the rules or checks enforced on the data columns of a table
NOT NULL
It will create a table with NOT NULL constraint to its first column
DEFAULT
DEFAULT constraint provides a default value to a column
UNIQUE
UNIQUE constraint ensures that all values in the column are different
CHECK
CHECK constraint ensures that all values in a column satisfy certain conditions
Primary Key
Primary key is used to uniquely identify each row in a table
Foreign Key
Viewing Table Structure
Desc or Describe command
It allows you to see the table structure
Modifying Data
Update Command
It will update the values of selected columns
Deleting Data
Delete Command
It will delete the entire row that will satisfy the condition
Ordering Records
Order by clause is used to sort the data in ascending or descending order of specified column
order by clause
It will return records in the ascending order of the specified column name's data
It will return records in the descending order of the specified column name's data
Ordering data on multiple columns
It will return records in the ascending order of column1 and descending order of column2
Grouping Result
It is used to arrange identical data into groups so that aggregate functions can work on them
Group by clause
It allows you to group two or more columns and then you can perform aggregate function on them
Having clause
Having clause is used to put conditions on groups
Altering Table
These commands allow you to change the structure of the table
To Add New Column
It will add a new column in your table
To Modify Old Column
It will update the data type or size of old column
To Change Name of Column
It will change the name of the old column in the table
Dropping Table
DROP command
It will delete the complete table from the database
MySQL Functions:
There are many functions in MySQL that perform some task or operation and return a single value
Text/String Functions
Text function work on strings
Char Function
It returns the character for each integer passed
Concat Function
It concatenates two strings
Lower/Lcase
It converts a string into lowercase
Upper/Ucase
It converts a string into uppercase
Substr
It extracts a substring from a given string
Trim
It removes leading and trailing spaces from a given string
Instr
It searches for given second string into the given first string
Length
It returns the length of given string in bytes
Numeric Functions
Numeric function works on numerical data and returns a single output
MOD
It returns modulus of two numbers
Power
It returns the number m raised to the nth power
Round
It returns a number rounded off number
Sqrt
It returns the square root of a given number
Truncate
It returns a number with some digits truncated
Date/Time Functions
These are used to fetch the current date and time and allow you to perform several operations on them
Curdate Function
It returns the current date
Date Function
It extracts the date part of the expression
Month Function
It returns the month from the date passed
Day Function
It returns the day part of a date
Year Function
It returns the year part of a date
Now Function
It returns the current date and time
Sysdate Function
It returns the time at which function executes
Aggregate Functions
Aggregate functions or multiple row functions work on multiple data and returns a single result
AVG Function
It calculates the average of given data
COUNT Function
It counts the number of rows in a given column
MAX Function
It returns the maximum value from a given column
MIN Function
It returns the minimum value from a given column
SUM Function
It returns the sum of values in given column
MySQL Joins
Join clause is used to combine or merge rows from two or more tables based on a related attribute
INNER JOIN
It returns all rows from multiple tables where the join condition is satisfied. It is the most common type of join.
LEFT OUTER JOIN
It returns all rows from the left-hand table specified in the ON condition and only those rows from the other table where the join condition is fulfilled.
RIGHT OUTER JOIN
It returns all rows from the RIGHT-hand table specified in the ON condition and only those rows from the other table where the join condition is satisfied
FULL JOIN
It combines the results of both left and right outer joins
SELF JOIN
In this join, table is joined with itself