Lab 5
Views and Roles
CSE 4308
DATABASE MANAGEMENT SYSTEMS LAB
SUBMITTED BY: MD. NAZMUS SADIQ
STUDENT ID: 210041139
SECTION: 1(A) CSE
In the provided SQL script, several SQL commands and statements are executed to create a database
schema for an educational institution, define roles and privileges for users, and perform various data
manipulation operations. Here's a step-by-step explanation of the actions taken:
1. **Table Creation**:
- A series of SQL `CREATE TABLE` statements are used to define the structure of the database. Tables
are created for `classroom`, `department`, `course`, `instructor`, `section`, `teaches`, `student`, `takes`,
`advisor`, `time_slot`, and `prereq`. These tables represent various aspects of the educational institution,
such as classrooms, departments, courses, instructors, students, and their relationships. Before creating
the tables, duplicacy was removed by deleting them if they already existed.
create table classroom
(building varchar(15),
room_number varchar(7),
capacity numeric(4,0),
primary key (building, room_number)
);
create table department
(dept_name varchar(20),
building varchar(15),
budget numeric(12,2) check (budget > 0),
primary key (dept_name)
);
create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department (dept_name)
on delete set null
);
create table instructor
(ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2) check (salary > 29000),
primary key (ID),
foreign key (dept_name) references department (dept_name)
on delete set null
);
create table section
(course_id varchar(8),
sec_id varchar(8),
semester varchar(6)
check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
year numeric(4,0) check (year > 1701 and year < 2100),
building varchar(15),
room_number varchar(7),
time_slot_id varchar(4),
primary key (course_id, sec_id, semester, year),
foreign key (course_id) references course (course_id)
on delete cascade,
foreign key (building, room_number) references classroom (building,
room_number)
on delete set null
);
create table teaches
(ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id, sec_id, semester, year) references section
(course_id, sec_id, semester, year)
on delete cascade,
foreign key (ID) references instructor (ID)
on delete cascade
);
create table student
(ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0) check (tot_cred >= 0),
primary key (ID),
foreign key (dept_name) references department (dept_name)
on delete set null
);
create table takes
(ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
grade varchar(2),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id, sec_id, semester, year) references section
(course_id, sec_id, semester, year)
on delete cascade,
foreign key (ID) references student (ID)
on delete cascade
);
create table advisor
(s_ID varchar(5),
i_ID varchar(5),
primary key (s_ID),
foreign key (i_ID) references instructor (ID)
on delete set null,
foreign key (s_ID) references student (ID)
on delete cascade
);
create table time_slot
(time_slot_id varchar(4),
day varchar(1),
start_hr numeric(2) check (start_hr >= 0 and start_hr < 24),
start_min numeric(2) check (start_min >= 0 and start_min < 60),
end_hr numeric(2) check (end_hr >= 0 and end_hr < 24),
end_min numeric(2) check (end_min >= 0 and end_min < 60),
primary key (time_slot_id, day, start_hr, start_min)
);
create table prereq
(course_id varchar(8),
prereq_id varchar(8),
primary key (course_id, prereq_id),
foreign key (course_id) references course (course_id)
on delete cascade,
foreign key (prereq_id) references course (course_id)
);
2. **Deletion of Data**:
- A series of `DELETE FROM` statements are used to remove any existing data from the tables. This
ensures that the database starts with no existing records.
delete from prereq;
delete from time_slot;
delete from advisor;
delete from takes;
delete from student;
delete from teaches;
delete from section;
delete from instructor;
delete from course;
delete from department;
delete from classroom;
3. **Insertion of Data**:
- A series of `INSERT INTO` statements are used to populate the tables with sample data. Data is
inserted into tables like `classroom`, `department`, `course`, `instructor`, `section`, `teaches`, `student`,
`takes`, `advisor`, and `time_slot`. This sample data represents courses, instructors, students, and their
enrollment information.
insert into classroom values ('Packard', '101', '500');
insert into department values ('Biology', 'Watson', '90000');
insert into course values ('BIO-101', 'Intro. to Biology', 'Biology', '4');
insert into instructor values ('10101', 'Srinivasan', 'Comp. Sci.', '65000');
insert into section values ('BIO-101', '1', 'Summer', '2017', 'Painter', '514',
'B');
insert into teaches values ('10101', 'CS-101', '1', 'Fall', '2017');
insert into student values ('44553', 'Peltier', 'Physics', '56');
insert into takes values ('12345', 'CS-101', '1', 'Fall', '2017', 'C');
insert into advisor values ('23121', '76543');
insert into time_slot values ('A', 'W', '8', '0', '8', '50');
insert into prereq values ('BIO-399', 'BIO-101');
4. **View Creation**:
- Two views are created using the `CREATE VIEW` statements. These views are `Advisor_Selection` and
`Student_Count`. Duplicacy was removed by deleting them if they already existed.
- `Advisor_Selection` is a view that selects information about instructors.
- `Student_Count` is a view that calculates the number of students each advisor is responsible for.
drop view Advisor_Selection;
drop view Student_Count;
create or replace view Advisor_Selection as select I.ID,I.name,I.dept_name from
instructor I;
create or replace view Student_Count as select S.name,
(SELECT COUNT(s_ID) FROM advisor A WHERE A.i_ID = S.ID) AS no_of_students
from Advisor_Selection S;
5. **Role Creation**:
- Four database roles are created using the `CREATE ROLE` statements. These roles are `student`,
`course_teacher`, `head_of_the_dept`, and `administrator`. Roles are used to grant specific privileges to
different user types. `. Duplicacy was removed by deleting them if they already existed.
drop role student;
drop role course_teacher;
drop role head_of_the_dept;
drop role administrator;
create role student;
create role course_teacher;
create role head_of_the_dept;
create role administrator;
6. **Privilege Granting**:
- A series of `GRANT` statements are used to assign specific privileges to roles and users:
- `student` role is granted the `SELECT` privilege on the `advisor` and `course` tables.
- `course_teacher` role is granted the `SELECT` privilege on the `student` table and the `SELECT` and
`UPDATE` privileges on the `course` table. It is also granted the `course_teacher` role.
- `head_of_the_dept` role is granted the `course_teacher` role, and it is also granted the `INSERT`
privilege on the `course` and `student` tables.
- `administrator` role is granted the `SELECT` and `UPDATE` privileges on the `department` table and
the `SELECT` privilege on the `instructor` table. `. Duplicacy was removed by deleting them if they
already existed.
grant select on advisor to student;
grant select on course to student;
grant select on course to course_teacher;
grant select on student to course_teacher;
grant course_teacher to head_of_the_dept;
grant insert on course to head_of_the_dept;
grant insert on student to head_of_the_dept;
grant select,update on department to administrator;
grant select on instructor to administrator;
7. **User Creation**:
- Four database users are created using the `CREATE USER` statements. These users are
`student_user`, `teacher_user`, `head_of_the_dept_user`, and `administrator_user`. Each user is
identified by a password. Duplicacy was removed by deleting them if they already existed.
drop user student_user cascade;
drop user teacher_user cascade;
drop user head_of_the_dept_user cascade;
drop user administrator_user cascade;
create user student_user identified by trinity;
grant create session , resource , create tablespace to student_user ;
create user teacher_user identified by trinity;
grant create session , resource , create tablespace to teacher_user ;
create user head_of_the_dept_user identified by trinity;
grant create session , resource , create tablespace to head_of_the_dept_user;
create user administrator_user identified by trinity;
grant create session , resource , create tablespace to administrator_user ;
8. **Privilege Assignment to Users**:
- Privileges are assigned to users using `GRANT` statements:
- `student_user` is granted the `student` role.
- `teacher_user` is granted the `course_teacher` role.
- `head_of_the_dept_user` is granted the `head_of_the_dept` role.
- `administrator_user` is granted the `administrator` role.
grant student to student_user;
grant course_teacher to teacher_user;
grant head_of_the_dept to head_of_the_dept_user;
grant administrator to administrator_user;
9. **Connection and Queries by Users**:
- `CONNECT` statements are used to connect to the database using different user accounts, each with
its assigned role and privileges. Users then execute `SELECT` statements to query data from various
tables.
In summary, the script defines the database schema, creates roles and users, assigns privileges, and
demonstrates how different users with distinct roles can access and manipulate data in the database.
This is a common practice in database management to ensure data security and proper access control.
connect student_user/trinity;
select * from advisor;
select * from course;
connect teacher_user/trinity;
select * from student;
select * from course;
connect head_of_the_dept_user/trinity;
select * from student;
select * from course;
connect administrator_user/trinity;
select * from department;
select * from instructor;