SQL the basics

By | May 15, 2006

Strange title isn’t. Is there such a thing as basic SQL(Structured Query Language). Lets try and find out. I will be covering some basics, thus creating a simple table. Making some basic queries and basics on T-SQL (Transact SQL).

Why use databases
Best to begin with a little story I suppose. Why would you want to use a database? Well it’s pretty easy. Databases give you the capability to store massive amounts of data in a orderly manner. With easy and structured access. I can hear those lovers of files shouting that you can do the same with files. I’ll grant them this, files can store information in the same way. But when it comes to quick access or easy manipulation then nothing beats databases.

Now I won’t bore you with the technical ways databases work. Meaning the locking mechanisms, reverting failures, tracking order and logging actions. (I could if I really wanted to, I mean we’ve all been taught how this works) Instead I will start out with the basics in table creation and querying.

Creating a table
The first thing you have to understand is the nature for SQL. Back when databases started there were many different companies creating them (still are), so there is a need for a standard. Having said this SQL is anything but standard. Every database supports different things, which makes SQL and database management in general very complicated. In this post I’ll be focusing on the MySQL variant.

We are going to create a table that will hold the information on personnel working for a small company. Now this means we need people’s home address information as well as the role they play and who is their boss.

Lets assume we want to know the following:

  • Personel info,
    • Home address
    • Telephone number
    • Birthday
    • Social security number
  • Employee info,
    • Employee number
    • Employee
    • Boss

Did you already notice me breaking them up in two separate groups, these groups will also be my two tables. Most times this grouping happens naturally. You’ve also got the fields for the table. So here are the two creation SQL queries.

CREATE TABLE t_Personel (
p_ID INT(4),
p_Name VARCHAR(50) NOT NULL,
P_Address VARCHAR(150) NOT NULL,
p_Birth DATE,
p_SSN INT(10),
CONSTRAINT PRIMARY KEY(p_ID)
);

CREATE TABLE t_Employee (
p_ID INT(4),
p_Employee INT(4),
p_Boss INT(4),
CONSTRAINT PRIMARY KEY(p_ID),
CONSTRAINT FOREIGN KEY(p_Employee) REFERENCES t_Personel(p_ID) ON DELETE CASCADE,
CONSTRAINT FOREIGN KEY(p_Boss) REFERENCES t_Employee(p_ID) ON DELETE SET NULL
);

Time to explain the queries a bit. Most of it is pretty basic. You create a table with a view fields and tell what datatype they should have. This also means you need to indicate the size of the field, this is the part between the brackets. Something new is the creating of the PRIMARY KEY, which tells the database this field should be unique and indexed. Another constraint we add is a foreign key. This tells the database that the information in the field also has to exist in the referenced tables field.

Querying the database
Now that you have the database all set up (pretend it is already filled with useful information) you’re ready to start asking it for information. Lets presume that you want information on the boss of ‘F. MacBrull’. Looking at the tables you’ll have to first get the employee information of the guy, which means looking him up in the t_Personel. So lets do this part already.

  SELECT FROM t_Personel p_ID WHERE p_Name = "F. MacBrull";

Now you have to find his employee information using the fetched information like this:

  SELECT b.p_Boss FROM t_Employee AS b WHERE p_Employee = (SELECT a.p_ID FROM t_Personel AS a WHERE p_Name = "F. MacBrull");

Found that one a bit tougher already. We’re just beginning! As promised you’ve just selected the number of the boss that belongs to the already found information on ‘F. MacBrull’. So lets try and get his name, after all this is what we are really after.

  SELECT c.p_Name FROM t_Personel WHERE c.p_ID = (SELECT b.p_Boss FROM t_Employee AS b WHERE p_Employee = (SELECT a.p_ID FROM t_Personel AS a WHERE p_Name = "F. MacBrull"));

Finally, we’ve got his bosses name. Some of you might ask: “Was all this really necessary?” NO! But it wouldn’t have been as much fun. And every time you query a database it takes time, so why not do it all at once.

Leave a Reply