Repository: macbre/sql-metadata

Issue:
Wrong `tables` in parsed result with sql like `ALTER TABLE ... ADD KEY ...`
Different results of `Parser(sql)` in these sqls:

- `ADD KEY`: columns are considered as tables, 2 results in `tables`
- `ADD INDEX`: 1 table matched

<img width="835" alt="Image" src="https://github.com/user-attachments/assets/dd2d4227-e89e-46ec-bb25-96f7919ce8e0" />

Hints from issue comments:
@macbre: ```sql
ALTER TABLE table ADD KEY `idx_foo` (`bar`);
ALTER TABLE table ADD INDEX `idx_foo` (`bar`);
```

Solution patch:
diff --git a/sql_metadata/keywords_lists.py b/sql_metadata/keywords_lists.py
index 4abeaea0..53f33342 100644
--- a/sql_metadata/keywords_lists.py
+++ b/sql_metadata/keywords_lists.py
@@ -125,6 +125,7 @@ class TokenType(str, Enum):
     "RETURNING",
     "VALUES",
     "INDEX",
+    "KEY",
     "WITH",
     "WINDOW",
 }


Test patch:
diff --git a/test/test_alter.py b/test/test_alter.py
new file mode 100644
index 00000000..572dba2c
--- /dev/null
+++ b/test/test_alter.py
@@ -0,0 +1,13 @@
+from sql_metadata import Parser, QueryType
+
+
+def test_alter_table_indices_key():
+    parser = Parser("ALTER TABLE foo_table ADD KEY `idx_foo` (`bar`);")
+    assert parser.query_type == QueryType.ALTER
+    assert parser.tables == ["foo_table"]
+
+
+def test_alter_table_indices_index():
+    parser = Parser("ALTER TABLE foo_table ADD INDEX `idx_foo` (`bar`);")
+    assert parser.query_type == QueryType.ALTER
+    assert parser.tables == ["foo_table"]