MY SQL INSTALLATION AND CONFIGERATION WITH QUERY BY , R.RAJAVEL (MAGNA COLLEGE OF ENGINEERING)
INTRODUCTION: MySQL, the most popular Open Source SQL database management system, is developed, distributed, and supported by MySQL AB. MySQL AB is a commercial company, founded by the MySQL developers. It is a second generation Open Source company that unites Open Source values and methodology with a successful business model.
MySQL is a fast, stable and true multi-user, multi-threaded SQL database server. SQL (Structured Query Language) is the most popular database query language in the world. The main goals of MySQL are speed, robustness and ease of use.
Installing MySQL on Linux It's simple to install MySQL on Linux using the RPM file. 1. Become the superuser if you are working in your account. (Type "su" and the prompt and give the root password). 2. Change to the directory that has the RPM download. 3. Type the following command at the prompt: rpm -ivh "mysql_file_name.rpm" Similarly you can also install the MySQL client and MySQL development RPMs if you've downloaded them. Alternatively, you can install the RPMs through GnoRPM (found under System).
You can check your configuration using the following command #netstat -tap Output Looks like below tcp 0 0 *:mysql *:* LISTEN 4997/mysqld MySQL comes with no root password as default. This is a huge security risk. You’ll need to set one. So that the local computer gets root access as well, you’ll need to set a password for that too. The local-machine-name is the name of the KonaLink1KonaLink1computer you’re working on. For more information see here #mysqladmin -u root password your-new-password #mysqladmin -h root@local-machine-name -u root -p password your-new-password #/etc/init.d/mysql restart
4. Now we'll set a password for the root user. Issue the following at the prompt. mysqladmin -u root password mysqldata where mysqldata is the password for the root. (Change this to anything you like). 5. It is now time to test the programs. Typing the following at the prompt starts the mysql client program. mysql -u root -p The system asks for the the password. Type the root password (mysqldata). If you don't get the prompt for password, it might be because MySQL Server is not running. To start the server, change to /etc/rc.d/init.d/ directory and issue the command ./mysql start (or mysql start depending on the value of the PATH variable on your system). Now invoke mysql client program.
6. Once MySQL client is running, you should get the mysql> prompt. Type the following at this prompt: show databases; 7. You should now get a display similar to: +----------------+ | Database | +----------------+ | mysql | | test | +----------------+ 2 rows in set (0.00 sec)
<ul><li>Configuring Your Application </li></ul><ul><li>After creating and testing the database, </li></ul><ul><li>you need to inform your application </li></ul><ul><li>of the database name, </li></ul><ul><li>the IP address of the database client server, </li></ul><ul><li>and the username and password of </li></ul><ul><li>the application's special MySQL user that will be accessing the data. </li></ul>
Frequently this registration process is done by the editing of a special application-specific configuration file either via a Web GUI or from the command line. Read your application's installation guide for details. You should always remember that MySQL is just a database that your application will use to store information. The application may be written in a variety of languages with Perl and PHP being the most popular. The base PHP and Perl RPMs are installed with Fedora Linux by default, but the packages used by these languages to talk to MySQL are not. You should also ensure that you install the RPMs listed in Table 34.1 on your MySQL clients to ensure compatibility. Use the yum utility discussed in Chapter 6, " Installing Linux Software ", if you are uncertain of the prerequisite RPMs needed.
<ul><ul><li>Table 34.1 Required PHP and Perl RPMs for MySQL Support </li></ul></ul><ul><li>RPM RPM </li></ul><ul><li>php-mysql MySQL database specific support for PHP </li></ul><ul><li>perl-DBI Provides a generic Perl interface for interacting with </li></ul><ul><li>relational databases </li></ul><ul><li>perl-DBD-MySQL MySQL database specific support for Perl </li></ul>
BASIC QURIES CREATE TABLE This command is used to create structure of the table. Syntax : Create table <tablename>(list of col Definition1,....); Example: Create table emp1 (Emp ID (number(3) primary key, Name(varchar(20), Age(number(), DOB(date));
DROP TABLE Syntax: Drop table [if exists] tbl_name Explanation: DROP TABLE removes one or more tables. All table data and the table definition are removed.You can use the keywords IF EXISTS to prevent an error from occurring for tables that don't exist.
DELETE Syntax: Delete from <tablename>; Example: Delete from emp1; Explanation: This command is used to delete the rows and column.
SELECT Syntax: Select * from <tablename>; Example: Select * from emp1; Explanation: This command is used to describe the structure of the table.
INSERT VALUE Syntax: Insert into <tablename> values (list of values); Example: Insert into emp1 values (11, Anu, 20,30-aug-1989); Explanation: This command is used to insert values into the structure of the table.
REPLACE Syntax: REPLACE [INTO] tbl_name [(col_name,...)] VALUES (expression,...) Explanation: REPLACE works exactly like INSERT, except that if an old record in the table has the same value as a new record on a unique index, the old record is deleted before the new record is inserted.
UPDATE Syntax: UPDATE [table] SET [column]=[value] WHERE [criteria] UPDATE Used_Vehicles SET mileage=66000 WHERE vehicle_id=1; UPDATE [table] SET [column]=[value] WHERE [criteria] Example: UPDATE Used_Vehicles SET mileage=66000 WHERE vehicle_id=1; Explanation: UPDATE updates columns in existing table rows with new values. The SET clause indicates which columns to modify and the values they should be given. The WHERE clause, if given, specifies which rows should be updated. Otherwise all rows are updated.
AS Syntax: SELECT <columns>FROM <existing_table_name>AS <new_table_name> Example: SELECT t1.name -> FROM artists -> AS t1; Explanation: It is used to create a shorthand reference to elements with long names to make the SQL statements shorter and reduce the chance of typos in the longer names.
ALTERING THE DATABASE STRUCTURE AND ADDING DATA Syntax: ALATER TABLE tablename ADD clm_name type Example: ALTER TABLE cds -> ADD producerID INT(3);
UNION JOINS Syntax: Select <fields>from <table> where <condition> union SELECT <fields> FROM <table>WHERE <condition> Example: SELECT artist FROM artists WHERE (artists.name LIKE 'P%') UNION SELECT artists.name FROM artists WHERE (artists.name LIKE 'G%'); Explanation: Union Joins allow the results of two queries to be combined into one outputted result set. This is done by having the 2 (or more) queries glued together by the UNION operator.
CREATING THE TEMPORARY TABLE Definition: The syntax for creating temporary tables is almost identical that used for creating a normal table. Except that there is an extra TEMPORARY clause. Syntax: CREATE TEMPORARY TABLE <table> (field definition) CREATE TEMPORARY TABLE <newtable>SELECT * FROM <oldtable>
TRUNCATE( ) Syntax: TRUNCATE(X,D) Use: This function is used to return the value of X truncated to D number of decimal places.
INSERT( ) Syntax: INSERT(str,pos,len,newstr) Use: Returns the string str, with the substring beginning at position pos and len characters long replaced by the string newstr.
SQL Constraints: Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement). We will focus on the following constraints: NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT
The TOP Clause 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 examble: SELECT TOP 2 * FROM Persons P_Id LastName FirstName Address City 1 HansenOla Timoteivn 10 Sandnes 2 Svendson ToveBorgvn 23 Sandnes
The LIKE Operator The LIKE operator is used to search for a specified pattern in a column. SQL LIKE Syntax SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern EXAMPLE: SELECT * FROM Persons WHERE City LIKE 's%' P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10
To export a database, use the mysqldump utility normally located in your mysql/bin directory . For example, to export all the tables and data for a database named guestdb. Syntax: mysqldump guestdb > guestdb.txt Exporting a Database
This will create a text file containing all the commands necessary to recreate all the tables and data found in guestdb. However, what if I want to export only one table? To do this the command is modified as follows assuming guestTbl is the table to be exported. Syntax: mysqldump guestdb guestTbl > guestdb.txt
With the data in a text file, its time to import the data back into MySQL. This can be done by passing the commands contained in the text file into the MySQL client. For example: mysql -p --user=username < guestdb.txt This passes all the commands in the file into the mysql client just like you were typing them in. Importing the Database