MySQL
Q:- 1 What is MySQL?
MySQL is currently the most popular open source database server in existence. On top of that,
it is very commonly used in conjunction with PHP scripts to create powerful and dynamic serverside applications.
MySQL has been criticized in the past for not supporting all the features of other popular and
more expensive DataBase Management Systems. However, MySQL continues to improve with
each release (currently version 5), and it has become widely popular with individuals and
businesses of many different sizes.
Advantages of MySQL
It's faster then SQL Server
It's free for personal AND commercial use
Q:-2 Types of Tables in MySQL
MySQL supports various of table types or storage engines to allow you to optimize your
database. The table types are available in MySQL are:
ISAM
MyISAM
InnoDB
BerkeleyDB (BDB)
MERGE
HEAP
The most important feature to make all the table types above distinction is transaction-safe or not.
Only InnoDB and BDB tables are transaction safe and only MyISAM tables support full-text
indexing and searching feature. MyISAM is also the default table type when you create table
without declaring which storage engine to use. Here are some major features of each table types:
ISAM
ISAM had been deprecated and removed from version 5.x. All of it functionality entire replace by
MyISAM. ISAM table has a hard size 4GB and is not portable.
MyISAM
MyISAM table type is default when you create table. MyISAM table work very fast but not
transaction-safe. The size of MyISAM table depends on the operating system and the data file are
portable from system to system. With MyISAM table type, you can have 64 keys per table and
maximum key length of 1024 bytes.
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)
9974846443
D.C.Shah BCA College , Mandvi
MySQL
InnoDB
Different from MyISAM table type, InnoDB table are transaction safe and supports row-level
locking. Foreign keys are supported in InnoDB tables. The data file of InnoDB table can be stored
in more than one file so the size of table depends on the disk space. Like the MyISAM table type,
data file of InnoDB is portable from system to system. The disadvantage of InnoDB in
comparison with MyISAM is it take more disk space.
BDB
BDB is similar to InnoDB in transaction safe. It supports page level locking but data file are not
portable.
MERGE
Merge table type is added to treat multiple MyISAM tables as a single table so it remove the size
limitation from MyISAM tables.
HEAP
Heap table is stored in memory so it is the fastest one. Because of storage mechanism, the data
will be lost when the power failure and sometime it can cause the server run out of memory. Heap
tables do not support columns with AUTO_INCREMENT, BLOB and TEXT characteristics.
Q:-3 Explain Data types in Mysql
Type
Size
Description
CHAR[Length]
Length bytes
A fixed-length field from 0 to 255
characters long.
VARCHAR(Length)
String length + 1 bytes
A fixed-length field from 0 to 255
characters long.
TINYTEXT
String length + 1 bytes
A string with a maximum length of
255 characters
TEXT
String length + 2 bytes
A string with a maximum length of
65,535 characters.
MEDIUMTEXT
String length + 3 bytes
A string with a maximum length of
16,777,215 characters.
LONGTEXT
String length + 4 bytes
A string with a maximum length of
4,294,967,295 characters.
TINYINT[Length]
1 byte
Range of -128 to 127 or 0 to 255
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)
9974846443
D.C.Shah BCA College , Mandvi
MySQL
unsigned.
SMALLINT[Length]
2 bytes
Range of -32,768 to 32,767 or 0 to
65,535 unsigned.
MEDIUMINT[Length]
3 bytes
Range of -8,388,608 to 8,388,607 or 0
to 16,777,215 unsigned
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)
9974846443
D.C.Shah BCA College , Mandvi
MySQL
INT[Length]
4 bytes
Range of -2,147,483,648 to
2,147,483,647 or 0 to 4,294,967,295
unsigned
BIGINT[Length]
8 bytes
Range of -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807 or 0 to
18,446,744,073,709,551,615 unsigned
FLOAT
4 bytes
A small number with a floating
decimal point.
DOUBLE[Length, Decimals]
8 bytes
A large number with a floating decimal
point.
DECIMAL[Length, Decimals]
Length +1 bytes or
A DOUBLE stored as a string,
allowing for a fixed decimal point.
Length + 2 bytes
DATE
3 bytes
In the format YYYY-MM-DD
DATETIME
8 bytes
In the format YYYY-MM-DD
HH:MM:SS.
TIMESTAMP
4 bytes
In the format
YYYYMMDDHHMMSS; acceptable
range ends in the year 2037.
TIME
3 bytes
In the format of HH:MM:SS.
ENUM
1 or 2 bytes
Short for enumeration, that is, each
column can have one of several
possible values.
SET
1, 2, 3, 4, or 8 bytes
Like ENUM except that each column
can have more than one of several
possible values.
Q:-4 Explain Truncate in Mysql.
It is useful to remove all the rows from the table.
ex TRUNCATE table persondetail;
It is work same like DELETE command. It is faster then DELETE command
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)
9974846443
D.C.Shah BCA College , Mandvi
MySQL
TRUNCATE statement first drop the table and recreate empty table again.
It is not transaction safe.
Q:-5 Explain Aliases
An alias can be used in a query select list to give a column a different name. You can use the alias
in GROUP BY, ORDER BY, or HAVING clauses to refer to the column:
Syntax:- select <fieldname> As <Aliasname> from <tablename>
Example:- select name as StudentName from student.
Output:- StudentName
vimal
vishal
Q:-6 Explain Insert , update, delete, select , create in Mysql.
1) Create:- The CREATE DATABASE statement is used to create a database in MySQL.
Syntax
CREATE DATABASE database_name
Exmaple:<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>
2)Insert:- The INSERT INTO statement is used to add new records to a database table.
Syntax:-It is possible to write the INSERT INTO statement in two forms.
The first form doesn't specify the column names where the data will be inserted, only their values:
INSERT INTO table_name
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)
9974846443
D.C.Shah BCA College , Mandvi
MySQL
VALUES (value1, value2, value3,...)
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
Example:$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_close($con);
?>
3)Select:- The SELECT statement is used to select data from a database.
Syntax
SELECT column_name(s)
FROM table_name
To get PHP to execute the statement above we must use the mysql_query() function. This
function is used to send a query or command to a MySQL connection.
Example:-The following example selects all the data stored in the "Persons" table (The *
character selects all the data in the table):
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)
9974846443
D.C.Shah BCA College , Mandvi
MySQL
mysql_close($con);
?>
4)Delete:- The DELETE FROM statement is used to delete records from a database table.
Syntax
DELETE FROM table_name
WHERE some_column = some_value
Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which
record or records that should be deleted. If you omit the WHERE clause, all records will be
deleted!
To get PHP to execute the statement above we must use the mysql_query() function. This
function is used to send a query or command to a MySQL connection.
Example:-
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
mysql_close($con);
?>
5)Update:- The UPDATE statement is used to update existing records in a table.
Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which
record or records that should be updated. If you omit the WHERE clause, all records will be
updated!
To get PHP to execute the statement above we must use the mysql_query() function. This
function is used to send a query or command to a MySQL connection.
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)
9974846443
D.C.Shah BCA College , Mandvi
MySQL
Example
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("UPDATE Persons SET Age = '36'
WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
mysql_close($con);
?>
Q:-7 Explain ORDER BY Clause in Mysql
The ORDER BY keyword is used to sort the data in a recordset.The ORDER BY keyword sort
the records in ascending order by default.If you want to sort the records in a descending order,
you can use the DESC keyword.
Syntax
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
Example:-The following example selects all the data stored in the "Persons" table, and sorts the
result by the "Age" column:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons ORDER BY age");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'];
echo " " . $row['LastName'];
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)
9974846443
D.C.Shah BCA College , Mandvi
MySQL
echo " " . $row['Age'];
echo "<br />";
}
mysql_close($con);
?>
Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)
9974846443
D.C.Shah BCA College , Mandvi