Below is my code but dont know how to validate it exactly in code.new to code
trigger AccountContactCheck on Account (before delete) {
Set<id> accountids=new set<id>();
for(account acc:trigger.old){
accountids.add(acc.id);
}Map<id,contact>conmap=new map<id,contact>([select accountid from contact where accountid in :accountids]);
System.debug('conmap>>>>'+conmap.keySet());
for(account acc:trigger.old){
if(conmap.get(acc.id)!=null){
System.debug('Inside If of account >>>>>>');
acc.adderror('Contact is associated with it.you cant delete account');
}
}