This document provides an overview of the MySQL database including:
- The different types of databases including MySQL, MS SQL Server, Oracle Server, and MS Access.
- The advantages of using MySQL such as being open-source, powerful, standard SQL language, works on many operating systems and with many languages.
- Key aspects of MySQL including queries, clauses, operators, keys, joins, and datatypes. Queries are used to manipulate and retrieve data while keys uniquely identify records. Joins combine data from multiple tables.
1. MYSQL DATABASE
● BY THIS YOU CAN EASILY LEARN THE
DATABASE.
● NOTES- BY MAYANK TIWARI
2. MYSQL DATABASE
1. INTRODUCTION OF MYSQL DATABASE
2. TYPES OF DATABASE
3. ADVANTAGE OF MYSQL DATABASE
4. MYSQL QUERIES
5. MYSQL CLAUSE
6. MYSQL OPERATORS
7. MYSQL JOINS
8. MYSQL KEYS
9. MYSQL DATATYPES
3. INTRODUCTION OF MYSQL
DATABASE
1. Database is the collection of data, information & records in a
organized way.
2. All database used SQL that is structural query language. If
you want to deal with the database you used the query
language.(query is the instruction to the database given by
developer or query is just the syntax you written in a
language.)(structural query means you write the syntax step
by step or a structural foam.)
3. Database is just like pointer who store the value,change the
value,get the value,post the value
4. We use data base for storing the DATAINFORMATION into it.
5. MYSQL database is the collection of data, information &
records in the form of tables,with related data.
4. TYPES OF DATABASE
RDBMS :-
1. MYSQL DATABASE.
2. MS SQL SERVER.
3. DB2.
4. ORACLE SERVER.
5. MS ACCESS.
Note - {Syntax of Queries in php :- join->clause-
>conditional operatores->group by ->order by.}
Note – All the sql quary syntax are written on that page.
http://www.w3schools.com/sql/sql_quickref.asp
5. ADVANTAGE OF MYSQL
DATABASE
1. MYSQL is released under an open-source license. So
you have nothing to pay to use it.
2. MYSQL is a very powerful program in its own right. It
handles a large subset of the functionality of the most
expensive and powerful database packages.
3. MYSQL uses a standard form of the well-known SQL
data language.
4. MYSQL works on many operating systems and with
many languages including PHP, PERL, C, C++, JAVA
etc.
6. 5. MYSQL works very quickly and works well even with
large data sets.
6. MYSQL is very friendly to PHP, the most appreciated
language for web development.
7. MYSQL supports large databases, up to 50 million
rows or more in a table. The default file size limit for a
table is 4GB, but you can increase this (if your
operating system can handle it) to a theoretical limit
of 8 million terabytes (TB).
8. MYSQL is customizable. The open source GPL
license allows programmers to modify the MYSQL
software to fit their own specific environments.mary
of the current situation
7. MYSQL QUERIES
What is DDL, DML and DCL?
1. Data Definition Language deals with database
schemas and descriptions of how the data should
reside in the database, therefore language
statements like CREATE TABLE or ALTER TABLE
belong to DDL.
2. DML deals with data manipulation, and therefore
includes most common SQL statements such
SELECT, INSERT, etc.
3. Data Control Language includes commands such as
GRANT, and mostly concerns with rights,
permissions and other controls of the database
system.
8. DDL
Data Definition Language (DDL) statements are
used to define the database structure or
schema.
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
TRUNCATE - remove all records from a table, including all spaces allocated
for the records are removed
COMMENT - add comments to the data dictionary
RENAME - rename an object
9. DML
Data Manipulation Language (DML) statements
are used for managing data within schema
objects.
SELECT - retrieve data from the a database
INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes all records from a table, the space for the records remain
MERGE - UPSERT operation (insert or update)
CALL - call a PL/SQL or Java subprogram EXPLAIN PLAN - explain access path to
data LOCK
TABLE - control concurrency
10. DCL
Data Control Language (DCL) statements.
GRANT - gives user's access privileges to database
REVOKE - withdraw access privileges given with the GRANT command
11. MYSQL QUERIES
1. Create database :- To create database & table. Syntax : {create
database database name;}
{CREATE TABLE authors (id INT, name VARCHAR(20), email
VARCHAR(20));}
2. Show :- To show the database & table you have. Syntax : {show
databases ;}{show table;}
3. Use :- To use data base. syntax : {use data base name;}
4. select * :- This is used to show the detail information of table.
syntax : {select*from table name}
5. Like :- It is used to get the data from the table which is most
12. 5. Alter & Modify :- To change the data type of table. syntax : {alter
table table name modify column name data type with length;}
6. Drop :- It is used to delete database,table,column,index,view.
Syntax :{ drop 'name of thing which you want to drop' 'giv
en' name;}
7. Update :- It is used to update the table data. syntax : {update
table name set column name=row name(cell value) where column
name=row name(cell value)}You can chose the multiple value also to
update.
8. Delete :- It is used to delete the data from table. syntax : {delete
from table name where column name = row name;}
9. Describe :- It is used to describe the the table structure. syntax :
{describe or desc table name;}
13. 10. Insert into :- It is used to insert data in table. syntax : {insert
into (all column name use , bet ween it) values( all values we want to
insert);}You can chose the multiple value also to insert in single
statement.
11. Specification :- If we want a specific data from the table :- syntax
: {select*from table name where column name= row name (cell
value);}
12. Sorting of table content :- It is used to sort the content of table in
ascending or descending order. Syntax :{select column name(by *
you can select all column of table) from table name order by column
name asc or desc;}
13. Group By :- This statement is used with aggregate function.where we
fetch the values of a column in group.
14. Having :- The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions.
10. SQL Having9. SQL Group By
10. SQL Having9. SQL Group By
14. MYSQL CLAUSE
1. where :- Use to go on the table row position. Or cell position.
2. From :- It is used to difine table name by using from.
3. To :- It is also for our convienace.
4. Add :- To add the column in the table.
5. Top :- The TOP clause is used to specify the number of records to
return.The TOP clause can be very useful on large tables with
thousands of records. Returning a large number of records can
impact on performance.
Note: Not all database systems support the TOP clause.
SQL Server Syntax
{SELECT TOP number|percent column_name(s) FROM table_name}
{SELECT TOP 2 * FROM table name;}
6. Having :- This clause is used with group by statement. Its also a
conditional operator.
15. MYSQL OPERATORS
1. Conditional operators(AND,OR,NOT) :- That are use to show the
data which is dependent on condition. Syntax :{update table name
set column name=value where column name=value and column
name =value;} same for or.
2. Union operator :- It is used to combine all the tables in database
& represented all tables data in a single table foam. Syntax :
{ select column name(for all column use * at the place of column
name) from table name union select*from table name;} it's not
showing the repeated data. But for all the repeated data use union
all at the place of union.
3. Between :- The BETWEEN operator is used in a WHERE clause to
select a range of data between two values.
4. In :- The IN operator allows you to specify multiple values in a
WHERE clause.syntax :{ SELECT column_name(s)FROM table_name WHERE
column_name IN (value1,value2,...)}
5. Like :- The LIKE operator is used in a WHERE clause to search for
a specified pattern in a column.
16. MYSQL KEYS
1. KEYS ARE DIFINE IN the tables to
FETCH THE UNIQE DATA.
2. You make so many keys in a table
according to your requirement.
17. TYPES OF KEYS
1. SUPER KEY
2. CANDIDATE KEY
3. PRIMARY KEY
4. UNIQE KEY
5. SIMPLE KEY
6. SECONDRY KEY OR ALTERNATIVE KAY
7. COMPOUND KEY
8. COMPOSITE KEY
9. FOREIGN KEY
18. 1. SUPER KEY :-
2. CANDIDATE KEY :-
3. SIMPLE KEY :-
1. SUPER KEY :-
An attribute or a combination of attribute
that is used to identify the records uniquely is known as Super Key.
A table can have many Super Keys,.
any combination which can identify the records uniquely will be a
Super Key.
2. CANDIDATE KEY :-
A candidate key is a field or
combination of fields that can act as a primary key field for that
table to uniquely identify each record in that table.
A candidate key is a sub set of a Super Keys.A candidate key is a
sub set of a Super Keys.
In order to be eligible for a candidate key it must pass certain
criteria.
It must contain unique values
It must not contain null values
It contains the minimum number of fields to ensure uniqueness
It must uniquely identify each record in the table
Once your candidate keys have been identified you can now select
one to be your primary key
19. 4. SECONDRY OR ALTERNATIVE KAY :-
5. COMPOUND KEY :-
6. COMPOSITE KEY :-
3. SIMPLE KEY :-A simple key consists of a single field to
uniquely identify a record.primary key is also known as simple key.
4. SECONDRY KEY OR ALTERNATIVE KAY :-
The attributes that are not even the Super Key but can be still used
for identification of records (not unique) are known as Secondary
Key.
Alternate Key can be any of the Candidate Keys except for the
Primary Key. It is a subset of candidate key.
5. COMPOUND KEY :-A Combination of more than one
column identifying records of a table uniquely, all the columns that
take part in the combination process are Simple Key’s.
6. COMPOSITE KEY :- If we use multiple attributes to create
a Primary Key then that Primary Key is called Composite Key. In this
all the other columns that take part in the combination process are
not simple key's.
20. PRIMARY KEY
1. The PRIMARY KEY constraint uniquely identifies
each record in a database table.
2. Primary keys must contain unique values.
3. A primary key column cannot contain NULL
values.
4. Each table should have a primary key, and each
table can have only ONE primary key.
NOTE - YOU CAN REMOVE KEY BY ALTER & DROP
AND ADD BY ALTER.& CONSTRAINT IS ALSO
THERE.
21. UNIQUE KEY
1. The UNIQUE constraint uniquely identifies each record in a
database table.
2. The UNIQUE and PRIMARY KEY constraints both provide a
guarantee for uniqueness for a column or set of columns.
3. A PRIMARY KEY constraint automatically has a UNIQUE
constraint defined on it.
4. Note - that you can have many UNIQUE constraints per
table, but only one PRIMARY KEY constraint per table.
22. FOREIGN KEY
1. A FOREIGN KEY in one table points to a PRIMARY
KEY in another table.
CREATE TABLE SECOND TABLE NAME(COLUMN 1 int NOT
NULL,COLUMN 2 int NOT NULL,COLUMN 3 int,PRIMARY KEY
(SECOND TABLE COLUMN 1),FOREIGN KEY (COLUMN 1 IN
TABLE FIRST) REFERENCES FIRST TABLE NAME (COLUMN 1
IN))
NOTE – FOREIGN KEY STABILISED THE RELATION BETWEEN
TWO TABLES WHICH CONTAINS THE AT LEAST ONE SAME
COLUMN.
WE CAN ADD & DELETE THE FOREIGN KEY BY ALTER & DROP
QUIREY.
23. MYSQL JOINS
1. JOINS ARE USE TO JOIN THE TABLES. & FETCH THE DATA OF
DIFFERENT TABLES WHICH ARE RELATED BY AT LEAST ONE
SIMILAR COLUMN BY SINGLE TABLE.In simple words joins are
join the tables.
2. The JOIN keyword is used in an SQL statement to query data from
two or more tables, based on a relationship between certain columns
in these tables.
3. Tables in a database are often related to each other with keys.
4. A primary key is a column (or a combination of columns) with a
unique value for each row. Each primary key value must be unique
within the table. The purpose is to bind data together, across tables,
without repeating all of the data in every table.
5. SQL joins are used to query data from two or more tables, based on
a relationship between certain columns in these tables.
24. Types of joins
1. Inner join
2. Outer join
3. Full join
2. Outer join :-
1. Left outer join
2. Right outer join
25. 1. Inner join :-The INNER JOIN keyword
returns rows when there is at least one
match in both tables.
SQL INNER JOIN Syntax
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
2. Left outer join :-The LEFT JOIN keyword
returns all rows from the left table
(table_name1), even if there are no
matches in the right table (table_name2).
26. SQL LEFT JOIN Syntax
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.
3. Right outer join :-The RIGHT JOIN keyword returns
all the rows from the right table (table_name2), even if there are
no matches in the left table (table_name1).
SQL RIGHT JOIN Syntax
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
PS: In some databases RIGHT JOIN is called RIGHT OUTER
JOIN.
27. 4. Full join :-The FULL JOIN keyword return rows when there
is a match in one of the tables. It shows all the row of right & left
table whether it is matched or not matched with each other. Mysql
has the lack support to full join. So we can't use the below syntax in
mysql. We use the last syntax in mysql to use full join.
Syntax: The basic syntax of FULL JOIN is as follows:
SELECT table1.column1, table2.column2...
FROM table1
FULL JOIN table2
ON table1.common_filed = table2.common_field;
Note - If your Database does not support FULL JOIN like MySQL does not
support FULL JOIN, then you can use UNION ALL clause to combile two JOINS
as follows:
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION ALL
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
28. MYSQL DATATYPES
MYSQL has many different data types, separated into three categories:
numeric, date and time, and string types.
1. Numeric Data Types :-
1. INT - A normal-sized integer that can be signed or unsigned. If signed,
the allowable range is from -2147483648 to 2147483647. If unsigned, the
allowable range is from 0 to 4294967295. You can specify a width of up to
11 digits.
2. TINYINT - A very small integer that can be signed or unsigned. If signed,
the allowable range is from -128 to 127. If unsigned, the allowable range is
from 0 to 255. You can specify a width of up to 4 digits.
3. SMALLINT - A small integer that can be signed or unsigned. If signed, the
allowable range is from -32768 to 32767. If unsigned, the allowable range
is from 0 to 65535. You can specify a width of up to 5 digits.
4. MEDIUMINT - A medium-sized integer that can be signed or unsigned. If
signed, the allowable range is from -8388608 to 8388607. If unsigned, the
allowable range is from 0 to 16777215. You can specify a width of up to 9
digits.
29. 2. Date and Time Types :-
1. DATE - A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-
31. For example, May 15, 1985 would be stored as 1985-05-15.
2. DATETIME - A date and time combination in YYYY-MM-DD HH:MM:SS
format, between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. For
example, 4:30 in the afternoon on December 25th, 1980 would be stored as
1980-12-25 16:30:00.
3. TIMESTAMP - A timestamp between midnight, January 1, 1970 and
sometime in 2037. This looks like the previous DATETIME format, only
without the hyphens between numbers; 3:30 in the afternoon on December
30th, 1973 would be stored as 19731230153000 ( YYYYMMDDHHMMSS ).
4. TIME - Stores the time in HH:MM:SS format.
5. YEAR(M) - Stores a year in 2-digit or 4-digit format. If the length is
specified as 2 (for example YEAR(2)), YEAR can be 1970 to 2069 (70 to 69).
If the length is specified as 4, YEAR can be 1901 to 2155. The default length
is 4.
30. 3. String Types :-
1. CHAR(M) - A fixed-length string between 1 and 255 characters in length
(for example CHAR(5)), right-padded with spaces to the specified length
when stored. Defining a length is not required, but the default is 1.
2. VARCHAR(M) - A variable-length string between 1 and 255 characters in
length; for example VARCHAR(25). You must define a length when creating a
VARCHAR field.
3. BLOB or TEXT - A field with a maximum length of 65535 characters.
BLOBs are "Binary Large Objects" and are used to store large amounts of
binary data, such as images or other types of files. Fields defined as TEXT
also hold large amounts of data; the difference between the two is that sorts
and comparisons on stored data are case sensitive on BLOBs and are not
case sensitive in TEXT fields. You do not specify a length with BLOB or TEXT.
31. 4. TINYBLOB or TINYTEXT - A BLOB or TEXT column with a maximum length
of 255 characters. You do not specify a length with TINYBLOB or TINYTEXT.
5. MEDIUMBLOB or MEDIUMTEXT - A BLOB or TEXT column with a maximum
length of 16777215 characters. You do not specify a length with
MEDIUMBLOB or MEDIUMTEXT.
6. LONGBLOB or LONGTEXT - A BLOB or TEXT column with a maximum
length of 4294967295 characters. You do not specify a length with LONGBLOB
or LONGTEXT.
7. ENUM - An enumeration, which is a fancy term for list. When defining an
ENUM, you are creating a list of items from which the value must be selected
(or it can be NULL). For example, if you wanted your field to contain "A" or
"B" or "C", you would define your ENUM as ENUM ('A', 'B', 'C') and only those
values (or NULL) could ever populate that field.
32. MYSQL FUNCTIONS
-: AGGREGATE FUNCTIONS :-
SQL aggregate functions return a
single value, calculated from values
in a column.
1. SQL avg()
2. SQL count()
3. SQL first()
4. SQL last()
5. SQL max()
6. SQL min()
7. SQL sum()
-:SCALAR FUNCTIONS :-
SQL scalar functions return a single
value, based on the input value.
1. SQL ucase()
2. SQL lcase()
3. SQL mid()
4. SQL len()
5. SQL round()
6. SQL now()
7. SQL format()
33. AGGREGATE 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 of column values
34. 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
35. NORMALIZATION
Normalization is the by which we refine the data or fillter the data
with uniqueness.
Normalization is the process where a database is designed in a way
that removes redundancies, and increases the clarity in organizing
data in a database.
Minimize data redundancy i.e. no unnecessarily duplication of data.
To make database structure flexible i.e. it should be possible to add
new data values and rows without reorganizing the database
structure.
Data should be consistent throughout the database i.e. it should not
suffer from following anomalies.
Insert Anomaly - Due to lack of data i.e., all the data available for insertion
such that null values in keys should be avoided. This kind of anomaly can
seriously damage a database
36. Update Anomaly - It is due to data redundancy i.e. multiple
occurrences of same values in a column. This can lead to
inefficiency.
Deletion Anomaly - It leads to loss of data for rows that are not
stored else where. It could result in loss of vital data.
Complex queries required by the user should be easy to handle.
On decomposition of a relation into smaller relations with fewer
attributes on normalization the resulting relations whenever joined
must result in the same relation without any extra rows. The join
operations can be performed in any order. This is known as
Lossless Join decomposition.
The resulting relations (tables) obtained on normalization should
possess the properties such as each row must be identified by a
unique key, no repeating groups, homogenous columns, each
column is assigned a unique name etc.
37. TYPES OF NORMAL FORM
1. 1st Normal Form or 1NF
2. 2nd Normal Form or 2NF
3. 3nd Normal Form or 3NF
4. BCNF (Boyce & Codd)
5. 4NF
6. 5NF
7. 6NF