Search
Left Quote    I have not failed. I've just found 10,000 ways that won't work.
- Thomas Alva Edison    
Right Quote
 
[login] | [Register]
 

Introduction to SQL

by: Jester
Page: 1 of 3
(View All)

Introduction


More and more people are getting into web designing, yet many are put off by SQL for reasons which I don't understand. I assure you it is very easy, in this tutorial I'll try to introduce you to the extreme basics of the structured query language.


The Database

In an SQL database there can be many tables, these tables store the data that you want to get at, let's have a look at an example of a simple table. Table: People


ID NAME GENDER LOCATION AGE
1 Bob Male UK 31
2 Jill Female USA 24
3 Joe Male Germany 19
4 Jeff Male France 46
5 Sue Female UK 29

Above we have a representation of a table in an SQL database. You can see why they are named tables. They have rows and columns, each row is a separate line in the database, and each column represents a data group for the table. We can use SQL to retrieve selective data, update and change data, delete data... etc. from this table.


Selecting

Note that the above table is named people, let's have a look at a very simple SELECT statement:




That's it, read it. Select name from, we are telling SQL to select the name column from the people table, what would this select?
Bob, Jill, Joe, Jeff, Sue

Simple enough right? We can select more than one column:




This would select the name column and the gender column, what would this select?
Bob, male, Jill, female, Joe, male, Jeff, male, Sue, female

There is a short-hand way to select all of the columns:




The * means select all columns (id, name, gender, location and age).

Where



We can use the WHERE statement to be selective.




I'm sure you can understand that. Select the name column from the table people where the gender column is equal to male. What would this select?
Bob, Joe, Jeff

We could select every column from the table where the gender is female.




That would select all the information from the table where the gender contains "female". Simple enough? SQL speaks for itself, it does what it says, it isn't difficult at all.


Insert

We can insert new rows into the database if we wish, here's the structure.




That statement would insert a new row. In the first brackets we list the columns in which we want to enter a value (all in this case), in the second set of brackets we enter the value. The order of the values listed in the first bracket must match the order of the values to insert in the second brackets. So now we have a new row.
Table: People


ID NAME GENDER LOCATION AGE
1 Bob Male UK 31
2 Jill Female USA 24
3 Joe Male Germany 19
4 Jeff Male France 46
5 Sue Female UK 29
6 Fred Male Chile 25

Delete

Maybe we want to delete a row from the table, or delete multiple rows, here's how we do it.




This statement would delete any row where the name is Joe, read the query, it does what it says. So now we would have:
Table: People


ID NAME GENDER LOCATION AGE
1 Bob Male UK 31
2 Jill Female USA 24
4 Jeff Male France 46
5 Sue Female UK 29
6 Fred Male Chile 25

We could delete all the males from the table.




Now we would have:


Table: People

ID NAME GENDER LOCATION AGE
2 Jill Female USA 24
5 Sue Female UK 29

It really does speak for itself, you just need to learn the statements.

Update

We can change the information in existing rows in the database using the UPDATE statement.




Update the table people and change the location to Ohio WHERE the name is Jill.We could update more than one column field if we wanted.




Just separate them with commas.

Create

We can use the CREATE TABLE statement to make new tables.




Create a table named other_people, now we have to define each column field and the type of data it will contain, here is a list of some of them:


  • INTEGER - A whole number.
  • VARCHAR (10) - Up to 10 characters.
  • CHAR (10) - Only 10 characters.
  • DATE - A date.
  • DATETIME - Date and time.
  • FLOAT - Floating point numbers.

Read the SQL statement, it speaks for itself.

Drop

We can delete tables from our database with the DROP TABLE statement.




It's as simple as that.

Conclusion

That about wraps up this introduction to SQL. This is the extreme basics, but it's these foundations you need so you can build on them and advance your skills in SQL. Practice with what you have learned, then look below if you want to learn more.


Bibliography

Here are a few links to learn more about SQL.

-Jester

1  |  2  |  3  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

24,011 Views
0 Total Comments
2 Rating of 5 (1 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

Your Article Here



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank