SlideShare una empresa de Scribd logo
1 de 7
SQL Question #1

 How can 2 views be used to delete data in
one of the views? V1 has two fields, K1 and
  K2. V2 has field K2 and I need to delete
   all rows in V2 based on K1 in V1 so the
 views can be joined v1.k2 = v2.k2 but how
   would the delete statement be written?
Choices?
1. delete from V2               2. delete from V2
    where K1 in (select V2.K1       where rowid in (select V2.rowid
               from V2, V1                     from V2, V1
               where V2.K2 =                   where V2.K2 =
      V1.K2);                      V1.K2);


3. delete from V2               4. delete from V2, V1
    where K2 in (select V2.K2       where V1.K2 = V2.K2;
               from V2, V1
               where V2.K2 =
      V1.K2);
Answers
 Both 2 and 3.
 2 is better because using rowid would make
  statement faster.
What is Rowid?
 A rowid is a pseudo column (like versions_xid),
  that uniquely identifies a row within a table, but not
  within a database.
 Every record has a unique ROWID within a
  database representing the physical location on
  disk where the record lives. Every record has a
  unique ROWID within a database representing the
  physical location on disk where the record lives.
 The ROWID gives us the physical address of a
  row and is the fastest way to access any row.
Common Case for Rowid
 Table V2 has a column "K1", which should
  be unique, but someone made a mistake
  and did not add a primary key or unique
  constraint, and there are now duplicate
  rows. We want to clean it up.
SQL Statement
delete from V2 a where exists (
  select 1 from V2 b where a.K1 = b.K1
      and b.ROWID < a.ROWID);
Explained
 The self-join query means "Delete all rows that
  have another row with the same K1, but "lesser"
  ROWID. Our choice of "lesser", while not logically
  required, so we prefer to preserve the row that is
  closer to the beginning of the table. The main point
  is without the ROWID, the only way to correct this
  table would be to create a new one based on the
  results of a query with GROUP BY K1, or to use
  other columns and methods to differentiate. While
  feasible, it might not be convenient for various
  reasons, so it is good to know the ROWID is
  available.

Más contenido relacionado

Más de Kai Liu (14)

Schedulers
SchedulersSchedulers
Schedulers
 
PL/SQL Example for IF .. ELSIF
PL/SQL Example for IF .. ELSIFPL/SQL Example for IF .. ELSIF
PL/SQL Example for IF .. ELSIF
 
Dms 2.0 Direction
Dms 2.0 DirectionDms 2.0 Direction
Dms 2.0 Direction
 
Dms 2.0 Plan Proposal
Dms 2.0 Plan ProposalDms 2.0 Plan Proposal
Dms 2.0 Plan Proposal
 
Toad
ToadToad
Toad
 
Dms Emailing Reports
Dms Emailing ReportsDms Emailing Reports
Dms Emailing Reports
 
QQ And Advance Query
QQ And Advance QueryQQ And Advance Query
QQ And Advance Query
 
Troubleshooting Batch Reports
Troubleshooting Batch ReportsTroubleshooting Batch Reports
Troubleshooting Batch Reports
 
Dms Reporting Criteria
Dms Reporting CriteriaDms Reporting Criteria
Dms Reporting Criteria
 
Dms Batch Reporting
Dms Batch ReportingDms Batch Reporting
Dms Batch Reporting
 
Dms Reporting Overview
Dms Reporting OverviewDms Reporting Overview
Dms Reporting Overview
 
Dms 2 Direction
Dms 2 DirectionDms 2 Direction
Dms 2 Direction
 
Dms Project
Dms ProjectDms Project
Dms Project
 
Dms Acknowledgements
Dms AcknowledgementsDms Acknowledgements
Dms Acknowledgements
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

SQL Question Tip

  • 1. SQL Question #1 How can 2 views be used to delete data in one of the views? V1 has two fields, K1 and K2. V2 has field K2 and I need to delete all rows in V2 based on K1 in V1 so the views can be joined v1.k2 = v2.k2 but how would the delete statement be written?
  • 2. Choices? 1. delete from V2 2. delete from V2 where K1 in (select V2.K1 where rowid in (select V2.rowid from V2, V1 from V2, V1 where V2.K2 = where V2.K2 = V1.K2); V1.K2); 3. delete from V2 4. delete from V2, V1 where K2 in (select V2.K2 where V1.K2 = V2.K2; from V2, V1 where V2.K2 = V1.K2);
  • 3. Answers  Both 2 and 3.  2 is better because using rowid would make statement faster.
  • 4. What is Rowid?  A rowid is a pseudo column (like versions_xid), that uniquely identifies a row within a table, but not within a database.  Every record has a unique ROWID within a database representing the physical location on disk where the record lives. Every record has a unique ROWID within a database representing the physical location on disk where the record lives.  The ROWID gives us the physical address of a row and is the fastest way to access any row.
  • 5. Common Case for Rowid  Table V2 has a column "K1", which should be unique, but someone made a mistake and did not add a primary key or unique constraint, and there are now duplicate rows. We want to clean it up.
  • 6. SQL Statement delete from V2 a where exists ( select 1 from V2 b where a.K1 = b.K1 and b.ROWID < a.ROWID);
  • 7. Explained  The self-join query means "Delete all rows that have another row with the same K1, but "lesser" ROWID. Our choice of "lesser", while not logically required, so we prefer to preserve the row that is closer to the beginning of the table. The main point is without the ROWID, the only way to correct this table would be to create a new one based on the results of a query with GROUP BY K1, or to use other columns and methods to differentiate. While feasible, it might not be convenient for various reasons, so it is good to know the ROWID is available.