Exercise 2: Dollar-Quoting vs. MySQL's DELIMITER Dance — Possible Solution ==================================================================== WHAT DOLLAR-QUOTING IS ------------------------------ Per this chapter, "the $$ ... $$ around the function body is dollar-quoting — a genuine Postgres-specific convenience letting a multi-line body be written without escaping internal quotes." Rather than wrapping a function's body in ordinary single quotes (which would require escaping every internal single quote used within the body itself), Postgres allows a body to be delimited by a pair of matching $$ markers (or a custom $tag$ ... $tag$ pair), letting the body's own text — including any quotes it contains — be written literally, without any escaping. WHY MYSQL NEEDS THE DELIMITER DANCE ------------------------------ Per this chapter, "MySQL's own DELIMITER // convention exists specifically because semicolons inside a stored procedure body would otherwise be misread as the end of the outer CREATE PROCEDURE statement, forcing a temporary delimiter change and a matching // at the end." MySQL's client normally treats a semicolon as "this statement is finished, send it now." Since a stored procedure body itself is full of semicolons (one after each internal statement), the client would incorrectly think the CREATE PROCEDURE statement ended partway through the body, at the first internal semicolon it encountered. To work around this, MySQL requires temporarily changing the statement terminator to something else (commonly //) before defining the procedure, and switching it back afterward — an extra, easy-to-forget ceremony surrounding every procedure/function definition. WHY DOLLAR-QUOTING IS A GENUINE ERGONOMIC IMPROVEMENT ------------------------------ Postgres's dollar-quoting sidesteps this problem entirely by using a DIFFERENT delimiter ($$ or $tag$) that has nothing to do with the semicolons used inside the body — the semicolons inside a PL/pgSQL function body never risk being misread as ending the outer CREATE FUNCTION statement, because the body's own boundary is marked by a completely distinct symbol. This means there's no need to change and then restore the statement delimiter at all — no DELIMITER // before, no // after, just the function body written directly inside its own $$ ... $$ markers. WHY THIS WORKS AS AN ANSWER ------------------------------ It explains dollar-quoting's actual mechanism, explains precisely WHY MySQL's DELIMITER convention is necessary in the first place (the semicolon-ambiguity problem), and connects the two to show specifically what extra ceremony dollar-quoting removes.