Weblog entry #86 for ajt

SQL Foo
Posted by ajt on Sun 6 May 2007 at 10:47
Tags:
Some time ago I decided to learn another computer programming language. Though I know and love Perl I thought it would be good for me to try something else, plus it's a useful transferable skill that's looks good on a CV.

I tried Java and I just can't get into it - it doesn't do what I want and can't see the point. Then I thought I'd have a go at Python, it's another trendy language but so far I've not got beyond thinking about it. I have improved my shell and awk quite a bit but it doesn't really count...

At work I'm learning and using ABAP (SAP's proprietary COBOL+SQL monster). So this weekend I decided to install PostgreSQL and MySQL and have a go at more SQL. I can access the databases easily from Perl, ABAP uses a sub-set of SQL and by using various DBs I should avoid becoming dependent of non-standard extensions. SQL may not be as rich as Python, Haskell or Prolog, but it's useful and easy to get into.

 

Comments on this Entry

Posted by simonw (84.45.xx.xx) on Sun 6 May 2007 at 22:41
[ Send Message | View Weblogs ]
I find SQL frustrating. The dialects are a nightmare. It is a language where interesting standards idea never really happened, but the implementations were successful because they came with tools that did useful stuff quickly for businesses.

That said I use it occasionally, and would be grateful if you find any good resources on how to solve common patterns.

One good example is hierarchy. SQL was invented to support relational databases. Hierarchies are a simple set of relationships, but SQL doesn't allow simple methods to query hierarchy (at least not broadly in common implementations).

For example in a relational database with parent child relationships it is easy to query for all the grandchildren of a node, but a nightmare to try and query all descendants of a node. There are extensions in Postgres, and clauses in Oracle's SQL that allow this sort of query, but by now it ought to have been standardized. I appreciate some of these "simple" relationships, lead to interesting implementation issues (loops and such like), but I doubt these are insurmountable.

As such there are some simple "workarounds" for that type of problem, that involve sticking extra labels in the data, but I see that as a simple failure of the standards to properly represent the kinds of data and queries people have in the real world.

[ Parent | Reply to this comment ]

Posted by ajt (85.211.xx.xx) on Mon 7 May 2007 at 19:07
[ Send Message | View Weblogs ]
SAP's ABAP SQL dialect is as far as I can tell very limited and quite primitive, you can't really do a lot with it.

I'm hoping that by starting with MySQL, PostgreSQL and SQLite I should learn the basics properly without picking up any bad habits. In theory I should be able to download and install DB/2 Express, Oracle Express and Firebird for Linux for free, but I think I have enough to get me going.

I don't want to become a DBA, but I'd like to know how things should be done, so I can do basic stuff from Perl without making a dog's dinner of it.

--
"It's Not Magic, It's Work"
Adam

[ Parent | Reply to this comment ]

Posted by Steve (80.68.xx.xx) on Tue 8 May 2007 at 11:15
[ Send Message | View Steve's Scratchpad | View Weblogs ]

I find SQL frustrating too, and agree with your comment about hierarchy.

I tend to spend a long time working out how to do something "new" for the first time, then treat it like a recipe in the future. This means that some of my code is not really optimal, but I only use basic JOINS and little more in my SQL.

A few days ago I was working on selecting related tags, for example, and I spent a long time on it. Joining a table with itself in a manner that would probably make somebody else weep.

As another random example of pain - trying to work out the age of a user based upon their date of birth stored in a database is harder than you'd think. (I initially tried "SELECT (YEAR(birthday)-YEAR(NOW()) FROM users .." but that was a little naive!)

Steve

[ Parent | Reply to this comment ]

Posted by daemon (155.232.xx.xx) on Sun 6 May 2007 at 22:59
[ Send Message | View Weblogs ]

Yeah, I never really liked Java either -- the hoops you have/had to jump through just to write out to stdout... Mind you that was a few years back, but I still can't really be arsed to try it out again.

SQL really sits in a different space to most programming languages though, as it really does fill a tightly specific niche (also, not being a "Computer Scientist" I couldn't say if it's "Turing complete" either).

A while back I was thinking about which programming languages are really necessary to know, and I narrowed it down to a list of about 5 that seemed about right for me (note, "for me", this will vary widely I'm sure):

1) C++ -- I originally thought C, seeing as I'm more comfortable with plain C and have more experience coding in it (not to say that I have much, just more than C++), but figured that C++ is a superset of C, and contains all of the C that I know how to use, and well, comes with OOP goodness that does have it's place.

2) Bash -- This one I won't budge on, Everybody should know at least sh, and preferably full bash, but I guess something like zsh is also cool if you're comfortable with it. A few years ago this slot would likely have been filled with ksh or pdksh, but I think we've all moved of from there.

3) Python -- my personal choice, where many others will choose Perl. Where this sits in my list allows the two to be interchangable, I just know Python better. Some folks might push for Pike/Ruby/Rexx/Tcl, but hell, I had to draw the line somewhere ;-)

4) SQL -- It is pretty specific to it's niche, and there's nothing else really competing for it's place. Of course, SQL is a bit of a moving target from one DMBS to another, but that's another story. Everyone should have the basics of SQL, and those basics are pretty well standardised.

5) Javascript -- This is a bit of an oddball, as I really haven't used javascript that much, and of the languages listed here, it's the one I know the least, but I can't help thinking that it's only going to get more important in the next few years, with the rise of AJAX and the semantic web.

Some folks will obviously shout out, what about XML? Well, it's name says it all, it's a markup language. Still bloody important though, it just doesn't fit in this list. But while we're on the subject, go look up "microformats", they're also going to be BIG in the near future.

Cheers.
:wq

[ Parent | Reply to this comment ]

Posted by ajt (204.193.xx.xx) on Tue 8 May 2007 at 13:36
[ Send Message | View Weblogs ]
I don't hate Java, I just can't get a long with it...

c/c++ I've never used, of the languages of it's age I used Pascal/OO Pascal which was nice, but I don't really need low level languages, as they don't do what I want doing.

Bash I use a lot, more in the last 12 months than ever before. I don't write long shell scripts - that's what Perl/Python/Ruby is for, but for system admin stuff Bash is essential

I considered Python, but I use Perl - one day I'll learn Python and Ruby as they function at the same level as Perl and thus do the kind of things I want to do.

SQL is the language of the moment...

Javascript is a nice language, I've used it on an off. It's actually a nice language it's just it's mostly abused by web developers with no programming background. Javascript suffers in the same way that PHP and ASP suffer, most users have no idea what they are doing and mostly write drivel.

--
"It's Not Magic, It's Work"
Adam

[ Parent | Reply to this comment ]

Posted by simonw (212.24.xx.xx) on Tue 8 May 2007 at 17:53
[ Send Message | View Weblogs ]
SQL niche is largely down to it being a standard.

I learnt SAS before I learnt SQL, and it shares the same "work on a set of records at a time" programming paradigm. But then most of the statistical languages (R being the main free software one I believe) share a similar method of operation.

SAS is immeasurably richer than SQL. It also provides a slightly procedural style construct for importing data, so you can use the procedures against any data format you can describe to SAS. Indeed you can use SQL (it has a SQL procedure), against any data you can describe to SAS, although the performance implications may be interesting if you point at a huge binary file, and ask for something that needs a lot of indexed to be performed efficiently.

There is less interest in "JOINS" (it does them but you don't tend to do it a lot) but that is more a domain thing, if you wanted to join the data to other data repeatedly, you'd put it in a database, as SAS is quite happy to read from databases as well as files.

I never tried hierarchy stuff in SAS, and I suspect it wouldn't handle it too well, as it wasn't the kind of thing you usually do in statistics, but I'm open to correction, and no doubt it has moved on since I learnt it well over a decade ago.

Of course the problem was it being proprietary, and I'd never recommend writing anything in proprietary languages - old lesson, relearnt several times.

Perhaps the thing to do is ignore the standards, make a language that does what I want, and then free it upon an unsuspecting world - the Larry Wall approach. Or just make Perl do it - the other Larry Wall approach ;)

PS: Javascript is a nice language, I haven't mastered it yet, but it has hidden power. A great pity it is mostly trapped inside browsers with hideously different environments in which it operates, so doing anything interesting usually involves "first hide all the differences between major browsers...".

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Quick Site Search