This study evaluated whether people with ankylosing spondylitis (AS) and spondyloarthritis are at higher risk of type 2 diabetes mellitus (T2DM). We used a sub-dataset of the National Health Insurance Research Database from 1996 to 2010 to established a AS cohort consisting new patients with AS or spondyloarthritis (N = 7,778) and a non-AS cohort without the diseases (N = 31,112). Incidences of T2DM in the two cohorts, hazard ratios (HRs) of risk of T2DM in association with AS, and cumulative probability of having T2DM were estimated by the end of 2010. The incidence of T2DM was 1.17-fold higher in the AS cohort than in the non-AS cohort (13.5 vs. 11.5, per 1,000 person-years), with an adjusted HR of 1.16 (95 % CI = 1.05-1.29). The T2DM incidence was higher for women than for men; while the Cox model measured sex-specific adjusted HR of T2DM was higher for men than for women. The incidence rate of T2DM increased with age in both cohorts, while the age-specific measures showed that the adjusted HR of T2DM was higher in young AS patients (≤50 years of age) than older ones, compared to their peers of non-AS group. The plot of Kaplan-Meier analysis showed that the overall probability of having T2DM was 2 % higher in the AS cohort than in the non-AS cohort (log-rank test: p < 0.0001). Patients with AS and spondyloarthritis have an increased risk of developing T2DM.