What Is Schema?

What Is Schema?

·

0 min read

what is schema.png

So here's how this topic came about: I had an online code discussion with someone where he was helping me try to fix a bug I had, then we got talking and he throws the question at me, " What is Schema? ", well, I replied, "Schema defines the way your data is saved in the database". While I knew that I understood what schema was, I decided to write an article on this and hopefully help someone who wants to understand what schema is and why it is needed for the database.

What is schema?

Well let's look at the definition google gives us, " a representation of a plan or theory in the form of an outline or model ". so in other words, a schema basically gives us an idea of what to expect from a particular process or person. So we could easily interprete things if we know what to expect, right? Same thing happens with our brains. Our brains create and use schemas as a short cut to make future encounters with similar situations easier to navigate.

What is a database Schema?

From the above definition of what a schema is, we can define a database schema as representation or model we define to tell our database what form of data to expect and how this data should be stored in the database. The schema define any kind of structure defined around the data. Therefore, we define models containing entities and relationships. Entities here would refer to the several fields expected to be stored in the the database, while Relationships would determine how a database model or table relates to another model or table. Here's an example of a relationship:

Let's say we define a database mode schemal for creating storing new students in our students' table in the school database:

[
    Name
    StudentId
    ClassId
    Age
]

And we define another model schema for adding classes to the class table in the school database.

[
    ClassId
    Teacher
]

Notice that ClassId appears as a field in the two schemas. While the schema for Class has the ClassId as a primary key because the ClassId represents which class it is, the Schema/model for Student has the ClassId field as a foreign key; Here the ClassId defines a relationship that specifies the class each student belongs to. SO we could say there is a relationship between the two models/tables, and assuming the students could only have one class but a class can contain many students, we say it is a one to many relationship.

Why do we need Database Schemas

It is easy to get lost in data without a schema. Imagine trying to make sense of data from different sources all jampacked together into one exta large folder. Sorting this kind of data to get what you need would take an incredible amount of time. So by defining a schema for our data we:

  • save time while perfoming queries to our database.
  • make sure that required fields in the table/collection are not excluded and are present before the data is saved.
  • help reduce code complexity of queries to the database.
  • help others understand our code and our thought process
  • help structure our database more accurately.

So here it is! Oh.. one more thing.. We use database schemas because it is cool, and I like it, so you should like it because it like it. Fair deal right?

Conclusion

Many times we fail to understand certain concepts because we try to make them too geeky or we've already defined a schema in our head defining a concept as hard. So we don't understand the concept because we have an existing mental picture of it being difficult ( we have a schema ).

I hope this helps. Follow me on twitter @spillcode, leave comments and reactions, share!

🔥❤