Top Banner
Page 1 MY SQL INSTALLATION AND CONFIGERATION WITH QUERY BY , A.ANANDHA GANESH (SMK FOMRA INSTITUTE OF TECHNOLOGY)
32
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: My sql

Page 1

MY SQL INSTALLATION AND CONFIGERATION WITH QUERY

BY ,A.ANANDHA GANESH

(SMK FOMRA INSTITUTE OF TECHNOLOGY)

Page 2: My sql

Page 2

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.

Page 3: My sql

Page 3

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.

Page 4: My sql

Page 4

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).

Page 5: My sql

Page 5

You can check your configuration using the following command#netstat -tapOutput Looks like belowtcp 0 0 *:mysql *:* LISTEN 4997/mysqldMySQL 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

Page 6: My sql

Page 6

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 systemasks 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.

Page 7: My sql

Page 7

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)

Page 8: My sql

Page 8

•Configuring Your Application

After creating and testing the database, you need to inform your application of the database name, the IP address of the database client server, and the username and password of the application's special MySQL user that will be accessing the data.

Page 9: My sql

Page 9

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 themost 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.

Page 10: My sql

Page 10

– Table 34.1 Required PHP and Perl RPMs for MySQL Support

RPM RPM php-mysql MySQL database specific support for PHP perl-DBI Provides a generic Perl interface for interacting with relational databases perl-DBD-MySQL MySQL database specific support for Perl

Page 11: My sql

Page 11

BASIC MySQL COMMANDBASIC MySQL COMMAND

* CREATE TABLE syntax

* DROP TABLE syntax

* DELETE syntax

* SELECT syntax

* JOIN syntax

* INSERT syntax

* REPLACE syntax

* UPDATE syntax

Page 12: My sql

Page 12

BASIC QURIESBASIC QURIES

CREATE TABLECREATE TABLE

This command is used to create structure of the table.

SyntaxSyntax:

Create table <tablename>(list of col Definition1,....);

Example:Example:

Create table emp1 (Emp ID (number(3) primary key, Name(varchar(20), Age(number(), DOB(date));

Page 13: My sql

Page 13

DROP TABLEDROP TABLE

Syntax:Syntax:

Drop table [if exists] tbl_name

Explanation: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.

Page 14: My sql

Page 14

DELETEDELETESyntax:

Delete from <tablename>;

Example:

Delete from emp1;

Explanation:

This command is used to delete the rows and column.

Page 15: My sql

Page 15

SELECTSyntax:Syntax:

Select * from <tablename>;

Example:Example:

Select * from emp1;

Explanation:

This command is used to describe the structure of the table.

Page 16: My sql

Page 16

INSERT VALUEINSERT 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.

Page 17: My sql

Page 17

REPLACEREPLACE

Syntax:Syntax:

REPLACE [INTO] tbl_name [(col_name,...)] VALUES (expression,...)

Explanation: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.

Page 18: My sql

Page 18

UPDATEUPDATESyntax: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.

Page 19: My sql

Page 19

ADVANCED COMMANDSADVANCED COMMANDS

AS ALTER and ADD UNION JOIN TEMPORARY Table

TRUNCATE Table

Page 20: My sql

Page 20

ASASSyntax:

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.

Page 21: My sql

Page 21

ALTERING THE DATABASE ALTERING THE DATABASE STRUCTURE AND ADDING STRUCTURE AND ADDING

DATADATASyntax:

ALATER TABLE tablename

ADD clm_name type

Example:Example:

ALTER TABLE cds

-> ADD producerID INT(3);

Page 22: My sql

Page 22

UNION JOINSUNION JOINSSyntax:

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.

Page 23: My sql

Page 23

CREATING THE TEMPORARY TABLECREATING THE TEMPORARY TABLEDefinition:

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>

Page 24: My sql

Page 24

TRUNCATE TABLETRUNCATE TABLE

Syntax:

TRUNCATE TABLE <table_name>

Example:

TRUNCATE TABLE emp1;

Page 25: My sql

Page 25

TRUNCATE( )TRUNCATE( )

Syntax:

TRUNCATE(X,D)

Use:

This function is used to return the value of X truncated to D number of decimal places.

Page 26: My sql

Page 26

INSERT( )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.

Page 27: My sql

Page 27

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

Page 28: My sql

Page 28

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

Page 29: My sql

Page 29

The LIKE OperatorThe 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

Page 30: My sql

Page 30

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:Syntax:

mysqldump guestdb > guestdb.txt

Exporting a DatabaseExporting a Database

Page 31: My sql

Page 31

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

Page 32: My sql

Page 32

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 DatabaseImporting the Database