The social network

Il problema

Schema relazionale

Rel_network

  • UTENTI(IDutente, Cognome, Nome, DataNascita, Username, Password, DataIscrizione, Stato, …)
  • AMICIZIE(utenteID1, utenteID2, Data/Ora, Stato)
  • PARAGRAFI(IDparagrafo, Titolo, Testo, Data/Ora, …, utenteID)
  • MIPIACEP(paragrafoID, utenteID, Data/Ora, …)
  • COMMENTI(IDcommento, Titolo, Testo, Data/Ora, …, paragrafoID, utenteID)
  • MIPIACEC(commentoID, utenteID, Data/Ora, …)

Query

  1. Gli utenti, in ordine alfabetico
    SELECT   *
    FROM     UTENTI
    ORDER BY Cognome, Nome
  2. I paragrafi di un certo utente
    SELECT PARAGRAFI.*
    FROM   PARAGRAFI
    WHERE  utenteID = '$codice'
  3. Le coppie di amici (richiedente, ricevente) e da quando
    SELECT   U1.*, U2.*, DATA
    FROM     (UTENTI AS U1 INNER JOIN AMICIZIE ON U1.IDutente = AMICIZIE.utenteID1)
              INNER JOIN UTENTI AS U2 ON AMICIZIE.utenteID2 = U2.IDutente
    ORDER BY U1.Cognome, U1.Nome, U2.Cognome, U2.Nome
  4. Tutte le coppie di amici in ordine di Username
    SELECT   U1.username AS User1, U2.username AS User2
    FROM     (UTENTI AS U1 INNER JOIN AMICIZIE ON U1.IDutente = AMICIZIE.utenteID1)
              INNER JOIN UTENTI AS U2 ON AMICIZIE.utenteID2 = U2.IDutente
    UNION
    SELECT   U2.username, U1.username
    FROM     (UTENTI AS U1 INNER JOIN AMICIZIE ON U1.IDutente = AMICIZIE.utenteID1)
              INNER JOIN UTENTI AS U2 ON AMICIZIE.utenteID2 = U2.IDutente
    ORDER BY User1, User2
  5. Tutti gli amici di un certo utente in ordine alfabetico
    SELECT   UTENTI . * , DATA
    FROM     UTENTI INNER JOIN AMICIZIE ON UTENTI.IDutente = AMICIZIE.utenteID1
    WHERE    AMICIZIE.utenteID2 = X
    UNION
    SELECT   UTENTI . * , DATA
    FROM     UTENTI INNER JOIN AMICIZIE ON UTENTI.IDutente = AMICIZIE.utenteID2
    WHERE    AMICIZIE.utenteID1 = X
    ORDER BY Cognome, Nome, DATA
  6. Gli utenti che hanno apprezzato un certo paragrafo
    SELECT   UTENTI.IDutente, UTENTI.Cognome, UTENTI.Nome
    FROM     UTENTI INNER JOIN MIPIACEP ON UTENTI.IDutente = MIPIACEP.utenteID
    WHERE    MIPIACEP.paragrafoID = [Codice paragrafo?]
    ORDER BY Cognome, Nome
  7. I commenti, con gli autori, di un certo paragrafo
    SELECT   COMMENTI.DATA, COMMENTI.Titolo, COMMENTI.Testo,
             UTENTI.IDutente, UTENTI.Cognome, UTENTI.Nome
    FROM     COMMENTI INNER JOIN UTENTI ON COMMENTI.utenteID = UTENTI.IDutente
    WHERE    COMMENTI.paragrafoID = [Codice paragrafo?]
    ORDER BY COMMENTI.DATA DESC
  8. Quante richieste di amicizia effettuate da un certo utente (tramite il codice)
    SELECT Count(*) AS Numero
    FROM   AMICIZIE
    WHERE  utenteID1 = [Codice?]
  9. Quante richieste di amicizia effettuate da ogni utente (il codice)
    SELECT   utenteID1 AS 'Codice utente', Count(*) AS Numero
    FROM     AMICIZIE
    GROUP BY utenteID1
    ORDER BY utenteID1
  10. Quante richieste di amicizia effettuate da ogni utente e in questo momento attive
    SELECT   utenteID1 AS 'Codice utente', Count(*) AS Numero
    FROM     AMICIZIE
    WHERE    Stato = 1
    GROUP BY utenteID1
    ORDER BY utenteID1
  11. Quanti commenti e quanti apprezzamenti per ogni paragrafo
    SELECT PARAGRAFI.*,
             (SELECT Count(*)
              FROM   COMMENTI
              WHERE  COMMENTI.paragrafoID = PARAGRAFI.IDparagrafo) AS 'Numero commenti' ,
             (SELECT Count(*)
              FROM   MIPIACEP
              WHERE  MIPIACEP.paragrafoID = PARAGRAFI.IDparagrafo) AS 'Numero mi piace'
    FROM     PARAGRAFI
    ORDER BY PARAGRAFI.DATA
  12. I codici dei 10 paragrafi più commentati
    SELECT   TOP 10 paragrafoID AS 'Codice paragrafo', Count(*) AS Numero
    FROM     COMMENTI
    GROUP BY paragrafoID
    ORDER BY Numero DESC
  13. I codici dei 10 utenti più commentati
    SELECT   TOP 10 PARAGRAFI.utenteID AS [Codice utente], Count(*) AS Numero
    FROM     PARAGRAFI INNER JOIN COMMENTI ON PARAGRAFI.IDparagrafo = COMMENTI.paragrafoID
    GROUP BY utenteID
    ORDER BY Count(*) DESC
  14. I 10 utenti più commentati
    SELECT   TOP 10 IDutente AS 'Codice utente', Cognome, Nome, Count(*) AS Numero
    FROM     (UTENTI INNER JOIN PARAGRAFI ON UTENTI.IDutente = PARAGRAFI.utenteID)
              INNER JOIN COMMENTI ON PARAGRAFI.IDparagrafo = COMMENTI.paragrafoID
    GROUP BY IDutente, Cognome, Nome
    ORDER BY Numero DESC
  15. I 10 utenti più apprezzati per i paragrafi
    SELECT   TOP 10 IDutente AS [Codice utente], Cognome, Nome, Count(*) AS Numero
    FROM     (UTENTI INNER JOIN PARAGRAFI ON UTENTI.IDutente = PARAGRAFI.utenteID)
              INNER JOIN MIPIACEP ON PARAGRAFI.IDparagrafo = MIPIACEP.paragrafoID
    GROUP BY IDutente, Cognome, Nome
    ORDER BY Count(*) DESC