Fortifications
From DwarfFortressWiki
You may be looking for the Fortification construction option (which you will need to make good fortifications anyway.)
You may be looking for the broader catagory of Fortress defense.
In Dwarf Fortress, you will often find yourself beset by hostile creatures looking to murder your dwarves and take their things. Adventuring parties of kobolds, goblins, and worse, in other words.
Assuming you want to stave off the ravenous hordes from getting to your hoards, you're probably going to want to build means of preventing them from getting into your fortress.
You're going to need walls, of course. One fairly popular way to start, especially among players who use cheating methods to bring far more things with their starting dwarves than an entire army of peasants have a hope of moving, is to bring a lot of stone and build walls around their starting wagon(s), then dig down (or in, if the wagon conveniently started near enough to a cliff face.)
Your basic goal is simple: Protect your dwarves and dwarven treasures from those things which would do them harm. You can simply accomplish this by walling up and refusing to go out, at least until a Megabeast shows up. However, this is often unsatisfactory, and leaves many gaps in your defenses. (Believe it or not, a solid wall is not a perfect defense.)
Dwarf Fortress is very open-ended, any number of engineering principles and fortification principles will work in DF that would work in the real high middle ages. Principles especially - a Moat with a drawbridge is perhaps the simplest Perfect Defense known to Dwarvenkind. The Fortifications article on Wikipedia is, of course, a good resource.
Simply shutting the outside world out and allowing invaders to mill about outside your moat is not always a desirable soloution, however, even if it is a perfect defense. They will, at the very least, prevent traders from arriving, and prevent any desired outdoor activities, such as wood harvesting. In addition, Dwarf Fortress players often find it enjoyable to perpetrate mass slaughter of invaders.
For this, you will need a more complicated defense than a passive ditch and walls.
Fortifications prevent movement through themselves, but allow projectiles to move through them. They may be carved from walls, whether those carved out of existing rock or constructed of any material, or they may be erected free-standing. (There is little reason whatsoever to erect free-standing fortifications outdoors, as constructed walls may be carved into fortifications and give you the benefit of a natural (non-constructed) floor in the square above which remains even when carved into fortifications; free-constructed fortifications do not).
Like Fortifications, Vertical Bars and Wall Grates will also allow projectiles to fire through them while impeding units' movement. Unlike Fortifications, Bars and Grates may be connected to a Lever, and opened or closed remotely - thus, they are good for forming portculli.
Channels may be used to form ditches, or moats. For defensive purposes they do not need to be filled with anything - as in the middle ages, a dry ditch is more than enough to prevent ground units from approaching (though of course, projectiles may be launched over it with impunity). With a retracting Bridge over the moat, any units or items on top of the bridge will be dropped into the moat (and, if the moat is filled with water, drown unless they can swim out; if it is filled with magma, they burn to death.) Drawbridges have a different effect they throw creatured a distance in the direction the bridge is raised. It is believed that creatures ove a certain size (elephants and Megabeats, among others) will prevent the bridge from being raised. Creatures on top of drawbridges will be utterly destroyed if they are flush against wall and have a floor tile above them, as will anything on a floor that is covered when the drawbridge falls. This offensive use of drawbridges is known as the Dwarven Atom Smasher.
Floodgates may be used as removable walls, although using a single-tile-wide drawbridge will be much more economical in terms of Mechanisms. (Be aware that Megabeasts can batter down both raised floodgates and drawbridges.)
Naturally, these precepts, simple in concept, give rise to almost any defensive stratagem. One common method is to build a walled tower above the entrance to your fortress, stationing Marksdwarves on the second floor. Another is to engineer a very long but narrow entrance, at the end of which are ballistae waiting to unload at unfortunate monsters in the field of fire. (In this case, the fortress designer is well-advised to use fortifications, grates, walls, ditches, or other means to prevent monsters from even approaching within about a screen and a half's distance, to prevent the (civilian) siege operators from running in fright from creatures which cannot harm them; indeed, ones to which they can do a great deal of harm.)

