Dr. Baranyai László

 

Hiányzó adatok SQL adatbázisban

júl 9, 21:08, |

 

Adatbázisokkal dolgozó alkalmazásokban szükség lehet az adatok ellenőrzésére, például hiányzó sorok detektálására. A feladat első közelítésben abszurdnak tűnik, hiszen ha nem létezik, hogyan is lehetne megtalálni? Aki tudja a megoldást, annak már könnyű. A többiek kutathatnak az interneten.

Minta adatok

A probléma bemutatásához készítsük el pl. alapanyagok (product) és azok rendelt mennyisége (orders) tábláit. A bemutatott kódok kipróbáltan működtek SQLite 3.6.16 adatbázis kezelővel. Íme a teszt adatok feltöltése az alapanyagokhoz:

BEGIN TRANSACTION;
CREATE TABLE product (pid INT, name TEXT);
INSERT INTO product VALUES(1,'Liszt');
INSERT INTO product VALUES(2,'Tojas');
INSERT INTO product VALUES(3,'Eleszto');
INSERT INTO product VALUES(5,'Olaj');
INSERT INTO product VALUES(6,'So');
COMMIT;

valamint a rendelésekhez:

BEGIN TRANSACTION;
CREATE TABLE orders (pcode INT, quantity REAL);
INSERT INTO orders VALUES(1,3.5);
INSERT INTO orders VALUES(1,0.5);
INSERT INTO orders VALUES(2,15.0);
INSERT INTO orders VALUES(4,2.0);
INSERT INTO orders VALUES(4,5.1);
INSERT INTO orders VALUES(6,1.2);
INSERT INTO orders VALUES(7,3.0);
COMMIT;

Természetesen egy üzleti alkalmazás ennél sokkal bonyolultabb, további oszlopokkal és indexekkel. Ezt mindenki saját fantáziájára bízom, itt csak a lényeget szeretném bemutatni.

SQL lekérdezések

Egy tipikus rendelési napló a következő módon hozható létre:

SELECT product.*,SUM(orders.quantity) FROM product,orders
 WHERE product.pid=orders.pcode
 GROUP BY product.pid
 ORDER BY product.name;

Aminek az eredménye:

1           Liszt       4.0
6           So          1.2
2           Tojas       15.0

Ez eddig rendben is lenne, de van-e esetleg olyan rendelt alapanyag, amelynek nincs párja a törzsadatok között?

SELECT DISTINCT orders.pcode FROM orders
 LEFT JOIN product ON orders.pcode=product.pid
 WHERE product.pid IS NULL;

Valóban, a fenti adatok alapján kiderül, hogy a 4 és 7 kódokhoz nem rendeltünk nevet, így a rendelési listában sem jelentek meg. Ez az ellenőrzés azért fontos, mert inkonzisztens adatokra hívja fel a figyelmet.

Fordított irányban is érdekes lehet a kérdés: van-e olyan alapanyag, amelyből nem rendeltünk?

SELECT DISTINCT product.name FROM product
 LEFT JOIN orders ON product.pid=orders.pcode
 WHERE orders.pcode IS NULL;

Aminek az eredménye a fenti adatokra:

Eleszto
Olaj

A DISTINCT kulcsszó azért szükséges, hogy csupán egyetlen példányban listázzuk ki a neveket, ne pedig az összes előfordulásukat.

Habár ez a trükk sok helyen fellelhető az interneten, gondoltam én is megosztom egy kisebb példával együtt.

 

Megjegyzés

Ehhez a cikkhez nem fűzhető megjegyzés!

Hazai időjárás

Magyarország domborzati hőtérképe Felhőkép

 

Hirdetések

 

Linux for open minds.

 

Legfrissebb cikkek

 

EurekAlert! - Agriculture

  • Study shows electron-beam irradiation reduces virus-related health risk in lettuce, spinach
    (Texas A&M AgriLife Communications) The recent study by scientists from the National Center for Electron Beam Research (Texas A&M University) and other entities has quantified the theoretical health-risk reduction from virus-related food-borne illness through the use of electron-beam irradiation.
  • Satellite tracking reveals sea turtle feeding hotspots
    (United States Geological Survey) Satellite tracking of threatened loggerhead sea turtles has revealed two previously unknown feeding "hotspots" in the Gulf of Mexico that are providing important habitat for at least three separate populations of the turtles.
  • Consumers willing to buy sustainable US cotton, MU researchers find
    (University of Missouri-Columbia) Researchers from the University of Missouri have found that United States consumers are more willing to buy clothing made from sustainably grown US cotton than apparel produced using conventional practices in an unknown location.
  • A new species of bamboo-feeding plant lice found in Costa Rica
    (Pensoft Publishers) Several periods of field work during 2008 have led to the discovery of a new species of bamboo-feeding plant lice in Costa Rica's high-altitude region Cerro de la Muerte. The discovery was made thanks to molecular data analysis of mitochondrial DNA. The collected records have also increased the overall knowledge of plant lice (one of the most dangerous agricultural pests worldwide) from the region with more that 20 percent. The study was published in the open-access journal ZooKeys.
  • Established journal Evolutionary Applications to publish under open-access model
    (Wiley-Blackwell) Wiley-Blackwell, the scientific, technical, medical and scholarly publishing business of John Wiley & Sons Inc., today announced that Evolutionary Applications has joined the Wiley Open Access publishing program. All newly published articles in the journal will be open access and free to view, download and share for non-commercial use.

EurekAlert!