Skip to content

Commit 1e1b25b

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

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

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

+62-1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,65 @@ After running the statement, you can verify the column was added with:
9393
DESCRIBE TABLE table_name;
9494
```
9595

96-
Let me know if you still encounter issues or need further clarification!
96+
Let me know if you still encounter issues or need further clarification!
97+
98+
---
99+
100+
The error you're encountering (`SQLCODE=-193`) in IBM Db2 occurs because you're trying to add a `NOT NULL` column to an existing table without specifying a `DEFAULT` value, and the table likely already contains rows. In Db2, when you add a new column with a `NOT NULL` constraint to a table that has existing data, you **must** provide a `DEFAULT` value. This is because Db2 needs to populate the new column with a value for all existing rows, and `NOT NULL` prohibits null values. Without a `DEFAULT`, Db2 doesn't know what to put in those rows, resulting in the error.
101+
102+
### Why This Happens
103+
- The `NOT NULL` constraint means the column cannot contain null values.
104+
- If the table is empty (no rows), adding a `NOT NULL` column without a `DEFAULT` is fine because there’s nothing to populate.
105+
- If the table has rows, Db2 requires a `DEFAULT` value to fill the new column for those existing rows.
106+
107+
### How to Fix It
108+
Add a `DEFAULT` clause to your `ALTER TABLE` statement to specify a value for existing rows. For a `BIGINT` column, a common default might be `0`, but you can choose any value that makes sense for your use case.
109+
110+
#### Corrected Syntax
111+
```sql
112+
ALTER TABLE table_name
113+
ADD COLUMN column_name BIGINT NOT NULL DEFAULT 0;
114+
```
115+
116+
### Example
117+
If your table is `employees` and you’re adding a column `employee_id`:
118+
```sql
119+
ALTER TABLE employees
120+
ADD COLUMN employee_id BIGINT NOT NULL DEFAULT 0;
121+
```
122+
This will:
123+
- Add the `employee_id` column as a `BIGINT`.
124+
- Set it to `NOT NULL`.
125+
- Populate it with `0` for all existing rows.
126+
127+
### Alternative Solutions
128+
1. **If You Don’t Want a Default Value**:
129+
- Add the column without `NOT NULL`, then update it manually and add the constraint later:
130+
```sql
131+
ALTER TABLE table_name
132+
ADD COLUMN column_name BIGINT;
133+
134+
UPDATE table_name
135+
SET column_name = 0; -- Or another value
136+
137+
ALTER TABLE table_name
138+
ALTER COLUMN column_name SET NOT NULL;
139+
```
140+
141+
2. **If the Table Should Be Empty**:
142+
- If you believe the table should have no rows and this error is unexpected, check the row count:
143+
```sql
144+
SELECT COUNT(*) FROM table_name;
145+
```
146+
- If it’s empty, the original command (`ADD COLUMN column_name BIGINT NOT NULL`) should work without a `DEFAULT`.
147+
148+
### Key Notes
149+
- Choose a `DEFAULT` value that aligns with your data model (e.g., `-1`, `0`, or another meaningful number).
150+
- After adding the column, you can update the values as needed:
151+
```sql
152+
UPDATE table_name
153+
SET column_name = <new_value>
154+
WHERE <condition>;
155+
```
156+
157+
Let me know if you need help picking a default value or troubleshooting further!

0 commit comments

Comments
 (0)