Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

5
How to add/drop a foreign key constraint on a single column postgreSQL
Post Body

howdy ya'll,

newbie here and I'm working my way through The Complete SQL Bootcamp: Zero to Hero by Jose Portilla on Udemy.

I'm confused as to how to go about adding or dropping a foreign key constraint on an already existing table for a single column. In the lecture he uses SET and DROP, with ALTER TABLE but then also briefly mentions ADD CONSTRAINT but doesn't differentiate when to use which one.

For example, I have 3 tables. One called teachers, one called students, and I tried making a third called stu_tea that just combines Teacher_id from teachers, and student_id from students. Obviously I could have done the foreign key stuff initially when creating stu_tea, but I wanted to see how it could be done after the fact.

I can't for the life of me figure out how to DROP the foreign key part.

To add one I got the following to work:

ALTER TABLE stu_tea
ADD FOREIGN KEY (teacher_id)
REFERENCES teachers(teacher_id);

but how would I drop that same constraint?

I've tried googling it, and only ended up more confused after trying every possible syntax I could find.

Any help and explanation is MUCH appreciated!

Happy Holidays Ya'll

Author
Account Strength
90%
Account Age
6 years
Verified Email
Yes
Verified Flair
No
Total Karma
1,742
Link Karma
937
Comment Karma
805
Profile updated: 5 days ago
Posts updated: 5 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago