Carmine_Annunziata

MysqlEcommerceTest

May 10th, 2022 (edited)
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.15 KB | None | 0 0
  1. drop database if exists e_commerce;
  2.  
  3. create database E_commerce;
  4. use E_commerce;
  5.  
  6. drop table if exists Utente;
  7. create table Prodotto(
  8.     Codice integer primary key auto_increment,
  9.     nome varchar(250) unique,
  10.     descrizione varchar(250),
  11.     codiceOrdine integer default null,
  12.     foreign key (codiceOrdine) references Ordine(Codice)
  13. );
  14.  
  15. create table Ordine(
  16.     Codice integer primary key auto_increment,
  17.     dataOrdine DATE,
  18.     codiceUtente varchar(250),
  19.     foreign key (codiceUtente) references Utente(CodiceFiscale) on delete cascade
  20. );
  21.  
  22. create table Utente(
  23.     CodiceFiscale varchar(250) primary key,
  24.     nominativo varchar(250)
  25. );
  26.  
  27. create table CategoriaProdotto(
  28.     Codice integer primary key auto_increment,
  29.     nome varchar(250)
  30. );
  31.  
  32. create table Prodotto_Categoria(
  33.     CodiceProdotto  integer,
  34.     CodiceCategoria integer,
  35.     foreign key (CodiceProdotto) references Prodotto(Codice) on delete cascade,
  36.     foreign key (CodiceCategoria) references CategoriaProdotto(Codice) on delete cascade,
  37.     unique(CodiceProdotto,CodiceCategoria)
  38. );
  39.  
  40. insert into prodotto(nome,descrizione,codiceOrdine) values
  41. ("aaa","aaa",null),
  42. ("bbb","bbb",null),
  43. ("ccc","ccc",null),
  44. ("ddd","ddd",null);
  45.  
  46. insert into CategoriaProdotto(nome) values
  47. ("aaa"),
  48. ("bbb"),
  49. ("ccc"),
  50. ("ddd");
  51.  
  52. insert into Prodotto_Categoria(CodiceProdotto,CodiceCategoria) values
  53. (1,2),
  54. (1,3),
  55. (1,4),
  56. (2,2),
  57. (2,3),
  58. (2,4);
  59.  
  60. insert into Utente(CodiceFiscale,nominativo) values
  61. ("aaa","aaa aaa"),
  62. ("bbb","bbb bbb"),
  63. ("ccc","ccc ccc"),
  64. ("ddd","ddd ddd"),
  65. ("eee","eee eee");
  66.  
  67.  
  68. insert into prodotto(nome,descrizione,codiceOrdine) values
  69. ("eee","aaa",1),
  70. ("fff","bbb",2),
  71. ("ggg","ccc",3),
  72. ("hhh","ddd",4);
  73.  
  74. insert into Ordine(dataOrdine,codiceUtente) values
  75. ("2001-05-25","aaa"),
  76. ("2002-06-26","ccc"),
  77. ("2004-10-22","bbb"),
  78. ("1201-05-25","bbb"),
  79. ("2001-05-19","ccc"),
  80. ("1001-05-25","aaa"),
  81. ("2003-05-26","bbb"),
  82. ("2031-12-25","aaa");
  83.  
  84. select * from Ordine where codiceUtente = "aaa";
  85. select * from Ordine
  86.     left join Prodotto on Ordine.Codice = Prodotto.codiceOrdine
  87.     join Utente on Ordine.codiceUtente = Utente.CodiceFiscale
  88.     where Utente.CodiceFiscale = "aaa";
Add Comment
Please, Sign In to add comment