MySQL Questions Answers

download MySQL Questions Answers

of 113

Transcript of MySQL Questions Answers

  • 7/23/2019 MySQL Questions Answers

    1/113

    Unit-1 Client/Server

    Concepts

    1- Explain the General MySQ

    Client Server Architecture.

    Answer- MySQL operates in a

    networked environment usingclient/server architecture. In

    other words, a central program

    acts as a server, and variou

    client programs connect to the

    server to make requests. A MySQ

    installation has the following

    major components: MySQServer, Client programs and

    MySQL non client utilities.

    MySQL Server

  • 7/23/2019 MySQL Questions Answers

    2/113

    MySQL Server, or mysqld, is the

    database server program. The

    server manages access to the

    actual database (schema) on disand in memory. MySQL Server i

    multi-threaded and support

    many simultaneous clien

    connections. Clients can connec

    via several connection protocols

    For managing database contents

    the MySQL server features amodular architecture.

    Client Programs

    These are programs that are usedfor communicating with the

    server to manipulate the

    databases. MySQL AB provide

  • 7/23/2019 MySQL Questions Answers

    3/113

    following client programs:

    MySQL Query Browse

    and MySQ

    Administrator are

    graphical interfaces to

    the server.

    mysql is a commandline program that act

    as a text-based fron

    end for the server.Other command-line

    clients include mysq

    import for importing

    data files, mysqldump

    for making backups

    mysql admin for serve

    administration, and

  • 7/23/2019 MySQL Questions Answers

    4/113

    mysqlcheck fo

    checking the integrity

    of the database files.

    MySQL Client/Server ModelMySQL runs on many varieties o

    Windows, Unix, and Linux. Bu

    client/server communication i

  • 7/23/2019 MySQL Questions Answers

    5/113

    not limited to environment

    where all computers run the

    same operating system. Clien

    programs can connect to a serverunning on the same host or a

    different host, and the client and

    server host need not have the

    same operating system. Fo

    example, client programs can be

    used on Windows to connect to a

    server that is running on Linux.

    2- What are MySQL clien

    programs and how to run

    them? Explain with suitableexamples.

    Answer-

  • 7/23/2019 MySQL Questions Answers

    6/113

    Client Programs

    These are programs that are usedfor communicating with the

    server to manipulate the

    databases. MySQL AB provide

    following client programs:

    MySQL Query Browse

    and MySQ

    Administrator: MySQWorkbench provide

    DBAs and developer

    an integrated tool

    environment for:Database Design

    &Modeling

    SQL Developmen

  • 7/23/2019 MySQL Questions Answers

    7/113

    Database

    Administration

    Database

    Migration

    mysql:mysql is one o

    the MySQL clien

    programs. It is acommand-line program

    that acts as a text

    based front end for the

    MySQL Server. It's used

    for issuing queries and

    viewing the result

    interactively from aterminal window.

    To determine the option

    supported by a MySQ

  • 7/23/2019 MySQL Questions Answers

    8/113

    program, invoke it with the

    --help option.

    shell>mysql help

    to connect to the server use

    the default hostname and

    username values with no

    password as follows:shell>mysql

    After successful connection

    you can select or changethe default database while

    running mysql, issue a USE

    db_name statement, where

    db_name is the name othe database you'd like to

    use.

    The following statemen

  • 7/23/2019 MySQL Questions Answers

    9/113

    makes world the defaul

    database:

    mysql> USE world;

    You can get curren

    database name like below:

    mysql> SELECTdatabase();

    To get all available

    databases, run the

    following command:

    mysql> SHOW

    databases;Other command-line

    clients

    mysql import fo

  • 7/23/2019 MySQL Questions Answers

    10/113

    importing data

    files

    mysqldump fo

    making backups

    mysql admin fo

    server

    administration,and

    mysqlcheck fo

    checking theintegrity of the

    database files.

  • 7/23/2019 MySQL Questions Answers

    11/113

    Unit-2 MySQL Client

    Program

    1- What are the two modes o

    interaction of MySQL Clien

    Program? Write down the

    differences between the twomodes.

    Answer- mysql is a general

    purpose client program fo

    issuing queries and retrieving

    their results. It can be used

    interactively or in batch mode to

    read queries from a file.To invoke mysql interactively

    from the command line, specify

    any necessary connection

  • 7/23/2019 MySQL Questions Answers

    12/113

    parameters after the command

    name:

    shell>mysql -u

    user_name -p -h

    host_name

    You can also provide a database

    name to select that database athe default database:

    shell>mysql -u

    user_name -p -hhost_namedb_name

    Batch mode is useful for running

    queries that have beenprewritten and stored in a file

    called script file. Its especially

    valuable for issuing a complex

    series of queries thats difficult to

  • 7/23/2019 MySQL Questions Answers

    13/113

    enter manually, or queries tha

    need to be run automatically by a

    job scheduler without use

    intervention.

    To process a script file is by

    executing it with

    a SOURCE command fromwithin mysql:

    mysql> SOURCE

    input_file;Notice that there are no quote

    around the name of the file.

    mysql executes the queries in thefile and displays any outpu

    produced.

    The file must be located on the

  • 7/23/2019 MySQL Questions Answers

    14/113

    client host where youre

    running mysql. The filename

    must either be an absolute

    pathname listing the full name othe file For example, if you

    started mysql on a Window

    machine in the C:\mysql directory

    and your script file

    is my_commands.sql in

    the C:\scripts directory, both o

    the following SOURCE commandtell mysql to execute the SQ

    statements in the file:

    mysql> SOURCEC:\scripts\my_commands.

    mysql> SOURCE

    ..\scripts\my_commands.

  • 7/23/2019 MySQL Questions Answers

    15/113

    2- What are statemen

    terminators in MySQL? Explain

    various options for statemen

    terminators in MySQL withsuitable examples.

    A terminator is necessary afte

    each statemen

    because mysql allows severa

    queries to be entered on a single

    input line. There are severa

    terminators to end a statementTwo terminators are the

    semicolon character (;) and

    the \g sequence. Theyre

    equivalent and may be used

    interchangeably:

    mysql> SELECT

    VERSION(), DATABASE();

  • 7/23/2019 MySQL Questions Answers

    16/113

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

    --------+

    | VERSION()

    DATABASE() |

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

    --------+

    | 5.0.10-beta-log

    world |

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

    --------+

    mysql> SELECT

    VERSION(), DATABASE()\g

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

    --------+

    | VERSION()

    DATABASE() |

  • 7/23/2019 MySQL Questions Answers

    17/113

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

    --------+

    | 5.0.10-beta-log

    world |

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

    --------+

    The \G sequence also terminate

    queries, but causes mysql to

    display query results in a vertica

    style that shows each output rowwith each column value on a

    separate line:

    mysql> SELECTVERSION(), DATABASE()\G

    ***********************

    1. row

  • 7/23/2019 MySQL Questions Answers

    18/113

    ***********************

    VERSION(): 5.0.10-beta-

    log

    DATABASE(): world

    The \G terminator is especially

    useful if a query produces very

    wide output lines because

    vertical format can make the

    result much easier to read.

    If you are using mysql to define a

    stored routine or a trigger tha

    uses compound statement syntax

    and consists of multiplestatements, the definition wil

    contain semicolons internally. In

    this case, it is necessary to

    redefine the ; terminator to

  • 7/23/2019 MySQL Questions Answers

    19/113

    cause mysql to pass semicolon

    in the definition to the serve

    rather than interpreting them

    itself.

    3- What are MySQL(Client

    Commands? What are long

    form and short form for them?Answer- When you issue an SQ

    statement while running mysql

    the program sends the statemen

    to the MySQL server to be

    executed. SELECT, INSERT, UPDAT

    and DELETE are examples of thi

    type of input. mysql alsounderstands a number of its own

    commands that arent SQ

    statements. The QUIT

    and SOURCE commands tha

  • 7/23/2019 MySQL Questions Answers

    20/113

    have already been discussed are

    examples of mysql commands

    Another example is STATUS

    which displays information abouthe current connection to the

    server, as well as statu

    information about the serve

    itself. Here is what a statu

    display might look like:

    mysql> STATUS;

    A full list of mysql commands can

    be obtained using

    the HELP command.mysql comma

    have both a long form and ashort form. The long form is a ful

    word (such as SOURCE, STATUS

    or HELP). The short form consist

  • 7/23/2019 MySQL Questions Answers

    21/113

    of a backslash followed by a

    single character (such as \., \s

    or \h). The long forms may be

    given in any lettercase. The shorforms are case sensitive.

    4- What are the different types o

    Output Formats in MySQLHow will you change you

    output from default format to

    another format?

    Answer- By

    default, mysql produces outpu

    in one of two formats, depending

    on whether you use it ininteractive or batch mode:

    1. When invoked

    interactively, mysql dis

  • 7/23/2019 MySQL Questions Answers

    22/113

    query output in a

    tabular format tha

    uses bars and dashe

    to display values linedup in boxed columns.

    2. When you

    invoke mysql with a

    file as its input source

    on the command

    line, mysql runs in

    batch mode with queryoutput displayed using

    tab character

    between data values.

    To override the default outpu

    format, use these options:

    a. --batch or -B

  • 7/23/2019 MySQL Questions Answers

    23/113

    Produce batch mode (tab

    delimited) output, even

    when running interactively.

    b. --table or -tProduce tabular outpu

    format, even when running

    in batch mode.

    In batch mode, you can use the -

    raw or -r option to suppres

    conversion of characters such a

    newline and carriage return to

    escape-sequences such

    as \n or \r. In raw mode, the

    characters are printed literally.To select an output forma

    different from either of the

    default formats, use these

  • 7/23/2019 MySQL Questions Answers

    24/113

    options:

    c. --html or -H

    Produce output in HTMformat.

    d. --xml or -X

    Produce output in XMformat.

    5- What is -safe--update option

    MySQL? What are its effects onMySQL statements?

    Answer- Its possible to

    inadvertently issue statement

    that modify many rows in a tableor that return extremely large

    result sets. The --safe

    updates option helps preven

  • 7/23/2019 MySQL Questions Answers

    25/113

    these problems. The option i

    particularly useful for people who

    are just learning to use MySQL. -

    safe-updates has the followingeffects:

    UPDATE and DELETE st

    are allowed only i

    they include

    a WHERE clause tha

    specifically identifie

    which records toupdate or delete by

    means of a key value

    or if they include

    a LIMIT clause.

    Output from single

    table SELECT statemen

    is restricted to no

  • 7/23/2019 MySQL Questions Answers

    26/113

    more than 1,000 row

    unless the statemen

    includes a LIMIT clause

    Multiple-table SELECT statemen

    are allowed only i

    MySQL will examine no

    more than 1,000,000

    rows to process the

    query.

    The --i-am-a-dummyoption ia synonym for --safe-updates.

  • 7/23/2019 MySQL Questions Answers

    27/113

    Unit-3 MySQL

    Architecture

    1- What is difference among

    mysqld, mysqladmin

    mysqldump, mysqlimport and

    mysqlcheck?Answer-

    mysqld

    "mysqld" is MySQL servedaemon program which run

    quietly in background on you

    computer system. Invoking

    "mysqld" will start the MySQ

    server on your system

    Terminating "mysqld" wil

    shutdown the MySQL server. Here

  • 7/23/2019 MySQL Questions Answers

    28/113

    is an example of invoking

    "mysqld" with the "--console

    option:

    >cd \mysql\bin

    >mysqld --console

    mysqladmin

    mysqladmin is a client too

    program for database serve

    administrators to manage a

    MySQL server remotely. Thesyntax to run mysqladmin is:

    \mysql\bin\mysqladmin

    [options] command[command-arg]

    It support a number of commonly

    used commands like:

  • 7/23/2019 MySQL Questions Answers

    29/113

    "mysqladmin shutdown" -Shuts down the server.

    "mysqladmin ping" -

    Checks if the server isalive or not.

    "mysqladmin status" -Displays several

    important server statusvalues.

    "mysqladmin version" -

    Displays versioninformation of theserver.

    "mysqladmin create

    databaseName" - Creates anew database.

    "mysqladmin dropdatabaseName" - Drops an

    existing database.

  • 7/23/2019 MySQL Questions Answers

    30/113

    Mysqldump

    Mysqldump is part of the mysqrelational database package

    which allows you to "dump" a

    database, or a collection o

    databases, for backup otransferral to another SQL server

    The server that imports the

    databases does not have to be

    mysql.

    The typical way to use

    mysqldump is one of the

    following commands:

    mysqldump [options]db_name [tables]

    mysqldump [options] --

  • 7/23/2019 MySQL Questions Answers

    31/113

    databases DB1 [DB2DB3...]

    mysqldump [options] --

    all-databases

    mysqlimport

    "mysqlimport" - A command-line

    interface for administrators o

    end users to load data files into

    tables program tool to load datainto tables. Here is a sample

    commands supported by

    "mysqlimport":

    "mysqlimport databaseName

    fileName" - Imports the data

    from the specified file to the

    specified database. The data wil

  • 7/23/2019 MySQL Questions Answers

    32/113

    be loaded into the table who'

    name matches the specified file

    name.

    mysqlcheck

    "mysqlcheck" is a command-line

    interface for administrators to

    check and repair tables. Here are

    some sample command

    supported by "mysqlcheck":"mysqlcheck databaseName

    tableName" - Checks the

    specified table in the specifieddatabase.

    "mysqlcheck databaseName"

    Checks all tables in the specified

    database.

  • 7/23/2019 MySQL Questions Answers

    33/113

    "mysqlcheck --all-databases"

    Checks all tables in all databases.

    "mysqlcheck --analyze

    databaseName tableName"

    Analyzes the specified table in

    the specified database.

    "mysqlcheck --repaidatabaseName tableName"

    Repairs the specified table in the

    specified database.

    2- What are differen

    communication protocols in

    MySQL? Explain them in brief.Answer- A MySQL client program

    can connect to a server running

    on the same machine. This is a

    local connection. A client can also

  • 7/23/2019 MySQL Questions Answers

    34/113

    connect to a server running on

    another machine, which is a

    remote connection.

    MySQL supports connection

    between clients and the serve

    using several networking

    protocols, as shown in thefollowing table.

    Protocol Types of

    Connections

    Suppor

    OperatSystem

    TCP/IP Local,

    remote

    All

    Unix

    socket

    Local only Unix on

  • 7/23/2019 MySQL Questions Answers

    35/113

    file

    Named

    pipe

    Local only Windo

    only

    Shared

    memory

    Local only Windo

    only

    Protocols are used for connecting

    to either local or remote servers.

    TCP/IP connections are

    supported by any

    MySQL server.

    Unix socket file

    connections aresupported by all Unix

    servers.

    Named-pipe

    connections are

  • 7/23/2019 MySQL Questions Answers

    36/113

    supported only on

    Windows.

    Shared-memory

    connections aresupported by al

    Windows servers.

    MySQL communication protocolare implemented by variou

    libraries and program drivers

    Client programs included with

    MySQL distribution

    (mysql, mysqladmin, and so

    forth) establish connections to

    the server using the native Cclient library.

    3- How is MySQL data stored on

    disk? Brief various directory

  • 7/23/2019 MySQL Questions Answers

    37/113

    (ies) and file(s) created mainly

    for MyISAM and InnoDB

    storage engines.

    Answer- MySQL Server uses disspace in several ways, primarily

    for directories and files that are

    found under a single location

    known as the server's data

    directory. The server uses its data

    directory to store all the

    following:

    Database directories. Each

    database corresponds to a single

    directory under the datadirectory, regardless of wha

    types of tables you create in the

    database. For example, a given

    database is represented by one

  • 7/23/2019 MySQL Questions Answers

    38/113

    directory whether it contain

    MyISAM tables, InnoDB tables, o

    a mix of the two.

    Table format files (.frm files) tha

    contains a description of table

    structure.Every table has its own

    .frm file, located in theappropriate database directory

    This is true no matter which

    storage engine manages the

    table.

    Data and index files are created

    for each table by some storage

    engines and placed in theappropriate database directory

    For example, the MyISAM storage

    engine creates a data file and an

  • 7/23/2019 MySQL Questions Answers

    39/113

    index file for each table.

    The InnoDB storage engine ha

    its own tablespace and log files

    The tablespace contains data and

    index information for all InnoDB

    tables, as well as the undo log

    that are needed if a transactionmust be rolled back. The log file

    record information abou

    committed transactions and are

    used to ensure that no data los

    occurs. By default, the tablespace

    and log files are located in the

    data directory. The defaultablespace file is named ibdata1

    and the default log files are

    named ib_logfile0 and ib_logfile1

    (It is also possible to configure

  • 7/23/2019 MySQL Questions Answers

    40/113

    InnoDB to use one tablespace file

    per table. In this case, InnoDB

    creates the tablespace file for a

    given table in the table'database directory.)

    Server log files and status files

    These files contain informationabout the statements that the

    server has been processing. Log

    are used for replication and data

    recovery, to obtain information

    for use in optimizing query

    performance, and to determine

    whether operational problemare occurring.

    4- MySQL Server allocate

    memory for what kinds o

  • 7/23/2019 MySQL Questions Answers

    41/113

    information? Explain the

    various types of buffer

    (caches) used by MySQL to

    hold information in memory.Answer- MySQL Server memory

    use includes data structures tha

    the server sets up to manage

    communication with clients and

    to process the contents o

    databases. The server allocate

    memory for many kinds oinformation as it runs:

    Thread handlers. The server i

    multi-threaded, and a thread i

    like a small process running

    inside the server. For each clien

    that connects, the serve

    allocates a thread to it to handle

  • 7/23/2019 MySQL Questions Answers

    42/113

    the connection. For performance

    reasons, the server maintains a

    small cache of thread handlers.

    The server uses several buffer(caches) to hold information in

    memory for the purpose o

    avoiding disk access when

    possible:

    a. Grant table buffers

    The grant tables store

    information abouMySQL user account

    and the privileges they

    have. The server load

    a copy of the gran

    tables into memory fo

    fast access-contro

    checking.

  • 7/23/2019 MySQL Questions Answers

    43/113

    b. A key buffer hold

    index block

    for MyISAM tables. By

    caching index blocks inmemory, the serve

    often can avoid

    reading index content

    repeatedly from dis

    for index-based

    retrievals and othe

    index-relatedoperations such a

    sorts.

    c. The table cache hold

    descriptors for open

    tables. For frequently

    used tables, keeping

    the descriptors in the

  • 7/23/2019 MySQL Questions Answers

    44/113

    cache avoids having to

    open the tables again

    and again.

    d. The server supports aquery cache tha

    speeds up processing

    of queries that are

    issued repeatedly.

    e. The host cache hold

    the results o

    hostname resolutionlookups. These result

    are cached to minimize

    the number of calls to

    the hostname resolver

    f. The InnoDB storage

    engine log

    information abou

  • 7/23/2019 MySQL Questions Answers

    45/113

    current transactions in

    a memory buffer

    When a transaction

    commits, the log buffeis flushed to

    the InnoDB log files

    providing a record on

    disk that can be used

    to recommit the

    transaction if it is los

    due to a crash. If thetransaction rolls bac

    instead, the flush to

    disk need not be done

    at all

  • 7/23/2019 MySQL Questions Answers

    46/113

    Unit-5 Locking

    1- What is locking? Explain it

    importance in MySQL withsuitable examples.

    Answer-Locking is a mechanism

    that prevents problems from

    occurring with simultaneous data

    access by multiple clients. Lock

    are managed by the server: I

    places a lock on data on behalf oone client to restrict access by

    other clients to the data until the

    lock has been released. The loc

    allows access to data by theclient that holds the lock, bu

    places limitations on wha

    operations can be done by othe

  • 7/23/2019 MySQL Questions Answers

    47/113

    clients that are contending fo

    access. The effect of the locking

    mechanism is to serialize acces

    to data so that when multipleclients want to perform

    conflicting operations, each mus

    wait its turn.

    Not all types of concurrent acces

    produce conflicts, so the type o

    locking that is necessary to allow

    a client access to data dependon whether the client wants to

    read or write:

    If a client wants to

    read data, other client

    that want to read the

    same data do no

    produce a conflict, and

  • 7/23/2019 MySQL Questions Answers

    48/113

    they all can read at the

    same time. However

    another client tha

    wants to write (modifydata must wait unti

    the read has finished.

    If a client wants to

    write data, all othe

    clients must wait unti

    the write has finished

    regardless of whethethose clients want to

    read or write.

    A lock on data can be acquired

    implicitly or explicitly:

    For a client that doe

    nothing special to

    acquire locks, the

  • 7/23/2019 MySQL Questions Answers

    49/113

    MySQL server implicitly

    acquires locks a

    necessary to proces

    the clients statementsafely. For example

    the server acquires a

    read lock when the

    client issue

    a SELECT statemen

    and a write lock when

    the client issuean INSERT statement.

    Explicit locking may be

    necessary when a

    client needs to

    perform an operation

    that spans multiple

    statements and tha

  • 7/23/2019 MySQL Questions Answers

    50/113

    must not be

    interrupted by othe

    clients. For example

    an application mighselect a value from one

    table and then use i

    to determine which

    records to update in a

    set of other tables.

    2- What are the levels at which

    data locking occurs in MySQLExplain their characteritics.

    Answer-Data locking in MySQ

    occurs at different levels. Explici

    locks acquired with LOCK

    TABLES are table locks. Fo

    implicit locks, the lock level tha

    MySQL uses depends on the

  • 7/23/2019 MySQL Questions Answers

    51/113

    storage engine:

    MyISAM, MEMORY

    and MERGE tables are

    locked at the tablelevel.

    BDB tables are locked

    at the page level.

    InnoDB tables are

    locked at the row level

    The different levels of locking

    granularity have differenconcurrency characteristics:

    Table locking is not a

    desirable as page o

    row locking fo

    concurrency in a mixed

    read/write

    environment. A table

  • 7/23/2019 MySQL Questions Answers

    52/113

    lock prevents othe

    clients from making

    any changes to the

    table, even if the clienthat holds the lock i

    not accessing the part

    of the table that othe

    clients want to modify

    With page and row

    locks, a client tha

    locks a page or rowdoes not preven

    changes by othe

    clients to other page

    or rows.

    Deadlock cannot occu

    with table locking as i

    can with page or row

  • 7/23/2019 MySQL Questions Answers

    53/113

    locking. For example

    with row-level locking

    two clients might each

    acquire a lock ondifferent rows. If each

    then tries to modify

    the row that the othe

    has locked, neithe

    client can proceed

    This is called

    deadlock.With table locking, the

    server can determine

    what locks are needed

    and acquire them

    before executing a

    statement, so deadloc

    never occurs.

  • 7/23/2019 MySQL Questions Answers

    54/113

    3- What are Implicit and Explici

    locking? Explain them with

    suitable commands and

    examples.Answer-

  • 7/23/2019 MySQL Questions Answers

    55/113

    Implicit Locking:

    For a client that does nothing

    special to acquire locks, the

    server implicitly acquires locks a

    necessary to process the client'

    statement safely. For example

    the server acquires a read locwhen the client issues a SELECT

    statement, and a write lock when

    the client issues an INSERT

    statement.

    Implicit locks are acquired only

    for the duration of a single

    statement.

    Explicit Locking:

    If implicit locking is not sufficien

    for a client's purposes, it can

  • 7/23/2019 MySQL Questions Answers

    56/113

    manage locks explicitly by

    acquiring them with LOCK TABLES

    and releasing them with UNLOCK

    TABLES.

    Explicit locking may be necessary

    when a client needs to perform

    an operation that spans multiplestatements that must not be

    interrupted by other clients. Fo

    example, an application migh

    select a value from one table and

    then use it to determine which

    records to update in a set o

    other tables. With implicit lockingExplicit locking can improve

    performance for multiple

    statements executed as a group

  • 7/23/2019 MySQL Questions Answers

    57/113

    while the lock is in effect.

    The LOCK TABLES statemen

    names each table to be locked

    and the type of lock to be

    acquired. The following

    statement acquires a read lock on

    the Country table and a writelock on the City table:

    LOCK TABLES Country

    READ, City WRITE;To use LOCK TABLES, you mus

    have the LOCK TABLES privilege

    and the SELECT privilege for each

    table to be locked.

    To release explicit locks, issue

    an UNLOCK TABLES statement

    This statement names no tables

  • 7/23/2019 MySQL Questions Answers

    58/113

    because it releases all explici

    locks held by the issuing client.

    4- What is Explicit locking

    Explain the various lock type

    available in Explicit Locking?

    Answer-

    Explicit Locking:

    If implicit locking is not sufficien

    for a client's purposes, it can

    manage locks explicitly byacquiring them with LOCK TABLES

    and releasing them with UNLOCK

    TABLES.

    Explicit locking may be necessary

    when a client needs to perform

    an operation that spans multiple

    statements that must not be

  • 7/23/2019 MySQL Questions Answers

    59/113

    interrupted by other clients. Fo

    example, an application migh

    select a value from one table and

    then use it to determine whichrecords to update in a set o

    other tables. With implicit locking

    The following statement acquirea read lock on the Country table

    and a write lock on the City table

    LOCK TABLES CountryREAD, City WRITE;

    The following list describes the

    available lock types and thei

    effects:

    READ

    A READ lock locks a table for read

  • 7/23/2019 MySQL Questions Answers

    60/113

    queries such as SELECT tha

    retrieve data from the table. I

    does not allow write operation

    even for the client that holds thelock. A client that wants to write

    to a table that is read-locked

    must wait until all client

    currently reading from it have

    finished and released their locks.

    WRITE

    A WRITE lock is an exclusive lock

    It can be acquired only when a

    table is not being used. Once

    acquired, only the client holdingthe write lock can read from o

    write to the table. Other client

    can neither read from nor write

  • 7/23/2019 MySQL Questions Answers

    61/113

    to it.

    READ LOCAL

    Locks a table for reading, buallows concurrent inserts. A

    concurrent insert is an exception

    to the readers block writers

    principle. It applies onlyto MyISAM tables. I

    a MyISAM table has no holes in

    the middle resulting from deletedor updated records, insert

    always take place at the end o

    the table. In that case, a clien

    that is reading from a table canlock it with a READ LOCAL lock to

    allow other clients to insert into

    the table while the client holding

  • 7/23/2019 MySQL Questions Answers

    62/113

    the read lock reads from it. I

    a MyISAM table does have holes

    you can remove them by

    using OPTIMIZE TABLE todefragment the table.

    LOW_PRIORITY WRITE

    Locks a table for writing, buacquires the lock with a lowe

    priority. That is, if the client mus

    wait for the lock, other clientthat request read locks during

    the wait are allowed to get thei

    locks first. A normal write loc

    request is satisfied when noother clients are using the table.

    5- What is Advisory locking? How

    are they implemented, explain

  • 7/23/2019 MySQL Questions Answers

    63/113

    with examples?

    Answer-

    Advisory Locking

    An advisory lock is a cooperativelock. That is, an advisory lock ha

    no power to prevent data acces

    by other clients, but instead i

    based on the concept that al

    clients will use an agreed-upon

    convention to cooperate for use

    of a resource.Advisory locks are implemented

    using a set of function calls. To

    acquire a lock, use

    the GET_LOCK()function:

    mysql> SELECT

    GET_LOCK('my lock', 5);

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

  • 7/23/2019 MySQL Questions Answers

    64/113

    -- +

    | GET_LOCK('my lock',

    5) |

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

    |

    |+----------------------

    -- +

    The first argument is a string tha

    specifies the name to be lockedand the second argument is a

    timeout value in seconds tha

    indicates how long to wait for the

    lock if it cannot be acquired

    immediately. GET_LOCK() return

    1 for success, 0 if a timeou

    occurs and the lock cannot be

  • 7/23/2019 MySQL Questions Answers

    65/113

    acquired, or NULL if an erro

    occurs.

    A client that has acquired an

    advisory lock can release it bycalling RELEASE_LOCK():

    mysql> SELECT

    RELEASE_LOCK('mylock');

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

    --- +

    | RELEASE_LOCK('mylock') |

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

    --- +

    |

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

    --- +

    RELEASE_LOCK() returns 1 if the

  • 7/23/2019 MySQL Questions Answers

    66/113

    lock was released successfully, 0 i

    the name was locked but not by

    the client requesting the release

    and NULL if the name was nolocked.

    An advisory lock also is released

    if the client makes another cal

    to GET_LOCK() or closes it

    connection to the server.

    Two other functions are available

    for checking the status oadvisory locks:

    IS_FREE_LOCK(lock_name) return

    1 if the name is not locked, 0 if i

    is locked, and NULL if an erro

    occurs.

    IS_USED_LOCK(lock_name) return

    the connection ID of the clien

  • 7/23/2019 MySQL Questions Answers

    67/113

    that holds the lock on the name

    or NULL if the name is not locked

    Unit-6 Storage Engines

    1- Explain the concept storage

    engine in MySQL. Which arethe most common storage

    engines in MySQL?

    Answer-MySQL Storage Engines

    All tables managed by MySQ

    Server have certain similaritiesFor example, every table in a

    database has a format (.frm) file

    in the database directory. Thi

    file, which stores the definition o

  • 7/23/2019 MySQL Questions Answers

    68/113

    the tables structure, is created

    by the server. Tables have

    differences as well, which are tied

    to the storage engines that theserver uses to manage table

    contents. Each storage engine ha

    a particular set of operationa

    characteristics. For example

    engines may create additiona

    disk files to accompany

    the .frm files, but the types ofiles that they create to manage

    data and index storage vary pe

    engine.

    When you create a table, you can

    choose what storage engine to

    use. Typically, this choice is made

    according to which storage

  • 7/23/2019 MySQL Questions Answers

    69/113

    engine offers features that bes

    fit the needs of your application.

    MyISAM table-leve

    locking works best fo

    a query mix that i

    heavily skewed toward

    retrievals and includefew updates

    Use InnoDB if you mus

    process a query mix

    containing many

    updates.

    InnoDBs use of row

    level locking and multiversioning provide

    good concurrency for a

    mix of retrievals and

  • 7/23/2019 MySQL Questions Answers

    70/113

    updates.

    To specify a storage engine

    explicitly in a CREATE

    TABLE statement, use

    an ENGINE option. The following

    statement creates t a

    an InnoDB table:CREATE TABLE t (i INT) ENGINE =

    InnoDB;

    To determine which storageengine is used for a given table

    you can use the SHOW CREATE

    TABLE or the SHOW TABLE

    STATUS statement:

    mysql> SHOW CREATE TABLECity\G

    The INFORMATION_SCHEMA

  • 7/23/2019 MySQL Questions Answers

    71/113

    TABLES table contains storage

    engine information as well:

    mysql> SELECT TABLE_NAME,

    ENGINE FROMINFORMATION_SCHEMA.TABLES

    -> WHERE TABLE_SCHEMA

    = 'world';To reduce memory use, don

    configure unneeded storage

    engines into the server. Thi

    requires that you compile MySQ

    from source rather than using a

    precompiled binary distribution

    To see what storage engines arecompiled into your server and

    whether they are available a

    runtime, use the SHOW

    ENGINES statement

  • 7/23/2019 MySQL Questions Answers

    72/113

    mysql> SHOW ENGINES\G

    2- What are characteristics o

    MyISAM storage engines

    What are the feature

    supported by MyISAM which

    will be used by MySQL in

    future?The following characteristics o

    the MyISAM storage engine are

    improvements over the olde

    ISAM engine:

    All data values are

    stored with the low

    byte first. This makethe data machine and

    operating system

    independent.

    Large files (up to 63-bi

  • 7/23/2019 MySQL Questions Answers

    73/113

    file length) are

    supported on

    filesystems and

    operating systems thasupport large files.

    Dynamic-sized rows are

    much less fragmented

    when mixing delete

    with updates and

    inserts. This is done by

    automaticallycombining adjacen

    deleted blocks and by

    extending blocks if the

    next block is deleted.

    The maximum numbe

    of indexes per table i

    64 (32 before MySQ

  • 7/23/2019 MySQL Questions Answers

    74/113

    4.1.2)..

    The maximum key

    length is 1000 byte

    (500 before MySQ4.1.2).

    BLOB and TEXT

    columns can be

    indexed.

    NULL values are

    allowed in indexed

    columns. This takes 0-1bytes per key.

    All numeric key value

    are stored with the

    high byte first to allow

    better index

    compression.

    Index files are usually

  • 7/23/2019 MySQL Questions Answers

    75/113

    much smaller with

    MyISAM than with

    ISAM.

    Internal handling oone AUTO_INCREMENT

    column per table

    MyISAM automatically

    updates this column

    for INSERT/UPDATE

    This make

    AUTO_INCREMENTcolumns faster (at leas

    10%)..

    If a table doesn't have

    free blocks in the

    middle of the data file

    you can INSERT new

    rows into it at the

  • 7/23/2019 MySQL Questions Answers

    76/113

    same time that othe

    threads are reading

    from the table. (These

    are known aconcurrent inserts.)

    You can put the data

    file and index file on

    different directories to

    get more speed with

    the DATA DIRECTORY

    and INDEX DIRECTORYtable options to

    CREATE TABLE.

    There is a flag in the

    MyISAM index file tha

    indicates whether the

    table was closed

    correctly. If mysqld i

  • 7/23/2019 MySQL Questions Answers

    77/113

    started with the -

    myisam-recover

    option, MyISAM table

    are automaticallychecked (and

    optionally repaired

    when opened if the

    table wasn't closed

    properly.

    myisamchk mark

    tables as checked iyou run it with the -

    update-state option

    myisamchk --fas

    checks only those

    tables that don't have

    this mark.

    myisamchk --analyze

  • 7/23/2019 MySQL Questions Answers

    78/113

    stores statistics for key

    parts, not only fo

    whole keys as in ISAM.

    myisampack can pacBLOB and VARCHAR

    columns; pack_isam

    cannot.

    MyISAM also supports the

    following features, which MySQ

    will be able to use in the nea

    future:

    Support for a true

    VARCHAR type; a

    VARCHAR columnstarts with a length

    stored in two bytes.

    Tables with VARCHAR

  • 7/23/2019 MySQL Questions Answers

    79/113

    may have fixed o

    dynamic record length

    VARCHAR and CHAR

    columns may be up to64KB.

    A hashed computed

    index can be used fo

    UNIQUE. This will allow

    you to have UNIQUE

    on any combination o

    columns in a table(You can't search on a

    UNIQUE computed

    index, however.)

    3- Explain advantages of MyISAM

    over InnoDB?

    Answer- The following are the

    advantages of MyISAM ove

  • 7/23/2019 MySQL Questions Answers

    80/113

    InnoDB:

    MyISAM tables are stored

    in separate files in

    compressed mode

    whereas InnoDB table

    are stored in table space.

    More options for furtheoptimization in MyISAM

    whereas there is no

    further optimization

    possible with InnoDB.

    Data except for TEXT and

    BLOB can occupy at mos

    8000 bytes in InnoDB.Full text indexing

    available in MyISAM

    whereas no full tex

    indexing is available fo

  • 7/23/2019 MySQL Questions Answers

    81/113

    InnoDB.

    Count (*) function

    execution is slower in

    InnoDB because of tablespace complexity.

    4- When is InnoDB engine

    preferred over MyISAM

    Explain the transaction mode

    of InnoDB engine.

    5- Differentiate MyISAM and

    InnoDB storage engines? Howthey differ in their locking

    mechanism?

    6- What is Merge table? When

    are they used? What are thei

    disadvantages?

    7- Write short notes on-

    i) Heap (Memory

  • 7/23/2019 MySQL Questions Answers

    82/113

    Engine

    ii) Federated Engine

    iii) Cluster Engine

  • 7/23/2019 MySQL Questions Answers

    83/113

    Unit-8 Backup and

    Recovery

    1- What are the principles o

    backup?

    Answer-

    Principles of backup

    Make backup

    regularly.

    Enable the binary logso that we have a

    record of change

    made after a given

    backup.

    Flush the logs when

    backing up so that the

    server will begin a new

  • 7/23/2019 MySQL Questions Answers

    84/113

    binary log file.

    Store the data

    directory and backup

    on different physicadevices so that a

    device failure canno

    destroy both.

    Include the backups in

    the regular filesystem

    backup procedures.

    2- Write difference between

    Binary and Textual backups.

    Answer-

    Binary Backup

    A copy of the files in

    which database

    contents are stored.

  • 7/23/2019 MySQL Questions Answers

    85/113

    Preserves the

    databases in exactly

    the same format in

    which MySQL itselstores them on disk.

    To restore copy the

    files back to thei

    original locations.

    Techniques

    copy command

    (such as cp otar)

    mysqlhotcopy

    InnoDB Ho

    Backup

    Its faster to make a

    binary backup.

    For transferring

  • 7/23/2019 MySQL Questions Answers

    86/113

    databases to anothe

    machine that uses a

    different architecture

    the files must bebinary portable.

    With binary backup

    methods, its necessary

    to make sure that the

    server does not modify

    the files while the

    backup is in progress.Binary backup

    depends on which

    storage engine created

    the tables

    Text Backup

    A text backup is a

  • 7/23/2019 MySQL Questions Answers

    87/113

    dump of database

    contents into text files

    Restoration involve

    loading the filecontents back into

    databases by

    processing them

    through the server.

    Techniques

    SELECT ... INTO

    OUTFILE SQstatement

    Mysqldump

    MySQL

    Administrator.

    Its slower to make a

    text backup because

    the server must read

  • 7/23/2019 MySQL Questions Answers

    88/113

    tables

    Text backups are

    portable and

    independent omachine architecture.

    With text backup

    methods, the serve

    must be running

    because it must read

    the files that are to be

    backed up.Text backup

    procedures are more

    general and can be

    used for tables created

    by any storage engine.

    3- What are the steps involved in

    making Binary MyISAM

  • 7/23/2019 MySQL Questions Answers

    89/113

    Backups?

    Answer-

    Making Binary MyISAM Backups

    To make a binarybackup of a MyISAM

    table, copy the .frm

    .MYD, and .MYI files.

    While copying the

    table must not be in

    use so:

    Either stop the servewhile copying the table

    Use an appropriate

    locking protocol to

    prevent server acces

    to the table.

    For example:

    mysql> USE

  • 7/23/2019 MySQL Questions Answers

    90/113

    world;mysql> LOCKTABLES

    Country READ;mysql> FLUSHTABLESCountry;

    After backup releasethe lock on the table:mysql> UNLOCKTABLES;

    To recover a MyISAMtable from a binary

    backup, stop the

    server, copy the

    backup table files intothe appropriate

    database directory

    and restart the server.

  • 7/23/2019 MySQL Questions Answers

    91/113

    4- What are the conditions fo

    Binary backup? Brief them fo

    MyISAM and InnoDB tables.

    Answer-Conditions for Binary Portability

    For MyISAM, binary

    portability means tha

    one can directly copy

    the files for a MyISAM

    table from one MySQ

    server to another on adifferent machine and

    the second server wil

    be able to access the

    table.

    For InnoDB, binary

    portability means tha

    one can directly copy

  • 7/23/2019 MySQL Questions Answers

    92/113

    the tablespace file

    from a MySQL serve

    on one machine to

    another server on adifferent machine and

    the second server wil

    be able to access the

    tablespace.

    MyISAM tables and

    InnoDB tablespaces are

    binary portable if twoconditions are met:

    Both M/Cs mus

    use 2s

    complement

    integer

    arithmetic.

    Both M/Cs mus

  • 7/23/2019 MySQL Questions Answers

    93/113

    use IEEE floating

    point format, o

    else the table

    must contain nofloating-point

    columns.

    We should use

    lowercase name

    for database

    and tables. Using

    lowercase nameallows binary

    portability

    between

    Windows and

    Unix.

    To force lowercase

    names, put following

  • 7/23/2019 MySQL Questions Answers

    94/113

    in an option file:

    [mysqld]

    lower_case_table

    5- How MySQL Administratohelps in Making Text Backups

    Explain the procedure.

    Answer-

    Making Text Backups with MySQ

    Administrator

    GUI program toprovide backup and

    restore capabilities.

    It generates backup

    files containing SQstatements that can be

    reloaded into you

    MySQL server to re

  • 7/23/2019 MySQL Questions Answers

    95/113

    create databases and

    tables.

    These files are simila

    to the SQL-formabackup files generated

    bymysqldump.

    MySQL Administrato

    stores backup

    configuration option

    as projects.

    One can select andexecute these project

    later to perform a

    backup operation

    based on a given set o

    specifications.

    The project approach

    enables us to easily

  • 7/23/2019 MySQL Questions Answers

    96/113

    select from among

    multiple types o

    backups.

    We can select backupprojects on demand o

    schedule them fo

    periodic execution.

    6- What is the big ibdata file tha

    is in all the backups?

    Answer- You might find you

    backup data taking more spacethan expected because of a large

    file with a name such as ibdata1

    This file represents the InnoDB

    system tablespace, which grow

    but never shrinks, and is included

    in every full and incrementa

    backup. To reduce the space

  • 7/23/2019 MySQL Questions Answers

    97/113

    taken up by this file in you

    backup data:

    After doing a full backup, do a

    succession of incrementa

    backups, which take up les

    space. The ibdata1 file in the

    incremental backups is typicallymuch smaller, containing only the

    portions of the system tablespace

    that changed since the ful

    backup.

    Set the configuration option

    innodb_file_per_table=1 before

    creating your biggest or mosactive InnoDB tables. Those

    tables are split off from the

    system tablespaces into separate

  • 7/23/2019 MySQL Questions Answers

    98/113

    .ibd files, which are more flexible

    in terms of freeing disk space

    when dropped or truncated, and

    can be individually included oexcluded from backups.

    If your system tablespace is very

    large because you created a highvolume of InnoDB data before

    turning on the

    innodb_file_per_table setting

    you might use mysqldump to

    dump the entire instance, then

    turn on innodb_file_per_table

    before re-creating it, so that althe table data is kept outside the

    system tablespace.

    7- Write short notes on-

  • 7/23/2019 MySQL Questions Answers

    99/113

    i) Mysqlhtocopy

    Answer-

    mysqlhotcopy

    A Perl scriptrequires the

    DBI module to

    be installed.

    It runs on Unix

    and NetWare.

    Works fo

    MyISAMtables but no

    InnoDB tables

    Operation o

    mysqlhotcopy

    is fast

    Steps

    involved:

  • 7/23/2019 MySQL Questions Answers

    100/113

    mysqlho

    connects

    to the

    localMySQL

    server

    locks the

    tables so

    that the

    server

    will nochange

    them,

    flushes

    the

    tables to

    make

    sure tha

  • 7/23/2019 MySQL Questions Answers

    101/113

    any

    pending

    changes

    arewritten

    to disk

    Copies

    the table

    files.

    When i

    hasfinished

    the copy

    operatio

    it

    unlocks

    the

    tables.

  • 7/23/2019 MySQL Questions Answers

    102/113

    Many options

    invoke it with

    the --help

    option.Some

    examples:

    Back up the

    world

    database to a

    directory

    named worldin the

    /var/archive

    directory:shell>mysqlhoworld/var/ar

    Back up only

  • 7/23/2019 MySQL Questions Answers

    103/113

    the tables in

    the world

    database

    whose namecontains

    Country:shell>mysqlhoworld./var/ar

    ii) InnoDB Hot Backup

    Answer-

    InnoDB Hot Backup

    The InnoDB

    Hot Backupprogram

    (ibbackup) is a

    commercial

    product

  • 7/23/2019 MySQL Questions Answers

    104/113

    available from

    Innobase Oy.

    It can back up

    InnoDB tablewhile the

    server i

    running

    without

    disturbing

    normal

    databaseactivity.

    Its available

    for Unix and

    Windows.

    iii) Mysqldump

    Answer-

    Mysqldump

  • 7/23/2019 MySQL Questions Answers

    105/113

    It can dump

    all databases

    specific

    databases, ospecific tables

    mysqldump

    can back up

    local o

    remote

    servers,

    although thedestination

    for the dump

    files depend

    on how you

    invoke it.

    It works fo

    tables created

  • 7/23/2019 MySQL Questions Answers

    106/113

    by any storage

    engine.

    Output file

    are written intext forma

    and are

    portable.

    Three genera

    modes o

    operation:

    Default:shell>mysqlduworld

    >world.sshell>mysqldu

    world

  • 7/23/2019 MySQL Questions Answers

    107/113

    CityCountry>

    city_co--databases

    optionshell>

    mysqldu--databasworld

    test>world_a

    --all-databases

    option

    shell>mysqldu--all-databas

    >

  • 7/23/2019 MySQL Questions Answers

    108/113

    alldb.s

    iv) Backing Up Log and

    Status Files

    Answer-Backing Up Log and

    Status Files

    In addition to backing up

    the databases, we

    should also back up the

    following files:

    Binary logfiles.

    Necessary

    because the

    binary logstore update

    that have

    been made

  • 7/23/2019 MySQL Questions Answers

    109/113

    after the

    backup wa

    made.

    Option fileused by the

    server (my.cn

    and my.in

    files). These

    files contain

    configuration

    informationthat must be

    restored afte

    a crash.

    Replication

    slave server

    create a

    master.info

  • 7/23/2019 MySQL Questions Answers

    110/113

    file tha

    contains

    information

    needed foconnecting to

    the maste

    server, and a

    relay-log.info

    file tha

    indicates the

    currentprogress in

    processing the

    relay logs.

    Replication

    slaves create

    data files fo

    processing

  • 7/23/2019 MySQL Questions Answers

    111/113

    LOAD DATA

    INFILE

    statements.

    These files arelocated in the

    directory

    named by the

    slave_load_tm

    system

    variable,

    which can beset by starting

    the serve

    with the -

    slave-load-

    tmpdir option

    If

    slave_load_tm

  • 7/23/2019 MySQL Questions Answers

    112/113

    is not set, the

    value of the

    tmpdir system

    variableapplies. The

    data files to

    back up have

    names

    beginning

    withSQL_LOAD

    To back up thepreceding

    files, you can

    use norma

    file system

    operation.

    Static file

    such as option

  • 7/23/2019 MySQL Questions Answers

    113/113

    files can be

    backed up

    with no

    specialprecautions.

    Dynamic file

    such as log

    that the serve

    changes as i

    runs are bes

    backed upwith the