Under what conditions is an open subscheme of an affine scheme affine and what ring corresponds to it? It is well known that an open subscheme of an affine scheme is not necessarily an affine one. But what are (if possible the most general) sufficient conditions for its affinity? And is it known how, in such cases, to directly describe its ring in terms of its corresponding radical ideal?
(I'm just starting to learn algebraic geometry, so any of your answers will most likely be useful to me)
 A: Here is the best general result I'm aware of:
Lemma. Let $A$ be a commutative ring with an ideal $I$, let $X = \operatorname{Spec} A$ with closed subset $Z = V(I)$ and open complement $U = D(I)$.

*

*If $Z$ is a Cartier divisor, then $U$ is affine and dense in $X$.

*If $A$ is Noetherian and $U$ is affine and dense in $X$, then $Z$ is a Weil divisor.
The assumption in (1) means that $I$ is locally generated by one element (i.e. there exist elements $f_1,\ldots,f_n \in A$ generating the unit ideal such that each $I_{f_i} \subseteq A_{f_i}$ is a principal ideal). The conclusion in (2) means that every minimal prime containing $I$ has height $1$, i.e. $Z$ has pure codimension $1$ in $X$.
Every Cartier divisor is a Weil divisor (this is Krull's principal ideal theorem), and the converse is true if $X$ has mild singularities. For this problem, you can take $X$ to be locally factorial (or locally $\mathbf Q$-factorial if you don't mind replacing your ideal by a suitable power); in particular it is true when $A$ is regular. This gives:
Corollary. Suppose $A$ is Noetherian and locally $\mathbf Q$-factorial, and that $U \subseteq X$ is dense. Then $U$ is affine if and only if every component of $Z$ has codimension $1$. $\square$
There is also something to say about the case where $Z$ contains an entire irreducible component of $X$, but let me not attempt this here. At any rate, algebraic geometers often stick to irreducible schemes, where any nonempty open subset is dense.
Proof of Lemma (sketch). Statement (1) is relatively easy if you know some scheme-theory. The slogan is that the property of a morphism being affine can be checked locally (Hartshorne, Exercise II.5.17). Let me do it in a way that also computes the ring $B = \Gamma(U,\mathcal O_U)$ in terms of $I$. By general stuff we get a morphism of schemes $\phi \colon U \to \operatorname{Spec} B$ (see e.g. Hartshorne, Exercise II.2.4), which we will show is an isomorphism.
Choose $f_1,\ldots,f_n \in A$ generating the unit ideal and elements¹⁾ $g_1,\ldots,g_n \in A$ such that $I_{f_i} = (g_i)_{f_i} \subseteq A_{f_i}$. Then
$$U \cap D(f_i) = D(g_i) \cap D(f_i) = D(f_ig_i) \cong \operatorname{Spec} A_{f_ig_i},$$
and these cover $U$ since the $D(f_i)$ cover $X$. The sheaf condition for $\mathcal O_U$ gives an exact sequence
$$0 \to B \to \bigoplus_{i=1}^n A_{f_ig_i} \to \bigoplus_{i<j} A_{f_ig_if_jg_j},\label{1}\tag{1}$$
where the second map takes $(a_i)_i$ to $(a_i-a_j)_{i<j}$. Localising \eqref{1} at $f_i$ on the one hand computes $B_{f_i}$ by exactness of localisation, but on the other hand computes $\Gamma(U \cap D(f_i),\mathcal O_U) = A_{f_ig_i}$ via the sheaf condition on $U \cap D(f_i)$. If $Y = \operatorname{Spec} B$, we see that both $D(f_i) \subseteq Y$²⁾ and its inverse image $U \cap D(f_i) \subseteq U$ are affine, with isomorphic coordinate rings, so $\phi$ is an isomorphism above $D(f_i) \subseteq Y$. Since these cover $Y$, we conclude that $\phi$ is an isomorphism, i.e. $U$ is affine. (See also Hartshorne, Exercises II.2.16, II.2.17, and II.5.17.)
Statement (2) is a little harder, and relies on the observation that for a Noetherian local ring $(R,\mathfrak m)$, the punctured spectrum $\operatorname{Spec} R \setminus\{\mathfrak m\}$ is affine if and only if $\dim R \leq 1$. See [Tag 0BCQ] for a proof. Statement (2) then follows by taking $(R,\mathfrak m)$ to be the localisation of $A$ at a minimal prime $\mathfrak p$ containing $I$. If $U$ is affine, then so is $U \times_X \operatorname{Spec} A_{\mathfrak p} = \operatorname{Spec} A_{\mathfrak p} \setminus \{\mathfrak p\}$ since a fibre product of affine schemes is affine. This forces $\operatorname{ht} \mathfrak p \leq 1$, i.e. every irreducible component of $Z$ has codimension $\leq 1$. The assumption that $U$ is dense means that every component of $Z$ has codimension exactly $1$. $\square$

¹⁾ A priori you only get $I_{f_i} = \big(\tfrac{g_i}{f_i^r}\big)_{f_i}$ for some $g_i \in A$ and $r\in \mathbf N$, but then $g_i$ also generates the ideal in $A_{f_i}$ since $f_i$ is invertible.
²⁾ This is abuse of notation: we should really take the ring homomorphism $\psi \colon A \to B$ and talk of $D(\psi(f_i)) \subseteq Y$. Then $B_{f_i}$ (localisation as $A$-modules) agrees with $B_{\psi(f_i)}$ (localisation as $B$-modules), etcetera. I hope this does not lead to confusion.
