Mysql - Introduction: What Is A Database?
Mysql - Introduction: What Is A Database?
MySQL – Introduction
*What is a Database?
-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.
*RDBMS Terminology
spreadsheet.
n: One column (data element) contains data of one and the same kind,
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:
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
-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.
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
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.
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>
</head>
<body>
<?php
if( !$retval )
?>
</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:******
-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.
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.
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.
- 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.
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:
You can use the DISTINCT command along with the SELECT statement to
find out unique records available in a table.
* The syntax for this statement combines a regular SELECT command with
INTO OUTFILE filename(any file name) at the end.
*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.
*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.
-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.