+ All Categories

MYSQL

Date post: 06-May-2015
Category:
Upload: ankush-jain
View: 6,177 times
Download: 0 times
Share this document with a friend
Description:
Learn MYSQL installation with help of screenshots and also learn various commands in MYSQL
76
MYSQL
Transcript
Page 1: MYSQL

MYSQL

Page 2: MYSQL

a multithreaded, multi-user SQL database management system.

A Database Management System which is available for both Linux and Windows.

Popular Open Source Database.

a relational database management system (RDBMS) that has more than 6 million

installations.

What is MYSQL?

Page 3: MYSQL

INSTALLATOIN, CONFIGURATION&

COMMANDS

-BY H. ANKUSH. JAIN

Page 4: MYSQL

INSTALLATION&

CONFIGURATION

Page 5: MYSQL

download the latest MySQL install package from the MySQL site.

I recommend you use the Windows Essentials package.

Double click on the installation package to initilize the installation wizard

Page 6: MYSQL

Step 1: Choose the setup type

use the Custom option so that you can define your installation path.

Press the Next button

Page 7: MYSQL

Step 2

Use the Change botton to specify your installation path,use a path without spaces(recommended). Click the Next button

Page 8: MYSQL

Step 3

select the Skip Sign-Up option(Account not needed)

click the Next button

Page 9: MYSQL

Step 4

Check the “Configure the MySQL Server now”

click the Finish button to complete the installtion wizard and start the configuration wizard.

Page 10: MYSQL

Step 5: Configuring MySQL

You should now be presented with the MySQL Server Instance Configuration Wizard.

Click the Next button to continue.

Page 11: MYSQL

Step 6

Select the Detailed Configuration option

Click the Next button

Page 12: MYSQL

Step 7

choose the server type

click the Next button to continue

Page 13: MYSQL

Step 8

Select your database usage option

Click the Next button

Page 14: MYSQL

Step 9 If you selected to

enables the InnoDB database engine, then you will be prompted to set a path for use by the InnoDB datafile.

Leave this as the defaultu

Click the Next button

Page 15: MYSQL

Step 10

set the estimated conccurrent connection usage.

using the Decision Support (DSS)/OLAP option which sets the concurrent connection limit to 20 which is pretty safe.

Click next button

Page 16: MYSQL

Step 11 Check the Enable

TCP/IP Networking option

uncheck the Enable Strict Mode option even though it is on by default and recommended.

Click next

Page 17: MYSQL

Step 12

Select Standard Character Set as default

Click next button

Page 18: MYSQL

Step 13 Check the Install As

Windows Service and Launch MySQL Server Automatically & Include Bin Directory in Windows PATH options so that you can execute the MySQL tools from anywhere when using the command line. This can be handy when creating automated scripts for backups, etc.

click the Next button

Page 19: MYSQL

Step 14 Set a strong

password (atleast 6 characters)

DON'T CHECK the Enable root access for remote machines option & Create An Anonymous Account option.

Click next button

Page 20: MYSQL

Step 15

Confrim your settings.

Click the Execute button

Press Finish to complete the configuration wizard and exit.

Page 21: MYSQL

COMMANDS

Page 22: MYSQL

Login to MySQL monitor

..\mysql\bin\mysql -u[username] -p[password]

Example:

..\mysql\bin\mysql -uroot -pmysecret

Page 23: MYSQL

Create a database on the sql server.

SYNTAX:

CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name

[create_specification] ...

create_specification:

[DEFAULT] CHARACTER SET [=] charset_name

| [DEFAULT] COLLATE [=] collation_name

u-1@srv-1 mysqlart $ mysql -u rootWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 5 to server version: 4.0.14-logType 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> create database sysops;Query OK, 1 row affected (0.00 sec)mysql> quitByeu-1@srv-1 mysqlart $

Example:

Page 24: MYSQL

List all databases on the sql server.

SYNTAX:

mysql> show databases;

mysql> SHOW DATABASES;

+----------+

| Database |

+----------+

| info     |

| java2s   |

| mysql    |

| t        |

| test     |

| ttt      |

+----------+

6 rows in set (0.00 sec)

Page 25: MYSQL

Switch to a database.

mysql> use [db name];

Page 26: MYSQL

To see all the tables in the db.

mysql> show tables;

Page 27: MYSQL

CREATE TABLESYNTAX: CREATE TABLE [table_name] (

[column_name1] INT AUTO_INCREMENT,

[column_name2] VARCHAR(30) NOT NULL,

[column_name3] ENUM('guest', 'customer', 'admin')NULL,

[column_name4] DATE NULL,

[column_name5] VARCHAR(30) NOT NULL,

[column_name6] DATETIME NOT NULL,

[column_name7] CHAR(1) NULL,

[column_name8] BLOB NULL,

[column_name9] TEXT NOT NULL,

UNIQUE(username),

PRIMARY KEY (column_name1)

);

Example:

CREATE TABLE user (

userid INT AUTO_INCREMENT,

username VARCHAR(30) NOT NULL,

group_type ENUM('guest', 'customer', 'admin') NULL,

date_of_birth DATE NULL,

password VARCHAR(30) NOT NULL,

registration_date DATETIME NOT NULL,

account_disable CHAR(1) NULL,

image BLOB NULL,

comment TEXT NOT NULL,

UNIQUE(username),

PRIMARY KEY (userid)

);

Page 28: MYSQL

INSERT STATEMENTS

Syntax:

INSERT INTO table_name ( `col_A`, `col_B`, `col_C`) VALUES ( `col_A_data`, `col_B_data`, `col_C_data`) ;

Example:

INSERT INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `Abbey Road`);

Page 29: MYSQL

REPLACE STATEMENTS

Syntax:

REPLACE INTO table_name ( `col_A`, `col_B`) VALUES ( `col A data`, `col B data`) ;

Example:

REPLACE INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `abbey road`);

Page 30: MYSQL

UPDATE STATEMENTS Syntax:

UPDATE table_name SET col_B='new_data' WHERE col_A='reference_data' ;

Example:

UPDATE music SET title='Come Together' WHERE id=1;

Page 31: MYSQL

Add a new column "male" in table user.

Syntax:

ALTER TABLE [table_name]

ADD COLUMN [column_name]

CHAR(1) NOT NULL;

Example: ALTER TABLE

user ADD COLUMN

male CHAR(1) NOT

NULL;

Page 32: MYSQL

Change column name "male" into "gender" in table user and change the type to VARCHAR(3) and allow NULL values.

Syntax:

ALTER TABLE [table_name]

CHANGE [old_column] [new_column]

VARCHAR(3) NULL;

Example:

ALTER TABLE user

CHANGE male gender

VARCHAR (3) NULL;

Page 33: MYSQL

Change the size of column "gender" from 3 to 6 in table user.

Syntax:

ALTER TABLE [table_name]

MODIFY [column_name] VARCHAR(6);

Example:

ALTER TABLE user

MODIFY gender VARCHAR(6);

Page 34: MYSQL

SELECT STATEMENTS

Syntax:

SELECT * FROM table_name WHERE 1 ;

Example:

SELECT * FROM music WHERE 1;

Page 35: MYSQL

DELETE STATEMENTS

Syntax:

DELETE FROM table_name WHERE column_name='search_data';

Example:

DELETE FROM music WHERE artist='the beatles';

Page 36: MYSQL

Show field formats of the selected table.

Syntax:

DESCRIBE [table_name];

Example:

DESCRIBE mos_menu;

Page 37: MYSQL

To see database's field formats.

mysql> describe [table name];

Page 38: MYSQL

To delete a db.

mysql> drop database [database name];

Example:

DROP DATABASE demodb;

Page 39: MYSQL

To delete a table.

mysql> drop table [table name];

Example:

DROP TABLE user;

Page 40: MYSQL

Show all data in a table.

mysql> SELECT * FROM [table name];

Example:

SELECT *

FROM mos_menu;

Page 41: MYSQL

Show all records from mos_menu table containing name "Home".

SELECT *

FROM [table_name]

WHERE [field_name]=[value];

Example:

SELECT *

FROM mos_menu

WHERE name = "Home";

Page 42: MYSQL

Returns the columns and column information pertaining to the

designated table.

mysql> show columns from [table name];

Page 43: MYSQL

Show certain selected rows with the value "whatever".

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

Page 44: MYSQL

Show all records containing the name "Bob" AND the phone

number '3444444'.

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number

= '3444444';

Page 45: MYSQL

Show all records not containing the name "Bob" AND the phone number '3444444' order by the

phone_number field.

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number

= '3444444' order by phone_number;

Page 46: MYSQL

Show all records starting with the letters 'bob' AND the phone

number '3444444'.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND

phone_number = '3444444';

Page 47: MYSQL

Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through

5.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND

phone_number = '3444444' limit 1,5;

Page 48: MYSQL

Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any

record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Page 49: MYSQL

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

Page 50: MYSQL

Show selected records sorted in an ascending (asc) or descending

(desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Page 51: MYSQL

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

Page 52: MYSQL

Sum column.

mysql> SELECT SUM(*) FROM [table name];

Page 53: MYSQL

Join tables on common columns.

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup

left join person on lookup.personid=person.personid=statement to join birthday in person table with primary

illustration id;

Page 54: MYSQL

Creating a new user. Login as root. Switch to the MySQL db. Make the

user. Update privs.

# mysql -u root -pmysql> use mysql;

mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));

mysql> flush privileges;

Page 55: MYSQL

Change a users password from unix shell.

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-

password'

Page 56: MYSQL

Change a users password from MySQL prompt. Login as root. Set

the password. Update privs.

# mysql -u root -pmysql> SET PASSWORD FOR

'user'@'hostname' = PASSWORD('passwordhere');

mysql> flush privileges;

Page 57: MYSQL

Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit

MySQL and restart MySQL server.

# /etc/init.d/mysql stop# mysqld_safe --skip-grant-tables &

# mysql -u rootmysql> use mysql;

mysql> update user set password=PASSWORD("newrootpassword") where

User='root';mysql> flush privileges;

mysql> quit# /etc/init.d/mysql stop# /etc/init.d/mysql start

Page 58: MYSQL

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Page 59: MYSQL

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

Page 60: MYSQL

Allow the user "bob" to connect to the server from localhost using the password

"passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -pmysql> use mysql;

mysql> grant usage on *.* to bob@localhost identified by 'passwd';

mysql> flush privileges;

Page 61: MYSQL

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant

privs. Update privs.

# mysql -u root -pmysql> use mysql;

mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Cre

ate_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');

mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;

mysql> flush privileges;

Page 62: MYSQL

To update info already in a table.

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv

= 'Y' where [field name] = 'user';

Page 63: MYSQL

Delete a row(s) from a table.

mysql> DELETE from [table name] where [field name] = 'whatever';

Page 64: MYSQL

Update database permissions/privilages.

mysql> flush privileges;

Page 65: MYSQL

Delete a column.

mysql> alter table [table name] drop column [column name];

Page 66: MYSQL

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Page 67: MYSQL

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar

(50);

Page 68: MYSQL

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Page 69: MYSQL

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);

Page 70: MYSQL

Delete unique from table.

mysql> alter table [table name] drop index [colmn name];

Page 71: MYSQL

Load a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE

[table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'

(field1,field2,field3);

Page 72: MYSQL

Dump all databases for backup. Backup file is sql commands to

recreate all db's.

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

Page 73: MYSQL

Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename

>/tmp/databasename.sql

Page 74: MYSQL

Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename >

/tmp/databasename.tablename.sql

Page 75: MYSQL

Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename <

/tmp/databasename.sql

Page 76: MYSQL

Thank You


Recommended