Nearly two decades after lawmakers began requiring background checks for gun buyers, significant gaps in the F.B.I.’s database of criminal and mental health records allow thousands of people to buy firearms every year who should be barred from doing so.

The database is incomplete because many states have not provided federal authorities with comprehensive records of people involuntarily committed or otherwise ruled mentally ill. Records are also spotty for several other categories of prohibited buyers, including those who have tested positive for illegal drugs or have a history of domestic violence.

While some states, including New York, have submitted more than 100,000 names of mentally ill people to the F.B.I. database, 19 — including New Jersey, Pennsylvania, Vermont, Maryland and Maine — have submitted fewer than 100 records and Rhode Island has submitted none, according to federal data compiled by Mayors Against Illegal Guns. That suggests that millions of names are missing from the federal database, gun control advocates and law enforcement officials say.