{"id":592,"date":"2012-12-20T22:41:52","date_gmt":"2012-12-21T05:41:52","guid":{"rendered":"http:\/\/www.1keydata.com\/blog\/?p=592"},"modified":"2012-12-20T22:41:52","modified_gmt":"2012-12-21T05:41:52","slug":"rename-table-in-sql","status":"publish","type":"post","link":"https:\/\/www.1keydata.com\/blog\/rename-table-in-sql.html","title":{"rendered":"Rename Table in SQL"},"content":{"rendered":"<p>Sometimes, it may be necessary to rename a table. There is no standard way to rename a table, and the implementation varies by RDBMS. Below we discuss how we can rename a table in MySQL, Oracle, and SQL Server.<\/p>\n<h3>MySQL<\/h3>\n<p>In MySQL, we can rename a table using one of the following methods:<\/p>\n<p><span style=\"text-decoration: underline;\">Method 1<\/span><\/p>\n<p>RENAME <em>OLD_TABLE_NAME<\/em> TO <em>NEW_TABLE_NAME<\/em><\/p>\n<p><span style=\"text-decoration: underline;\">Method 2<\/span><\/p>\n<p>ALTER TABLE <em>OLD_TABLE_NAME<\/em><br \/>\nRENAME TO <em>NEW_TABLE_NAME<\/em><\/p>\n<p><!--more-->For example, to change the name of a table from <em>Top_Scores<\/em> to <em>Best_Scores<\/em>, we can use either of the following:<\/p>\n<p>RENAME Top_Scores TO Best_Scores;<\/p>\n<p>ALTER TABLE Top_Scores<br \/>\nRENAME TO Best_Scores;<\/p>\n<h3>Oracle<\/h3>\n<p>In Oracle, we can rename a table using the same two ways we listed above for MySQL.<\/p>\n<h3>SQL Server<\/h3>\n<p>In SQL Server, one cannot use SQL to rename a table. Instead, it is necessary to use the sp_rename stored procedure to do so. The syntax is:<\/p>\n<p>sp_rename &#8216;OLD_TABLE_NAME&#8217;, &#8216;NEW_TABLE_NAME&#8217;<\/p>\n<p>Note the single quote above. Using a double quote or no quote will both result in an error. To rename a table from <em>Top_Scores<\/em> to <em>Best_Scores<\/em>, we use the following:<\/p>\n<p>sp_rename &#8216;Top_Scores&#8217;, &#8216;Best_Scores&#8217;;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shows the syntax to rename a table in MySQL, Oracle, and SQL Server.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[247],"tags":[290,289],"_links":{"self":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts\/592"}],"collection":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/comments?post=592"}],"version-history":[{"count":10,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts\/592\/revisions"}],"predecessor-version":[{"id":602,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/posts\/592\/revisions\/602"}],"wp:attachment":[{"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/media?parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/categories?post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.1keydata.com\/blog\/wp-json\/wp\/v2\/tags?post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}