They were both needed because they both gave a set of laws or rules for the people to follow; the Code of Hammurabi was given to the Babylonian people as laws for when something wrong was done while the ten commandments were given to the Israelite people as rules/law to follow from their God.
I hoped this helped a little! >3