Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ADO.NET : SQL In Simple English - Part 1
 

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 !!

Are there better ways to use SELECT?
Yes there are.. and now you will learn some of the better ways of using the SELECT along with some other SQL terms such as DISTINCT , ORDER , MAX , MIN , AVG , etc..

For all the examples in this article we would be using a sample database table which is shown below :

Table Name : people
lastname firstname age address city
Gates Anthony 11 Circuit City Bangalore
Hunter Jason 41 Oak Street San Jose
Gates Bill 59 Microsoft Corp. Redmond
Kanetkar Yashwant 38 Rajabhai Street Nagpur

How could I get distinct entries from a table?
The SELECT statement in conjunction with DISTINCT lets you select a set of distinct values from a table in a database. The values selected from the database table would ofcourse depend on the various conditions that are specified in the SQL query. Here are some ways to use the DISTINCT keyword.

SELECT DISTINCT lastname FROM people

Would return a recordset with 3 records. Each record would have 1 value in it. So basically the first record would contain 'Gates', the second would contain 'Hunter' and the third would contain 'Kanetkar'. Inspite of the lastname 'Gates' being present twice in the table, only one occurrence of it will be considered since the DISTINCT keyword was used in the SQL statement.

Is there a way to get the results of a Query sorted in any order?
Yes there are ways which will sort the results and return the sorted results to your program.. thus saving you the pain of carrying out the sorting yourself. The ORDER BY keyword is used for sorting.

SELECT firstname, age, city FROM people ORDER BY firstname

Would return a recordset with 4 records. Each record would have 3 values corresponding to firstname, age and city. But the specialty of this statement is that the records would be sorted according to the firstname in ascending alphabetical order (A first - Z last).e.g. The first record would be that corresponding to the person whose firstname is 'Anthony' , followed by 'Bill' and then 'Jason' and finally 'Yashwant'.

SELECT firstname, age, city FROM people ORDER BY firstname DESC

Would return a recordset with 4 record as the above case, but this time the records would be in the reverse order. Namely the first record would be 'Yashwant' and the fourth one would be 'Anthony'.

How can I find the total number of records in a table?
You could use the COUNT keyword in many ways.. here are some ways.

SELECT COUNT(*) FROM people WHERE age > 40

Would return a recordset consisting of 1 value. Contrary to previous SQL statements the COUNT statement return one value which directly indicates the total number of records existing in the database that fulfill your conditions e.g. In our case the above statement would return a value of 2.

SELECT COUNT(city) FROM people

Would return a recordset consisting of 1 value. And that value would be equal to 4. The important point to note is that this statement return the total number of Non-Null entries only.

SELECT DISTINCT COUNT(lastname) FROM people

Would return a recordset consisting of 1 value. And that value would be equal to 3. Remember that when you use the COUNT keyword you do not get the actual lastname of the persons but you only get the total number of records that exist in the database that match your requirements. And in this case since DISTINCT was also used it would find the total number of records where there are distinct firstname only.

I heard there is some mathematical stuff in SQL?
Yeah.. there are many simple operations that you could do in order to formulate some useful information from a database rather than getting simple records from the database. Here are a few examples of these mathematical operations.

SELECT AVG(age) FROM people

Would return 1 value corresponding to the average age of all the persons that exist in the table people.

SELECT AVG(age) FROM people WHERE age > 30

You should be able to figure that out yourself.. if not please start reading right from the first article in this series ;-)

SELECT MAX(age) FROM people

Returns the maximum age among all the persons in the table people.

SELECT MIN(age) FROM people

Returns the minimum age among all the persons in the table people.

SELECT SUM(age) FROM people WHERE age > 20

Returns the total sum of all the ages of the persons whose age is above 20 from the table people.

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 !!

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 !!

Are there better ways to use SELECT?
Yes there are.. and now you will learn some of the better ways of using the SELECT along with some other SQL terms such as DISTINCT , ORDER , MAX , MIN , AVG , etc..

For all the examples in this article we would be using a sample database table which is shown below :

Table Name : people
lastname firstname age address city
Gates Anthony 11 Circuit City Bangalore
Hunter Jason 41 Oak Street San Jose
Gates Bill 59 Microsoft Corp. Redmond
Kanetkar Yashwant 38 Rajabhai Street Nagpur

How could I get distinct entries from a table?
The SELECT statement in conjunction with DISTINCT lets you select a set of distinct values from a table in a database. The values selected from the database table would ofcourse depend on the various conditions that are specified in the SQL query. Here are some ways to use the DISTINCT keyword.

SELECT DISTINCT lastname FROM people

Would return a recordset with 3 records. Each record would have 1 value in it. So basically the first record would contain 'Gates', the second would contain 'Hunter' and the third would contain 'Kanetkar'. Inspite of the lastname 'Gates' being present twice in the table, only one occurrence of it will be considered since the DISTINCT keyword was used in the SQL statement.

Is there a way to get the results of a Query sorted in any order?
Yes there are ways which will sort the results and return the sorted results to your program.. thus saving you the pain of carrying out the sorting yourself. The ORDER BY keyword is used for sorting.

SELECT firstname, age, city FROM people ORDER BY firstname

Would return a recordset with 4 records. Each record would have 3 values corresponding to firstname, age and city. But the specialty of this statement is that the records would be sorted according to the firstname in ascending alphabetical order (A first - Z last).e.g. The first record would be that corresponding to the person whose firstname is 'Anthony' , followed by 'Bill' and then 'Jason' and finally 'Yashwant'.

SELECT firstname, age, city FROM people ORDER BY firstname DESC

Would return a recordset with 4 record as the above case, but this time the records would be in the reverse order. Namely the first record would be 'Yashwant' and the fourth one would be 'Anthony'.

How can I find the total number of records in a table?
You could use the COUNT keyword in many ways.. here are some ways.

SELECT COUNT(*) FROM people WHERE age > 40

Would return a recordset consisting of 1 value. Contrary to previous SQL statements the COUNT statement return one value which directly indicates the total number of records existing in the database that fulfill your conditions e.g. In our case the above statement would return a value of 2.

SELECT COUNT(city) FROM people

Would return a recordset consisting of 1 value. And that value would be equal to 4. The important point to note is that this statement return the total number of Non-Null entries only.

SELECT DISTINCT COUNT(lastname) FROM people

Would return a recordset consisting of 1 value. And that value would be equal to 3. Remember that when you use the COUNT keyword you do not get the actual lastname of the persons but you only get the total number of records that exist in the database that match your requirements. And in this case since DISTINCT was also used it would find the total number of records where there are distinct firstname only.

I heard there is some mathematical stuff in SQL?
Yeah.. there are many simple operations that you could do in order to formulate some useful information from a database rather than getting simple records from the database. Here are a few examples of these mathematical operations.

SELECT AVG(age) FROM people

Would return 1 value corresponding to the average age of all the persons that exist in the table people.

SELECT AVG(age) FROM people WHERE age > 30

You should be able to figure that out yourself.. if not please start reading right from the first article in this series ;-)

SELECT MAX(age) FROM people

Returns the maximum age among all the persons in the table people.

SELECT MIN(age) FROM people

Returns the minimum age among all the persons in the table people.

SELECT SUM(age) FROM people WHERE age > 20

Returns the total sum of all the ages of the persons whose age is above 20 from the table people.

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 !!


Previous ( 1 Gone )( 1 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 1, Comments: 1 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. inserting more fields for a servlet program.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.