MySQL
  Home arrow MySQL arrow Page 4 - Beginning MySQL Tutorial
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Beginning MySQL Tutorial
By: W.J. Gilmore
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 218
    1999-04-03

    Table of Contents:
  • Beginning MySQL Tutorial
  • Part 1: At First Glance
  • Part 2: Datatypes and Tables
  • Part 3: Manipulating the Database
  • Part 4: Advanced MySQL Commands

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Beginning MySQL Tutorial - Part 3: Manipulating the Database


    (Page 4 of 5 )

    A database can be manipulated in four possible ways: addition, deletion, modification, and search. These topics will all be briefly covered in the following two sections. However, before we begin, I would like to highlight the fact that SQL, like many computer languages, is somewhat particular about command syntax. The slightest error in placement of a parentheses, comma, or semicolon will almost surely end in error. As a result, take care to be attentive of command syntax.

    Insertion of records

    Note: The originally created table, test, created in the last section will be used to illustrate the examples in this section. Here it is again, for quick reference:


    mysql> CREATE TABLE test ( > name VARCHAR (15), > email VARCHAR (25), > phone_number INT, > ID INT NOT NULL AUTO_INCREMENT, > PRIMARY KEY (ID));

    Insertion of data into the table is accomplished, logically enough, using the INSERT command.


    mysql> INSERT INTO test VALUES mysql> ('Bugs Bunny', 'carrots@devshed.com', mysql> 5554321, NULL);

    Result, assuming the command was correctly entered:


    Query OK, 1 row affected (0.02 sec) mysql>

    So what happened?

    • Single quotations were placed around the datatypes VARCHAR. All datatypes of type STRING (i.e. char, varchar, text, blob, etc.) must be surrounded in single quotes, or an error will occur.
    • There were no single quotes surrounding the phone number. Datatypes of type INT do not require single quotes.
    • NULL ? A NULL allows any datatype with the characteristic AUTO_INCREMENT to be automatically assigned a value. If it is the first record inserted into the database, it is assigned the value '1'. Otherwise, it is assigned the previously inserted value + 1 (i.e. if the previously inserted value was '2', then the next would be '3'). In addition, the insertion of NULL into a variable of type TIMESTAMP causes that variable to be given the value of the current date.

    Note: It is of importance to remember that the same number of values must be inserted as datatypes are contained within a record. In the above example, if one attempted to insert only three values instead of four, the insertion would fail. The same result applies if one attempted to insert five values.

    Example:


    mysql> insert into test values('doggy'); ERROR 1058: Column count doesn't match value count mysql>

    Note (2): One of the advantageous aspects of MySQL is it's ability to convert without trouble between datatypes. MySQL automatically converts between integers, strings, and dates without problems.

    Selection

    A database would not be much use if one was not able to search and extract data from it. In MySql terms, this is accomplished through the SELECT statement.


    mysql> SELECT * FROM test mysql> WHERE (name = "Bugs Bunny");
    Result:

    nameemailphoneID   
    Bugs Bunnycarrots@devshed.com55543211

    Let's assume we have inserted four differing records, all bearing the same name of "Bugs Bunny", yet having different email addresses and phone numbers. The table test, would look somewhat like the following:

    nameemailphoneID   
    Bugs Bunnycarrots@devshed.com55543211
    Bugs Bunnypeppers@devshed.com55543312
    Bugs Bunnylettuce@devshed.com55543413
    Bugs Bunnycelery@devshed.com55543514

    Deletion

    One can also delete records inserted into the table. This is accomplished through the DELETE command.


    mysql> DELETE FROM test mysql> WHERE (name = "Bugs Bunny");

    Result:
    This would result in the deletion of all records within the table test containing name "Bugs Bunny".

    Another example:


    mysql> DELETE FROM test mysql> WHERE (phone_number = 5554321);

    Result: (Using the previously illustrated example)

    nameemailphoneID   
    Bugs Bunnypeppers@devshed.com55543312
    Bugs Bunnylettuce@devshed.com55543413
    Bugs Bunnycelery@devshed.com55543514

    Modification

    MySQL also has the capability of modifying data already entered into the table. This is accomplished through the UPDATE command.


    mysql> UPDATE test SET name = 'Daffy Duck' mysql> WHERE name = "Bugs Bunny";

    nameemailphoneID   
    Daffy Duckpeppers@devshed.com55543312
    Daffy Ducklettuce@devshed.com55543413
    Daffy Duckcelery@devshed.com55543514

    This section, we covered the core MySQL database manipulation functions, basic insertion, deletion, modification, and search. The next section will elaborate on these capabilities, providing extended functioning and flexibility when manipulating the database.

    More MySQL Articles
    More By W.J. Gilmore


       · A very simple and quick introduction to MYSQL, a useful reference for any...
       · Thanks for the help. I have been trying to find a cut and dried MySQL tutorial for...
       · its really helpful. it is very clear and easily understandable. thanks for this...
     

       

    MYSQL ARTICLES

    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT