Home >Database >Mysql Tutorial >Can a Systematic Approach Translate Human-Readable Query Descriptions into SQL?
Problem:
Whenever faced with a human-readable description of a query, developers typically rely on heuristics and brainstorming to translate it into a SQL query. However, is there a systematic and mathematical approach to this translation process?
Answer:
Yes, there is a systematic approach to constructing SQL queries from human-readable descriptions. It involves understanding the correspondence between natural language expressions, logical expressions, relational algebra expressions, and SQL expressions.
Consider the following human-readable description:
Find all people who are liked by someone but who don't like Ed.
Predicate of the Likes table:
[person] likes [liked]
Relational Algebra Expression:
FOR SOME x, Likes(person, x) AND Likes(x, liked) AND person = 'Bob' AND NOT Likes(x, 'Ed')
SQL Query:
SELECT DISTINCT l1.liker AS person, l2.liked AS liked FROM Likes l1 INNER JOIN Likes l2 ON l1.liked = l2.liker WHERE l1.liker = 'Bob' AND NOT (l1.liked, 'Ed') IN (SELECT * FROM Likes)
The above is the detailed content of Can a Systematic Approach Translate Human-Readable Query Descriptions into SQL?. For more information, please follow other related articles on the PHP Chinese website!