{"id":9571,"date":"2022-08-08T07:05:00","date_gmt":"2022-08-08T05:05:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=9571"},"modified":"2022-08-09T18:18:45","modified_gmt":"2022-08-09T16:18:45","slug":"drop-index-sql-server-script","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/drop-index-sql-server-script\/","title":{"rendered":"Drop an index in SQL Server with a script"},"content":{"rendered":"\n<p>Another IT and more specifically a database tutorial on how to drop an index from a table in SQL Server. An index is associated with an existing table in a database. It can be a relational or transactional database.<\/p>\n\n\n\n<p>For example, learn here why it is important to implement <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/indexes\/reorganize-and-rebuild-indexes?view=sql-server-ver16\" target=\"_blank\" rel=\"noreferrer noopener\">maintenance <\/a>plans to improve performance and management of MS SQL resources.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-drop-an-index-in-sql-server-with-a-script-drop-index\">Drop an index in SQL Server with a script (DROP INDEX)<\/h2>\n\n\n\n<p>Firstly, to delete an unnecessary index, simply copy and paste this sample code and adjust it with the index and table names. This code will delete the index created in this article.<\/p>\n\n\n\n<p>It is also possible to delete an index from the SSMS GUI.<\/p>\n\n\n\n<p>This SQL Server command uses the DROP command and the INDEX keyword. The full name of the index and of course the table name follow the DROP INDEX command.<\/p>\n\n\n\n<p>Indeed, an index has a unique name for a given table. Name the indexes with the table prefix. To distinguish them and make it easier to manage the T-SQL code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">It is possible to have the same index name on different SQL Server tables<\/h3>\n\n\n\n<p>In this SQL script, the two tables SALES and SALES_2 have the same index created with the same name.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE [dbo].[SALES]\n(\n   [Year] TINYINT,\n   [MonthName] NVARCHAR(50),\n   [MonthCurrent] BIT,\n   [NumberMonth] TINYINT,\n   [EmployeeNumber] SMALLINT,\n   [NumberOfClients] INTEGER,\n   [NumberOfSales] BIGINT,\n   [Amount_ET] NUMERIC(15,5),\n   [Amount_IT] DECIMAL(15,5)\n);\n\nCREATE INDEX indexYearMonth\nON [dbo].[SALES] ([Year], [MonthName]);\nGO\n\nCREATE TABLE [dbo].[SALES_2]\n(\n   [Year] TINYINT,\n   [MonthName] NVARCHAR(50),\n   [MonthCurrent] BIT,\n   [NumberMonth] TINYINT,\n   [EmployeeNumber] SMALLINT,\n   [NumberOfClients] INTEGER,\n   [NumberOfSales] BIGINT,\n   [Amount_ET] NUMERIC(15,5),\n   [Amount_IT] DECIMAL(15,5)\n);\n\nCREATE INDEX indexYearMonth\nON [dbo].[SALES_2] ([Year], [MonthName]);\nGO<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Script to delete a MS SQL index from a table<\/h3>\n\n\n\n<p>Secondly, this is the same syntax as most objects deleted with Data Definition Language (DDL) queries.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DROP INDEX indexYearMonth\n   ON [dbo].[SALES] ;\nGO<\/pre>\n\n\n\n<p>In addition, please note that after deleting the index, all queries will use <strong><a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-primary-key\/\">the primary key of the SQL table<\/a><\/strong>. This can have a huge impact on performance.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-tutoriels-sql-et-it wp-block-embed-tutoriels-sql-et-it\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Be4qARIW15\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-primary-key\/\">How to create a SQL Server primary key?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to create a SQL Server primary key?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-primary-key\/embed\/#?secret=qBEcO6xArS#?secret=Be4qARIW15\" data-secret=\"Be4qARIW15\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Another IT and more specifically a database tutorial on how to drop an index from a table in SQL Server. An index is associated with an existing table in a database. It can be a relational or transactional database. <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/drop-index-sql-server-script\/\" title=\"Drop an index in SQL Server with a script\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":5608,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-9571","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/9571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=9571"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/9571\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/5608"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=9571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=9571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=9571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}