The solution of your problem lies in the fact that you use too many spaces to indent your code in the lstlisting environment. Hence, you could use the literate option to overcome the problem, as suggested here: listings: Set tab size while using spaces for indentation.
Here is a MWE that solves your problem by adjusting the number of spaces by using the package listings:
\documentclass[10pt]{article}
\usepackage{geometry}
\usepackage[rgb,dvipsnames]{xcolor}
\definecolor{background}{rgb}{0.94,0.95,0.96}
\usepackage{listings,eulervm,palatino}
\lstset{language=SQL,%frame=ltrb,
backgroundcolor=\color{background},
keywordstyle=\ttfamily\color{OliveGreen},
identifierstyle=\ttfamily\color{CadetBlue}\bfseries,
commentstyle=\color{Brown},
stringstyle=\ttfamily,
tabsize=1,
% literate={\ \ }{{\ }}1, reduce the tab width from double to single spacing
numbers=left,
xleftmargin=2em,
% frame=single,
% framexleftmargin=1.5em % Uncomment these two lines if you prefer to have the frame
}
\begin{document}
\section*{DDL di creazione del database}
\begin{lstlisting}[caption={Creazione tabella 123r}, basicstyle=\small]
create table SP (
SNum varchar(3),
PNum varchar(3),
QTY decimal(5) not null,
constraint SP_PK primary key(SNum, PNum),
constraint SP_FK_S foreign key(SNum) references S(SNum) on delete cascade,
constraint SP_FK_P foreign key(PNum) references P(PNum) on delete cascade
);
\end{lstlisting}
\end{document}
which produces the following:
Another way could be using the package minted, which I find more straightforward than listings.
Here is a MWE using minted:
\documentclass[10pt]{article}
\usepackage{geometry}
\usepackage[rgb,dvipsnames]{xcolor}
\definecolor{background}{rgb}{0.94,0.95,0.96}
\usepackage{minted,eulervm,palatino}
\usemintedstyle[sql]{tango} % tango is the color style
\renewcommand\listoflistingscaption{List of source codes}
% : > pygmentize -L styles # shows all the possible color styles for the package minted
% : > pdflatex -shell-escape file.tex # compile with the -shell-escape option active
\begin{document}
\listoflistings
\section*{DDL di creazione del database}
\begin{listing}[ht]
\begin{minted}[gobble=1,
bgcolor=background,
fontsize=\small,
linenos=true,
xleftmargin=1.5em]{sql}
create table SP (
SNum varchar(3),
PNum varchar(3),
QTY decimal(5) not null,
constraint SP_PK primary key(SNum, PNum),
constraint SP_FK_S foreign key(SNum) references S(SNum) on delete cascade,
constraint SP_FK_P foreign key(PNum) references P(PNum) on delete cascade
);
\end{minted}
\caption{Creation of table 123r}
\end{listing}
\end{document}
which produces the following:

\documentclassand end with\end{document}– Salim Bou Dec 28 '16 at 18:44