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

# vsql_luhn extension for MySQL

> The vsql_luhn community extension validates Luhn check digits in MySQL and generates them, for card numbers, IMEI codes, barcodes, and similar identifiers.

Card numbers, IMEI codes, barcodes, and Canadian social insurance numbers all
carry a Luhn check digit, and a wrong digit is the most common way a typed
identifier goes bad. `vsql_luhn` checks that digit and generates it, so a
`CHECK` constraint can refuse a bad number before it reaches a row.

|                              |                                                                     |
| ---------------------------- | ------------------------------------------------------------------- |
| **Maintainer**               | [Anurag Ojha](https://github.com/intojhanurag)                      |
| **Source and documentation** | [intojhanurag/vsql-luhn](https://github.com/intojhanurag/vsql-luhn) |
| **License**                  | GPL-2.0                                                             |

<Note>
  A third party writes and maintains this extension. VillageSQL does not build,
  test, or ship it, and the description below follows the maintainer's own
  documentation. Read that documentation before you rely on it.
</Note>

## Install

Build it from the repository, then install it into the server:

```sql theme={null}
INSTALL EXTENSION vsql_luhn;
```

## What it adds

| Function                   | Returns | What it does                                                                                                                                       |
| -------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `luhn_valid(value)`        | int     | 1 when the string passes the checksum, 0 when it does not. Whitespace and hyphens are ignored, and non-numeric text returns 0 rather than an error |
| `luhn_checkdigit(partial)` | text    | Appends the correct check digit to a partial identifier                                                                                            |

Because `luhn_valid` answers 0 instead of failing on text that is not a number,
it drops straight into a `WHERE` clause over a column of unvalidated input.

## See also

* [Available extensions](/docs/mysql-9.7/stable/extensions) — the full catalog
* [intojhanurag/vsql-luhn](https://github.com/intojhanurag/vsql-luhn) — source, examples, and the known limitations
