A few years ago NoSQL might have sounded like a alien monster, but given the advances in the technology and how modern web and mobile applications are storing and accessing data, it is and has become a critical part of today's development.
Couchbase makes it easier than ever to make the switch (or addition) from a relational database to NoSQL with the addition of its latest querying technology.
What You Already Know About SQL
You're probably coming from a relational database management system (RDBMS) such as MySQL, SQL Server, Oracle, or one of the many other popular SQL databases. All these databases share a few common things:
- Use tables that contain data inside rows and columns
- Constrained to particular data types (for the most part)
The data in these tables might be related.
Data Relationships Between Tables
If you've been using an RDBMS long enough, you'll know that many of your tables will be related. For example, let's say you have the following two tables:
A relationship is established between these two tables on the user_id of the profile table and the id of the user table. This is because every user will have a profile. The relationship of course is based on foreign and primary keys.
Joining Table Data
Joining data from two tables is a common practice in an RDBMS. Continuing on the sample I listed above, the following will probably look familiar to you:
FROM tblUser AS u
JOIN tblProfile AS p ON u.id = p.user_id
You must be asking, where am I going with this, because this is some pretty basic stuff. You'll see in a moment.
What You Think You Know About NoSQL
With Couchbase and NoSQL databases in general your data is stored as JSON. The whole concept of tables and rows is thrown out the window which can feel a little strange at first. For example, what happens if you have the following two JSON documents:
name: "Nic Raboy",
The data in these documents remain unstructured, meaning you are not confined to the schema of a table. If you wanted to add or remove content, you can do so without touching any other documents or changing the allowed structure.
How you would query for these documents would vary between NoSQL platforms, but one thing would remain consistent. If you wanted to join these two documents you would have to make two calls to the database server-side and reconstruct the data into a single document. For example, take this psuedocode:
get all documents from profile view
get user where user id is profile.user_id
do something with profile and user document
It isn't very fun having to join documents that way, and it can get quite messy if you have to join more than one document.
How Couchbase Bridges SQL And NoSQL
With Couchbase 4.0 you no longer have to design complicated server-side logic for accomplishing the simple queries you'd typically see in an RDBMS.
Couchbase 4.0 introduces N1QL which can be better thought of as an extension to SQL. What this means for you is that you can use the SQL language that you already know and love, with NoSQL data. Using N1QL you can create the following query for the documents mentioned earlier:
FROM `bucket-name` AS u
JOIN `bucket-name` AS p ON KEYS p.user_id
Not so bad right? Sure there are a few differences to what you might be used to in MySQL or similar, but not enough to make it complicated to understand or learn.
So what other advantages does Couchbase's N1QL bring to the world of NoSQL? Many things, but for example, here are a few:
- All the heavy lifting for getting and manipulating data happens in Couchbase, not your application server.
- You get all your favorite SQL commands, plus extras.
- It is fast
So can it be included in the programming languages that you already use?
SQL Works In Just About Every Programming Language, But Will N1QL?
There is a Couchbase SDK for most of the popular programming languages. Whether you're developing in Java, Node.js, or .NET, you can use N1QL in your application just as easy as you can use SQL.
Making the switch from a database that uses SQL to NoSQL can seem like a scary move, but with Couchbase 4.0 and N1QL, the transition between the two platforms is not too different than what you already know. To further get a sense of what the transition would entail, check out this very thorough and interactive N1QL tutorial.