In 1219, the city was conquered by the Danish king Waldemar II the Victorious, who ordered the existing castle to be demolished and a new fortress built in its place. And that's probably why in Estonian and Finnish the name Tallinn translates as "Danish city."
In 1227, the Danes lost the city to the Livonian Order, who named the city Rewal.