> ## Documentation Index
> Fetch the complete documentation index at: https://docs.countrystatecity.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Database Schema

> Versioned export tables, nullable relationships, county links and verified SQL queries for the CountryStateCity database

## Database architecture

This reference describes the SQLite export in [v5.2.0-export.1](https://github.com/dr5hn/countries-states-cities-database/releases/tag/v5.2.0-export.1), published 3 November 2026. Types, indexes, constraints and file layout can differ between export formats and releases. Inspect the source file you install.

The underlying dataset links regions, subregions, countries, states, counties, city records and postcodes. A city can have a null state link; city names are not unique identifiers. Coordinates describe points, not boundary polygons or validated delivery addresses.

> [!NOTE]
>
> Export column names and SDK model attributes can differ. For this snapshot, country records use `phonecode`, states use `iso2`, and cities use `state_code`. The Python package models expose `phone_code` and `state_code` for some of those fields. Do not infer SQL column names from an API response or package model.

## Core tables

### regions

The pinned SQLite snapshot contains 6 rows.

| Column | Declared SQLite type | Nullable |
| - | - | - |
| `id` | `INTEGER` | Primary key |
| `name` | `VARCHAR(100)` | No |
| `translations` | `TEXT` | Yes |
| `created_at` | `DATETIME` | Yes |
| `updated_at` | `DATETIME` | No |
| `flag` | `TINYINT` | No |
| `wikiDataId` | `VARCHAR(255)` | Yes |

### subregions

The pinned SQLite snapshot contains 22 rows.

| Column | Declared SQLite type | Nullable |
| - | - | - |
| `id` | `INTEGER` | Primary key |
| `name` | `VARCHAR(100)` | No |
| `translations` | `TEXT` | Yes |
| `region_id` | `MEDIUMINT` | No |
| `created_at` | `DATETIME` | Yes |
| `updated_at` | `DATETIME` | No |
| `flag` | `TINYINT` | No |
| `wikiDataId` | `VARCHAR(255)` | Yes |

### countries

The pinned SQLite snapshot contains 250 rows.

| Column | Declared SQLite type | Nullable |
| - | - | - |
| `id` | `INTEGER` | Primary key |
| `name` | `VARCHAR(100)` | No |
| `iso3` | `CHARACTER(3)` | Yes |
| `numeric_code` | `CHARACTER(3)` | Yes |
| `iso2` | `CHARACTER(2)` | Yes |
| `phonecode` | `VARCHAR(255)` | Yes |
| `capital` | `VARCHAR(255)` | Yes |
| `currency` | `VARCHAR(255)` | Yes |
| `currency_name` | `VARCHAR(255)` | Yes |
| `currency_symbol` | `VARCHAR(255)` | Yes |
| `tld` | `VARCHAR(255)` | Yes |
| `native` | `VARCHAR(255)` | Yes |
| `population` | `BIGINT` | Yes |
| `gdp` | `BIGINT` | Yes |
| `region` | `VARCHAR(255)` | Yes |
| `region_id` | `MEDIUMINT` | Yes |
| `subregion` | `VARCHAR(255)` | Yes |
| `subregion_id` | `MEDIUMINT` | Yes |
| `nationality` | `VARCHAR(255)` | Yes |
| `area_sq_km` | `DOUBLE` | Yes |
| `postal_code_format` | `VARCHAR(255)` | Yes |
| `postal_code_regex` | `VARCHAR(255)` | Yes |
| `timezones` | `TEXT` | Yes |
| `translations` | `TEXT` | Yes |
| `latitude` | `DECIMAL` | Yes |
| `longitude` | `DECIMAL` | Yes |
| `emoji` | `VARCHAR(191)` | Yes |
| `emojiU` | `VARCHAR(191)` | Yes |
| `created_at` | `DATETIME` | Yes |
| `updated_at` | `DATETIME` | No |
| `flag` | `TINYINT` | No |
| `wikiDataId` | `VARCHAR(255)` | Yes |

### states

The pinned SQLite snapshot contains 5,327 rows.

| Column | Declared SQLite type | Nullable |
| - | - | - |
| `id` | `INTEGER` | Primary key |
| `name` | `VARCHAR(255)` | No |
| `country_id` | `MEDIUMINT` | No |
| `country_code` | `CHARACTER(2)` | No |
| `fips_code` | `VARCHAR(255)` | Yes |
| `iso2` | `VARCHAR(255)` | Yes |
| `iso3166_2` | `VARCHAR(10)` | Yes |
| `type` | `VARCHAR(191)` | Yes |
| `type_local` | `VARCHAR(191)` | Yes |
| `level` | `INTEGER` | Yes |
| `parent_id` | `INTEGER` | Yes |
| `native` | `VARCHAR(255)` | Yes |
| `latitude` | `DECIMAL` | Yes |
| `longitude` | `DECIMAL` | Yes |
| `timezone` | `VARCHAR(255)` | Yes |
| `translations` | `TEXT` | Yes |
| `created_at` | `DATETIME` | Yes |
| `updated_at` | `DATETIME` | No |
| `flag` | `TINYINT` | No |
| `wikiDataId` | `VARCHAR(255)` | Yes |
| `population` | `VARCHAR(255)` | Yes |

### counties

The pinned SQLite snapshot contains 14,058 rows.

| Column | Declared SQLite type | Nullable |
| - | - | - |
| `id` | `INTEGER` | Primary key |
| `name` | `VARCHAR(255)` | No |
| `state_id` | `MEDIUMINT` | Yes |
| `state_code` | `VARCHAR(255)` | Yes |
| `country_id` | `MEDIUMINT` | No |
| `country_code` | `CHARACTER(2)` | No |
| `type` | `VARCHAR(191)` | Yes |
| `type_local` | `VARCHAR(191)` | Yes |
| `level` | `INTEGER` | Yes |
| `parent_id` | `MEDIUMINT` | Yes |
| `latitude` | `DECIMAL` | No |
| `longitude` | `DECIMAL` | No |
| `native` | `VARCHAR(255)` | Yes |
| `population` | `BIGINT` | Yes |
| `timezone` | `VARCHAR(255)` | Yes |
| `translations` | `TEXT` | Yes |
| `created_at` | `DATETIME` | No |
| `updated_at` | `DATETIME` | No |
| `flag` | `TINYINT` | No |
| `wikiDataId` | `VARCHAR(255)` | Yes |

### county\_states

The pinned SQLite snapshot contains 16 rows.

| Column | Declared SQLite type | Nullable |
| - | - | - |
| `county_id` | `MEDIUMINT` | Primary key |
| `state_id` | `MEDIUMINT` | Primary key |

### cities

The pinned SQLite snapshot contains 156,169 rows.

| Column | Declared SQLite type | Nullable |
| - | - | - |
| `id` | `INTEGER` | Primary key |
| `name` | `VARCHAR(255)` | No |
| `state_id` | `MEDIUMINT` | Yes |
| `county_id` | `MEDIUMINT` | Yes |
| `state_code` | `VARCHAR(255)` | Yes |
| `country_id` | `MEDIUMINT` | No |
| `country_code` | `CHARACTER(2)` | No |
| `type` | `VARCHAR(191)` | Yes |
| `type_local` | `VARCHAR(191)` | Yes |
| `level` | `INTEGER` | Yes |
| `parent_id` | `INTEGER` | Yes |
| `latitude` | `DECIMAL` | No |
| `longitude` | `DECIMAL` | No |
| `native` | `VARCHAR(255)` | Yes |
| `population` | `BIGINT` | Yes |
| `population_year` | `SMALLINT` | Yes |
| `timezone` | `VARCHAR(255)` | Yes |
| `translations` | `TEXT` | Yes |
| `created_at` | `DATETIME` | No |
| `updated_at` | `DATETIME` | No |
| `flag` | `TINYINT` | No |
| `wikiDataId` | `VARCHAR(255)` | Yes |

### postcodes

The pinned SQLite snapshot contains 728,556 rows.

| Column | Declared SQLite type | Nullable |
| - | - | - |
| `id` | `INTEGER` | Primary key |
| `code` | `VARCHAR(20)` | No |
| `country_id` | `MEDIUMINT` | No |
| `country_code` | `CHARACTER(2)` | No |
| `state_id` | `MEDIUMINT` | Yes |
| `state_code` | `VARCHAR(255)` | Yes |
| `city_id` | `MEDIUMINT` | Yes |
| `locality_name` | `VARCHAR(255)` | Yes |
| `type` | `VARCHAR(32)` | Yes |
| `latitude` | `DECIMAL` | Yes |
| `longitude` | `DECIMAL` | Yes |
| `source` | `VARCHAR(64)` | Yes |
| `wikiDataId` | `VARCHAR(255)` | Yes |
| `created_at` | `DATETIME` | No |
| `updated_at` | `DATETIME` | No |
| `flag` | `TINYINT` | No |

## Relationships and constraints

* `subregions.region_id` links to `regions.id`.
* Countries can link to a region and subregion through `region_id` and `subregion_id`.
* `states.country_id` links to `countries.id`. Subdivision classifications and hierarchy levels vary by country.
* `cities.country_id` links to `countries.id`; `cities.state_id` and `cities.county_id` can be null.
* A county can use `counties.state_id` and/or additional rows in `county_states`. Preserve both when looking up county-to-state relationships.
* Postcodes carry a required country link and optional state/city links. Read the [postcode reference](https://docs.countrystatecity.in/database/postcodes.md) for coverage and validation limits.

### Foreign key constraints

Inspect and verify constraints in the export for your engine. In SQLite:

```sql theme={null}
PRAGMA foreign_keys = ON;
PRAGMA foreign_key_list(cities);
PRAGMA foreign_key_check;
```

Enabling foreign keys is a connection setting. `foreign_key_check` returns violations; no rows means no violations were found. The checked snapshot returns no rows.

## Common queries

Country and state names are joined from their tables rather than read from nonexistent `cities.country_name` or `cities.state_name` columns:

```sql theme={null}
SELECT ci.id, ci.name AS city, st.name AS state, co.name AS country
FROM cities AS ci
JOIN countries AS co ON co.id = ci.country_id
LEFT JOIN states AS st ON st.id = ci.state_id
WHERE co.iso2 = 'IN'
ORDER BY ci.id
LIMIT 10;
```

Get county-to-state links without discarding counties that use the scalar state column:

```sql theme={null}
SELECT id AS county_id, state_id
FROM counties
WHERE state_id IS NOT NULL
UNION
SELECT county_id, state_id FROM county_states;
```

Use a left join for optional parents. Keep IDs and their country relationships when storing selected places; matching names alone can combine unrelated places.

## Data types by platform

SQLite stores JSON-bearing fields such as `translations` and `timezones` as text in this snapshot. PostgreSQL, MySQL, MongoDB and other exports can represent these values differently. Check nulls and parse JSON only when the value is present and valid.

SQL Server uses tables in the `world` schema inside the selected database, such as `world.cities`. PostgreSQL uses `public.cities`. Use the appropriate engine syntax; SQL dialects and import formats are not interchangeable.

## Snapshot updates

Keep a source release and its notices with your local copy. Review release migrations when IDs are merged or removed; a change of version can affect joins and stored selections. API responses, npm/PyPI packages and downloaded files can describe different versions.

## Next steps

[Choose an installation recipe](https://docs.countrystatecity.in/database/installation/index.md), inspect the [release snapshot](https://docs.countrystatecity.in/database/overview.md#database-statistics), or use the [hosted API](https://docs.countrystatecity.in/api/introduction.md) for managed access. For API field availability, compare the current [plans](https://countrystatecity.in/pricing/).


## Related topics

- [Self-hosting and extending CSC data](https://docs.countrystatecity.in/database/self-hosting-and-custom-data.md)
- [Multi-Level Territories](https://docs.countrystatecity.in/database/territories.md)
- [City Types](https://docs.countrystatecity.in/database/city-types.md)
- [Postcodes](https://docs.countrystatecity.in/database/postcodes.md)
- [Export Formats](https://docs.countrystatecity.in/tools/export-tool/formats.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.