Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL-140 Adding a Foreign Key to an Existing SQL Table #252

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--adding a new column for foreign key
ALTER TABLE Student
ADD department_id INT;

--adding a foreign key to the student table
ALTER TABLE Student
ADD CONSTRAINT fk_student_department
FOREIGN KEY (department_id) REFERENCES Department(id);

--adding unique constraint in the department table
ALTER TABLE Department
ADD CONSTRAINT uq_department UNIQUE (id, name);

--adding a foreign key to multiple columns
ALTER TABLE Course
ADD CONSTRAINT fk_course_department FOREIGN KEY (department_id, name)
REFERENCES Department (id, name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--adding a new column for foreign key
ALTER TABLE Student
ADD COLUMN department_id INT;

-- adding a foreign key to an existing table
ALTER TABLE Student
ADD CONSTRAINT fk_student_department
FOREIGN KEY (department_id) REFERENCES Department(id);

--adding unique constraints in the department table
ALTER TABLE Department
ADD CONSTRAINT uq_department UNIQUE (id, name);

--adding a foreign key to multiple columns
ALTER TABLE Course
ADD CONSTRAINT fk_course_department FOREIGN KEY (department_id, name)
REFERENCES Department (id, name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--adding a new column
ALTER TABLE Student
ADD COLUMN department_id INT;

--adding a foreign key constraint to an existing table
ALTER TABLE Student
ADD CONSTRAINT fk_student_department
FOREIGN KEY (department_id) REFERENCES Department(id);

--adding a foreign key constraint on multiple columns
ALTER TABLE Course
ADD CONSTRAINT fk_course_department
FOREIGN KEY (department_id, name) REFERENCES Department (id, name);