[Ilugc] SQL tutorial part II

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Mon, 2 Apr 2012 03:26:43 +0530

We see SQL now.

We saw a bit yesterday.

By the way, my projects were featured in LinuxToday.

http://www.linuxtoday.com/infrastructure/2011120900239RVOOBD

I have also updated the latest images now.

http://liveusb-openbsd.sf.net

http://livecd-openbsd.sf.net

It started as a fun project 3 years ago, hopefully it will get
somewhere with time.

Now for SQL.

SQL is structured query language.

You can query a database/table in various ways.

It is like saying:

What all questions can you ask from an answer?

Right?

So if you have data organized in multiple columns and rows, you can
 operate on the matrix in very many ways.

SQL allows you all the flexibility you need.

Really cool.

Columns are sorted with the first argument to the select command.

Rows are selected as the last argument.

You can say.

sqlite> select id from marks where name = "rahim";

Or

sqlite> select phy from marks where distinct phy;

The great thing about SQL is that you can order the results, automatic sorting,
maximum, minimum and average computation comes with batteries.

Like python. ;)

sqlite> select max(phy) from marks;

It is amazing.

Now I did not say why we specify NULL as the first argument when we insert?

It is an auto increment primary key ID value.

Very good to have on all tables you create.

The table is indexed that way and stored internally to do quick searches against
 this field.

As I said, every row/record/data set is a key value pair.

And in our case as is true in most , the key is the id and the entire row is
 the set of values.

We can also have multiple keys, called tertiary and secondary keys.

Which can index faster and perform searches for us. You can also join
tables but
 I normally don't do it as it helps us combine multiple SQL tables.

Now let us talk about database.

A database contains multiple tables.

And you can also have any number of databases.

But let us not complicate.

Any fool can complicate things. Let us simplify.

SQL helps us do complex arithmetic on data without you having to write a lot of
 buggy unpredictable code.

In my product SpamCheetah I use SQL everywhere I want to retrieve user
stats like

1) How many attachments were totally received?

2) How many mails from who to who?

3) How many rejections were due to SPF mismatch?

And any arbitrary question user can come up with.

Your creativity alone is the limit.

How?

Using SQL and sqlite.

But of course the backend is build using CGI, Perl and C code.

And UNIX domain sockets.

You can build a web form to do a backend SQL query and return to the user.

This will look like magic.

If you have a website and do this, people will think you are a mini google. :)

Okay that is taking it a bit far.

-Girish
-- 
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in

Other related posts:

  • » [Ilugc] SQL tutorial part II - Girish Venkatachalam