oracle-databaseinsert-updatealter-tablelive-update

How do I make changes to a "live database"?


I have already created a vehicle_unit table

 CREATE TABLE vehicle_unit (
 garage_code NUMERIC(2) NOT NULL,
 vunit_id  NUMERIC(6) NOT NULL,
 vunit_purchase_price NUMERIC(7,2),
 vunit_exhibition_flag CHAR(1) NOT NULL,
 vehicle_insurance_id VARCHAR(20) NOT NULL,
 vunit_rego VARCHAR(8) NOT NULL
);

And inserted some values in them:

INSERT INTO vehicle_unit(vunit_purchase_price,vunit_exhibition_flag,vehicle_insurance_id,vunit_rego,vunit_id, garage_code)
SELECT 50000.00, 'R', 'sports-ute-449-12b', 'RD3161',garage_count_vehicles, garage_code
FROM garage
WHERE garage_email='caulfield@rdbms.example.com';

If I want to add a NON NULL column called condition_flag how do I change this live database? Do I use alter table?But how do I insert this new value?

EDIT: I tried 'Alter table" but I got an error saying table must be empty to add mandatory (NOT NULL) column.


Solution

  • Before the ALTER ... NOT NULL statement, update all existing records to have a value (if they already don't). Then issue your ALTER ... NOT NULL, or as already recommended by JNevill, also add a DEFAULT clause as well (that will save you from modifying code that may not be providing values during INSERTs).