Top Banner

of 42

Mysql Informational Document

Apr 05, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/31/2019 Mysql Informational Document

    1/42

    Introduction to Databases

    Client-serverInstalling Mysql

  • 7/31/2019 Mysql Informational Document

    2/42

    What are databases

    A database is a collection of related data

    It represents some aspects of the real world.

    It is a logically coherent collection of data.

    It is designed for specific purpose and intended users.

    It can be at varying size and complexity

    It can be generated and maintained manually

  • 7/31/2019 Mysql Informational Document

    3/42

    What are databases

    A database managementsystem (DBMS) consistsof these components:

    Database applicationsare often built on aclient-server model.

    Stored databasedefinitions (meta-

    data)

    Software to access stored data

    Software to process queries

    Application programs/queries

    DBMSsoftware

    DBMS

    system

    Stored database

    Users/Programmers

    clients

    server

  • 7/31/2019 Mysql Informational Document

    4/42

    The clients and the server

    Source: http://infomotions.com/musings/waves/clientservercomputing.html

  • 7/31/2019 Mysql Informational Document

    5/42

    The clients and the server

    The client's responsibility includes:

    1. Handle the user interface.

    2. Translate the user's request into

    the desired protocol.3. Send the request to the server.

    4. Wait for the server's response.

    5. Translate the response into"human-readable" results.

    6. Present the results to the user.

    The server's functions include:

    1. Listen for a client's query.

    2. Process that query.

    3.

    Return the results back tothe client.

    Source: http://infomotions.com/musings/waves/clientservercomputing.html

  • 7/31/2019 Mysql Informational Document

    6/42

    Database client: Interfacing (input)

    Example: When you go to the NVCC library to do an article search

    This is the

    interface thatallows user tospecify thesearch criteria:

  • 7/31/2019 Mysql Informational Document

    7/42

    Database client: Interfacing (input)

    Example: When you go to the NVCC library to do an article search

    Search for mysql as a keyword anywhere in

    the Northern Virginia CC database, in alllanguages and in all years, and in all formats

  • 7/31/2019 Mysql Informational Document

    8/42

    Database client: Communicationwith the server

    Example: When you go to the NVCC library to do an article search

    Client converts the informationto a database query language

    and send to the server (usuallyacross the internet)

    Search for mysql as a keyword anywhere in

    the Northern Virginia CC database, in alllanguages and in all years, and in all formats

    SELECT * FROM Northern_Virginia_CC_database

    WHERE keyword=mysql

    This is the actual stuff being sent out to the DBMS to do the search

    This is what the user means to find:

  • 7/31/2019 Mysql Informational Document

    9/42

    Database client: Interfacing (output)

    Example: When you go to the NVCC library to do an article search

    Server returnsa list of resultsto the client.

    The clientdisplays the

    results in ahuman-friendlyformat

  • 7/31/2019 Mysql Informational Document

    10/42

    Database client: Interfacing (output)

    Example: When you go to the NVCC library to do an article search

    The clientdisplays thedetails of arecord in thesearch result ina human-friendly format

  • 7/31/2019 Mysql Informational Document

    11/42

    The whole process of a databasequery

    Example: When you go to the NVCC library to do an article search

    User enterssearch criteria

    Database managementsystem at the server

    Client displays

    search resultto the user

    SELECT * FROM

    Northern_Virginia_CC_database

    WHERE keyword=mysql

    Client convertssearch criteria to aquery and sends it tothe server

    We are going tolook at the thingshappening at theserver behind the

    scene here.

  • 7/31/2019 Mysql Informational Document

    12/42

    Mysql

    A freeware

    Two main components:

    The server (called mysql-server) The client (called mysql-client)

    We are interested primarily in the server.

  • 7/31/2019 Mysql Informational Document

    13/42

    How to install mysql-server inubuntu (internet required)

    If you have installed ubuntu, this is pretty straightforward:

    1. Open a terminal by pressing Ctrl-Alt-T (on a windows keyboard) orCtrl-Option-T (on a mac keyboard)

    2. Type: sudo apt-get install mysql-server

    You will need to enter the administrater password (that is thepassword for your ubuntu account) to do this installation.

    https://help.ubuntu.com/11.04/serverguide/C/mysql.html

    A video to show how to install mysql-server in ubuntu:

    http://www.youtube.com/watch?v=bt2jACLDLmE

    https://help.ubuntu.com/11.04/serverguide/C/mysql.htmlhttps://help.ubuntu.com/11.04/serverguide/C/mysql.html
  • 7/31/2019 Mysql Informational Document

    14/42

    Useful tools on ubuntu

    To open a terminal:Open a terminal by pressing Ctrl-Alt-T (on a windows

    keyboard) or Ctrl-Option-T (on a mac keyboard)

    To edit a text file:

    pico (In pico, the symbol ^ means the controlkey. So ^G means control-G)

    For more advanced users: vi

  • 7/31/2019 Mysql Informational Document

    15/42

    Databases in Mysql

    To show, create, use and drop databases:

    To show all the databases accessible to you:show databases;

    To create a database called cars:create database cars;

    To use the database called cars:use cars;

    To drop a database called cars:drop database cars;

    All commands endwith semicolon ;

  • 7/31/2019 Mysql Informational Document

    16/42

    Getting mysql

    At the ubuntu xterm prompt, type:

    sudo apt-get install mysql-server

    .

  • 7/31/2019 Mysql Informational Document

    17/42

    Starting mysql

    At the ubuntu xterm prompt: mysql -u root (require password)

    You will see the prompt:mysql>

    .

  • 7/31/2019 Mysql Informational Document

    18/42

    Show all databases

    To see the databases on the server, type command:

    show databases;

    These three are default databases.There is nothing else.

  • 7/31/2019 Mysql Informational Document

    19/42

    Create a database

    To create a database called fruits on the server, type command:

    create database fruits;

    There is now one more database,

    called fruits.

  • 7/31/2019 Mysql Informational Document

    20/42

    Use a database (must be created already)

    To use the database called fruits that is already created on theserver, type command:

    use fruits;

    You can use the following command to see what is inside the fruitsdatabase:

    show tables;

    There is nothing in the fruitsdatabase.

  • 7/31/2019 Mysql Informational Document

    21/42

    Create a table within adatabase

    A database is a collection of tables.

    Example: A database about fruits

    Nutritiontable

    Fruits database

    CultivationtableRecipe

    table

    The nutrition table may containthe following information:

    Fruit name, Calories per fruit, Sugar per fruit, Vitamin A content, Vitamin C content,

  • 7/31/2019 Mysql Informational Document

    22/42

    Create a table within adatabase

    A database is a collection of tables. To create a new table callednutrition in the fruit database, type command:

    create table nutrition (name varchar(20),

    calories int, sugar int, vitaminA int, vitamic

    int);

    There is now one table in the fruitsdatabase.

  • 7/31/2019 Mysql Informational Document

    23/42

    To see the description of a table

    To see the description of the nutrition table in the fruit database,type command:

    describe nutrition;

    There are 5 fields in thenutrition table.

  • 7/31/2019 Mysql Informational Document

    24/42

    To insert an entry into a table

    Suppose we have the following information about thenutrition of apple and we want to insert an entry it intothe nutrition table of the database.

    Apple (one fruit) contains:

    95 Calories

    19g of sugars

    2% of an adults daily need of vitamin A 14% of an adults daily need of vitamin C

  • 7/31/2019 Mysql Informational Document

    25/42

    To insert an entry into a table

    This command insert an entry called apple into the table:

    insert into nutrition values(apple, 95, 19, 2, 14);

    You can span yourcommand over multiplelines. It is terminated only bythe semicolon ;

  • 7/31/2019 Mysql Informational Document

    26/42

    To see all entries in a table

    This command shows all the entries in the nutrition table:

    select * from nutrition;

    The nutrition table has onlyone entry, which is apple.

    The * means everything.

  • 7/31/2019 Mysql Informational Document

    27/42

    Entering data into the database

    There are a few ways to enter data into a table of adatabase.

    Entering one entry at a time

    Loading a whole file of entries

    Suppose you want to enter large quantity of data, youmay have the data stored in a file, and then load it into

    mysql. We are going to show how to do that. First, you must quit mysql.

    Then you can use pico to edit the file.

    You can load the file in mysql when you are done.

  • 7/31/2019 Mysql Informational Document

    28/42

    To quit an interactive sessionwith mysql

    To quit an interactive session with the mysql server, typecommand:

    quit;

    Now the prompt changes back to the linux xterm prompt.

  • 7/31/2019 Mysql Informational Document

    29/42

    To edit a file of data entries

    You can edit a file of data entries using pico. The data file needs tofollow this format:

    One data entry per line

    Each field must be separated by a tab

    Do not leave any empty lines after the

    last line. Make sure your cursor cannotmove past here when you are done.

    Name this file: fruit-nutrition.txt

  • 7/31/2019 Mysql Informational Document

    30/42

    Permanence of data in thedatabase

    Data entered into a database arepermanent. If you quit now, and come

    back later, you can still see the data. Now let us return to mysql to see that.

  • 7/31/2019 Mysql Informational Document

    31/42

    To return to mysql

    To return to mysql, type:mysql -u root

    Then, we can type use fruits, and then check the table.

    Once inside mysql, we canuse these commands tocheck the database

    We can see that

    the nutrition tableis still there, andthe apple entry isstill in the table.

  • 7/31/2019 Mysql Informational Document

    32/42

    Adding and deleting data in atable

    Suppose we want to delete all the existing datain the nutrition table, and then load the tablewith the data from the file called fruit-nutrition.txtthat we created.

    We need to do this in two steps.

    First, delete existing entries in the table.

    Second, load the data from the file into the table.

  • 7/31/2019 Mysql Informational Document

    33/42

    Deleting data in a table

    To delete all data entries from the nutrition table, type command:

    delete from nutrition;

    We can see that the nutrition

    table is now empty.

  • 7/31/2019 Mysql Informational Document

    34/42

    Loading a file of data into atable

    To load a file (called fruit-nutrition.txt) of data entries into the nutrition table, typecommand:

    load data local infile fruit-nutrition.txt into table

    nutrition;

    We can see that

    the nutrition tableis now loadedwith data fromthe file.

  • 7/31/2019 Mysql Informational Document

    35/42

    To add an entry into an existingtable

    This command insert an entry called aple into the table:

    insert into nutrition values(aple, 95, 19, 2, 14);

    (Note: the name aple is an intentional misspell of apple)

    The newentry is atthe bottom

  • 7/31/2019 Mysql Informational Document

    36/42

    To update an entry in anexisting table

    There are a few ways to correct or update adata entry in the table:

    Delete the entry and enter a new one

    Change the field of an entry

  • 7/31/2019 Mysql Informational Document

    37/42

    To delete an entry in an existingtable

    This command deletes the entry aple from the nutrition table:

    delete from nutrition where name=aple;

    The aple

    entry is gone.

  • 7/31/2019 Mysql Informational Document

    38/42

    To reinsert an entry into a table

    This command insert a new entry of apple into the table:

    insert into nutrition values(apple, 95, 19, 2, 14);

    The new entryof apple entry

    is added.

  • 7/31/2019 Mysql Informational Document

    39/42

    To change the field of an entryin a table

    This command changes the vitaminC field of apple from 14 to 20:

    update nutrition set vitaminC=20 where name=apple;

    The new valueof applesvitaminC filedis 20.

  • 7/31/2019 Mysql Informational Document

    40/42

    To display selected fields

    This command displays the fruits by their names and vitaminCcontents, in sorted order of the vitaminC field:

    Select name, vitaminC from nutrition order by vitaminC;

  • 7/31/2019 Mysql Informational Document

    41/42

    To select fields with constraints

    This command select the names of those fruits that have theirvitaminC fields greater than value 100:

    Select name from nutrition where vitaminC>100;

  • 7/31/2019 Mysql Informational Document

    42/42

    More tutorials on mysql

    For more examples on mysql see:

    MySQL 5.6 Reference Manual: Chapter 3 Tutorial

    http://dev.mysql.com/doc/refman/5.6/en/tutorial.html

    http://dev.mysql.com/doc/refman/5.6/en/tutorial.htmlhttp://dev.mysql.com/doc/refman/5.6/en/tutorial.html