AWS

Redshift RENAME Table

Just like other SQL databases, the data in Amazon Redshift is stored in the form of tables. These tables are created and named by the user according to his requirements. But sometimes, you encounter situations where you want to edit or change your database, like renaming database tables or columns, deleting tables, changing ownership and many other things. In this article, we will look at how to rename a table in the Amazon Redshift cluster.

Amazon Redshift

Amazon Redshift is an SQL-based data warehousing service provided by AWS. It is very efficient for running complex queries on massive datasets using a larger number of processing nodes that take up the entire job and return the results of the analysis in the desired pattern. It is a fully serverless offering by AWS, so you don’t have to worry about the underlying infrastructure. The group of the Redshift nodes is called a Redshift cluster. The cluster can be configured with a maximum of 128 nodes to carry heavy big data jobs.

Configuring a Redshift Cluster

First, we are going to create and configure a new Redshift cluster. So, log in to your AWS account and search for Amazon Redshift.

Click on “Create cluster” to configure a new Redshift cluster. Since Redshift is a serverless service by AWS, you don’t need to manage and configure large details. Instead, all this can be done in a few clicks.

Next, you need to provide the cluster name, select the type according to your requirements, and provide database credentials, and your Redshift cluster will be configured.

So, our Redshift cluster is ready, and we will move on.

Creating Table in Redshift Database

To start with our Redshift database, let’s create a demo table with the name admin_team and having four columns named serial_number, employee_name, employee_ID and date_of_joining. Use the following query to create the table in the Redshift cluster:

create table admin_team (
  serial_number int,
  employee_name varchar,
  employee_id int,
  date_of_joining date
)

Now, let’s create another table named IT_team with the same four columns.

create table IT_team (
  serial_number int,
  employee_name varchar,
  employee_id int,
  date_of_joining date
)

So, our tables have been created. Now, we are adding some random fake data to these tables. This is done by writing a simple SQL query.

insert into admin_team
(serial_number, employee_name, employee_id, date_of_joining)
values
('01', 'John', '101', '2020-02-10'),
('02', 'Robert', '102', '2021-05-20'),
('03', 'Harry', '103', '2021-08-15');

The data can be viewed in the “table details” section.

Similarly, we can add some data in the other table named IT_team. Use the following query to add data to the table:

insert into it_team
(serial_number, employee_name, employee_id, date_of_joining)
values
('01', 'James', '101', '2020-03-10'),
('02', 'Mary', '102', '2020-05-20'),
('03', 'David', '103', '2021-08-15');

Now that our tables are ready, let’s move to the next step of renaming the tables.

Renaming Table

Suppose your team expands, and you have to acquire multiple offices. You want to manage all the employee data separately for each branch. For this, you want to edit the table names to specify the office branch with them, and it is possible in Amazon Redshift to rename a table.

We will use the “alter table” command to rename a table. This command is found under the DDL (Data Definition Language) section of the SQL commands. DDL commands are used to create and modify the object structure in a database. Commonly used DDL commands are CREATE, DROP, ALTER, RENAME, COMMENT, etc.

Here, we are going to rename the table name admin_team to admin_team_office_1. This will easily distinguish the staff in different branches. The syntax to rename the table is short and simple. Use the following query to rename the table:

alter table admin_team
rename to admin_team_office_1

Once this is done, let’s do the same for the other table. Here, the table name it_team is updated to it_team_office_1. Use the following query to rename this table:

alter table it_team
rename to it_team_office_1

As you can see, everything remained the same. Our data is untouched, and the table name is altered.

Conclusion

The table name can be easily changed in the Redshift database. As Redshift is a SQL based database, we have utilized the “alter table”, which is a simple DDL command. You can also rename columns, add new columns, alter sort keys or even change the table owner using the same alter table command. These tools are convenient and easy to use when you want to make small changes in your database without disturbing the overall structure and also save a lot of time.

About the author

Zain Abideen

A DevOps Engineer with expertise in provisioning and managing servers on AWS and Software delivery lifecycle (SDLC) automation. I'm from Gujranwala, Pakistan and currently working as a DevOps engineer.