Library tutorials & articles

SQL In Simple English

Inserting, Deleting & Updating

What is the INSERT statement? How do I use it?
The INSERT statement lets you insert information into a database. A few examples are shown below

INSERT INTO people VALUES ('Bush', 'George', 47 , 'White House', 'Washington')
Would insert a new row at the bottom of the table people consisting of the values in parentheses in the above statement.

INSERT INTO people (lastname, city) VALUES ('Gates', 'Redmond')
Would insert a new row at the bottom of the table people consisting of only 2 values as present in the above statement, namely 'Gates' and 'Redmond'. The remaining columns for that particular record would be left empty (null).

Note : A null value is different from 0 or ''(Empty String). A perfect example of this would be a column describing the hair colour for many people. In case the person is bald then the value of the colour should be null rather than empty. This would be perfect from the database design view. A particular entity which doesn't exist should be represented similarly and not by empty Strings.


How do I delete a record from a database?
Use the DELETE statement to remove records or any particular column values from a database.

DELETE FROM people WHERE lastname = 'Hunter'
Would remove the entire record which represents any person whose lastname is 'Hunter'. In our case it would remove 1 record from the sample database table people. It would remove all the values that were a part of that record.


Is there a way to update any record in a database?

Yes. You could use the UPDATE statement. The update statement updates (or replaces) those values that were specified in the SQL statement with the new values provided.

UPDATE people SET age = 50, city = 'Mumbai' WHERE (lastname = 'Hunter' AND firstname='Jason')
Would change Jason Hunter's age from 41 to 50 and would make him shift his residence from 'San Jose' to 'Mumbai'. Isn't that cool?? A new Java Guru is Mumbai !!

Comments

  1. 10 Jun 2003 at 14:57

    Hi I would first like to say your site has been really helpful to me.


    When using ASP to display database information in a web page you might use


    [courier new]
    Set adoConnect = Server.CreateObject("ADODB.Connection")
    adoConnect.Open "Reports"
    Set onIndex = Server.CreateObject("ADODB.Recordset")


    strSQL = "SELECT * FROM index WHERE number=10"


    onIndex.Open strSQL, adoConnect
    [/courier new]


    Then if you wanted to display the recordset you could use


    [courier new]
    <% onIndex=("Number") %>
    [/courier new]


    but what I can't work out is if your select statement uses COUNT how do you display the result


    [courier new]
    Set adoConnect = Server.CreateObject("ADODB.Connection")
    adoConnect.Open "Reports"
    Set onIndex = Server.CreateObject("ADODB.Recordset")


    strSQL = "SELECT COUNT(*) FROM index WHERE number=10"


    onIndex.Open strSQL, adoConnect
    [/courier new]

  2. 06 Mar 2002 at 09:17

    sorry probably it has been too long  and may be you've got the answer but try this
    select  * from table where date> #2/2/1979#

  3. 26 Feb 2002 at 12:35

    hi..
    check this ... should work


    select * from tablename where DateOfBirth<TODATE('10/9/1976','mm/dd/yyyy')


    try using TO_DATE with the date you want.. i have used this before in this manner..


    hope it helps


    bye

  4. 26 Feb 2002 at 06:47

    Thanks, very helpful.


    What about dates though?  What is the code for finding dates using the SELECT statement eg 'SELECT from * WHERE DateOfBirth < 10/9/76 10:01:10', I can't get it to work.

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of SQL In Simple English.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • Stack Overflow: Podcast #31

    This is the thirty-first episode of the StackOverflow podcast, where&#10;Joel and Jeff discuss.. stuff!&#10;&#10;Based on some comments from Podcast #30, we now know that &ldquo;Learning about NP-Completeness from Jeff is like learning about irony from Alanis Morissette&rdquo;. It&rsquo;s funny b...

Related jobs

Events coming up

  • Jun 16

    Code Generation 2009

    Cambridge, United Kingdom

    A developer event with a practical focus on helping people get to grips with code generation tools and technologies.