Skip to content

Commit 0607aa6

Browse files
committed
docs(notes): add ibm-db2 column note
1 parent add9feb commit 0607aa6

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

Diff for: notes/2025-04-09-ibm-db2-add-column-en.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,59 @@ ADD COLUMN phone_number VARCHAR(15);
3838
- You need appropriate permissions (e.g., `ALTER` privilege) on the table to perform this operation.
3939
- If the table is large, this operation might take some time and could lock the table, depending on the Db2 version and configuration.
4040

41-
Let me know if you need help with a specific example or additional details!
41+
Let me know if you need help with a specific example or additional details!
42+
43+
---
44+
45+
The error you're encountering (`SQLCODE=-104, SQLSTATE=42601`) in IBM Db2 indicates a syntax issue with your `ALTER TABLE` statement. Specifically, `BIGINT(8)` is not a valid syntax in Db2. Unlike some other databases (e.g., MySQL), Db2 does not use a length or precision specifier like `(8)` with the `BIGINT` data type. In Db2, `BIGINT` is a fixed 8-byte integer type that can store values from `-2^63` to `2^63-1`, and you simply declare it as `BIGINT` without additional parameters.
46+
47+
### Correct Syntax
48+
To add a `BIGINT` column with a `NOT NULL` constraint, use this:
49+
50+
```sql
51+
ALTER TABLE table_name
52+
ADD COLUMN column_name BIGINT NOT NULL;
53+
```
54+
55+
### Example
56+
If your table is called `employees` and you want to add a column `employee_id` as a `BIGINT` that cannot be null:
57+
58+
```sql
59+
ALTER TABLE employees
60+
ADD COLUMN employee_id BIGINT NOT NULL;
61+
```
62+
63+
### Why the Error Occurred
64+
- The `(8)` in `BIGINT(8)` is invalid in Db2 because `BIGINT` doesn't take a length argument. This syntax might be familiar from other databases like MySQL, where it specifies display width, but Db2 doesn't support it.
65+
- The parser encountered `BIGINT(8)` and flagged it as an "unexpected token," leading to the `SQLCODE=-104` error.
66+
67+
### Fixing the Issue
68+
1. Remove the `(8)` from your statement.
69+
2. If you need to specify a default value (since `NOT NULL` requires existing rows to have a value), add a `DEFAULT` clause. For example:
70+
71+
```sql
72+
ALTER TABLE employees
73+
ADD COLUMN employee_id BIGINT NOT NULL DEFAULT 0;
74+
```
75+
76+
Without a `DEFAULT`, adding a `NOT NULL` column to a table with existing rows will fail because Db2 won't know what value to assign to the new column for those rows.
77+
78+
### Steps to Resolve
79+
- If your table has no rows yet, simply use:
80+
```sql
81+
ALTER TABLE table_name
82+
ADD COLUMN column_name BIGINT NOT NULL;
83+
```
84+
- If your table already has rows, include a `DEFAULT` value:
85+
```sql
86+
ALTER TABLE table_name
87+
ADD COLUMN column_name BIGINT NOT NULL DEFAULT 0;
88+
```
89+
90+
### Verification
91+
After running the statement, you can verify the column was added with:
92+
```sql
93+
DESCRIBE TABLE table_name;
94+
```
95+
96+
Let me know if you still encounter issues or need further clarification!

0 commit comments

Comments
 (0)