That’s a great question.
In places that’s what is done – they are called hard defences. Soft defences (sand bags etc) can be very effective where flooding isn’t all that common, or where the flooding is likely to be short-lived. Building structures costs a lot of money, and takes a lot of time, whereas sandbags are quick and easy to fill and deploy and are relatively inexpensive.
The big problem with building rigid structures along coasts is that coasts can be very active and they change rapidly through time. Any rigid structure can be undercut or bypassed by active coastlines. And, if you design something to keep out a 10m flood, for example, you can be sure that the very next year you’ll get an 11m flood.