my sql with querys

32
Page 1 MY SQL INSTALLATION AND CONFIGERATION WITH QUERY BY , NIRMAL FELIX.K

Upload: nirmal-felix

Post on 17-Dec-2014

1.259 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: My sql with querys

Page 1

MY SQL INSTALLATION AND CONFIGERATION WITH QUERY

BY ,NIRMAL FELIX.K

Page 2: My sql with querys

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 with querys

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 with querys

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 with querys

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 with querys

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 with querys

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 with querys

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 with querys

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 with querys

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 with querys

Page 11

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 12: My sql with querys

Page 12

BASIC MySQL COMMANDBASIC MySQL COMMAND

* CREATE TABLE syntax

* DROP TABLE syntax

* DELETE syntax

* SELECT syntax

* JOIN syntax

* INSERT syntax

* REPLACE syntax

* UPDATE syntax

Page 13: My sql with querys

Page 13

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 14: My sql with querys

Page 14

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 15: My sql with querys

Page 15

DELETEDELETESyntax:

Delete from <tablename>;

Example:

Delete from emp1;

Explanation:

This command is used to delete the rows and column.

Page 16: My sql with querys

Page 16

SELECTSyntax:Syntax:

Select * from <tablename>;

Example:Example:

Select * from emp1;

Explanation:

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

Page 17: My sql with querys

Page 17

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 18: My sql with querys

Page 18

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 19: My sql with querys

Page 19

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 20: My sql with querys

Page 20

ADVANCED COMMANDSADVANCED COMMANDS

AS ALTER and ADD UNION JOIN TEMPORARY Table

TRUNCATE Table

Page 21: My sql with querys

Page 21

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 22: My sql with querys

Page 22

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 23: My sql with querys

Page 23

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 24: My sql with querys

Page 24

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 25: My sql with querys

Page 25

TRUNCATE TABLETRUNCATE TABLE

Syntax:

TRUNCATE TABLE <table_name>

Example:

TRUNCATE TABLE emp1;

Page 26: My sql with querys

Page 26

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 27: My sql with querys

Page 27

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 28: My sql with querys

Page 28

The UPDATE Statement

The UPDATE statement is used to update existing records in a table.SQL UPDATE SyntaxUPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

Page 29: My sql with querys

Page 29

The DELETE Statement

The DELETE statement is used to delete rows in a table.SQL DELETE SyntaxDELETE FROM table_nameWHERE some_column=some_value

Page 30: My sql with querys

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 with querys

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 with querys

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