{"id":413149,"date":"2024-02-21T21:51:07","date_gmt":"2024-02-22T05:51:07","guid":{"rendered":"https:\/\/linuxhint.com\/?p=413149"},"modified":"2024-02-24T11:41:09","modified_gmt":"2024-02-24T19:41:09","slug":"sql-change-table-name","status":"publish","type":"post","link":"https:\/\/linuxhint.com\/sql-change-table-name\/","title":{"rendered":"Change the Table Name in SQL"},"content":{"rendered":"<div id=\"wpbody\">\nIn SQL, tables are some of the most fundamental and essential components of a relational database. However, like anything in the development world, you may encounter such instances where you need to change the name of an existing table.<\/p>\n<p>For example, you may be restructuring the database, improving the readability, etc. Whatever the reason, table renaming is a common occurrence in relational database.<\/p>\n<p>In this tutorial, we will walk you through the syntax and usage of the process of renaming a table in SQL.<\/p>\n<h2>Sample Data:<\/h2>\n<p>Before we dive into the process of renaming a table in SQL, let us set up a basic table with a sample data for demonstration purposes.<\/p>\n<p>Let\u2019s create a basic table that stores the network information:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\">CREATE TABLE network_devices (<br \/>\n<br \/>\n&nbsp; device_id INT PRIMARY KEY,<br \/>\n<br \/>\n&nbsp; device_name VARCHAR(50),<br \/>\n<br \/>\n&nbsp; device_type VARCHAR(50),<br \/>\n<br \/>\n&nbsp; ip_address VARCHAR(15),<br \/>\n<br \/>\n&nbsp; mac_address VARCHAR(17)<br \/>\n<br \/>\n);<\/div><\/div>\n<p>This should create a table that contains the network information such as the device ID, device name, type, IP address, and MAC address.<\/p>\n<h2>Rename a Table in SQL<\/h2>\n<p>In SQL, we can rename a table using the ALTER TABLE statement followed by the RENAME TO clause.<\/p>\n<p>We can express the syntax as shown in the following:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\">ALTER TABLE current_table_name<br \/>\n<br \/>\nRENAME TO new_table_name;<\/div><\/div>\n<p>It is good to ensure that the specified table exists in the database. Otherwise, the query returns an error.<\/p>\n<h2>Example Usage:<\/h2>\n<p>Consider the following example that demonstrates the use of the ALTER TABLE clause to rename the table from \u201cnetwork_devices\u201d to \u201cnetwork_mapping\u201d:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\">ALTER TABLE network_devices<br \/>\n<br \/>\nRENAME TO network_mapping;<\/div><\/div>\n<p>This should rename the specified table to the new name.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this post, we explored the ALTER TABLE and RENAME TO clause to rename an existing table to a new table.<\/p>\n<p>NOTE: It is good to keep in mind that using this clause does not affect any object that references the table. For example, if you have any foreign constraints that reference the table, you have to manually adjust the values to reflect the new name.\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Comprehensive tutorial on the process of using the ALTER TABLE statement and RENAME TO clause to rename an existing table to a new table along with examples.<\/p>\n","protected":false},"author":111,"featured_media":413249,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_wp_convertkit_post_meta":{"form":"-1","landing_page":"","tag":"0","restrict_content":"0"},"footnotes":""},"categories":[2451],"tags":[],"class_list":["post-413149","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-standard"],"_links":{"self":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/413149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/comments?post=413149"}],"version-history":[{"count":0,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/413149\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media\/413249"}],"wp:attachment":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media?parent=413149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/categories?post=413149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/tags?post=413149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}