Summary: In the previous tutorials you learn different ways to query data from database table by using SQL SELECT statement. Are you wonder that how data is added into those table? In this tutorial, you will learn do it by using SQL INSERT statement.

INSERT Statement

INSERT statement allows you to insert one or more rows to the table. In MySQL, the INSERT statement forms are listed as follows:

1 INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
2 [INTO] table_name [(column_name,...)]
3 VALUES ((expression | DEFAULT),...),(...),...
1 INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
2 [INTO] table_name [(column_name,...)]
1 INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
2 [INTO] table_name
3 SET column_name=(expression | DEFAULT), ...

 

As you can see INTO in the INSERT statement is optional. In the first form, you insert a new data row into an existing table by specifying the column name and data for each. As an example to insert a new office to the offices table in the sample database you can do as follows:

01 INSERT INTO classicmodels.offices
02 (officeCode,
03 city,
04 phone,
05 addressLine1,
06 addressLine2,
07 state,
08 country,
09 postalCode,
10 territory
11 )
12 VALUES
13 ('8',
14 'Boston',
15 '+1 215 837 0825',
16 '1550 dummy street',
17 'dummy address',
18 'MA',
19 'USA',
20 '02107',
21 'NA'
22 )

In the second form, instead of providing explicit data, you select it from other table by using SELECT statement. This form allows you to copy some or some part of data from other table to the inserted table. As an example, we can create a temporary table and insert all offices which locate in US into that one by using this query:

1 INSERT INTO temp_table
2 SELECT * FROM offices WHERE country = 'US'

The third form enables you to specify the column you want to insert the data. For example, we have the query like this:

1 INSERT INTO productlines
2 SET productLine = 'Luxury Cars'

It means we only insert the data into productLine column in productLines table.

Related posts:

  1. Working with Tables – Part II
  2. Managing Database Index in MySQL
  3. Working with Database Table – Part I
  4. Using MySQL SELECT Statement to Query Data
  5. Changing Table Structure Using MySQL ALTER TABLE

Leave your comment

You must be logged in to post a comment.


Link Exchange Spritual Place for Bhakti | bhaktiguru.com Business Franchise in India | businessfranchiseindia.com Free Directory Submission & Link Submission | ab-directory.com Big Dwarka Business Listing | bigdwarka.com Kolkata Online Business Listing | onlinekolkata.com Patna Online Business Listing | onlinepatna.com Dwarka Online Business Listing | onlinedwarka.com Bihar Online | First Bihar's Portal | bihar-online.com Big Bihar Business Listing | bigbihar.com Dynamic Website Development | dynamicwebsitedeveloper.com Noida Online Business Listing | onlinenoida.com SEO Service Delhi & India | seoservicedelhi.com SEO Experts Delhi & India | seoexpertsdelhi.com Big Patna Business Listing | bigpatna.com Bihar schools Listing Free Schools Ad | biharschools.com Creative Websoft Solutions | Web Development India | creativewebsoft.us Bulk SMS Providers India | bulksmsproviders.net Online cbse Guides | onlinecbseguides.com Big Kolkata Business Listing | bigkolkata.com Handicap Shaadi.com | handicapshaadi.com Handicap Marriage.com | handicapmarriage.com India Home Tutors.com | indiahometutors.com Indian Property Guides.com | indianpropertyguides.com Online Stocks Community.com | onlinestockscommunity.com Free Article Submission | ab-articles.com Secret Dating Online.com | secretdatingonline.com Private DatingOnline.com | privatedatingonline.com Indian businessguides.com | indianbusinessguides.com Software Engineer Delhi | Meenu Khanna Bollywood & Hot Images | hibdy.com Free Computer & Online PHP Training | php2php.com Free Ignou Project & Assignment | ignouinfo.com PHP Programmer India | Web Developer India | bageshsingh.com Creative Websoft Solutions | Web Development IndiaBlog Link Exchange http://www.php2php.com/tutorial-blog/http://bageshsingh.com/bagesh-blog/http://www.bhaktiguru.com/bhaktigurublog/http://www.hibdy.com/desi-masala-blog/