Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

MySQL Session 14

This document provides an overview of creating, querying, and managing databases using MySQL, focusing on exporting and importing data, as well as managing user accounts. It details various SQL commands and utilities for exporting data to files, importing data from text files, and creating user accounts. The document includes examples and syntax for executing these operations effectively.

Uploaded by

osukashalom47
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MySQL Session 14

This document provides an overview of creating, querying, and managing databases using MySQL, focusing on exporting and importing data, as well as managing user accounts. It details various SQL commands and utilities for exporting data to files, importing data from text files, and creating user accounts. The document includes examples and syntax for executing these operations effectively.

Uploaded by

osukashalom47
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 51

Creating, Querying, and Managing Databases Using

MySQL
Objectives

In this session, you will learn to:


Export Data
Import Data
Manage user accounts

Ver. 1.0 Slide 1 of 51


Creating, Querying, and Managing Databases Using
MySQL
Exporting Data

MySQL provides the facility to the user to export data into


files outside the database by using various MySQL utilities
and statements.
These SQL statements and utilities allow large amount of
data to be transferred into and outside the database.

Ver. 1.0 Slide 2 of 51


Creating, Querying, and Managing Databases Using
MySQL
Exporting Data to an Out File

You may want to export the data into a format that can be
further imported to MS Excel or MS Access format.
The INTO OUTFILE variant of the SELECT statement
allows you to do the same.
It can be used to take backup of the complete database or
specific sections of the database into an out file.

Ver. 1.0 Slide 3 of 51


Creating, Querying, and Managing Databases Using
MySQL
Exporting Data to an Out File (Contd.)

Syntax:
Creating Compound Statement
SELECT [select options go here] INTO OUTFILE
‘<filename>’ [<export option> [<export option>]]
<export option>::=
{FIELDS
[TERMINATED BY ‘<value>’]
[[OPTIONALLY] ENCLOSED BY ‘<value>’]
[ESCAPED BY ‘<value>’]}

Is an optional
Refers
Refers
Refers
Refers to
totothe
to the
the clause
thename ofthat
character
character
character the specifies
fileis
that
used
used orused
to
to how
variablethe
to
before
escape
separateany text
which
and file is
the
after
special
the values formatted.
result
inthe
charactersof
the text
the select
in statement
occurrence
present
file. everyisfield
theoffile. exported.
in the text file.

Ver. 1.0 Slide 4 of 51


Creating, Querying, and Managing Databases Using
MySQL
Exporting Data to an Out File (Contd.)

Creating Compound Statement


| {LINES
[STARTING BY ‘<value>’]
[TERMINATED BY ‘<value>’]}

Is an optional
Refers clause that
to the character indicating
specifies
that the
thethe
identifies end
delimiters
of a line.
beginningused
The
of afor
default
line in
identifying
value
the is file.
text \n,thewhich
beginning
represents
and end
a line
of break.
a line in the text file.

Ver. 1.0 Slide 5 of 51


Creating, Querying, and Managing Databases Using
MySQL
Exporting Data to an Out File (Contd.)

For example:
Creating ond Statement
SELECT * FROM film
INTO OUTFILE 'd:film_details.txt';
For example:
Creatingompoatement
SELECT * FROM film
INTO OUTFILE 'd:film1_details.txt' FIELDS
TERMINATED BY ',' ENCLOSED BY '"';
For example:
Creating omament
SELECT * FROM film
INTO OUTFILE 'd:film2_details.txt' LINES STARTING
BY '*' TERMINATED BY '$$';
Exports all the data from the film table to an out file named as
film_details.txt. using
film2_details.txt
film1_details.txt using linedelimiters.
field delimiters.

Ver. 1.0 Slide 6 of 51


Creating, Querying, and Managing Databases Using
MySQL
Exporting Data to a Dump File

Sometimes, you need to export a single record or a column


containing binary data such as an MS Word file or an image
file.
MySQL allows you to achieve this by using the INTO
DUMPFILE option in the SELECT statement.
Syntax:
SELECT [select options go here] INTO DUMPFILE
‘<filename>’
ForIsexample:
the name of the dump file.
SELECT Picture FROM Sakila.Staff WHERE Staff_ID=1
INTO DUMPFILE 'd:pic.bin';
The value in the picture field of the Staff table having
Staff_ID as 1 is exported into the dump file named pic.bin.

Ver. 1.0 Slide 7 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Exporting Data

Problem Statement:
You are a database administrator at Rox Luxurious Hotels Ltd. You
want to export all the details of the hotels to a text file. This will
enable you to share the hotel details with users who do not have
MySQL installed on their machines. The details of the hotels are
stored in the hotels table. For readability, you want that the records
of the hotels table, in the text file, should be separated by commas
and enclosed within double quotes. In addition, each row of the
record should begin with a dollar sign and end with double
asterisks.

Ver. 1.0 Slide 8 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Exporting Data(Contd.)

Solution:
To solve the preceding problem, you need to perform the following
tasks:
Formulate the statement to export the table data.
Execute the statement to create the text file.

Ver. 1.0 Slide 9 of 51


Creating, Querying, and Managing Databases Using
MySQL
Importing Data

MySQL provides the following utilities to import data from


various file formats such as text files:
Mysql Utility
Mysqlimport Utility

Ver. 1.0 Slide 10 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using MySQL Utility

MySQL utility provides the following commands to import


data into the MySQL database:
The LOAD DATA statement
The source command
The mysql command

Ver. 1.0 Slide 11 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using MySQL Utility (Contd.)

The LOAD DATA statement:


Allows you to directly import values from a delimited text file.
Syntax:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE
‘<filename>’
[REPLACE | IGNORE]
INTO TABLE <table name>
[<import option> [<import option>]]
[IGNORE <number> LINES]
[(<column name> [{, <column name>}...])]

Allows
Replaces
Ignores
Inserts
Specifies
Delays
Is used
the the
the retrieval
the
some
when
file new
that
which rows
rows
name
execution
the of
existingof data
rows
ofwithout
column
of the
the
database
has the the from
with
out
names
table
data is the
file
where
tostored
LOAD be clients
new
inwhile
replacingthe
DATA
loaded.table
onthe the and
rows.
importing
the theto
existing
where
data is
statement
client insertion
theadded.
rows.
the
be data
until
machine. of
table.
is
there
data
to
arebe inloaded
no that table
clientsand concurrently.
should
to read from bethe
separated
table. by commas.

Ver. 1.0 Slide 12 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using MySQL Utility (Contd.)

<import option>::=
{FIELDS | COLUMNS}
[TERMINATED BY ‘<value>’]
[[OPTIONALLY] ENCLOSED BY ‘<value>’]
[ESCAPED BY ‘<value>’]}
| {LINES
[STARTING BY ‘<value>’]
[TERMINATED BY ‘<value>’]}

Ver. 1.0 Slide 13 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using MySQL Utility (Contd.)

For example:
LOAD DATA INFILE "d:/Austria_cities.txt" INTO TABLE
Austria_Cities;

Import the data from the Austria_cities.txt text file to the


Austria_Cities table.

Ver. 1.0 Slide 14 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using MySQL Utility (Contd.)

The source command:


The source command can be used to import data into
MySQL tables by executing SQL statements stored in a sql file
from the mysql command prompt.
To use a source command, you need to determine the path
and the filename of the file that contains the MySQL
statements and commands.
Syntax:
mysql>source <file_name>
Specifies the path and filename of the sql file that needs to be
executed.

Ver. 1.0 Slide 15 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using MySQL Utility (Contd.)

The mysql command:


Can be executed from the operating system command prompt
to execute SQL statements and mysql commands from within
a text file without invoking the command line client.
The mysql command is the same command that is used to
invoke the mysql client utility.

Ver. 1.0 Slide 16 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using MySQL Utility (Contd.)

For example:
insert into country values (110,'New Aqua','2007-03-10
05:34:00');
insert into country values (111, 'New Carib','2007-04-
11 06:44:00');
insert into country values (112, 'New Zeal', '2007-03-
07 03:54:00');
insert into country values (113, 'New Dreams', '2007-
05-08 04:33:00');
insert into country values (114, 'New Aliver', '2007-
07-09 02:22:00');

The following commands are typed in a text file and saved using
the name emdata.sql.

Ver. 1.0 Slide 17 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using MySQL Utility (Contd.)

Now issue the following statement to import the given data into
the country table:
mysql –h localhost –u root –p sakila <
“<path>:emdata.sql”

Should
The data
beofreplaced
the emdata.sql
by the path
file isthat
imported
is usedintothe
save the file.
country
table.

Ver. 1.0 Slide 18 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using mysqlimport Utility

MySQL provides a utility called mysqlimport that allows the


user to import the delimited data present in a text file into
the MySQL database.
The mysqlimport utility uses the same import options as the
LOAD DATA statement.
Syntax:
Shell>mysqlimport [options] <database_name> file1
[file 2…]

IsRefers
the name
replaced
Refers to of import
by
to the
the the path
the textoptions.
database file from which the
ofname.
mysqlimport data needs to be
utility.
imported.

Ver. 1.0 Slide 19 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using mysqlimport Utility (Contd.)

The following table displays the options that can be used


with the mysqlimport command.

Command Description Example

--columns=column_list Specifies the columns into --columns=film_id,title


which the data needs to be
imported.

--compress Compresses the --compress


information that is
exchanged between the
client and the server.

Ver. 1.0 Slide 20 of 51


Creating, Querying, and Managing Databases Using
MySQL
Using mysqlimport Utility (Contd.)

For example:
mysqlimport production_management_system
d:order_status.txt;

The data in the order_status.txt file is imported to the


table order_status in the database
production_management_system.

Ver. 1.0 Slide 21 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Importing Data

Problem Statement:
You are a Database Administrator at Rox Luxurious Hotels Ltd.
The management has recently acquired a small chain of hotels in
the country suburbs. However, the acquired company was
maintaining the information regarding its hotels and other details in
delimited text files. The management wants to import this
information into its centralized database. How will you achieve
this?
Prerequisite: To perform this activity, you need the hotel.txt text
file. Ask your faculty to provide the same and save it in the D: drive
of the system where the MySQL server is installed.

Ver. 1.0 Slide 22 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Importing Data (Contd.)

Solution:
To solve the preceding problem, you need to perform the following
tasks:
Create a table.
Create a statement to import data into the created table.
Execute the statement and verify the output.

Ver. 1.0 Slide 23 of 51


Creating, Querying, and Managing Databases Using
MySQL
Managing User Accounts

MySQL allows the authorized users to connect to the


MySQL server to allow them access or manipulate the
secured data.
For accessing and manipulating e secured data, a user
needs to have a valid user account.
The Database Administrator is responsible for creating such
user accounts.
MySQL provides various SQL statements that can be used
for creating and managing the user accounts.

Ver. 1.0 Slide 24 of 51


Creating, Querying, and Managing Databases Using
MySQL
Creating a User Account

A user account:
Is defined in terms of a user name and a host.
May also be associated with a password.
A user name can be used to uniquely identify a user account.
MySQL provides its users the flexibility to create user accounts
with or without a password.

Ver. 1.0 Slide 25 of 51


Creating, Querying, and Managing Databases Using
MySQL
Creating a User Account (Contd.)

When MySQL is installed on a system, some of the user


accounts are automatically created.
The user table contains records only for these accounts until
any other user account is created by the Database
Administrator, explicitly.
These user accounts are known as the initial set of accounts.
The initial set of accounts can be categorized as:
Superuser accounts This account is assigned all the privileges
by
Aredefault, and neither
the userwith
whouser
logsnames
in to the
Anonymous accounts associated nor
MySQL server by using this account is
with passwords, by default. The users who
known
connectastoathe
rootMySQL
user. server by using an
anonymous account are known as
anonymous users.

Ver. 1.0 Slide 26 of 51


Creating, Querying, and Managing Databases Using
MySQL
Creating a User Account (Contd.)

The CREATE USER statement:


Is used for creating user accounts.
Syntax:
CREATE USER <account_name> IDENTIFIED BY [PASSWORD]
‘password’ [,<account_name> IDENTIFIED BY [PASSWORD]
’password’,[…]]

Is an
theoptional
placeholder
Specifies keyword
for the
the password that
account
thatcan
is tobename.
beused before
given to thespecifying
new user a
password for the new user account.
account.

Ver. 1.0 Slide 27 of 51


Creating, Querying, and Managing Databases Using
MySQL
Creating a User Account (Contd.)

For example:
CREATE USER 'john'@'localhost'
IDENTIFIED BY 'password_john';
Creates a new user account for John.

For example:
CREATE USER 'joe'
IDENTIFIED BY 'password_joe';
Creates a new user account for Joe with joe as the user name
and password_joe as the password. Allows the users to
connect to the MySQL server through any host.

For example:
CREATE USER 'almin'@'localhost';
Creates a user account for Almin with almin as the user name
and localhost as the host value.

Ver. 1.0 Slide 28 of 51


Creating, Querying, and Managing Databases Using
MySQL
Creating a User Account (Contd.)

MySQL also allows the creation of user accounts by


modifying the grant tables directly.
To accomplish this, you can issue the INSERT statement on
the user table of the mysql database.
The FLUSH PRIVILEGES statement:
Should be executed to inform the MySQL server to reload all
the grant tables in memory.
Is not executed immediately after executing the INSERT
statement, the changes will not be reflected to the grant tables
until the MySQL server is restarted.
While creating user account using the INSERT statement,
you need to use either the PASSWORD() function or the
OLD_PASSWORD()function.

Ver. 1.0 Slide 29 of 51


Creating, Querying, and Managing Databases Using
MySQL
Creating a User Account (Contd.)

For example:
INSERT INTO mysql.user
VALUES('%','harry',PASSWORD('password@123'),'Y','Y'
,'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y
','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','
Y','Y','','','','',0,0,0,0,'','');

Creates a new user account for Harry with harry as the user
name and % as the host value. In addition, all the columns of the
user table are assigned some values.
Also uses the PASSWORD function to specify password@123 as
password.

Ver. 1.0 Slide 30 of 51


Creating, Querying, and Managing Databases Using
MySQL
Modifying a User Account

The Database Administrator is responsible not only for


creating the user accounts but also for modifying the same.
As a Database Administrator, you may need to perform the
following operations on a user account:
Rename a user account
Change the password

Ver. 1.0 Slide 31 of 51


Creating, Querying, and Managing Databases Using
MySQL
Modifying a User Account (Contd.)

The RENAME USER statement:


Is used to rename a user account.
Syntax:
RENAME USER old_account_name TO new_account_name,
[ old_account_name TO new_account_name,..]
Is the place holder for the new account
current name
account to be
name given to the
associated with
user account
the user to be
account torenamed.
be renamed.

For example:
RENAME USER 'joe' TO 'joe_louise'@'localhost';

It changes the user name for Joe’s user account from joe to
joe_louise and the host value to localhost.

Ver. 1.0 Slide 32 of 51


Creating, Querying, and Managing Databases Using
MySQL
Modifying a User Account (Contd.)

Changing the password:


MySQL allows you to modify the password associated with an
user account or assign a new password to an account that is
not assigned any password.
MySQL provides the following ways:
Using the SET PASSWORD statement
Using the UPDATE statement

Ver. 1.0 Slide 33 of 51


Creating, Querying, and Managing Databases Using
MySQL
Modifying a User Account (Contd.)

The SET PASSWORD statement:


Allows you to set a new password for an existing user account or
change the current password associated with a user account.
Syntax:
SET PASSWORD [FOR account_name]=
{
PASSWORD('PASSWORD')| OLD_PASSWORD('PASSWORD')
|'encrypted_password'
}

Specifies
Is the an encrypted
placeholder
an optional clause binary
for that
the string
account
can be value.
name
used if for
youthe user
want to whose
specify the
password is to for
account name be whom
set or changed. If this
the password is clause is not
to be set specified
or changed.
in the SET PASSWORD statement, the MySQL server sets or
changes the password for the current user.

Ver. 1.0 Slide 34 of 51


Creating, Querying, and Managing Databases Using
MySQL
Modifying a User Account (Contd.)

For example:
SET PASSWORD FOR 'almin'@'localhost' =
PASSWORD('password_almin');

It assigns password_almin as password to Alimn’s user


account.

Ver. 1.0 Slide 35 of 51


Creating, Querying, and Managing Databases Using
MySQL
Modifying a User Account (Contd.)

The UPDATE statement:


Is used only to change the password associated with a user
account.
Is issued on the user table of the mysql database and cannot be
used to assign a password to the user account that is not
associated with any password.

Ver. 1.0 Slide 36 of 51


Creating, Querying, and Managing Databases Using
MySQL
Modifying a User Account (Contd.)

For example:
UPDATE mysql.user
SET Password=PASSWORD('newpass@123')
Where User='John' and Host='localhost';
The SET clause is used to modify the value in the Password
column of the user table for the record that corresponds to
John’s user account.

Ver. 1.0 Slide 37 of 51


Creating, Querying, and Managing Databases Using
MySQL
Deleting a User Account

To delete a user account, MySQL provides the following


ways:
Using the DROP USER statement
Using the DELETE statement

Ver. 1.0 Slide 38 of 51


Creating, Querying, and Managing Databases Using
MySQL
Deleting a User Account (Contd.)

The DROP USER statement:


Can be used for deleting user accounts.
Syntax:
DROP USER account_name [, account_name,…]
For example:
Is the placeholder for the account name associated with the user
account to be removed.
DROP USER 'john'@'localhost';
Deletes the account for John.

Ver. 1.0 Slide 39 of 51


Creating, Querying, and Managing Databases Using
MySQL
Deleting a User Account (Contd.)

The DELETE statement:


Can be issued on the user table of the mysql database to delete
a user account.
Its execution should be followed by the execution of the FLUSH
PRIVILIGES statement.
For example:
DELETE FROM user
WHERE user='harry';

Deletes the user account for harry.

Ver. 1.0 Slide 40 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Creating User Accounts

Problem Statement:
Consider the scenario of Rox Luxurious Hotels Ltd. The hotel
reservation system is centralized and provides hotel-booking facilities to
the customers, irrespective of the geographical diversities. The entire
data at the hotel reservation system is managed by the Database
Administrator. In addition, he/she is responsible for securing the
databases to allow only authorized users to access or manipulate them.
Therefore, once the hotel reservation system is developed, it becomes
the responsibility of the Database Administrator to create user accounts
for the employees who can access and manipulate the hotel reservation
system. In addition, the Database Administrator needs to create user
accounts for the agents who can access the hotel reservation system.
The following table lists the user names and their user types for which
the user accounts need to be created by the Database Administrator.

Ver. 1.0 Slide 41 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Creating User Accounts (Contd.)

Once all the user accounts are created, the Database


Administrator wants to ensure that the information regarding
these user accounts are entered into the system. Help the
Database Administrator in accomplishing this task.

Ver. 1.0 Slide 42 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Creating User Accounts (Contd.)

Solution:
To solve the preceding problem, you need to perform the following
tasks:
Create the user accounts.
Verify the creation of the user accounts.

Ver. 1.0 Slide 43 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Modifying User Accounts

Problem Statement:
The Database Administrator at Rox Luxurious Ltd. suspects that
the passwords of some user accounts have been leaked out.
These passwords might be misused by any unauthorized user.
This may result in illegal access and manipulation of the secured
data at the hotel reservation system. Therefore, the Database
Administrator decided to change the user names and reset the
passwords associated with these user accounts. The following
table lists the user names, new user names, and new passwords to
be assigned to the user accounts.

Ver. 1.0 Slide 44 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Modifying User Accounts (Contd.)

In addition to this, the Database Administrator needs to delete the


user account for Kimberly Clark who earlier used to provide agent
services to Rox Luxurious Hotels Ltd. but has now ceased to provide
the services. Help the Database Administrator in accomplishing these
tasks.

Ver. 1.0 Slide 45 of 51


Creating, Querying, and Managing Databases Using
MySQL
Demo: Modifying User Accounts (Contd.)

Solution:
To solve the preceding problem, you need to perform the following
tasks:
Change the user names and reset the passwords associated with
the user accounts.
Verify the changes made to the user accounts.
Delete the user account.
Verify the deletion of the user account.

Ver. 1.0 Slide 46 of 51


Creating, Querying, and Managing Databases Using
MySQL
Summary

In this session, you learned that:


MySQL provides the facility to the user to export data into files
outside the database by using various MySQL utilities and
statements.
The INTO OUTFILE variant of the SELECT statement allows
you to do the same. In addition, it can be used to take backup
of the complete database or specific sections of the database
into an out file.
MySQL provides the mysql and mysqlimport utility to import
data from various file formats, such as text files, into the
database tables.
The LOAD DATA statement allows you to directly import values
from a delimited text file.
The source command can be used to import data into MySQL
tables by executing SQL statements stored in a sql file from
the mysql command prompt.

Ver. 1.0 Slide 47 of 51


Creating, Querying, and Managing Databases Using
MySQL
Summary (Contd.)

The mysql command can be executed from the operating


system command prompt to execute SQL statements and
mysql commands from within a text file without invoking the
command line client.
MySQL provides a utility called mysqlimport that is actually
a command line version of the LOAD DATA statement.
A user account is defined in terms of a user name and a host.
The information about each user account, such as the host,
user name, password, and global privileges assigned to it, is
stored in the user table of the mysql system database as a
separate record.
When MySQL is installed on a system, some of the user
accounts are automatically created in it. These user accounts
are known as the initial set of accounts.

Ver. 1.0 Slide 48 of 51


Creating, Querying, and Managing Databases Using
MySQL
Summary (Contd.)

The initial set of accounts can be categorized as:


Superuser accounts
Anonymous accounts
A Database Administrator can create user accounts in any of
the following ways:
Using the CREATE USER statement
Using the INSERT statement
The FLUSH PRIVILEGES statement should be executed to
inform the MySQL server to reload all the grant tables in
memory.
The user accounts can be renamed by using the RENAME
USER statement. Renaming the user accounts includes
changing the user names and/or the host values associated
with them.

Ver. 1.0 Slide 49 of 51


Creating, Querying, and Managing Databases Using
MySQL
Summary (Contd.)

MySQL allows you to modify the password associated with a


user account or assign a new password to an account that is
not assigned any password. For this, MySQL provides the
following ways:
Using the SET PASSWORD statement
Using the UPDATE statement
The SET PASSWORD statement allows you to set a new
password for an existing user account or change the current
password associated with a user account.
The UPDATE statement, issued on the user table of the mysql
database, is used to change the password associated with an
existing user account.

Ver. 1.0 Slide 50 of 51


Creating, Querying, and Managing Databases Using
MySQL
Summary (Contd.)

To delete a user account, MySQL provides the following ways:


Using the DROP USER statement
Using the DELETE statement
The GRANT statement can be used to create a user account as
well as to assign privileges to it.

Ver. 1.0 Slide 51 of 51

You might also like