Good point. In a magical setting - especially a (typically high) magical setting like core D&D - there are numerous solutions to everyday problems. The earthen (or even stone) walls that surround the town could have been raised from numerous castings of Move Earth, Wall of Stone, etc. Move Earth could be used to begin with, surrounding the large town / small city within a week or so if 3-4 casters have the spell and work every day at it. The walls would be ~ 30 ft high, 30 ft long, and 10 ft deep - per casting. With 3-4 casters working at it, the wall could be 30 ft deep (normal for ancient and even middle ages times). It would take ~190 castings to surround a settlement one-quarter mile in diameter with such a wall.
Wall of Stone can be used to create a 5 ft by 5 ft by 10 inch block (by a tenth level caster). Or, if a more square block is wished, the spell could conceivably be used to create a cube of stone ~33 inches to a side. In all likelihood, it would be better to create the 10 inch five foot slabs, buttressing them with thick walls of earth (from move earth) on either side. Move Earth may not sound like much, but recall that the walls of Babylon were not (early on) stone. They were baked earth, perhaps with some straw mixed in. All in all, given one years time, a contracted group of, say, 4-5 9th level wizards, could easily complete the job. And if the trading city is prosperous enough to have a steady population of ~8000 in a desert, it should be able to contract such a group easily.
Water is never a major issue. A casting of Wall of Ice by a seventh level wizard or sorcerer will create a cube of ice about 8.3 feet to a side. After that melts the resulting mass of water is enough to fill an volume ~8.12 feet to a side. (The ice melts automatically after 70 minutes, for a 7th level caster.) That is a bit over 535 cubic feet of water. From the description of "Create Water" we know that "... one cubic foot of water contains roughly 8 gallons ...". So with that one spell we have ~4,281 gallons of (nearly glacier cold) water (after a bit over an hour for melting, of course). That's about half a gallon per person - not quite enough for every day usage in the city, but if there are 3-4 casters capable of this spell then the city will have enough drinking water to wait out any siege, even if their well / oasis runs dry.
For another example of a major problem in RL that is almost trivial in core D&D consider the following. All the assumptions made about how much food is produced per acreage does not take into account the idea of a local druid (or cleric with the plant domain) helping out. The third level spell "Plant Growth" has a secondary usage of "Enrichment," described below
srd said:
Enrichment: This effect targets plants within a range of one-half mile, raising their potential productivity over the course of the next year to one-third above normal.
So, four castings of this - one at each of the cardinal points. In fact, I can see a cleric with the Plant domain being in demand in such a setting. Perhaps every year, just before planting, the cleric goes through a ritual on the Spring Equinox or such, praying at a small shrine at each of the cardinal points, casting Plant Growth [Enrichment]. The end result is that instead of 3 acres feeding three people, instead 3 acres feeds 4 people.
Hmm, maybe a local cleric with Plant and Water domain is the major local religious figure? Of course, this is also a desert, so perhaps an allied (or enemical) cleric with the sun / fire domain is also present - or better yet, Sun and Earth (sand, rock, bare ground). The Plant / Water cleric focuses on praying to a deity to aid, while the Sun / Earth focuses on praying to a deity to placate. Or maybe it is a more elemental arrangement?
Water / Plant (major festival on Winter Solstice, before planting)
Earth / Animal (major festival on Spring Equinox)
Fire / Sun (major festival on Summer Solstice)
Air / Travel (major festival on Autumn Equinox, after harvest, before / as caravans leave)
Notice I changed the Water festival / blessing of the four shrines to Winter Solstice. It occurred to me that planting could begin far earlier in a desert, where the heat comes sooner than typical elsewhere, especially if water is ever plentiful. The blessing at the Spring Equinox could be as animals are being bred, or right before such, hoping for strong offspring, etc. The Fire festival would be during the hottest part of the year, in part a placation, asking the Fire based deity to be merciful, not overly burning, hold off on drought, etc. The Air festival would be in part a blessing on the caravans as they prepare to leave.
So, all in all, presuming a typically magical core D&D settlement, the settlement could - within just a few years - have strong and commendable walls of layered foot-thick stone and baked adobe earth, a nice sized oasis with absolutely massive supplies of water (Perhaps all those 'move earth' spells created a massive cavern a hundred feet or so beneath the lands just beyond the city - which was then filled with melted walls of ice to help support the weight of the ceiling, keeping it from collapsing and creating a massive year long reserve of water for the whole city in the process.), and fields capable of feeding far more than expected. After all, who's to say that the clerics or wizards living in or near the city didn't research a new spell - Improved Plant Growth (level 5 instead of level 3) that doubles the growth for each field. No, a city in a desert, if sufficiently magic, will have little trouble surviving.
All this presumes just core D&D. Once you start adding third party ideas - such as ritual magic involving multiple casters to cast spells beyond what any singular caster in the group could have accomplished at their own level of power - the situation becomes both more favorable and more complex.
Out of curiosity, how old is the city? Has it only recently grown to this size, or has it existed at this size (and location) for hundreds of years - if not longer? If it has existed for hundreds of years, then even without magic the walls could be impressive, having been built over scores of generations to hold out raiders, desert monsters, etc. The protective magic might be even more impressive. Permanent spells might have been cast every other generation or so for half a hundred generations. Wards might be in place that only need a once a year ritual to renew their strength (perhaps sapping the casting ability of those involved for a day or so as a result).