intro
ALTER TABLE queries are some of the most frequent friends for those who modify data within database management systems – read this blog and learn how they work internally.
ALTER TABLE
queries help us modify – or alter – data in our databases. These kinds of queries are amongst some of the most frequently used queries that help us add, delete, or modify data within our tables, and the statement is also used to add, modify, or drop indexes on a certain table.
The Basics
The ALTER TABLE
statement looks like so:
The structure of the statement is rather simple – first, the table name, then the action with any additional actions (the add column statement above also modifies the length of the column before adding it on the table.) For example, if we’d like to add a fulltext index on a table called demo, we would do everything like so:
Rather simple, right? This simple statement has a lot of use cases – care to go through them with us?
The Use Cases of ALTER TABLE
The ALTER TABLE
statement can be used to:
By now, you should get it – the ALTER TABLE
statement can perform pretty much any action related to modifying data within a table.
How Does ALTER TABLE Work?
The way ALTER TABLE
works is a little different to SELECT
, INSERT
, UPDATE
, or DELETE
queries that you are so used to – once the statement is used, your database management system will go through a couple of phases (for convenience, the original table that you run queries on will be called A, and the other will be called B):
In most cases, this process will take miliseconds and you won’t even notice it as you go along – yet, in some cases, this process can also take hours or even weeks to complete. Everything depends on your database configuration – all database management systems make use of parameters defined within a file that they’re dependent upon when completing such operations:
To optimize the performance of ALTER TABLE
, optimize the setting that deals with the inner working of data within your database instance: in MySQL, that’s innodb-buffer-pool-size
. Setting the buffer pool size to 60-80% of the RAM available within your system is a good idea – for those who wonder, the buffer pool size and related settings in other database management systems refer to the amount of operating memory that can be used for mission-critical queries that modify data – such queries include ALTER TABLE
as well (you should probably also look at the settings below the buffer pool size for the sake of your database, but that’s a topic for another blog.)
Optimizing ALTER TABLE Further
Setting the innodb-buffer-pool-size
parameter to an optimal size will be a good starting point, however, there are also a couple of things that you need to keep in mind as well:
Having this in mind, keep in mind that for the ALTER TABLE
query to work well, you need to have a decent amount of storage space as well – if there’s not enough storage space on the disk, your database management system will present an error. The inner workings of ALTER TABLE
will usually be quick unless you’re dealing with hundreds of millions of rows and above – in that case, you may see no results until the query has finished executing (many database management systems come with storage engines that support the ACID functionality which is one of the primary reasons of you seeing no results in this case.)
Also, do note that not all operations that alter the data within the table have to necessarily work with the data itself – renaming columns, working with partitions, changing the row format or the storage engine will usually be blazing quick operations regardless of how many rows your table has because the query simply won’t touch them and work on the surface level.
Optimizing Databases
After you’ve optimized your ALTER TABLE
queries, it’s time to scratch past the surface level for your database management system as well. That can be accomplished by using proven SQL clients like DbVisualizer – its powerful features will help you work with everything ranging from query maintenance to visualizing your queries in real-time – you will even be able to see how certain tables in your database infrastructure look like when they’re drawn out if you head over to the References section:
You will be able to observe information relevant to the data within your tables, columns that you’ve created, DbVisualizer will show you the row count within the table, and provide you information about indexes you’ve built as well:
If you wish, you will also be able to copy over the DDL to make a copy of the table and the data within it – just head over to the DDL section and copy everything over there:
DbVisualizer also comes with other features unique to itself. Some of them allow you to improve the security of your work within the database management systems as well – by setting permissions, you will be able to allow or deny SQL code to be executed as well:
Did we tell you that you can evaluate DbVisualizer for free and join the realms of NASA, Volkswagen, and other companies using the tool? Give it a try today!
Conclusion
In this blog, we’ve walked you through one of the most important queries when modifying data within your database infrastructure – the ALTER TABLE
query. You’ve learned what it is, how it works internally, and how it can help you achieve your goals within the database space.
Follow our blog for more updates, and we’ll see you in the next blog!