SQL: The Basics

SQL is a domain spe­cific lan­guage (a lan­guage designed to do one thing and knock the socks off at doing it) that just accesses databases.

Okay but what is a data­base? Well a data­base is essen­tially a col­lec­tion of data, yeah I know that doesn’t help much.

To clar­ify we’ll play a lit­tle men­tal exer­cise. Imag­ine you have a piece of soft­ware that you want to track pur­chases of your t-shirts for this store that you have. Well what are you track­ing? T-shirts or lets call them prod­ucts, sales that you make, and your cus­tomers. Each of those prod­ucts, cus­tomers and sales that you have are sep­a­rate col­lec­tions of data that you want to store. Prod­ucts are the t-shirts and each of those has some infor­ma­tion tied to the par­tic­u­lar shirt that you want to store sep­a­rate from the cus­tomer who is dif­fer­ent from the sales. For instance a prod­uct could have size, sku, price, etc, while a cus­tomer could have a list­ing of their name, num­ber, sales they are asso­ci­ated with and pos­si­bly if they owe you money or not. Get­ting the idea?

So a data­base is essen­tially a programmer’s tool to store all this data that’s rel­a­tively both fast and easy to main­tain, build, store and ensure remains accurate.

There are tons of dif­fer­ent types of data­bases, the most com­mon one cur­rently is called a Rela­tional Data­base (RDMS). The idea behind this is that there are defin­able types stored in the data­base that can relate to other types in the data­base. So again going back to our exam­ple, we would have prod­ucts, cus­tomers and sales as the defin­able types and cus­tomer and sales would relate like we talked about. A RDMS has a data­base that stores tables that con­tain these defin­able types (the prod­ucts, cus­tomer, etc). Each of these tables has entries that con­tain the infor­ma­tion you have provided.

So in our exam­ple a prod­uct has a sku, price and size. Each of these lit­tle bits of infor­ma­tion that is asso­ci­ated with the defi­an­ble type (the prod­uct in this case) is a col­umn. So each data­base has tables (the types) which have columns (the infor­ma­tion asso­ci­ated with a type), the last piece in the puz­zle is the actual data stored in the data­base. After all what good is a data­base if we have no infor­ma­tion? The infor­ma­tion or entries in our table are called the rows.

So here is a sam­ple data­base entry and what it would look like:

DatabaseSample2

Pretty sim­ple once it’s explained. Next I’ll start going through MySQL in par­tic­u­lar and some more relat­ing to the lan­guage SQL.

Comments are disabled for this post