0% found this document useful (0 votes)
24 views

DBMSP4

The document describes creating database tables with constraints and inserting sample data. It demonstrates adding, modifying and deleting records while observing constraint validations. Key operations covered are primary keys, foreign keys, check constraints, cascading deletes and updates.

Uploaded by

mitaleeextra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

DBMSP4

The document describes creating database tables with constraints and inserting sample data. It demonstrates adding, modifying and deleting records while observing constraint validations. Key operations covered are primary keys, foreign keys, check constraints, cascading deletes and updates.

Uploaded by

mitaleeextra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1.

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 .

SQL> create table Professors (


2 ProfessorID int primary key,
3 Name varchar(30) not null unique,
4 Department varchar(30) not null check(Department in('CSE','EC','EI','Mechanical','Chemical')));

Table created.

SQL> create table Courses (


2 CourseID int primary key,
3 CourseName varchar(30) not null unique,
4 Department varchar(30) not null check(Department in('CSE','EC','EI','Mechanical','Chemical')),
5 ProfessorID int references Professors(ProfessorID));

Table created.

SQL> create table Enrollments (


2 EnrollmentID int primary key,
3 StudentID char(8) references Students(StudentID),
4 CourseID int references Courses(CourseID),
5 Semester int not null check (Semester >=1 and Semester<=8));

Table created.

SQL> create table CourseComponents (


2 ComponentID int primary key,
3 CourseID int references Courses(CourseID),
4 ComponentName varchar(30) not null,
5 Weightage int check (weightage >0 and weightage <11));
Table created.

SQL> create table Marks (


2 MarkID int constraint RepeatedID primary key,
3 EnrollmentID int references Enrollments(EnrollmentID),
4 ComponentID int references CourseComponents(ComponentID ),
5 Score int default 0);

Table created.

2. Insert sample data into each table.


SQL> insert into professors values(23,'Monika Shah','CSE');
1 row created.
SQL> insert into professors values (24,'Anuja Nair','CSE');
1 row created.
SQL> insert into professors values (25,'Tarjini Vyas','EI');
1 row created.
SQL> insert into courses values (501,'DBMS','CSE',23);
1 row created.
SQL> insert into courses values (302,'Instruments','EI',25);
1 row created.

SQL> insert into CourseComponents values(9000,501,'SEE',4);


1 row created.
SQL> insert into CourseComponents values (9001,302,'SE',3);
1 row created.

SQL> insert into enrollments values (405,'22BCE190',501,3);


1 row created.
SQL> insert into enrollments values (406,'22BCE194',302,3);
1 row created.

SQL> insert into marks values (90091,405,9000,79);


1 row created.
SQL> insert into marks values (90092,406,9001,NULL);
1 row 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';

SQL> ALTER TABLE enrollments DROP CONSTRAINT SYS_C00220638;


Table altered.

SQL> ALTER TABLE enrollments


2 ADD CONSTRAINT SYS_C00220638 FOREIGN KEY (StudentID)
3 REFERENCES Students(StudentID)
4 ON DELETE CASCADE;
Table altered.

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';

SQL> ALTER TABLE enrollments DROP CONSTRAINT SYS_C00220639;


Table altered.

SQL> ALTER TABLE Enrollments


2 ADD CONSTRAINT fk_CourseID
3 FOREIGN KEY (CourseID)

4 REFERENCES Courses(CourseID)
5 ON DELETE SET NULL;
Table altered.

SQL> delete courses where courseid=501;


1 row deleted.

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.

SQL> select constraint_name,column_name


2 from user_cons_columns
3 where table_name='ENROLLMENTS';

SQL> ALTER TABLE enrollments DROP CONSTRAINT FK_COURSEID;


Table altered.

SQL> ALTER TABLE Enrollments


2 ADD CONSTRAINT fk_CourseID
3 FOREIGN KEY (CourseID)
4 REFERENCES Courses(CourseID);

Table altered.

SQL> DELETE COURSES WHERE COURSEID=302;


DELETE COURSES WHERE COURSEID=302
*
ERROR at line 1:
ORA-02292: integrity constraint (BCE22190.FK_COURSEID) violated - child record found

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.

SQL> UPDATE STUDENTS


2 SET STUDENTID='22BCE191'
3 WHERE STUDENTID='22BCE190';

1 row updated.
8. Delete a student from Students and observe the cascade effect on Enrollments.

SQL> delete students where studentid='22BCE194';


1 row deleted.

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

10. Add Marks and Grade columns to Enrollments.


SQL> alter table enrollments add marks int;
Table altered.
SQL> alter table enrollments add grade varchar(3);
Table altered.
LEARNINGS

=========>> 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.

Solution: StudentID datatype is defined as char(8) to match the Students table.


SQL> create table Enrollments (
2 EnrollmentID int primary key,
3 StudentID char(8) references Students(StudentID),
4 CourseID int references Courses(CourseID),
5 Semester varchar(30) check (Semester >=1 and Semester<=8));
StudentID char(8) references Students(StudentID),
*
ERROR at line 3:
ORA-02270: no matching unique or primary key for this column-list
Error: StudentID is being declared as a foreign key here but it is not yet a primary key in the Students table.

Solution: Set StudentID as primary key in Students table.


SQL> alter table students modify studentid char(8) primary key;
alter table students modify studentid char(8) primary key
*
ERROR at line 1:
ORA-02437: cannot validate (BCE22190.SYS_C00220623) - primary key violated
Error: StudentID can’t be declared as a primary key because all the entries in that column are not unique.

Solution: Delete the duplicate values.

SQL> delete students where studentid='22BCE181';


2 rows deleted.
SQL> delete students where studentid='22BCE187';
2 rows deleted.
SQL> alter table students modify studentid char(8) primary key;
Table altered.

=========>> QUERY 2: Checking constraints


--------------------------------> Professors table
SQL> insert into professors values(24,'Monika Shah','CSE');
insert into professors values(24,'Monika Shah','CSE')
*
ERROR at line 1:
ORA-00001: unique constraint (BCE22190.SYS_C00220519) violated
Error: Unique constraint on the Name column is violated as the same name is being repeated in another record.
Solution: Enter a record with a different name.
SQL> insert into professors values (23,'Anuja Nair','CSE');
insert into professors values (23,'Anuja Nair','CSE')
*
ERROR at line 1:
ORA-00001: unique constraint (BCE22190.SYS_C00220518) violated
Error: Primary key’s unique constraint on the ProfessorID column is violated as the same ID is being repeated in
another record.
Solution: Enter a record with a different ProfessorID.

SQL> insert into professors values (25,'Tarjini Vyas','Pharmacy');


insert into professors values (25,'Tarjini Vyas','Pharmacy')
*
ERROR at line 1:
ORA-02290: check constraint (BCE22190.SYS_C00220517) violated
Error: Check constraint on the Department column is violated as the entered value is not present in the given list
('CSE','EC','EI','Mechanical','Chemical')
Solution: Enter a record with a Department value from the list.

--------------------------------> Courses table


SQL> insert into courses values (302,'Devices','EI',26);
insert into courses values (302,'Devices','EI',26)
*
ERROR at line 1:
ORA-00001: unique constraint (BCE22190.SYS_C00220556) violated
Error: Primary key’s unique constraint on the CourseID column is violated as the same ID is being repeated in another
record.
Solution: Enter a record with a different CourseID.

SQL> insert into courses values (404,'ABCD',NULL,24);


insert into courses values (404,'ABCD',NULL,24)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("BCE22190"."COURSES"."DEPARTMENT")
Error: Not null constraint on the Department column is violated as we try to enter a NULL value.
Solution: Enter a record without a NULL value for Department.

SQL> insert into courses values (404,'ABCD','Pharma',24);


insert into courses values (404,'ABCD','Pharma',24)
*
ERROR at line 1:
ORA-02290: check constraint (BCE22190.SYS_C00220555) violated
Error: Check constraint on the Department column is violated as the entered value is not present in the given list
('CSE','EC','EI','Mechanical','Chemical')
Solution: Enter a record with a Department value from the list.

--------------------------------> CourseComponents table


SQL> insert into CourseComponents values (901,501,'SEE',0.4);
insert into C ourseComponents values (901,501,'SEE',0.4)
*
ERROR at line 1:
ORA-02290: check constraint (BCE22190.SYS_C00220656) violated
Error: The Weightage column’s check constraint has set the values between 0 and 1 only to be allowed, however, the
weightage column only accepts int so values between 0 and 1 won’t be taken.
Solution: Modify the constraint on the Weightage column.
SQL> alter table CourseComponents modify weightage int check (weightage >0 and weightage <11);
Table altered.
Error: We ended up setting another constraint on the Weightage column so now there’s 2 and neither of them can be
satisfied.
Solution: Get the constraint names, and their corresponding column names. Drop the old constraint.

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.

--------------------------------> CourseComponents table


SQL> insert into CourseComponents values (9001,402,'CE',6);
insert into CourseComponents values (9001,402,'CE',6)
*
ERROR at line 1:
ORA-00001: unique constraint (BCE22190.SYS_C00220657) violated

SQL> insert into CourseComponents values (9002,401,'SEE',4);


insert into CourseComponents values (9002,401,'SEE',4)
*
ERROR at line 1:
ORA-02291: integrity constraint (BCE22190.SYS_C00220658) violated - parent key not found
Error: Parent key not found as we tried to enter a record with a CourseID that is not given in the Courses table.
Solution: Add a record with the new CourseID in the Courses table first and then try again, or try to use existing
CourseID’s.

SQL> insert into CourseComponents values (9002,302,'SEE',12);


insert into CourseComponents values (9002,302,'SEE',12)
*
ERROR at line 1:
ORA-02290: check constraint (BCE22190.SYS_C00222162) violated

--------------------------------> Enrollments table


SQL> insert into enrollments values (407,'22BCE197',501,9);
insert into enrollments values (407,'22BCE197',501,9)
*
ERROR at line 1:
ORA-02290: check constraint (BCE22190.SYS_C00220636) violated

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

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

--------------------------------> Marks table


SQL> insert into marks values (90092,406,9001,NULL);
insert into marks values (90092,406,9001,NULL)
*
ERROR at line 1:
ORA-00001: unique constraint (BCE22190.REPEATEDID) violated
Error: Unique constraint violated, the constraint was named REPEATEDID for easy understanding of the cause of the
error.
Solution: Add a record with the unique MarkID.

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';

SQL> ALTER TABLE COURSECOMPONENTS DROP CONSTRAINT SYS_C00220658;


Table altered.
SQL> ALTER TABLE CourseComponents
2 ADD CONSTRAINT SYS_C00220658 FOREIGN KEY (CourseID)
3 REFERENCES Courses(CourseID)
4 ON DELETE CASCADE;
Table altered.

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:

SQL> ALTER TABLE Enrollments


2 ADD CONSTRAINT fk_CourseID
3 FOREIGN KEY (CourseID)
4 REFERENCES Courses(CourseID)
5 ON DELETE RESTRICT;
ON DELETE RESTRICT
*
ERROR at line 5:
ORA-00905: missing keyword

Error: Missing key word.


Solution: Try using “NO ACTION” instead of “RESTRICT”.

SQL> ALTER TABLE Enrollments


2 ADD CONSTRAINT fk_CourseID
3 FOREIGN KEY (CourseID)
4 REFERENCES Courses(CourseID)
5 ON DELETE NO ACTION;
ON DELETE NO ACTION
*
ERROR at line 5:
ORA-00905: missing keyword

Error: Missing key word.


Solution: The phrase “ON DELETE RESTRICT” or “ON DELETE NO ACTION” is not supposed to be used in the script
because it is a default setting.

You might also like