-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalter-table.sql
More file actions
47 lines (36 loc) · 923 Bytes
/
alter-table.sql
File metadata and controls
47 lines (36 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- 1. Add the column (initially nullable)
ALTER TABLE cars
ADD COLUMN dealership_id INTEGER;
-- 2. Insert data to backfill the dealership_id column
-- Update cars, set the dealership_id to 1
-- where the dealership_id IS NULL
UPDATE cars SET
dealership_id = 1
WHERE
dealership_id IS NULL;
-- 3. Add the NOT NULL constraint
ALTER TABLE cars
ALTER COLUMN dealership_id SET NOT NULL;
-- 4. Add the foreign key constraint
ALTER TABLE cars
ADD CONSTRAINT dealership_fk FOREIGN KEY (dealership_id)
REFERENCES dealerships(id);
/*
Alter the cars table
add a not null constraint to these columns:
brand
model
year
price
color
condition
sold
*/
ALTER TABLE cars
ALTER COLUMN brand SET NOT NULL,
ALTER COLUMN model SET NOT NULL,
ALTER COLUMN year SET NOT NULL,
ALTER COLUMN price SET NOT NULL,
ALTER COLUMN color SET NOT NULL,
ALTER COLUMN condition SET NOT NULL,
ALTER COLUMN sold SET NOT NULL;