{"id":1138,"date":"2024-07-31T11:25:44","date_gmt":"2024-07-31T09:25:44","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1138"},"modified":"2024-07-31T11:25:44","modified_gmt":"2024-07-31T09:25:44","slug":"27-indices","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/databases\/sql\/27-indices\/","title":{"rendered":"27. \u00cdndices"},"content":{"rendered":"\n<p>En este cap\u00edtulo veremos una de las partes importantes de las bases de datos, se trata de los <strong>\u00edndices.<\/strong><\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Tipos de \u00edndices<\/h2>\n\n\n\n<p>Principalmente hay 3 tipos de \u00edndices:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Llaves primarias.<\/strong><\/li>\n\n\n\n<li><strong>Campos \u00fanicos.<\/strong><\/li>\n\n\n\n<li><strong>Llaves for\u00e1neas.<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Declarar <strong>\u00edndices<\/strong> en nuestras <strong>bases de datos<\/strong> nos ayuda a mejorar la velocidad a la hora de buscar en nuestras <strong>bases de datos<\/strong>. Los <strong>\u00edndices<\/strong> van a ocupar espacio de almacenamiento nuestra <strong>base de datos<\/strong>, pero nos ayudan a que la <strong>base de datos<\/strong> sepa como poder ubicar un <strong>\u00edndice<\/strong> en una <strong>tabla,<\/strong> y que al ir a hacer consultas en base a ese <strong>\u00edndice,<\/strong> las consultas puedan ser m\u00e1s veloces que en base a un campo que no est\u00e9 indexado.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ver los \u00edndices de una tabla<\/h2>\n\n\n\n<p>Para ello tenemos la siguiente sintaxis.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SHOW INDEX FROM usuarios;<\/pre>\n\n\n\n<p>Veamos un ejm para entenderlo.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE bandas (\nid INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\nnombre VARCHAR(255),\nestilo VARCHAR(255),\nINDEX i_estilo (estilo),\nINDEX i_nombre (nombre)\nINDEX i_bandas (estilo, nombre) -- \u00cdndice m\u00faltiple\n)\n\n-- Mostramos los \u00edndices de la tabla bandas\nSHOW INDEX FROM bandas;<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Nota:<\/strong> si todos nuestros campos son \u00edndices, no estamos haciendo una buena optimizaci\u00f3n de nuestra <strong>base de datos<\/strong>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u00cdndice de tipo <em>FULLTEXT<\/em><\/h2>\n\n\n\n<p>Este tipo crea un <strong>\u00edndice<\/strong> a partir de varios campos, con lo que se puede hacer un <strong><em>SELECT<\/em><\/strong> especial, en el cual podemos hacer una b\u00fasqueda hacia varios campos con un texto o un valor en particular, pero en lugar de hacerlo a un campo en particular, lo hacemos a varios. Se puede decir que es <strong><em>una b\u00fasqueda al estilo de Google. <\/em><\/strong>Este tipo de <strong>\u00edndice<\/strong> suele demandar m\u00e1s tiempo al motor de <strong>base de datos<\/strong>, por lo que hay que usarlo con cautela.<\/p>\n\n\n\n<p>Para crearlo se utiliza la siguiente sintaxis.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE bandas (\nid INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\nnombre VARCHAR(50) UNIQUE,\nestilo VARCHAR(50),\nbio TEXT\nFULLTEXT INDEX fi_search (nombre, estilo, bio)\n)\n\n-- Cuando se haga una b\u00fasqueda, se buscar\u00e1 por los campos nombre, estilo y bio.\n\n-- Hacer un QUERY de tipo buscador, buscamos la palabra 'Heavy Metal'\nSELECT * FROM bandas \n WHERE MATCH(nombre, estilo, bio)\n AGAINST ('Heavy Metal' IN BOOLEAN MODE);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Modificaci\u00f3n de \u00edndices<\/h2>\n\n\n\n<p>Ahora vamos a ver como agregar o eliminar <strong>\u00edndices,<\/strong> campos \u00fanicos e incluso llaves <strong>principales.<\/strong> Para ello vamos a utilizar la siguiente sintaxis. La palabra reservada <strong><em>CONSTRAINT<\/em><\/strong> es s\u00f3lo para <strong>llaves primarias<\/strong> y <strong>campos \u00fanicos<\/strong>. La palabra reservada <strong><em>MODIFY<\/em><\/strong> es para el resto.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ALTER TABLE bandas ADD CONSTRAINT pk_banda_id PRIMARY KEY (banda_id);\n\n-- Hacer autoincremental el campo banda_id\nALTER TABLE bandas MODIFY COLUMN banda_id INT AUTO_INCREMENT;\n\n-- Hacer el campo estilo como un campo \u00fanico\nALTER TABLE bandas CONSTRAINT uq_estilo UNIQUE(estilo);\n\n-- Agregar \u00edndice com\u00fan y corriente al campo bio\nALTER TABLE bandas ADD INDEX i_bio (bio);\n\n-- Unir estilo y bio\nALTER TABLE bandas ADD INDEX i_estilo_bio (estilo, bio);\n\n-- Crear campo de tipo FULLTEXT\nALTER TABLE bandas ADD FULLTEXT INDEX fi_search (nombre, estilo bio);\n\n-- Eliminar \u00edndices\n-- Eliminamos el \u00edndice fi_search\nALTER TABLE bandas DROP INDEX fi_search;\n-- Eliminamos el \u00edndice i_estilo_bio\nALTER TABLE bandas DROP INDEX i_estilo_bio;\n\n-- Para llaves primarias y campos \u00fanicos se ha de utilizar DROP y CONSTRAIN simult\u00e1neamente\n-- Eliminar campo \u00fanico uq_estilo\nALTER TABLE bandas DROP CONSTRAINT uq_estilo;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo veremos una de las partes importantes de las bases de datos, se trata de los \u00edndices.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1076,"menu_order":26,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-1138","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En este cap\u00edtulo veremos una de las partes importantes de las bases de datos, se trata de los \u00edndices.","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=1138"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1138\/revisions"}],"predecessor-version":[{"id":1139,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1138\/revisions\/1139"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1076"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}