diff --git a/sql-queries-6/add-foreign-key-existing-table/add-foreign-key-mssql.sql b/sql-queries-6/add-foreign-key-existing-table/add-foreign-key-mssql.sql new file mode 100644 index 00000000..89fb908c --- /dev/null +++ b/sql-queries-6/add-foreign-key-existing-table/add-foreign-key-mssql.sql @@ -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); diff --git a/sql-queries-6/add-foreign-key-existing-table/add-foreign-key-mysql.sql b/sql-queries-6/add-foreign-key-existing-table/add-foreign-key-mysql.sql new file mode 100644 index 00000000..33a09c53 --- /dev/null +++ b/sql-queries-6/add-foreign-key-existing-table/add-foreign-key-mysql.sql @@ -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); diff --git a/sql-queries-6/add-foreign-key-existing-table/add-foreign-key-postgresql.sql b/sql-queries-6/add-foreign-key-existing-table/add-foreign-key-postgresql.sql new file mode 100644 index 00000000..9b103e4a --- /dev/null +++ b/sql-queries-6/add-foreign-key-existing-table/add-foreign-key-postgresql.sql @@ -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);