We’ve all been there. SQL can be a bit frustrating if you haven’t written it before, or perhaps it’s just been a while. Often we can write our SQL by use of a web app framework like Ruby on Rails 🚂 . This makes it super convenient to build web applications quickly. For those who are not familiar with SQL(structured query language), it’s a special language used to interact with your database. This is definitely worth taking the time to learn, especially if you’ll be dealing with relational databases.
In this blog, I will be starting with something easy. I will create a table with some simple SQL. I’ll go through each step so we can understand a bit of the syntax and how to write to our database. Let’s dive in 🐋!
Let’s take a look at this SQL statement below. We can see that it’s written very similarly to the English language. You can almost read it like a sentence, right? That’s because it was designed to be expressive, cool right? Just keep in mind it’s meant to manipulate data, like a 🔨 would-be to nails. 😳
CREATE TABLE pandas( 🐼
In the statement above, I’m trying to create a table that holds some information about pandas. I’ve created four columns to hold our important information. Let’s take this apart, piece by piece, to understand the instructions we are giving to our database!
CREATE TABLE pandasCREATE TABLE === Keyword
pandas === Identifier
The first part of our statement is the “CREATE TABLE” or Keyword; this will tell the database what we are trying to do. We signify this with words in ALL CAPS. Next, we have the lowercase “pandas” or identifier, which tells the database what our target is. These two words will create the table itself.
CREATE TABLE pandas(
name === column name
VARCHAR(50) === datatype w/ limit of 50 characters !
We need to give this table some columns as well. This means will need to create a set of parenthesis and add each column in. Will create a name, which comes first and is in lower case, and the data type, which will be in ALL CAPS.
A good way to look at this is to make a blueprint for how your data needs to look before it enters the database. In other words, if you try to add a panda’s data and its name is over 50 characters, the database will reject the entry. The same idea with weight data, if it isn’t an Integer or number without a decimal, will also be rejected.
Congrats, you’ve created a table. A small victory, but an important lesson about syntax and a great way to get into writing SQL! Stay tuned 📺; I’ll be writing a blog on inserting data into our newly created table! Feel free to read ahead, test the query, and play around with this concept. You’ll find some resources in the links below. Happy coding 😊
More on SQL: https://www.w3schools.com/sql/Sandbox for SQL: http://sqlfiddle.com/More on SQL Data types: https://www.tutorialspoint.com/sql/sql-data-types.htm