{"id":274,"date":"2015-11-18T18:42:08","date_gmt":"2015-11-19T00:42:08","guid":{"rendered":"http:\/\/naps.com.mx\/blog\/?p=274"},"modified":"2017-11-16T12:15:51","modified_gmt":"2017-11-16T18:15:51","slug":"creacion-de-tablas-en-mysql","status":"publish","type":"post","link":"https:\/\/naps.com.mx\/blog\/creacion-de-tablas-en-mysql\/","title":{"rendered":"Creaci\u00f3n de tablas en MySql usando c\u00f3digo"},"content":{"rendered":"<p>Una vez <a href=\"http:\/\/naps.com.mx\/blog\/tercera-forma-normal-en-el-modelo-relacional-3fn\/\">dise\u00f1ada <\/a>nuestra base de datos ha llegado el momento de crearla. Y para esto hoy utilizaremos c\u00f3digo escrito en SQL(Lenguaje Estructurado de Consultas) utilizando MySQL para la creaci\u00f3n de tablas.<\/p>\n<div id=\"attachment_287\" style=\"width: 835px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/CrearBDMysql.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-287\" class=\"size-full wp-image-287\" src=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/CrearBDMysql.jpg\" alt=\"creaci\u00f3n de tablas en MySQL\" width=\"825\" height=\"550\" srcset=\"https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/CrearBDMysql.jpg 825w, https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/CrearBDMysql-300x200.jpg 300w\" sizes=\"auto, (max-width: 825px) 100vw, 825px\" \/><\/a><p id=\"caption-attachment-287\" class=\"wp-caption-text\">Analizamos la creaci\u00f3n de tablas en MySQL<\/p><\/div>\n<p><!--more--><\/p>\n<h3><\/h3>\n<h3>Creaci\u00f3n y uso de la Base de Datos<\/h3>\n<p>Para crear las tablas necesitamos una Base de Datos. Para crear la BD utilizamos el siguiente comando:<\/p>\n<pre class=\"theme:tomorrow-night lang:mysql decode:true \">CREATE DATABASE empresa;<\/pre>\n<p>Y obtendremos el siguiente resultado:<\/p>\n<p><a href=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createdb1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-276\" src=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createdb1.png\" alt=\"createdb1\" width=\"528\" height=\"98\" srcset=\"https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createdb1.png 528w, https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createdb1-300x56.png 300w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><\/a><\/p>\n<p>Una vez creada la BD procederemos a utilizarla:<\/p>\n<pre class=\"lang:default decode:true \">USE empresa;<\/pre>\n<p>Lo que nos arrojar\u00e1:<\/p>\n<p><a href=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/usedb2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-277\" src=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/usedb2.png\" alt=\"usedb2\" width=\"276\" height=\"69\" \/><\/a><\/p>\n<h3>Creaci\u00f3n de tablas<\/h3>\n<p>Ahora crearemos las tablas. Si tenemos la siguiente relaci\u00f3n normalizada:<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: center;\" colspan=\"3\" width=\"200\"><strong>Art\u00edculos<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"65\"><strong>PK<\/strong><\/td>\n<td width=\"68\"><\/td>\n<td width=\"66\"><\/td>\n<\/tr>\n<tr>\n<td width=\"65\"><strong>C\u00f3digo de art\u00edculo<\/strong><\/td>\n<td width=\"68\"><strong>Nombre de art\u00edculo<\/strong><\/td>\n<td width=\"66\"><strong>Precio unitario del art\u00edculo<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"65\">01<\/td>\n<td width=\"68\">CAMISA<\/td>\n<td width=\"66\">50<\/td>\n<\/tr>\n<tr>\n<td width=\"65\">02<\/td>\n<td width=\"68\">ZAPATOS<\/td>\n<td width=\"66\">80<\/td>\n<\/tr>\n<tr>\n<td width=\"65\">05<\/td>\n<td width=\"68\">MESA<\/td>\n<td width=\"66\">100<\/td>\n<\/tr>\n<tr>\n<td width=\"65\">09<\/td>\n<td width=\"68\">TINTA<\/td>\n<td width=\"66\">25<\/td>\n<\/tr>\n<tr>\n<td width=\"65\">13<\/td>\n<td width=\"68\">CUADRO<\/td>\n<td width=\"66\">90<\/td>\n<\/tr>\n<tr>\n<td width=\"65\">05<\/td>\n<td width=\"68\">MESA<\/td>\n<td width=\"66\">100<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Y deseamos crear la tabla utilizando MySQL entonces el c\u00f3digo ser\u00eda:<\/p>\n<pre class=\"lang:default decode:true \">CREATE TABLE articulos (codigo_articulo VARCHAR(8),\r\n\r\n     nombre_articulo VARCHAR(30) NOT NULL,\r\n\r\n     precio_unitario NUMERIC(8,3));<\/pre>\n<p>Con el siguiente resultado:<\/p>\n<p><a href=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-278\" src=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable3.png\" alt=\"createtable3\" width=\"869\" height=\"130\" srcset=\"https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable3.png 869w, https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable3-300x45.png 300w\" sizes=\"auto, (max-width: 869px) 100vw, 869px\" \/><\/a><\/p>\n<p>Lo mismo hacernos con el resto de las relaciones:<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: center;\" colspan=\"2\" width=\"130\"><strong>Clientes<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"61\"><strong>Pk<\/strong><\/td>\n<td width=\"68\"><\/td>\n<\/tr>\n<tr>\n<td width=\"61\"><strong>C\u00f3digo de cliente<\/strong><\/td>\n<td width=\"68\"><strong>Nombre de cliente<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"61\">01<\/td>\n<td width=\"68\">PEREZ<\/td>\n<\/tr>\n<tr>\n<td width=\"61\">33<\/td>\n<td width=\"68\">GARC\u00cdA<\/td>\n<\/tr>\n<tr>\n<td width=\"61\">45<\/td>\n<td width=\"68\">GOMEZ<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: center;\" colspan=\"7\" width=\"463\"><strong>Facturas<\/strong><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\" width=\"138\"><strong>PK<\/strong><\/td>\n<td width=\"63\"><\/td>\n<td width=\"65\"><\/td>\n<td width=\"61\"><\/td>\n<td width=\"68\"><\/td>\n<td width=\"68\"><\/td>\n<\/tr>\n<tr>\n<td width=\"69\"><strong>Sucursal<\/strong><\/td>\n<td width=\"69\"><strong>N\u00famero de factura<\/strong><\/td>\n<td width=\"63\"><strong>Fecha de la factura<\/strong><\/td>\n<td width=\"65\"><strong>Forma de pago<\/strong><\/td>\n<td width=\"61\"><strong>C\u00f3digo de cliente<\/strong><\/td>\n<td width=\"68\"><strong>Nombre de cliente<\/strong><\/td>\n<td width=\"68\"><strong>Total de factura<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"69\">01<\/td>\n<td width=\"69\">100<\/td>\n<td width=\"63\">1\/10\/15<\/td>\n<td width=\"65\">Cr\u00e9dito<\/td>\n<td width=\"61\">01<\/td>\n<td width=\"68\">PEREZ<\/td>\n<td width=\"68\">440<\/td>\n<\/tr>\n<tr>\n<td width=\"69\">01<\/td>\n<td width=\"69\">101<\/td>\n<td width=\"63\">2\/10\/15<\/td>\n<td width=\"65\">Contado<\/td>\n<td width=\"61\">33<\/td>\n<td width=\"68\">GARC\u00cdA<\/td>\n<td width=\"68\">100<\/td>\n<\/tr>\n<tr>\n<td width=\"69\">02<\/td>\n<td width=\"69\">100<\/td>\n<td width=\"63\">3\/10\/15<\/td>\n<td width=\"65\">Cr\u00e9dito<\/td>\n<td width=\"61\">45<\/td>\n<td width=\"68\">GOMEZ<\/td>\n<td width=\"68\">550<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: center;\" colspan=\"6\" width=\"410\"><strong>Detalle de Factura<\/strong><\/td>\n<\/tr>\n<tr>\n<td colspan=\"3\" width=\"202\"><strong>PK<\/strong><\/td>\n<td width=\"73\"><\/td>\n<td width=\"66\"><\/td>\n<td width=\"69\"><\/td>\n<\/tr>\n<tr>\n<td width=\"69\"><strong>Sucursal<\/strong><\/td>\n<td width=\"68\"><strong>N\u00famero de factura<\/strong><\/td>\n<td width=\"65\"><strong>C\u00f3digo de art\u00edculo<\/strong><\/td>\n<td width=\"73\"><strong>Cantidad del art\u00edculo<\/strong><\/td>\n<td width=\"66\"><strong>Precio unitario del art\u00edculo<\/strong><\/td>\n<td width=\"69\"><strong>Subtotal del art\u00edculo<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"69\">01<\/td>\n<td width=\"68\">100<\/td>\n<td width=\"65\">01<\/td>\n<td width=\"73\">2<\/td>\n<td width=\"66\">50<\/td>\n<td width=\"69\">100<\/td>\n<\/tr>\n<tr>\n<td width=\"69\">01<\/td>\n<td width=\"68\">100<\/td>\n<td width=\"65\">02<\/td>\n<td width=\"73\">3<\/td>\n<td width=\"66\">80<\/td>\n<td width=\"69\">240<\/td>\n<\/tr>\n<tr>\n<td width=\"69\">01<\/td>\n<td width=\"68\">100<\/td>\n<td width=\"65\">05<\/td>\n<td width=\"73\">1<\/td>\n<td width=\"66\">100<\/td>\n<td width=\"69\">100<\/td>\n<\/tr>\n<tr>\n<td width=\"69\">01<\/td>\n<td width=\"68\">101<\/td>\n<td width=\"65\">09<\/td>\n<td width=\"73\">4<\/td>\n<td width=\"66\">25<\/td>\n<td width=\"69\">100<\/td>\n<\/tr>\n<tr>\n<td width=\"69\">02<\/td>\n<td width=\"68\">100<\/td>\n<td width=\"65\">13<\/td>\n<td width=\"73\">5<\/td>\n<td width=\"66\">90<\/td>\n<td width=\"69\">450<\/td>\n<\/tr>\n<tr>\n<td width=\"69\">02<\/td>\n<td width=\"68\">100<\/td>\n<td width=\"65\">05<\/td>\n<td width=\"73\">1<\/td>\n<td width=\"66\">100<\/td>\n<td width=\"69\">100<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\" width=\"137\">Fk<\/td>\n<td width=\"65\">Fk<\/td>\n<td width=\"73\"><\/td>\n<td width=\"66\"><\/td>\n<td width=\"69\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Utilizando el c\u00f3digo:<\/p>\n<pre class=\"lang:default decode:true\">CREATE TABLE clientes (codigo_cliente VARCHAR(8),\r\n\r\n     nombre_cliente VARCHAR(30) NOT NULL);<\/pre>\n<pre class=\"lang:default decode:true  \">CREATE TABLE facturas (sucursal NUMERIC(2),\r\n\r\n     numero_factura NUMERIC(2),\r\n\r\n     fecha_factura DATE,\r\n\r\n     forma_pago VARCHAR(8),\r\n\r\n     codigo_cliente VARCHAR(8),\r\n\r\n     total_factura NUMERIC(14,2));<\/pre>\n<pre class=\"lang:default decode:true\">CREATE TABLE detalle_factura (sucursal NUMERIC(2),\r\n\r\n     numero_factura NUMERIC(2),\r\n\r\n     codigo_articulo VARCHAR(8),\r\n\r\n     cantidad_articulo NUMERIC(8,3) NOT NULL,\r\n\r\n     precio_unitario NUMERIC(8,3),\r\n\r\n     subtotal NUMERIC(9,3));<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Por lo que obtendremos:<\/p>\n<p><a href=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-280\" src=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable4.png\" alt=\"createtable4\" width=\"808\" height=\"117\" srcset=\"https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable4.png 808w, https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable4-300x43.png 300w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-281\" src=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable5.png\" alt=\"createtable5\" width=\"736\" height=\"227\" srcset=\"https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable5.png 736w, https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable5-300x93.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-282\" src=\"http:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable6.png\" alt=\"createtable6\" width=\"809\" height=\"221\" srcset=\"https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable6.png 809w, https:\/\/naps.com.mx\/blog\/wp-content\/uploads\/2015\/11\/createtable6-300x82.png 300w\" sizes=\"auto, (max-width: 809px) 100vw, 809px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Comentarios<\/strong>:<\/p>\n<p>Tipos de datos utilizados en este ejercicio:<\/p>\n<p><strong>VARCHAR<\/strong>(M). Dato de clase Cadena; almacena una cadena de longitud variable de 0 a 255 caracteres. (Tambi\u00e9n se utilizan: CHAR(M), TEXT y LONGTEXT entre otros).<\/p>\n<p><strong>NUMERIC<\/strong>(M,D). Dato de clase Num\u00e9rico; almacena un n\u00famero en coma flotante almacenado como cadena en un rango que va de +2,225073E-308 hasta +-1,7976931E-308. (Tambi\u00e9n se puede utilizar DECIMAL(M,D), DOUBLE(M,D), FLOAT(M,D) y en el caso de enteros INT y BIGINT).<\/p>\n<p><strong>DATE<\/strong>. Dato de clase Fecha y Hora; DATE solo almacena la fecha en formato AAAA-MM-DD en un rango de 1000-01-01 a 9999-12-31. (Tambi\u00e9n se puede utilizar TIME, DATETIME, TIMESTAMP y YEAR).<\/p>\n<p><em>Nota<\/em>: M indica la m\u00e1xima anchura de visualizaci\u00f3n y P el n\u00famero de d\u00edgitos que siguen al punto decimal.<\/p>\n<p><strong>NOT NULL<\/strong><\/p>\n<p>Indica que no se permiten valores NULL en la columna.<\/p>\n<p>&nbsp;<\/p>\n<p>Conclusiones<\/p>\n<p>El presente ejercicio nos permiti\u00f3 analizar la creaci\u00f3n de tablas en MySQL, utilizando el Lenguaje Estructurado de Consultas para realizar el c\u00f3digo. Analizamos tambien los tipos de datos que utilizamos.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Una vez dise\u00f1ada nuestra base de datos ha llegado el momento de crearla. Y para esto hoy utilizaremos c\u00f3digo escrito en SQL(Lenguaje Estructurado de Consultas) utilizando MySQL para la creaci\u00f3n de tablas.<\/p>\n","protected":false},"author":1,"featured_media":286,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"amp_status":"","footnotes":""},"categories":[49],"tags":[94,95,93],"class_list":["post-274","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bases-de-datos","tag-create-database","tag-create-table","tag-curso-mysql"],"_links":{"self":[{"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/comments?post=274"}],"version-history":[{"count":11,"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":1071,"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/posts\/274\/revisions\/1071"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/media\/286"}],"wp:attachment":[{"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/media?parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/categories?post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/naps.com.mx\/blog\/wp-json\/wp\/v2\/tags?post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}