SlideShare uma empresa Scribd logo
1 de 3
Baixar para ler offline
                                                    Departamento de Engenharia Informática            2008/2009 

                                                    Bases de Dados                                        1º semestre 
                                                     Lab 10: Transacções 
                                                                
 
                                                                  
                                                             1ª Parte 
 
1. Abra uma linha de comando para o sistema e use o script create‐bank.sql para recriar 
    a base de dados “bank”. 
     
2. Escreva uma consulta para obter os dados das contas do cliente Smith. 
         
3. O cliente Smith pretende transferir 500€ a conta A‐444 para a conta A‐215. Inicie uma 
    transacção com o comando START TRANSACTION; 
         
4. Coloque 500€ na conta A‐215. 
         
5. Consulte os saldos das contas do cliente Smith. Neste momento quanto dinheiro tem o 
    cliente Smith no banco? 
         
6. Mantendo a transacção em aberto, abra uma segunda linha de comando do sistema e 
    ligue‐se à mesma base de dados. 
         
7. Consulte  os  saldos  das contas  do  cliente Smith.  Afinal  quanto  dinheiro tem o  cliente 
    Smith no banco? 
         
8. Na primeira linha de comando, onde está a correr a transacção, retire 500€ da conta 
    A‐444. 
         
9. Na segunda linha de comando consulte os saldos das contas do cliente Smith. 
         
10. Na primeira linha de comando confirme a transacção. 
 
11. Na  segunda  linha  de  comando  consulte  novamente  os  saldos  das  contas  do  cliente 
    Smith. Confirme os resultados da transacção. 
 
 
                                            2ª Parte 
 
12. O  cliente  Smith  acabou  de  abastecer  25€  de  gasolina  numa  estação  de  serviço  e  vai 
    pagar  com  multibanco  (conta  A‐444).  Ao  mesmo  tempo  a  sua  esposa,  que  tem  um 
    cartão multibanco para a mesma conta, vai levantar 50€ numa máquina multibanco do 
    outro lado da cidade. 


IST/DEI                                                  Pág. 1 de 3                                              
 
BD 


     
13. Na primeira linha de comando, inicie uma transacção. 
         
14. Na segunda linha de comando, inicie uma transacção. 
         
15. Na primeira transacção retire 25€ da conta (abastecimento). 
         
16. Na  segunda  transacção  retire  50€  da  conta  (levantamento).  O  que  se  passa  quando 
    tenta fazer isto? Porquê? 
         
17. A linha telefónica da estação de serviço está intermitente e a ligação cai. O pagamento 
    que estava a ser feito tem de ser cancelado. Cancele essa transacção. 
         
18. Ao  mesmo  tempo  que  faz  a  alínea  17,  repare  no  que  acontece  na  segunda  linha  de 
    comando. Como explica este fenómeno? 
         
19. Confirme a transacção de levantamento. 
         
20. Consulte os saldos das contas do cliente Smith. 
         
 
                                            3ª Parte 
         
21. No que se segue utilize apenas uma linha de comando. 
     
22. Inicie uma transacção. 
 
23. Altere a tabela depositor e remova todas as chaves estrangeiras. 
    Use d depositor para ver que chaves estrangeiras existem. 
    Use comandos na forma: ALTER TABLE (…) DROP CONSTRAINT (…) 
         
24. Escreva  um  comando  SQL  para  apagar  da  tabela  account  todas  as  contas  do  cliente 
    Smith. Execute esse comando. 
         
25. Escreva uma consulta para verificar se as contas do cliente Smith deixaram de existir. 
 
26. Cancele a transacção. 
         
27. Verifique que as contas do cliente Smith foram repostas. 
         
28. Verifique que as chaves estrangeiras da tabela depositor foram repostas. 
         


IST/DEI                                  Pág. 2 de 3                                          
 
BD 


                                          4ª Parte 
 
1. Considere  o  seguinte  grafo  de  precedências  que  representa  os  conflitos  existentes 
   num  escalonamento  que  envolve  cinco  transacções.  O  escalonamento  é  serializável? 
   Justifique. 




                                                           
 
2. Considere as seguintes transacções: 
 




                                                                                   
 
    Inicialmente a base de dados tem A = 0 e B = 0. Para que a base de dados se mantenha 
    consistente é necessário que A = 0 ou B = 0. 
 
    (a) Mostre  que  os  escalonamentos  em  série  destas  duas  transacções  mantêm  a 
        consistência da base de dados. 
    (b) Apresente um exemplo de um escalonamento concorrente de T1 e T2 que não seja 
        serializável. Explique porquê. 
    (c) Existe  algum  escalonamento  concorrente  de  T1  e  T2  que  seja  serializável? 
        Justifique. 
    (d) Adicione instruções de lock‐S(), lock‐X() e unlock() às duas transacções de modo a 
        que obedeçam ao protocolo em duas fases. 
    (e) Com o resultado da alínea (d) dê um exemplo de um escalonamento concorrente 
        que resulte em deadlock. 
    (f) Adicione  instruções  de  lock()  e  unlock()  às  duas  transacções  de  modo  a  que 
        obedeçam a um protocolo em árvore, em que a árvore é simplesmente A → B. 
    (g) Com o resultado da alínea (f) são possíveis deadlocks? Justifique. 




IST/DEI                                 Pág. 3 de 3                                        
 

Mais conteúdo relacionado

Destaque

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destaque (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Bases de Dados Lab 10: Transações

  • 1.                                                     Departamento de Engenharia Informática  2008/2009                                           Bases de Dados                                        1º semestre         Lab 10: Transacções        1ª Parte    1. Abra uma linha de comando para o sistema e use o script create‐bank.sql para recriar  a base de dados “bank”.    2. Escreva uma consulta para obter os dados das contas do cliente Smith.    3. O cliente Smith pretende transferir 500€ a conta A‐444 para a conta A‐215. Inicie uma  transacção com o comando START TRANSACTION;    4. Coloque 500€ na conta A‐215.    5. Consulte os saldos das contas do cliente Smith. Neste momento quanto dinheiro tem o  cliente Smith no banco?    6. Mantendo a transacção em aberto, abra uma segunda linha de comando do sistema e  ligue‐se à mesma base de dados.    7. Consulte  os  saldos  das contas  do  cliente Smith.  Afinal  quanto  dinheiro tem o  cliente  Smith no banco?    8. Na primeira linha de comando, onde está a correr a transacção, retire 500€ da conta  A‐444.    9. Na segunda linha de comando consulte os saldos das contas do cliente Smith.    10. Na primeira linha de comando confirme a transacção.    11. Na  segunda  linha  de  comando  consulte  novamente  os  saldos  das  contas  do  cliente  Smith. Confirme os resultados da transacção.      2ª Parte    12. O  cliente  Smith  acabou  de  abastecer  25€  de  gasolina  numa  estação  de  serviço  e  vai  pagar  com  multibanco  (conta  A‐444).  Ao  mesmo  tempo  a  sua  esposa,  que  tem  um  cartão multibanco para a mesma conta, vai levantar 50€ numa máquina multibanco do  outro lado da cidade.  IST/DEI  Pág. 1 de 3     
  • 2. BD    13. Na primeira linha de comando, inicie uma transacção.    14. Na segunda linha de comando, inicie uma transacção.    15. Na primeira transacção retire 25€ da conta (abastecimento).    16. Na  segunda  transacção  retire  50€  da  conta  (levantamento).  O  que  se  passa  quando  tenta fazer isto? Porquê?    17. A linha telefónica da estação de serviço está intermitente e a ligação cai. O pagamento  que estava a ser feito tem de ser cancelado. Cancele essa transacção.    18. Ao  mesmo  tempo  que  faz  a  alínea  17,  repare  no  que  acontece  na  segunda  linha  de  comando. Como explica este fenómeno?    19. Confirme a transacção de levantamento.    20. Consulte os saldos das contas do cliente Smith.      3ª Parte    21. No que se segue utilize apenas uma linha de comando.    22. Inicie uma transacção.    23. Altere a tabela depositor e remova todas as chaves estrangeiras.  Use d depositor para ver que chaves estrangeiras existem.  Use comandos na forma: ALTER TABLE (…) DROP CONSTRAINT (…)    24. Escreva  um  comando  SQL  para  apagar  da  tabela  account  todas  as  contas  do  cliente  Smith. Execute esse comando.    25. Escreva uma consulta para verificar se as contas do cliente Smith deixaram de existir.    26. Cancele a transacção.    27. Verifique que as contas do cliente Smith foram repostas.    28. Verifique que as chaves estrangeiras da tabela depositor foram repostas.    IST/DEI  Pág. 2 de 3     
  • 3. BD  4ª Parte    1. Considere  o  seguinte  grafo  de  precedências  que  representa  os  conflitos  existentes  num  escalonamento  que  envolve  cinco  transacções.  O  escalonamento  é  serializável?  Justifique.      2. Considere as seguintes transacções:        Inicialmente a base de dados tem A = 0 e B = 0. Para que a base de dados se mantenha  consistente é necessário que A = 0 ou B = 0.    (a) Mostre  que  os  escalonamentos  em  série  destas  duas  transacções  mantêm  a  consistência da base de dados.  (b) Apresente um exemplo de um escalonamento concorrente de T1 e T2 que não seja  serializável. Explique porquê.  (c) Existe  algum  escalonamento  concorrente  de  T1  e  T2  que  seja  serializável?  Justifique.  (d) Adicione instruções de lock‐S(), lock‐X() e unlock() às duas transacções de modo a  que obedeçam ao protocolo em duas fases.  (e) Com o resultado da alínea (d) dê um exemplo de um escalonamento concorrente  que resulte em deadlock.  (f) Adicione  instruções  de  lock()  e  unlock()  às  duas  transacções  de  modo  a  que  obedeçam a um protocolo em árvore, em que a árvore é simplesmente A → B.  (g) Com o resultado da alínea (f) são possíveis deadlocks? Justifique.  IST/DEI  Pág. 3 de 3