Give me a SQL query to add a "status" column to an orders table that defaults to PENDING. Set it to COMPLETE if "completed_at" is set.'
1
$
@Faheem4166
posted
8 months ago
SQL: Add Status Column
Default (GPT-3.5)
User 'Give me a SQL query to add a "status" column to an orders table that defaults to PENDING. Set it to COMPLETE if "completed_at" is set.'
ChatGPT You can use the following SQL query to add a "status" column to an orders table with a default value of "PENDING" and set it to "COMPLETE" if the "completed_at" column is not null:
sql Copy code ALTER TABLE orders ADD COLUMN status VARCHAR(10) DEFAULT 'PENDING';
UPDATE orders SET status = 'COMPLETE' WHERE completed_at IS NOT NULL; This query first adds the "status" column with a default value of "PENDING" to the "orders" table. Then, it updates the "status" to "COMPLETE" for rows where the "completed_at" column is not null, effectively setting it to "COMPLETE" when an order is completed.