Question: What's wrong with my code? I am working in postgres and keep getting errors when I try to insert data. Please help. 3 . Create

What's wrong with my code?
I am working in postgres and keep getting errors when I try to insert data. Please help.
3. Create a trigger function on the CUSTOMER table called reset_password() that will set the Password column to the word RESET when a new row is inserted into the table. Create a trigger to use the trigger function. Then, insert a new row into CUSTOMER with the following values:
Customer_id: (do NOT include the customer_id on the insert. It will be updated automatically).
Email_address: your own email address
Password: NewPass
First_name: your first name
Last_name: your last name
Shipping_address_id: 515
Billing_address_id: 515
After the insert, do: select * from customer where email_address =, replacing with the email that you entered above, and screenshot the resulting row showing that the trigger worked correctly (the Password column should have RESET).
CREATE OR REPLACE FUNCTION reset_password()
RETURNS TRIGGER AS $$
BEGIN
NEW.password := 'RESET';
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER reset_password_trigger
BEFORE INSERT ON customer
FOR EACH ROW
EXECUTE FUNCTION reset_password();
Insert into CUSTOMER (email_address, password, first_name, last_name, shipping_address_id, billing_address_id)
values ('xxxxxxxxxxxxxxx', 'NewPass', 'xxx','xxxx',515,515);
Select * from customer
where email_address ='xxxxxxxxxxx';
ERROR: Key (shipping_address_id)=(515) is not present in table "address".insert or update on table "customer" violates foreign key constraint "customer_shipping_address_id_fkey"
ERROR: insert or update on table "customer" violates foreign key constraint "customer_shipping_address_id_fkey"
SQL state: 23503
Detail: Key (shipping_address_id)=(515) is not present in table "address".

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!