Sunday 14 October 2012

SQL Basics


SQL - What's a Database?
A database is nothing more than an empty shell, like a vacant warehouse. It offers no real functionality what so ever, other than holding a name. Tables are the next tier of our tree offering a wide scope of functionality. If you follow our warehouse example, a SQL table would be the physical shelving inside our vacant warehouse. Each SQL table is capable of housing 1024 columns(shelves). Depending on the situation, your goods may require reorganization, reshelving, or removal. SQL tables can be manipulated in this same way or in any fashion the situation calls for.

SQL - Platforms
A SQL platform acts as the stage for building and developing your databases. Several different platforms exist including:
- IBM's DB2
- MySQL
- PostgreSQL
- Oracle
- Microsoft's SQL Server

SQL - MySQL and PostgreSQL
MySQL and PostgreSQL are open source database programs rich in functionality and flexibility. They are often the choice of web developers and small businesses simply because they get the job done for a very reasonable price. Also they will go anywhere and can operate on nearly every operating system available.

SQL - SQL Server
Microsoft's SQL Server is steadily on the rise in the commercial world gaining popularity slowly. This platform has a GUI "Windows" type interface and is also rich with functionality. A free trial version can be downloaded at the Microsoft web site, however it is only available to Windows users.

SQL - DB2 and Oracle
By far the selection of choice for large corporations is either Oracle or DB2. Companies that have large ties to IBM stick to their DB2 software whereas others have made the switch to Oracle. These systems run on personal computers as well as large corporate mainframes.

SQL - Queries
Queries are the backbone of SQL. Query is a loose term that refers to a widely available set of SQL commands called clauses. Each clause (command) performs some sort of function against the database. For instance, the create clause creates tables and databases and the select clause selects rows that have been inserted into your tables. We will dive deeper in detail as this tutorial continues but for now let's take a look at some query structure. Query construction begins with one of the following clauses:
Add
Drop
Create
Insert
Select
Update
Replace
Delete

Queries are loosely typed into your SQL prompt. Spacing and line breaks are not very important as we will discuss further in our SQL Syntax lesson. We now know that a query begins with a clause, what comes next depends on the clause we select and we will be covering all the clauses as the tutorial progresses. For now, let's take a look at some syntax.


SQL - Query Syntax
The syntax of a query is loose, meaning you are free to place line breaks where you please without injuring the code. Few instances require parentheses, including the insert statement listed below. Parentheses will also be covered during our Functions lesson. Be sure to end all query statements with a semicolon (;).

SQL Code: 
                    SELECT * FROM table_name; 

The above code selects every row and every column from a hypothetical table (table_one) and prints it to our prompt. Here's a look at a few more queries that should become second nature to you as the tutorial continues.

SQL Code: 
INSERT INTO table_name (column_one,column_two) VALUES(value_one,value_two);

SQL Code:  
UPDATE table_name SET column_one = value_one, column_two = value_two;

Queries are how you communicate to your database program. Nearly everything typed at a SQL command prompt is a query.


No comments:

Post a Comment