To visit my site

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#


To contact us

© 2002 - 2015 All Rights Reserved Total Application Works



  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
  • <html>
    <head>
    <title>Example of a link </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/SQL/MY_SQL_INSERT.html">
    Lists and their Tags </a>
    </table>
    </body>
    </html>




SQL - Insert Keyword

Objectives

The objective of the lesson is to describe the SQL INSERT INTO command and present examples that show how to use it. An example can be found at Tutorial: Basic Client/Server Application IV in the source file tcpServer.java found in zip file . The INSERT command is used in the source file tcpServer.java on line 368.

Introduction

So far we have used the SQL SELECT command to retrieve data from a database. Once data has been retrieved, it can be displayed or edited within an application program. However, a question arises: "How does data get into a database?" In this tutorial, we will describe how data is entered into a database using the INSERT INTO command.

Insert Keyword

The SQL insert command is used to insert a row of data into a SQL compliant table. The syntax for this command is:

  
Insert into table1 ( column1, column2, column3)
values ( value1, value2, value3);
 

or
  
Insert into table1 values ( value1, value2, value3);
 

The INSERT INTO statement is used to insert new rows into a table.

In my Basic Client Server Address Book application, I use a Java application to initialize my database (see lines 149 to 190 in referenced application). In this application, I am using the following form of the SQL INSERT INTO

  
Insert into table1 ( column1, column2, column3)
values ( value1, value2, value3);
 

The code looks like:
  
sysPrint("\nReady to initialize database." ) ;
statement = connect.createStatement();

query = "SELECT * FROM addresses "  ;
rs = statement.executeQuery( query );
if ( !rs.next() ) {
   for ( int ii = 0 ; ii < addrRecord.length ; ii++ ) {

      query = "INSERT INTO Addresses (" +
                 "firstname, lastname, address, city, " +
                 "stateorprovince, postalcode, country, " +
                 "emailaddress, homephone, faxnumber" +
                 ") VALUES ('" +
                 addrRecord[ii][ 0 ] + "', '" +
                 addrRecord[ii][ 1 ] + "', '" +
                 addrRecord[ii][ 2 ] + "', '" +
                 addrRecord[ii][ 3 ] + "', '" +
                 addrRecord[ii][ 4 ] + "', '" +
                 addrRecord[ii][ 5 ] + "', '" +
                 addrRecord[ii][ 6 ] + "', '" +
                 addrRecord[ii][ 7 ] + "', '" +
                 addrRecord[ii][ 8 ] + "', '" +
                 addrRecord[ii][ 9 ] + "')";

       // this line sends the SQL commnad to the database
      int result = statement.executeUpdate( query );
      if ( result == 1 )
            display.append("\nThe record was successful in init().") ;

      display.append("\nThe init record written was " +
            addrRecord[ii][ 0 ] +
            addrRecord[ii][ 1 ] +
            addrRecord[ii][ 2 ] +
            addrRecord[ii][ 3 ] +
            addrRecord[ii][ 4 ] +
            addrRecord[ii][ 5 ] +
            addrRecord[ii][ 6 ] +
            addrRecord[ii][ 7 ] +
            addrRecord[ii][ 8 ] +
            addrRecord[ii][ 9 ]  );

   }  // end of for loop  */
}    // End of if-else
  

The result of executing the above code is:

 ID  
 FirstName  
 LastName  
 Address  
 City  
 StateOrProvince  
 PostalCode  
 Country  
 EmailAddress  
 HomePhone  
 FaxNumber  
14JulieSmith123 Oak St.RaleighNC27500USAMyID@MyISP919-555-3782919-555-5782
15MaryEasley123 Walnut St.ApexNC27500USAMyID@MyISP919-555-5552919-555-6552
16CatoWillingham123 Jones St.DurhamNC27500USAMyID@MyISP919-555-3492919-555-5492
17MikeJordan123 Saunders St.ApexNC27500USAMyID@MyISP919-555-3882919-555-5882
18CaroleFusemi123 Oak LaneRaleighNC27500USAMyID@MyISP919-555-3472919-555-5472
19PeterWie123 Oak WayGarnerNC27500USAMyID@MyISP919-555-3642919-555-5642
20DonaldHill123 Oak Blvd.CaryNC27500USAMyID@MyISP919-555-3452919-555-5452
21ReginaBell123 Walnut LaneApexNC27500USAMyID@MyISP919-555-3342919-555-5342
22AltheaKeyes123 Walnut WayRaleighNC27500USAMyID@MyISP919-555-3332919-555-5332
23CharlesLawrence123 Whatis St.GarnerNC27500USAMyID@MyISP919-555-3222919-555-5222
24RobertUrengo123 Academy St.DurhamNC27500USAMyID@MyISP919-555-3112919-555-5112
25JohnWieland123 MaIn St.ApexNC27709USAMyID@MyISP(919)-444-9088(919)-444-9089
26KeithMakey404 Walnut Ave.RaleighNC27511USAMyID@MyISP(919)-555-6789(919)-444-9089

Summary

  • Use the command INSERT INTO to add a row of data to a database. It is of the format:
      
    Insert into table1 ( column1, column2, column3)
    values ( value1, value2, value3);
     
    or
      
    Insert into table1 values ( value1, value2, value3);
     

  • You must specify the values to be inserted.
  • When performing an insert, enclose character values in single quotes.
  • Do not enclose numeric values in single quotes.












[
HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2015 All Rights Reserved Total Application Works