0% found this document useful (0 votes)
147 views12 pages

Mysql - Introduction: What Is A Database?

The document provides an overview of MySQL including: - What is a database and relational database management system (RDBMS) - Key terminology like database, table, row, column, primary key - Benefits of MySQL like being open source, powerful, compatible with many languages - How to install MySQL on Linux/UNIX using RPM packages and on Windows with an installer - How to connect to MySQL from the command line or using PHP functions - That temporary tables store temporary data and are deleted when the client session terminates

Uploaded by

tejashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views12 pages

Mysql - Introduction: What Is A Database?

The document provides an overview of MySQL including: - What is a database and relational database management system (RDBMS) - Key terminology like database, table, row, column, primary key - Benefits of MySQL like being open source, powerful, compatible with many languages - How to install MySQL on Linux/UNIX using RPM packages and on Windows with an installer - How to connect to MySQL from the command line or using PHP functions - That temporary tables store temporary data and are deleted when the client session terminates

Uploaded by

tejashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

MySQL – Introduction

*What is a Database?

-A database is a separate application that stores a collection of data.

-Other kinds of data stores can also be used, such as files on the file system or
large hash tables in memory, but data fetching and writing would not be so
fast and easy with those type of systems.

-Nowadays, we use relational database management systems (RDBMS) to


store and manage huge volume of data. This is called relational database
because all the data is stored into different tables and relations using primary
keys or other keys known as Foreign Keys.

A Relational DataBase Management System (RDBMS) is a software that:

*RDBMS Terminology

Database: A database is a collection of tables, with related data.

spreadsheet.
n: One column (data element) contains data of one and the same kind,

occur twice in one table. With a key, you can


only find one row.

column is not sufficiently unique.


MySQL Databas
 is a fast, easy-to-use RDBMS
 MYSQL is developed, marketed and supported by MySQL AB, which is
a Swedish company
 MySQL is released under an open-source license. So you have
nothing to pay to use it.
 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.
 uses a standard form of the well-known SQL data language.
 MySQL works on many operating systems and with many
languages including PHP, PERL, C, C++, JAVA, etc.
 MySQL works very quickly and works well even with large
data sets.
 MySQL is very friendly to PHP, the most appreciated language
for web development.
 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).

2.MySQL – Installation
 Installing MySQL on Linux/UNIX
 The recommended way to install MySQL on a Linux system is via
RPM.
 MySQL AB makes the following RPMs available for download on its
website:

MySQL – The MySQL database server manages the databases and


tables, controls user access and processes the SQL queries.
–client – MySQL client programs, which make it possible
to connect to and interact with the server.
MySQL–devel – Libraries and header files that come in handy
when compiling other programs that use MySQL.
MySQL–shared – Shared libraries for the MySQL client.
MySQL–bench – Benchmark and performance testing tools for
the MySQL database server.
 The MySQL RPMs listed here are all built on a SuSE Linux System,
but they will usually work on other Linux variants with no difficulty.
 steps to proceed with the installation:

command. Remember to replace the filename in italics with the file


name of your RPM.
[root@host]# rpm -i MySQL-5.0.9-0.i386.rpm

 The following code box has an optional but recommended step to install
the remaining RPMs in the same manner:
[root@host]# rpm -i MySQL-client-5.0.9-0.i386.rpm
[root@host]# rpm -i MySQL-devel-5.0.9-0.i386.rpm
[root@host]# rpm -i MySQL-shared-5.0.9-0.i386.rpm
[root@host]# rpm -i MySQL-bench-5.0.9-0.i386.rpm

*Installing MySQL on Window


 The default installation on any version of Windows is now much easier than
it used to be, as MySQL
 now comes neatly packaged with an installer. Simply download the installer
package, unzip it anywhere and run the setup.exe file.
 by default will install everything under C:\mysql.

-MySQL will not add itself to the start menu, and there is no particularly nice
GUI way to stop the server either. Therefore, if you tend to start the server by
double clicking the mysqld executable, you should remember to halt the
process by hand by using mysqladmin, Task List, Task Manager, or other
Windows-specific means.

*Verifying MySQL Installation

 you can verify that everything is working as it should be via some simple tests.
Use the mysqladmin Utility to Obtain Server Status

-Use mysqladmin binary to check the server version. This binary would be
available in /usr/bin on linux and in C:\mysql\bin on windows.
[root@host]# mysqladmin --version

*Running MySQL at Boot Time

If you want to run the MySQL server at boot time, then make sure you have the
following entry in the /etc/rc.local file.
-Also, you should have the mysqld binary in the /etc/init.d/ directory.

3.MySQL – PHP Syntax


MySQL works very well in combination of various programming languages like
PERL, C, C++, JAVA and PHP. Out of these languages, PHP is the most
popular one because of its web application development capabilities.
 PHP provides various functions to access the MySQL database and to
manipulate the data records inside the MySQL database.

The PHP functions for use with MySQL have the following general format:

mysql_function(value,value,...);

-The second part of the function name is specific to the function, usually a word
that describes what the function does.

mysqli_connect($connect);

mysqli_query($connect,"SQL statement");

The following example shows a generic syntax of PHP to call any MySQL
function.
<html>

<head>

<title>PHP with MySQL</title>

</head>

<body>

<?php

$retval = mysql_function(value, [value,...]);

if( !$retval )

die ( "Error: a related error message" );

// Otherwise MySQL or PHP Statements

?>

</body>

</html>
4. MySQL – Connection

-You can establish the MySQL database using the mysql binary at the command
prompt.
Example:
Here is a simple example to connect to the MySQL server from the command
prompt –
[root@host]# mysql -u root -p
Enter password:******

The following code block shows the result of above command:

Welcome to the MySQL monitor. Commands end with ; or \g.


Your MySQL connection id is 2854760 to server version: 5.0.9
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
-In the above example, we have used root as a user, but you can use any
other user as well.

-You can disconnect from the MySQL database anytime using the exit
command at mysql> prompt.

mysql> exit
Bye
*MySQL Connection Using PHP Script
 -PHP provides mysql_connect() function to open a database connection.
This function takes five parameters and returns a MySQL link identifier
on success or FALSE on failure.

Syntax:

connection mysql_connect(server,user,passwd,new_link,client_flag);
Parameters Description

1.server (Optional) – The host name running the database server. If not specified,
then the default value will be – localhost:3306.

2.user (Optional) – The username accessing the database. If not specified, then the
default will be the name of the user that owns the server process.

3.passwd (Optional) – The password of the user accessing the database. If not
specified, then the default will be an empty password.

4.new_link (Optional) – If a second call is made to mysql_connect() with the same


arguments, no new connection will be established; instead, the identifier of
the already opened connection will be returned.

5.client_flags (Optional) – A combination of the following constants:


– Use SSL encryption.

– Use compression protocol.

– Allow space after


function names.

E – Allow interactive timeout


seconds of inactivity before closing the connection.
-You can disconnect from the MySQL database anytime using another PHP
function mysql_close(). This function takes a single parameter, which is a
connection returned by the mysql_connect() function.
Syntax:

bool mysql_close ( resource $link_identifier );

5. MySQL – Temporary Tables

 he temporary tables could be very useful in some cases to keep temporary data.
The most important thing that should be known for temporary tables is that they
will be deleted when the current client session terminates.

 Temporary tables were added in the MySQL Version 3.23. If you use an
older version of MySQL than 3.23, you cannot use the temporary tables, but
you can use Heap Tables.

* As stated earlier, temporary tables will only last as long as the session is alive.
* If you run the code in a PHP script, the temporary table will be destroyed
automatically when the script finishes executing
* If you are connected to the MySQL database server through the MySQL
client program, then the temporary table will exist until you close the client or
manually destroy the table.

* Dropping Temporary Tables

 By default, all the temporary tables are deleted by MySQL when your database
connection gets terminated. Still if you want to delete them in between, then
you do so by issuing the DROP TABLE command.

6.MySQL – Handling Duplicates

- Generally, tables or result sets sometimes contain duplicate records. Most of the
times it is allowed but sometimes it is required to stop duplicate records. It is
required to identify duplicate records and remove them from the table.
*Preventing Duplicates from Occurring in a Table
You can use a PRIMARY KEY or a UNIQUE Index on a table with the
appropriate fields to stop duplicate records.

 Let us take an example – The following table contains no such index or primary
key, so it would allow duplicate records for first_name and last_name.

CREATE TABLE person_tbl


(
first_name CHAR(20),
last_name CHAR(20),
sex CHAR(10)
);

 To prevent multiple records with the same first and last name values from being
created in this table, add a PRIMARY KEY to its definition. When you do this,
it is also necessary to declare the indexed columns to be NOT NULL, because
a PRIMARY KEY does not allow NULL values:

CREATE TABLE person_tbl


(
first_name CHAR(20) NOT NULL,
last_name CHAR(20) NOT NULL,
sex CHAR(10),
PRIMARY KEY (last_name, first_name)
);

 Another way to enforce uniqueness is to add a UNIQUE index rather than a


PRIMARY KEY to a table.

IN above program use UNIQUE instead of PRIMARY KEY


*Counting and Identifying Duplicates

mysql> SELECT COUNT(*) as repetitions, last_name, first_name

-> FROM person_tbl

-> GROUP BY last_name, first_name

-> HAVING repetitions > 1;

*Eliminating Duplicates from a Query Result

You can use the DISTINCT command along with the SELECT statement to
find out unique records available in a table.

mysql> SELECT DISTINCT last_name, first_name


-> FROM person_tbl
-> ORDER BY last_name;

*An alternative to the DISTINCT command is to add a GROUP BY clause that


names the columns you are selecting. This has the effect of removing duplicates
and selecting only the unique combinations of values in the specified columns.

mysql> SELECT last_name, first_name


-> FROM person_tbl
-> GROUP BY (last_name, first_name);

7.MySQL – SQL Injection


*If you take user input through a webpage and insert it into a MySQL
database, there's a chance that you have left yourself wide open for a security
issue known as SQL Injection.
 usually occurs when you ask a user for input, like their name and instead of
a name they give you a MySQL statement

8. MySQL – Database Exports


*Exporting Data with the SELECT ... INTO OUTFILE Statement

* The syntax for this statement combines a regular SELECT command with
INTO OUTFILE filename(any file name) at the end.

mysql> SELECT * FROM tutorials_tbl


-> INTO OUTFILE '/tmp/tutorials.txt';

*You can change the output format using various options to indicate how
to quote and delimit columns and records. To export the tutorial_tbl table in
a CSV format with CRLF-terminated lines, use the following code.

mysql> SELECT * FROM passwd INTO OUTFILE '/tmp/tutorials.txt'


-> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
-> LINES TERMINATED BY '\r\n';

8. MySQL – Database Import


*There are two simple ways in MySQL to load data into the MySQL database
from a previously backed up file.
With : 1.LOAD DATA 2. Mysqlimport
(acts as a bulk data loader) (acts as a wrapper)

1. Importing Data with LOAD DATA

LOAD DATA statement that acts as a bulk data loader.


Here is an example statement that reads a file dump.txt from your current
directory and loads it into the table mytbl in the current database.

mysql> LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl

To specify a file format explicitly, use a FIELDS clause to describe the


characteristics of fields within a line, and a LINES clause to specify the line-
ending sequence.

mysql> LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl


-> FIELDS TERMINATED BY ':'
-> LINES TERMINATED BY '\r\n';

*The LOAD DATA command assumes the columns in the datafile have the same
order as the columns in the table.

*Suppose your table has columns a, b, and c, but successive columns in the datafile
correspond to columns b, c, and a.
You can load the file as shown in the following code block.

mysql> LOAD DATA LOCAL INFILE 'dump.txt'


-> INTO TABLE mytbl (b, c, a);

*Importing Data with mysqlimport

-acts as a wrapper around LOAD DATA, so that you can load the input files
directly from the command line.

To load data from the dump.txt into mytbl, use the following command at
the UNIX prompt.

$ mysqlimport -u root -p --local database_name dump.txt


password *****
The mysqlimport statement uses the --columns option to specify the column order.

$ mysqlimport -u root -p --local --columns=b,c,a \


database_name dump.txt
password *****

You might also like