I have a database with the following hierarchy.
- A dataset can have multiple scans (
foreign key scan.id_dataset -> dataset.id
) - A scan can have multiple labels (
foreign key label.id_scan -> scan.id
) - A label has a labeltype (
foreign key label.id_labeltype-> labeltype.id
) - The dataset, scan and labeltype tables all have a
name
column
I want to delete a label based on the names of the dataset, scan and labeltype. Here is my working code using the python mysql-connector:
def delete_by_labeltype(
self, dataset_name: str, scan_name, labeltype_name: str
) -> int:
sql = (
"DELETE l.* FROM label l "
"INNER JOIN labeltype lt "
"ON l.id_labeltype = lt.id "
"WHERE lt.name = %s AND l.id_scan = ("
" SELECT scan.id FROM scan "
" INNER JOIN dataset "
" ON dataset.id = scan.id_dataset "
" WHERE scan.name = %s AND dataset.name = %s"
");"
)
with self.connection.cursor() as cursor:
cursor.execute(
sql,
(
labeltype_name,
scan_name,
dataset_name,
),
)
self.connection.commit()
return cursor.rowcount
The function is within a class handling access to the label table so connection is already established (and later closed correctly). I am simply wondering if the query is the best way to do this, since I am not working with SQL too often.