Advertisement
teslariu

Codigo clase SQL

Nov 3rd, 2023
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.75 KB | None | 0 0
  1. /*
  2. Create table Productos(
  3. idProducto int(11) unsigned not null auto_increment primary key,
  4. Nombre varchar(50) not null,
  5. Precio double,
  6. Marca varchar(45) not null,
  7. Categoria varchar(30) not null,
  8. Stock int(6) not null,  
  9. Disponible boolean default false
  10. );
  11. */
  12. -- drop table if exist
  13. -- create table Talles (talle ENUM('S','M','L','XL','XXL') NOT NULL DEFAULT 'L');
  14.  
  15. -- alter: modifica una TABLA
  16. -- add agrega una columna
  17. -- DROP elimina una columna
  18. -- DELETE elimina un registro
  19. -- change modifica EL NOMBRE DE una columna y OPCIONALMENTE la longitud del tipo de dato
  20. -- MODIFY: MODIFICA EL TIPO DE DATO SOLAMENTE
  21. -- RENAME: renombra una tabla
  22. /* ALTER TABLE <nombre tabla> RENAME >otro nombre>
  23.    RENAME TABLE <nombre tabla> TO <otro nombre>*/
  24.  
  25. -- delete from cursosql  -- > POSIBLE INYECCION SQL
  26.  
  27. -- RENAME TABLE productos TO articulos;
  28.  
  29. -- articulosidProductotallesTER TABLE articulos DROP primary key;
  30.  
  31.  
  32. # alter table talles add column idProducto int not null unique;
  33. # alter table talles drop primary key;
  34. # alter table talles add column idTalle int(2) not null primary key;
  35. # select * from talles;
  36.  
  37. --  ----------------------
  38. -- alter table talles add constraint idProducto
  39. -- foreign key(idProducto) REFERENCES articulos(idProducto);
  40. -- -----------------------------
  41. # INSERCION DE DATOS
  42. # FORMA COMPLETA
  43. -- INSERT INTO articulos(Nombre,Precio,Marca,Categoria,Stock,disponible,comentarios)
  44. -- VALUES ("IPHONE 11", 789.56, "Apple", "Smartphone", 122, false, "Importado de EEUU");
  45. -- select * from articulos
  46.  
  47. # FORMA SQL
  48. -- INSERT INTO articulos
  49. -- SET Nombre="Iphone 12", Precio=880, Marca="Apple", Categoria="Smartphone", Stock=1000,
  50. -- disponible= True, comentarios="Con cargador y auriculares";
  51.  
  52. -- FORMA SIMPLIFICADA
  53. -- insert into articulos
  54. -- VALUES (3,"Iphone 10",1020.66,"Apple","Smartphone",777,false,"Sin cargador,auriculares extra");
  55. -- select * from articulos
  56.  
  57. -- select count(*) from nacimientos
  58.  
  59. -- OTROS FORMATOS DE BBDD: en Excel  --> csv, formato "WEB":  JSON (JavaScript Object Notation)
  60. -- select IdPedido, Cargo, Cargo * 1.21 as "Cargo con IVA" from pedidos_neptuno order by Cargo, IdPedido DESC
  61.  
  62. /*
  63. alter table clientes
  64. modify COD_CLIENTE varchar(5) not null primary key,
  65. modify EMPRESA varchar(25) not null,
  66. modify DIRECCION varchar(25) not null,
  67. modify CIUDAD varchar(20) not null,
  68. modify TELEFONO int unsigned not null,
  69. modify RESPONSABLE varchar(20) not null;
  70. */
  71. -- Sentencias: SELECT: seleccionar, ordenar, agrupar y filtrar
  72. -- clausulas: FROM, WHERE, GROUP BY,HAVING, ORDER BY
  73. -- otras cláusulas limit
  74.  
  75.  
  76. -- select * from nacimientos order by talla desc limit 5
  77.  
  78. -- select COD_PRODUCTO,SECCION,
  79. -- NOMBRE as "Nombre Producto",IMPORTADO,ORIGEN,DIA,MES,ANO
  80. -- from productos  limit 5 offset 9;
  81.  
  82. -- Operadores de comparacion: =  <   > <=   >=   <>
  83.  
  84. /*
  85. select COD_PRODUCTO,nombre
  86.     from productos
  87. where
  88.     COD_PRODUCTO = 4
  89.     OR COD_PRODUCTO = 8
  90.     OR COD_PRODUCTO = 10;
  91. */
  92.  
  93. -- select * from nacimientos where EDAD_MADRE < 13 OR EDAD_MADRE > 50
  94.  
  95. -- 2 + 7 * 4 = 30   --> operadores aritmeticos
  96. -- (2 + 7) * 4 = 36
  97. -- operadores de comparacion: < > = .....
  98. -- operadores logicos; or and not
  99. -- Precedencia: aritmeticos, comparacion, logicos
  100.  
  101. # ARROJAN EL MISMO RESULTADO
  102. -- a) select * from nacimientos where not (EDAD_MADRE < 13 OR EDAD_MADRE > 50);
  103. -- a) select * from nacimientos where EDAD_MADRE between 13 and 50
  104.  
  105. -- b) select * from nacimientos where EDAD_MADRE < 14 OR EDAD_MADRE > 50;  -- ....11,13,51,52,....
  106. -- b) select * from nacimientos where EDAD_MADRE NOT between 14 and 50
  107.  
  108. -- c) select * from nacimientos where EDAD_MADRE = 13 or EDAD_MADRE = 16 or EDAD_MADRE = 18;
  109. -- c) select * from nacimientos where edad_madre in (13,16,18) order by EDAD_MADRE;
  110.  
  111. --  OPERADORES LIKE/NOT LIKE  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement