Based on current research, it is known that the gastrointestinal tract microbiota and its genome play a crucial role in mental illnesses. Studies indicate a direct correlation between gastrointestinal tract microbiota and the onset of dementia, mediated by metabolic diseases and low-grade inflammation. The association between various gastrointestinal symptoms and neurodegenerative diseases has been recently discussed. However, there is a lack of research regarding the comparative effects of different surgical procedures on neurodegenerative diseases. Therefore, this study primarily focuses on comparing the association between various gastrointestinal surgeries and dementia, aiming to provide guidance for future clinical practice. A nationwide study using the Taiwanese National Health Insurance Research Database included 26 059 patients diagnosed with dementia or Alzheimer's disease and 104 236 controls without diseases. Primary exposures were defined as alimentary surgeries, encompassing cholecystectomy, gastrectomy, bowel resection, and appendectomy. Conditional logistic regression was used to examine the odds ratio and 95% confidence interval for prior alimentary surgery between cases and controls. The results showed that individuals with dementia had a higher rate of gastrectomy. Additionally, individuals with dementia seemed to exhibit a reduced rate of cholecystectomy and appendectomy. Regarding Alzheimer's disease, all four alimentary surgeries showed comparable trends to those observed with dementia. No significant interaction was observed between alimentary surgery and dementia among the four types of surgery evaluated. Our study demonstrates that gastrectomy is associated with an elevated risk of dementia. We aim to uncover more direct evidence in future experiments.