Universal properties of immersion of schemes In this answer of Martin Brandenburg he refers to the universal properties of the open and closed immersion. As this looks kind of useful I tried to find a reference but I couldn't find any. So my question is

Is there a way to see an open immersion and a closed immersion (and perhaps general immersion) of schemes as a solution to a universal problem?

 A: Now I can answer this. 
To simplify notation I will talk about subschemes rather than general immersions, although this are essentially the same.


*

*Universal property of open subschemes: Let $X$ be a scheme and $U\subseteq X$ an open subscheme.  A morphism of schemes $f:Y\rightarrow X$ has a unique factorization $f:Y\xrightarrow{f'} U\xrightarrow{i}X$ iff $f(X)\subseteq U$ as sets.


Proof: Only $\Leftarrow$ is non trivial. If $f(X)\subseteq U$ then $f=i\circ f'$ as maps between sets. Now as $f_*=i_*\circ f'_*$ and $i^{-1}\circ i_*=\text{Id}$, we can apply the functor $i^{-1}$ to the morphism
$f^\sharp:\mathcal{O}_X\rightarrow f_*\mathcal{O}_Y$
to get a morphism $\mathcal{O}_U\rightarrow f'_*\mathcal{O}_Y$. If we define $f'^\sharp$ as this morphism we can see the factorization $f=i\circ f'$ at the level of schemes. $\square$


*

*Universal property of closed subschemes: Let $Z\subseteq X$ be defined by the quasicoherent ideal $\mathcal{J}\subset \mathcal{O}_X$, i.e, $Z=\text{Supp}(\mathcal{O}_X/\mathcal{J})$ and $\mathcal{O}_Z=i^{-1}(\mathcal{O}_X/\mathcal{J})$. 
Then a morphism $f:Y\rightarrow X$ has a unique factorization $f:Y\xrightarrow{f'} Z\xrightarrow{i}X$ iff $f$ vanish over $\mathcal{J}$, i.e, $f^\sharp(\mathcal{J}(U))=0 \ \forall U\subseteq X$. 


Proof: $\Leftarrow$ is the most interesting part.
First we prove that $f$ can be factored as a map of sets, for this we need to prove that $f(Y)\subseteq Z$ or equivalently $(\mathcal{O}_X/\mathcal{J})_{f(y)}\neq 0 \ \forall y\in Y$. This follows because as $f$ vanish at $\mathcal{J}$ the morphism $f^\sharp$ can be factored to a morphism $\mathcal{O}_X/\mathcal{J}\rightarrow f_*\mathcal{O}_Y$ and using the $(f^{-1},f_*)$ adjunction we get a morphism $f^{-1}(\mathcal{O}_X/\mathcal{J})\rightarrow \mathcal{O}_Y$. Now looking at the stalk at $y\in Y$ we get a ring morphism $(\mathcal{O}_X/\mathcal{J})_{f(y)}\rightarrow \mathcal{O}_{Y,y}$ and as $\mathcal{O}_{Y,y}\neq 0$ we should have $(\mathcal{O}_X/\mathcal{J})_{f(y)}\neq 0$.
Now we show that $f=i\circ f'$ can be extended to the level of schemes. As we already have $f'$ at the level of sets is enough to define $f'^\sharp$. For this, again using the identities $f_*=i_*\circ f'_*$ and $i^{-1}\circ i_*=\text{Id}$ we can apply $i^{-1}$ to the morphism $\mathcal{O}_X/\mathcal{J}\rightarrow f_*\mathcal{O}_Y$ to get a morphism $i^{-1}(\mathcal{O}_X/\mathcal{J})\rightarrow f'_*\mathcal{O}_Y$ that we can use as $f'^\sharp$. $\square$
Using a combination of this two properties is possible to give a universal property for locally closed subschemes but I am not sure how useful it is.
Notes: 


*

*The condition $f^\sharp(\mathcal{J}(U))=0 \; \forall U\subseteq X$ can be restated as $f^*(\mathcal{J})=0$

*For a locally ringed space $(X,\mathcal{O}_X)$ and a subset $i:Y\hookrightarrow X$ the locally ringed space  $(Y,i^{-1}\mathcal{O}_X)$ has the following property: A morphism $f:Z\rightarrow X$ factorize through $Y$ iff $f(Z)\subset Y$ as sets. This generalize the universal property for open immersions and gives a universal property for the inclusions $\text{Spec } \mathcal{O}_{X,x}\hookrightarrow X$ and $\text{Spec }k(x)\hookrightarrow X$.
