Вы находитесь на странице: 1из 8

Experiment 01 

 
Aim  
Experiment to Design EER diagram for given case study, convert it into relational model 
(Schema) and execute different types of queries, procedures and triggers on it. 
Queries:7,Procedure:-2,functions:-1,Trigger-2 
 
Case Study  
The Social Networking Management System revolves around the main entity PERSON 
which is the user who wants to join the networking site.  
The 'ADMINISTRATOR' authenticates the details of the person. He has his own login-id 
and password. He verifies user details like name, id, email, sex, birthdate. 
The entity PERSON has an id/username which serves as a displayname, his name, 
email-id, gender, birth-date and his age. 
His ACTIVITIES involve sharing posts,statuses. The user can also like posts,comment 
on them, attend events that he is interested in 
The user can also connect with FRIENDS. Every friend is a user and inherits the default 
attributes of a user of the social networking site such as a displayname, gender, 
email-id, etc. 
The user can communicate with other users by MESSAGES. The date/time when the 
message is sent and is received is recorded. The message type/format indicates the 
type of the message(text,image,GIF,etc). 
The person can join a GROUP which has a name, date created, type(public/restricted) 
and the number of members.  
 
 
 
 
 
 
 
 
 
 
 
   
EER Diagram  
 

 
Fig.​ EER Diagram 
 
 
Table Creation 
create table PERSON 

ID VARCHAR(150) PRIMARY KEY, 
EMAIL_ID VARCHAR(300) NOT NULL UNIQUE, 
GENDER VARCHAR(1) NOT NULL, 
B_DATE DATE  
); 
 
create table ADMINISTRATOR 

LOGIN_ID VARCHAR(150) PRIMARY KEY, 
AD_PASSWORD VARCHAR(100) NOT NULL UNIQUE, 
); 
 
 
create table ACTIVITY 

ACTIVITY_ID VARCHAR(100) PRIMary key, 
A_MESSAGES VARCHAR(400), 
A_STATUS VARCHAR(100), 
LIKES INT, 
COMMENTS INT,  
); 
 
create table PGROUP 

NAME VARCHAR(50) PRIMARY KEY, 
DATE_CREATED DATETIME, 
GROUP_TYPE VARCHAR(10), 
MEMBERS INT, 
); 
 
create table FRIENDS 

P_ID VARCHAR(150) FOREIGN KEY(P_ID) REFERENCES 
PERSON(DISPLAYNAME), 
ID VARCHAR(150) NOT NULL, 
NAME VARCHAR(100) NOT NULL, 
EMAIL_ID VARCHAR(300) NOT NULL UNIQUE, 
GENDER VARCHAR(1) NOT NULL, 
B_DATE DATE, 
PRIMARY KEY(P_ID,DISPLAYNAME) 
); 
 
create table authenticates 

ADMIN_ID VARCHAR(150) FOREIGN KEY(ADMINID) REFERENCES 
ADMINISTRATOR(LOGIN_ID), 
P_ID VARCHAR(150) FOREIGN KEY (PID) REFERENCES 
PERSON(DISPLAYNAME), 
PRIMARY KEY(ADMINID,PID), 
); 
 
 
create table joins
(
P_IDJ VARCHAR(150) FOREIGN KEY (P_IDJ) REFERENCES PERSON(ID),
G_NAME VARCHAR(50) FOREIGN KEY(G_NAME) REFERENCES PGROUP(NAME),
PRIMARY KEY(P_IDJ,G_NAME),
);
 
create table performs
(
P_IDP VARCHAR(150) FOREIGN KEY (P_IDP) REFERENCES PERSON(ID),
ACTIVITY_IDP VARCHAR(100) FOREIGN KEY(ACTIVITY_IDP) REFERENCES
ACTIVITY(ACTIVITY_ID),
PRIMARY KEY(P_IDP,ACTIVITY_IDP),
);
 
SCHEMA: 
 

 
 
Triggers 

TRIGGER 1:
create trigger insert_values on PGROUP for insert as
begin
declare @type as varchar(10)
select @type=group_type from inserted

if @type = 'PRIVATE'
begin
rollback
end
else
begin
commit
End
end

O/p TRIGGER 1:

TRIGGER 2:
create trigger insert_values on PGROUP for insert as
begin
declare @type as varchar(10), @members as int
select @type=group_type from inserted
select @members=members from inserted

if @members>=500
begin
Update PGROUP set GROUP_TYPE=’PUBLIC’
end
else
begin
Update PGROUP set GROUP_TYPE=’PRIVATE’
End
end

 
Stored Procedure 
PROC 1:

CREATE PROCEDURE X @gender varchar(1)


AS
BEGIN
SELECT * FROM PERSON WHERE GENDER = @gender
END

EXEC X "M";

O/p PROC 1:
PROC 2:

create procedure get_friendslist @person_id as varchar(150)


as
begin
Select * from joins where P_IDJ in (select ID from FRIENDS where P_ID = @person_id)
end

exec get_friendslist 'gauravsahu'

O/p PROC 2:

 
 
FUNCTION:

Create function friend_count(@P_ID as varchar)


returns table as
Return (select P_ID,count(*) as No_of_friends from FRIENDS where P_ID= @P_ID
group by P_ID)

select * from friend_count('gauravsahu');

O/p FUNCTION:

 
 
   
QUERIES  
1) 
→ ​select * from person where id in (select P_IDJ from joins where G_NAME in (select name 
from PGROUP where MEMBERS > 125 )); 

2) 
→​SELECT P.P_IDP, A.A_MESSAGES, A.LIKES FROM PERFORMS as P, ACTIVITY as A where 
LIKES = (select min(LIKES) from Activity); 
 

 
 
 
 
3) 
→ SELECT * FROM ADMINISTRATOR LEFT OUTER JOIN PGROUP ON GROUP_TYPE in 
(select GROUP_TYPE from administrator) ; 

 
 
 
4) 
→ SELECT * FROM PGROUP WHERE MEMBERS >125 AND NAME LIKE 'D1%' 

 
 
5) 
→ Select * from joins where P_IDJ in (select ID from FRIENDS where P_ID = ‘anuragshelar’) 

6)
→ select P_ID,count(*) as No_of_friends from FRIENDS where P_ID= ‘gauravsahu’ group by 
P_ID 

7) 
→ select ID from PERSON where ID in (select P_IDP from PERFORMS where ACTIVITY_IDP in 
(select ACTIVITY_ID from ACTIVITY where A_STATUS='HAPPY')); 

 
 

Вам также может понравиться