MySQL
What is SQL?
MySQL is a open source Relational Database Management System. MySQL is very fast, reliable and flexible Database Management System.
MySQL Features
- MySQL command line tool is very powerful and can be used to run SQL queries against database.
- MySQL supports indexing and binary objects.
- It allows to changes to structure of table while server is running.
- It is a very fast thread-based memory allocation system.
Advantages of MySQL:
- Reliability and Performance.
- Cross-Platform support:
- Powerful Uncomplicated software:
Classification of SQL statements
SQL can be divided into following parts:
1. Data Manipulation Language (DML) commands — A DML is a language that enables users to access on manipulates data as organized by the appropriate data model.
2. Data Definition Language (DDL) commands — The DDL commands, as the name suggests, allow you to perform tasks related to data definition. The DDL part of SQL permits database tables to be created or deleted
3. Transaction Control Language (TCL) commands — The TCL commands are used to manage and control the transactions of data in database.
SIMPLE QUERIES IN SQL
- SQL SELECT Statement
The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set.
- The SQL SELECT DISTINCT Statement
The DISTINCT keyword can be used to return only distinct (different) values. - The WHERE Clause
The WHERE clause is used to extract only those records that fulfil a specified criterion. Operators Allowed in the WHERE Clause are =, <>,>,<,<=,>=,between, like, in.
The AND & OR Operators
The AND & OR operators are used to filter records based on more than one condition. The AND operator displays a record if both the first condition and the second condition is true. And OR operator displays a record if either the first condition or the second condition is true.
The ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, then DESC keyword is used.
The INSERT INTO statement is used to insert a new row in a table.
The IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
The LIKE operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
The BETWEEN Operator
The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates.
SQL Wildcards
SQL wildcards can substitute for one or more
characters when searching for data in a database.
SQL wildcards must be used with the SQL LIKE operator.
With SQL, the following wildcards can be used: %,_,[charlist],[^charlist] or [!charlist]
SQL Alias
A table or column can be given another name by using an alias. This can be a good thing to do if very long or complex table names or column names are present. Aliases can be anything.
SQL Functions
Useful functions :
- AVG() – Returns the average value
- COUNT() – Returns the number of rows
- FIRST() – Returns the first value
- LAST() – Returns the last value
- MAX() – Returns the largest value
- MIN() – Returns the smallest value
- SUM() – Returns the sum
SQL Scalar functions
SQL scalar functions return
a single value, based on the input value.
Useful scalar functions:
- UCASE() – Converts a field to upper case
- LCASE() – Converts a field to lower case
- MID() – Extract characters from a text field
- LEN() – Returns the length of a text field
- ROUND() – Rounds a numeric field to the number of decimals specified
- NOW() – Returns the current system date and time
- FORMAT() – Formats how a field is to be displayed
The HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.