In: Computer Science
Project: Building a Relational Database Management System
This project is where you show off your ability to (1) translate a business requirement into a database design, (2) design a database using one-to-many and many-to-many relationships, and (3) know when to use LEFT and/or RIGHT JOINs to build result sets for reporting.
An organization grants key-card access to rooms based on groups that key-card holders belong to. You may assume that users below to only one group. Your job is to design the database that supports the key-card system.
There are six users, and four groups. Modesto and Ayine are in group “I.T.” Christopher and Cheong woo are in group “Sales”. There are four rooms: “101”, “102”, “Auditorium A”, and “Auditorium B”. Saulat is in group “Administration.” Group “Operations” currently doesn’t have any users assigned. I.T. should be able to access Rooms 101 and 102. Sales should be able to access Rooms 102 and Auditorium A. Administration does not have access to any rooms. Heidy is a new employee, who has not yet been assigned to any group.
After you determine the tables any relationships between the tables (One to many? Many to one? Many to many?), you should create the tables and populate them with the information indicated above.
Next, write SELECT statements that provide the following information:
• All groups, and the users in each group. A group should appear even if there are no users assigned to the group.
• All rooms, and the groups assigned to each room. The rooms should appear even if no groups have been assigned to them.
• A list of users, the groups that they belong to, and the rooms to which they are assigned. This should be sorted alphabetically by user, then by group, then by room.
To manage this system, I have used 4 database tables names Users, Groups, Rooms, Access and created new fields UserId, GroupId and RoomId inorder to maintain the integrity of the data. Structure of these tables are as below:
The relationship between these entities are represented as below:
-------------------------------------
1. Creating the table Users:
CREATE TABLE Users(
UserID int NOT NULL,
UserName varchar(50) NOT NULL,
GroupID int,
PRIMARY KEY (UserID), FOREIGN KEY (GroupId)
REFERENCES Groups(GroupId) ); );
2. Creating the table Groups:
CREATE TABLE Groups(
GroupId int NOT NULL,
GroupName varchar(50) NOT NULL,
PRIMARY KEY (GroupId) );
3. Creating the table Rooms:
CREATE TABLE Rooms(
RoomId int NOT NULL,
RoomName varchar(50) NOT NULL,
PRIMARY KEY (RoomId) );
3. Creating the table Access:
CREATE TABLE Access(
GroupId int NOT NULL,
RoomId int NOT NULL, PRIMARY KEY (RoomId), FOREIGN KEY (RoomId)
REFERENCES Rooms(RoomId), FOREIGN KEY (GroupId) REFERENCES
Groups(GroupId) );
-------------------------------------------------------
Inserting data into Users table:
1. INSERT INTO Users VALUES (1,' Modesto','1);
2. INSERT INTO Users VALUES (2,' Ayine ',1);
3. INSERT INTO Users VALUES (3,' Christopher',2);
4. INSERT INTO Users VALUES (4,' Cheong',2);
5. INSERT INTO Users VALUES (5,' Saulat',3);
6. INSERT INTO Users VALUES (6,' Heidy', NULL);
Inserting data into Groups table:
1. INSERT INTO Groups VALUES (1,' I.T.' );
2. INSERT INTO Groups VALUES (2,' Sales' );
3. INSERT INTO Groups VALUES (3,' Administration' );
4. INSERT INTO Groups VALUES (4,' Operationes.' );
Inserting data into Rooms table:
1. INSERT INTO Rooms VALUES (1,'101' );
2. INSERT INTO Rooms VALUES (2,'102' );
3. INSERT INTO Rooms VALUES (3,' Auditorium A' );
4. INSERT INTO Rooms VALUES (4,' Auditorium B' );
Inserting data into Access table:
1. INSERT INTO Access VALUES (1,1);
2. INSERT INTO Access VALUES (1,2);
3. INSERT INTO Access VALUES (2,2);
4. INSERT INTO Access VALUES (2,3);
5. INSERT INTO Access VALUES (3,NULL);
-------------------------------------------------------------------------------------------------------
1. Select G.GroupId, G.GroupName, U.UserID from Groups G LEFT JOIN Users U on G.GroupId=U.GroupID;
2. Select R.RoomId, R.RoomName, A.GroupName from Rooms R LEFT JOIN Access A on R.RoomId=A.RoomsID;
3. Select U.UserID, U.UserName, R.RoomName, G.GroupName from User U LEFT JOIN Access on U.GRoupID=A.GroupID LEFT JOIN Rooms R on A.RoomID=R.RoomId order by U.UserName,G.GroupName,R.RoomName;