The INSERT Statement

Part of the You Can Learn SQL series.
By Ken Brown
Editor, YouCanLearnSeries.com
Updated:
October 29, 2004

This is a two part article on the INSERT Statement
INSERT Statement, Simple Inserts and Basic Inserts.
INSERT Statement, Using a SELECT statement to insert values.

4. Insert information with fewer columns than in the table.

Sometimes your table will contain multiple columns. You may not have all the information you need at the time you want to create the row to populate all the columns that are available. That's okay. As long as you define the column or columns you want to populate and provide a value for each column you'll be okay.

Imagine you have a table with four columns, Col1, Col2, COl3, and Col4. Say you have information for two of the four columns. You can successfully insert a new row into the table using the following command.


The only caveat you have to be concerned with is the columns you don't populate must allow NULL values. Otherwise you will receive an error message similar to "NUll values not allowed." and the INSERT will fail. At that point you have to identify what columns failed and provide a value for those columns before you can successfully insert that row.

5. A table with an identity column

Sometimes you will encounter a table that has an identity column. That is a column that is populated with the "next" available number. The SQL server will normally insert the number into the row you create automatically, so you don't have to be concerned with it.

If you have a table with three columns, identity column, col1 and col2. Then you only have to insert values into col1 and col2. The identity column will be populated automatically with the next appropriate value when you execute the INSERT statement.


6. Using a SELECT statement to insert values

Sometimes you want to create new new tables and then populate the new table with values from an older table. If you create a new table called users that contains only name and email address. If you already have a table that contains those fields, then you can load the new table with a SELECT statement from the old table.


Every row in oldtable will be read, the name and email will be extracted and then inserted into the new users table.

There you have six useful ways to use the INSERT statement to add rows of information into your tables.

Happy programming.

Go To Page  1 / 2

Save up to 40% (468x60 white)
Keep It Cool Polo Shirt

Great Gifts For Gardeners

These Great Gifts For Gardeners are sure to be a hit with the garden lovers in your life. Choose the Keep It Cool polo shirt or the I Fear No Weed embroidered shirt or even the fun Anvil Pique all cotton Papa Was A Bad Seed polo shirt.

Each unique image was created especially for You Can Learn Series and your loved one will truly treasure the wonderful comments they receive when they wear the shirts.

Now check out the You Can Learn Series Online Store. Coffee mugs, t-shirts and caps.

Home| About Us | NewsLetters | Links| Contact Us |

Copyright © 2004-2012 You Can Learn Series

Hosted Exchange
Reduce Costs; Improve Uptime with Hosted Email
Sacramento Computer Support
Computer and Technical Support
Better Public Schools
Why America's Public Schools are the Best Place for Kids
You Can Learn Series - Life is a Symphony of Experiences
You Can Learn Series Home Page You Can Learn Series
You Can Learn Series Home Page Home      Web DirectoryWeb Directory      About the creators of YouCanLearnSeriesAbout Us      Contact YouCanLearnSeriesContact Us      Site map of YouCanLearnSeriesSite Map      Privacy Policy of You Can Learn SeriesPrivacy Policy       Terms of Use of You Can Learn Series web siteTerms of Use       YouCanLearnSeries RSS feed for SQL tips and tutorials     
C# and ASP.Net Programming TipsC# and ASP.Net      SQL Tutorial, tips and tricks for DBA'sSQL       Landscaping Tips for Home GardenersLandscaping       WeightLifting Tips for the Young and OldWeightLifting       Good health TipsGoodHealth       Learn about Great Travel DestinationsTravel     Web Business TipsWeb Business      Services Available from You Can Learn SeriesServices    



You Can Learn C# and ASP.Net SeriesC# and ASP.Net

You Can Learn SQL SeriesSQL
  Data Definition LanguageData Definition Language
  Data Manipulation LanguageData Manipulation Language
  What is a DatabaseWhat is a Database
  What is SQLWhat is SQL
  SELECT StatementSELECT Statement
  INSERT StatementINSERT Statement
  DELETE StatementDELETE Statement
  UPDATE StatementUPDATE Statement


You Can Learn Landscaping and Gardening SeriesLandscape Tips
You Can Learn WeightLifting SeriesWeightLift Tips
You Can Learn Good Health SeriesGood Health Tips
You Can Learn Series NewslettersNewsletters
Learn about Great Travel DestinationsTravel
You Can Learn Series Web Business TipsWeb Business Tips
Services Available from You Can Learn SeriesServices
Money Saving Coupons from You Can Learn SeriesCoupons

Shop the one-cent sale at SpringHillNursery.com!


Americas Public Schools

Sunset Midwestern Landscaping Book