Beginners Guide for Coding SQL (sql, database programming, computer programming, how to program, sql for dummies)
Introduction
A database serves as a container. It is a storage where programmer scan organize data in a constructive manner. Keep in mind that there is no need to create a database if you are only dealing with few amounts of data. For instance, handling hundreds of Excel spreadsheets require a database while five spreadsheets can be dealt with even without a database. In other words, organizing a threshold amount of data inside a database will save you from complex data management.
A Closer Look On Database
Organizing all data inside a database allows you to easily execute tasks such as querying the data, updating the data, and deleting previous data. Most of all, a database prevents conflicts from having multiple copies of data.
Let us say, for instance, that a company has to manage its overall financial expenses. Basically, there is a specific department to handle this and such department has several people in it. Now if these people have their own copies of the spreadsheets, then it will be somewhat difficult to come up with non-conflicting data considering the possibility of human errors.
A database, on the other hand, will be able to store and organize the data with higher accuracy and lesser conflicts.
Another example to help you understand what a database is, is by looking at your phones contact list. Each contact is a data and your smartphone serves as the database. With just a touch of a few buttons, you can easily get the contact you are looking for. You can easily edit the details of each contact as well. This advance data management is way better than listing each contact in a pen-and-paper phone directory.
Types of Databases
The different types of database are as follows:
- Relational Databases
- Object-Oriented Databases
- Document-Based Databases
In this eBook, I will mainly focus on relational databases. Nevertheless, I will not hesitate to include the other two types if deemed necessary.
You might have been introduced to some sort of new database type such as NoSQL. They are considered to be under the category of the document-based databases. These databases are now becoming popular and you might think that it is better to study them instead of learning SQL.
Please do not consider the relational databases to be the things of the past. A lot of databases that we still have today use the relational model. Moreover, SQL is useful when it comes to relational data queries. Besides, the query language of newly popularized databases is still somehow related to SQLsince they were mostly developed after SQL.
In other words, jumping into the new type of databases without understanding SQL will give you tons of challenges along the way. Hence, you have made the right decision of learning SQL first before moving forward to other databases like MongoDB.
Without further ado, let me now bring you to the world of SQL.
Chapter 1 A Closer Look on Relational Model
The Structured Query Language, or SQL, was originally designed for relational model. It is the language used to define the relationships between multiple data. It is also the database programming language used to query data in a relationalschema.
So when did this all began?
The first relational database was created in the 60s and the programming in this model were primarily defined by mathematics.In fact, this model of database was based on Tuple Relational Calculus and Relational Algebra.
Dont worry, though, there is no need for you to jump into algebra and calculus to handle relational databases. SQL will make relational database programming easier.
Tables, Columns, and Rows
In the relational database, the data is stored in a table. In this model, all tables have names. For instance, you may name a table as people while you may name another table as sales. These names refer to the kind of data stored inside the table. In other words, the table we call people contains data of people while the table we call sales contains data of sales.
Every table features a set of columns. These columns define the different pieces of data within the table. This what makes the relational model different from other types of databases. You will notice that there are some databases that store data altogether. In a relational model, however, all data is separated by columns and each column has a name. Each column can also restrict the category and size of data that will be added in it. For instance, some columns can only store strings while some columns can only store numbers. The restriction assigned to each column depends on the creator of the database.
Columns are also categorized by being required and not required. A required column means that every row in the table must have a data which corresponds to what the column requires. A not required column, on the other hand, means that we can assert a NULL value for each row under that column. As you go through SQL, you will find that NULL has a lot uses in database programming.
The primary point of using rows is to have retrievable data and containers where you can store data. Why do we store data? Simply because we want to get that data again in the future and we simply want a place for it to stay for the mean time. Hence, the core idea of a database.
Keys
One of the vital elements of a relational database are keys. We now know that a table has several columns and each column has names. Each column has related properties as well. One of these properties that we can assert in a column is a key.
In a relational database, we always assign a primary key for each row. This special type of key is used to uniquely determine a specific row. This means that the value of that key must remain unique and must not have any duplicates within the table. If, and only if, there is another table which contains that key in our relation model; then, we may merge those two tables together.
When merging two tables, there will always be primary keys that have no duplicates. Upon merging, you will notice that there will be a new column in the table with these keys. These keys are now known as the Foreign keys. They serve as links to the primary keys of the second table, allowing you to conveniently link rows from two separated tables.
Aside from primary keys and foreign keys, we also have the natural keys. Natural keys are global. This means that you cannot change them since they are used all over the globe to identify a single entity. Published books, for instance, always have natural keys which we know as ISBN. Each published book has its own unique ISBN which you need to use as primary keys when handling relational database for books. This is different when handling databases with unnatural keys like emails, people, where you need to invent your own primary keys for each row.