MySQL Session 14
MySQL Session 14
MySQL
Objectives
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.
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.
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.
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.
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.
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.
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.
<import option>::=
{FIELDS | COLUMNS}
[TERMINATED BY ‘<value>’]
[[OPTIONALLY] ENCLOSED BY ‘<value>’]
[ESCAPED BY ‘<value>’]}
| {LINES
[STARTING BY ‘<value>’]
[TERMINATED BY ‘<value>’]}
For example:
LOAD DATA INFILE "d:/Austria_cities.txt" INTO TABLE
Austria_Cities;
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.
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.
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.
For example:
mysqlimport production_management_system
d:order_status.txt;
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.
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.
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.
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.
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.
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.
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.
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.
For example:
SET PASSWORD FOR 'almin'@'localhost' =
PASSWORD('password_almin');
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.
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.
Solution:
To solve the preceding problem, you need to perform the following
tasks:
Create the user accounts.
Verify the creation of the 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.
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.