Why Database Schemas Are Important & Types to Know About
Data is only as useful as the database it’s stored in. Here are some best practices for designing your own. The post Why Database Schemas Are Important & Types to Know About appeared first on Codecademy Blog.
Websites and applications consist of many parts — they can have a front end, a back end, and a database to connect to. Even desktop software often needs to connect to databases.
Databases are essential for storing and accessing data efficiently. They keep data organized in structured or semi-structured formats and allow multiple applications to access the data simultaneously.
For example, consider an online store. It needs a database to manage information about products, customers, orders, and inventory. When a customer places an order, the database records the details of the transaction, updates the inventory, and helps process the payment.
Ahead, we’ll explore the database schema — a crucial component that can help you create a database that fits the needs of your application.
Learn something new for free
What is a database schema?
Most applications store some kind of data. For example, a chat app might store user data to identify users and their message data. But before you can store data, you need to figure out what kind of data you need.
A database schema is a blueprint of how the data for an application will look. While not an actual part of your database, it describes the shape of your data so that you can design the tables in the database. In fact, you can often use the same schema to design a database for any type of database management system (DBMS).
A database schema breaks down the following details:
- Table structures, including column names and their data types
- Constraints on the data, such as length, uniqueness, etc.
- Unique keys and ids of tables
- Relationships between tables
Database schemas come in all sizes and levels of complexity, but they should be detailed enough to help Database Administrators and Developers create a database for the application, and Software Developers can use them as a guide for the type of data the app will process. The process of designing a database schema is called data modeling.
Here’s an example of a database schema that would be used to maintain book inventory:
An actual database schema would have more details, and more data, but this is a good starting point.
Database schema types
There are three types of database schemas that define a database’s structure at different levels of abstraction.
Physical schema
The physical schema represents how the data is stored on the disk. This is the lowest form of abstraction. It will be proprietary to the type of DBMS you’re using, and it contains the actual code used to create a fresh instance of your database from scratch.
Logical schema
The logical schema is the conceptual model of the database and what we are usually referring to when we say, “database schema.” The logical schema is platform agnostic and can be used to design a database for a variety of vendors. A logical database schema explains how the data is organized into tables and how the tables are related to each other.
Sometimes database designers use a tool called an entity-relationship diagram (ERD) that displays tables with their column names, data types, and links between them to signify relationships.
View schema
A view database schema describes how users interact with the database. Users don’t know how the data is stored or where, so this schema can be less detailed than a logical schema. Application developers use view schemas to design the data structures in their apps.
The importance of database schema design
The data modeling step of designing an application is very important. You can think of the database schema as the cornerstone of your app because both the database and application code depend on it. By designing a database’s structure abstractly, you can make sure it’s well-planned before writing code or creating tables.
An incorrectly designed database can have a lot of problems:
- Data could be lost or stored incorrectly. For example, if the timestamp in the message table of the chat app only stored the date as a string without time instead of a Unix or similar timestamp.
- Data could be duplicated in tables unnecessarily. For example, if we stored a complete copy of all the user data with each message in our chat app.
- The database could run inefficiently. For example, if we didn’t create foreign keys between the user table and the message table.
A database schema is both a blueprint for the data in your app and a brainstorming tool. It helps you visualize how the app will run before you start writing code and identify the flaws in your concept. The design of your app may change once you start the data modeling process.
Once an application gets released, it can be hard to change the database schema. Changing the structure of a database on a live application isn’t always possible and may require downtime to make the changes to tables, migrate data to the new structure, and convert data types; so it pays to take the time to design a database schema well in the first place.
Database schema design best practices
The best practices for designing a database schema will vary depending on the DBMS you’re using and your app’s requirements, but here are a few things you should consider when designing one.
Naming conventions
While you can change a database schema after you start coding, it won’t be fun. It’s usually best to establish some naming conventions before you start designing your schema and stick to it. This can also depend on the type of database system you’re using.
Most database systems have reserved words, like “table”, that you never want to use as a name for a table or column. Most don’t allow you to put spaces in table or column names without putting quotes around it, which can be annoying and cause issues. So you have to decide if you‘ll use camelCase, underscores_between_words, or another naming convention for table and column names that have more than one word. Will the table holding user data be called user or users?
Constraints
When you use a constraint on a table column, you restrict the type of data it can receive. If it should always only be a certain type of data, then constrain it to that specific type of data. Constraints block queries from happening when incorrect data is inserted into a database.
Technically, you can create a database without constraints. It’ll still work, but users who query it will have to know the details of your database schema. They’ll also be more likely to corrupt the data in your database. By setting primary key and foreign key constraints on your tables, you hard code the rules to your data relationships into the database structure so they’re always followed.
Indexes
Indexes can increase the performance of your queries. They allow for quicker searching and filtering of those columns that are often queried, so it helps to put indexes on those columns that will be searched often — but only those columns, because unnecessary indexes can also slow things down.
Double-check data types
Consider the future of your application and how it will be used when you set the column’s data type. Once the application is already in use, changing the data type can be tough and result in data loss if done incorrectly. Here’s an example where choosing the wrong data type can cause problems.
Some database engines have multiple definitions for the integer type that restrict the size of the integer. Integers are like ids in database systems. The small integer type may be limited to 65,535. If you expect to have more than 65,000 users, you might want to consider using a BIGINT type for the id of the table that can hold much larger numbers.
Documentation
Documenting your database schema is crucial so that Database and Application Developers know how the data is structured and how to retrieve and store the data they use. You can use an entity-relationship diagram both to design the schema and as documentation you can provide to developers.
Learn more about working with databases
Designing a database schema for an application is an important step in making sure the application supports its user’s needs and runs efficiently. It’s the cornerstone of your application. Both the data structures used in your application and the structure of the tables in your database will depend on it.
To learn more about designing databases and the best practices you should follow when creating a schema, check out Design Databases with PostgreSQL. It will teach you the basics of designing a database schema that fits the requirements of your applications, how to add constraints to your database, and how to optimize its speed. And if you want to learn more about working with databases, check out the courses below.
Related courses
The post Why Database Schemas Are Important & Types to Know About appeared first on Codecademy Blog.
What's Your Reaction?