DBMSP4
DBMSP4
Create the Students, Professors, Courses, Enrollments, CourseComponents, and Marks tables with appropriate
primary keys, foreign keys, other constraints(check, not null, unique) , and default value wherever required .
Table created.
Table created.
Table created.
Table created.
3. Add an age column to the Student table and update the age of all students.
SQL> alter table students add age int;
Table altered.
SQL> update students set age=(2024-TO_CHAR(DOB,'YYYY'));
6 rows updated.
4. Set the foreign key constraint on Enrollments such that deleting a student record in Students automatically
deletes related records in Enrollments.
SQL> select constraint_name,column_name
2 from user_cons_columns
3 where table_name='ENROLLMENTS';
5. Modify the foreign key constraint on Enrollments so that deleting a course record in Courses sets the CourseID
in Enrollments to NULL.
SQL> select constraint_name,column_name
2 from user_cons_columns
3 where table_name='ENROLLMENTS';
4 REFERENCES Courses(CourseID)
5 ON DELETE SET NULL;
Table altered.
6. Try deleting a record in Courses that is referenced in Enrollments to observe the RESTRICT action preventing
the deletion.
Note:
Restrict action is set by default for every foreign key in the table.
RESTRICT : Rejects the delete or update operation for the parent table. Specifying RESTRICT (or NO ACTION ) is the
same as omitting the ON DELETE or ON UPDATE clause. For example, if a primary key value is referenced by a value in
the foreign key, then the referenced primary key value cannot be deleted because of the dependent data.
Table altered.
7. Change the StudentID in Students and observe the automatic update in Enrollments.
SQL> CREATE OR REPLACE TRIGGER AUTOUPDATE
2 AFTER UPDATE ON STUDENTS
3 FOR EACH ROW
4 DECLARE
5 BEGIN
6 IF :NEW.STUDENTID <> :OLD.STUDENTID THEN
7 UPDATE ENROLLMENTS
8 SET STUDENTID = :NEW.STUDENTID
9 WHERE STUDENTID = :OLD.STUDENTID;
10 END IF;
11 END;
12 /
Trigger created.
1 row updated.
8. Delete a student from Students and observe the cascade effect on Enrollments.
9. Attempt to insert a record into Enrollments with a non-existent StudentID or CourseID to demonstrate constraint
violation.
non-existent StudentID:
SQL> insert into enrollments values (407,'26BCE197',501,4);
insert into enrollments values (407,'26BCE197',501,4)
*
ERROR at line 1:
ORA-02291: integrity constraint (BCE22190.SYS_C00220638) violated - parent key not found
non-existent CourseID:
SQL> insert into enrollments values (407,'22BCE197',801,4);
insert into enrollments values (407,'22BCE197',801,4)
*
ERROR at line 1:
ORA-02291: integrity constraint (BCE22190.SYS_C00220639) violated - parent key not found
=========>> QUERY 1:
SQL> create table Enrollments (
2 EnrollmentID int primary key,
3 StudentID int references Students(StudentID),
4 CourseID int references Courses(CourseID),
5 Semester varchar(30) check (Semester >=1 and Semester<=8));
StudentID int references Students(StudentID),
*
ERROR at line 3:
ORA-02267: column type incompatible with referenced column type
Error: StudentID datatype in Students table is defined as char(8) but we tried to define it as int here.
SQL> ED
1* select CONSTRAINT_NAME, CONSTRAINT_TYPE, SEARCH_CONDITION from USER_CONSTRAINTS where
TABLE_NAME='COURSECOMPONENTS'
SQL> /
SQL> ed
1 select constraint_name,column_name
2 from user_cons_columns
3* where table_name='COURSECOMPONENTS'
SQL> /
SQL> ALTER TABLE COURSECOMPONENTS DROP CONSTRAINT SYS_C00220656;
Table altered.
Common Error: Child record found when trying to delete a record from 1 table that has a column as a foreign key in
another table and that table’s foreign key constraint is not set to “ON DELETE CASCADE”.
ERROR at line 1:
ORA-02292: integrity constraint (BCE22190.SYS_C00220658) violated - child record found
Solution: Drop that constraint from the other after generating the constraint names, then add a new constraint for the
foreign key with the “ON DELETE CASCADE” attribute.
SQL> select constraint_name,column_name
2 from user_cons_columns
3 where table_name='COURSECOMPONENTS';
Note:
ON DELETE CASCADE specifies that when rows containing referenced key values are deleted from a parent table, rows in
child tables with dependent foreign keys are also deleted.
ON DELETE SET NULL clause specifies that the child data is set to NULL when the parent data is deleted. The child data is
not deleted.
=========>> QUERY 6: