Q:

How to deal with violent language from superior in workplace

Background:
I am junior developer (about 1 year of experience) who was recently hired at a company smaller than 150 people less than 6 months ago. The web software team is made up of less than 5 people, and everyone in it is focused on front-end web development besides the senior who does all the back-end work to keep the website running. 
The senior engineer has been with the company for more than 20 years and wrote most of the website and all of the back-end framework code that we use to serve the website (without which the website would break). I generally try to avoid asking him questions because I can tell he is stressed, and from his body language, replies, and tone I have interpreted that he finds my questions very annoying and a waste of time.
Main issue:
Today I was trying to fix an issue with a web page I was updating, and this page was not showing any information from the database. I was asking a coworker (not the senior) if they could look at my code to see where I was making a mistake.  
They started to explain what I was doing wrong, at which point the senior engineer walked over from his office, and started to ask me questions about the code I had written into the page and what the mistake I had made was. I tried to answer all of his questions but he was getting angry because I was taking a long time to explain.
When I explained the mistake, he angrily yelled at me and said "If you use a comma delimited string as a SQL parameter on the website again, I will beat you senseless". I replied that I understood and will not use it. He then told me to look up another object type to pass to the database and then he left.
I understand that I am a junior developer and that I need to respect and listen to the more knowledgeable superior. However after the discussion I felt very sad and angry because what he had said to me. I felt like quitting right there. I am afraid that the senior engineer might ask for me to be fired (he said he has done it to others in the past), which would look bad on my record. But I feel like quitting so soon could be bad too.
Should I stay and accept that I need to write better code to avoid events like this? Is what the senior engineer said normal?

A:

When I explained the mistake, he angrily yelled at me and said "If you use a comma delimited string as a SQL parameter on the website again, I will beat you senseless"

This also makes me sad and angry. A teachable moment between you and the senior developer that was completely wasted. No mistake deserves this kind of treatment.
It's completely inappropriate for him to hint, suggest, or imply violent behavior, even as a figure of speech. It's abusive and unacceptable.

I felt like quitting right there.

Sometimes it pays to listen to your gut.

The senior engineer has been with the company for more than 20 years and wrote most of the website and all of the back-end framework code

Sounds like everyone considers this engineer irreplaceable, and he's taken advantage of that situation to create his own little dictatorship.
He may be burned out. In any event I do not see him moderating his behavior anytime soon. People like this generally get forced out at some point, and look out when that happens.

Is what the senior engineer said normal? 

No. It happens too often unfortunately, but is by no means behavior you should accept.

Should I stay and accept that I need to write better code to avoid events like this? 

Everyone needs to write better code as part of becoming a better developer, but not for the purpose of warding off abuse.
No code you write will ever be good enough for this person. If he enjoys belittling people, he will continue to find mistakes, no matter how small, and make you feel bad about them, to make himself feel superior.
I would stay only as long as it takes to find a new job, in one of the many places that value, teach, and encourage developers no matter their level of experience.

