Sei sulla pagina 1di 12

if exists (select * from dbo.

sysobjects where id = object_id(N'[Class]') and


OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [Class]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[Student]') and


OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [Student]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[Marks]') and


OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [Marks]
GO

CREATE TABLE Class


(
ClassID INT PRIMARY KEY,
ClassName VARCHAR(10),
[Section] VARCHAR(1)
)
GO

CREATE TABLE Student


(
StudentID INT PRIMARY KEY,
StudentName VARCHAR(10),
ClassID INT FOREIGN KEY REFERENCES Class(ClassID)
)
GO

CREATE TABLE Marks


(
StudentID INT FOREIGN KEY REFERENCES Student(StudentID),
Marks1 DECIMAL(5,2),
Marks2 DECIMAL(5,2),
Marks3 DECIMAL(5,2)
)
GO

--Class
INSERT INTO Class (ClassID, ClassName, [Section])
VALUES(1, 'First', 'A')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(2, 'First', 'B')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(3, 'Second', 'A')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(4, 'Second', 'B')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(5, 'Third', 'A')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(6, 'Third', 'B')

--Student Records
INSERT INTO Student(StudentID, StudentName, ClassID)
VALUES(1, 'Stud1', 1)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(2, 'Stud2', 1)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(3, 'Stud3', 2)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(4, 'Stud4', 1)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(5, 'Stud5', 2)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(6, 'Stud6', 3)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(7, 'Stud7', 4)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(8, 'Stud8', 5)

INSERT INTO Student(StudentID, StudentName)


VALUES(9, 'Stud9')
-- Marks Records

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(1, 73, 72, 74)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(2, 63, 62, 64)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(3, 43, 60, 60)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(4, 53, 50, 55)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(5, 83, 42, 44)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(6, 60, 60, 60)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(7, 63, 92, 94)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(8, 83, 42, 94)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(9, 43, 42, 42)

Write the following Functions, Views & Stored Procedure

1. Create a View “ViewClassStudents” which should display StudentID, StudentName,


ClassName, [Section] using Student, Class Table
2. Display the Students who are studying ClassName = First and Section “A” with the
following columns StudentID, StudentName, ClassName, [Section] using
ViewClassStudents
3. Create a Stored Procedure USPClassStudents which should display the records from
ViewClassStudents
Add two Parameters @ClassName, @Section
Note:
When I run Exec USPClassStudents it should display all records irrespective of
ClassName and Section
When I run Exec USPClassStudents @ClassName = “First” it should display all records
with respective to ClassName
When I run Exec USPClassStudents @ClassName = “First”, @Section=”A” it should
display all records with respective to ClassName and Section
4. Write a function TotalMarks by passing three Parameters Marks1, Marks2, Marks3
It should return TotalMarks = Marks1+Marks2+Marks3
5. Display the records with the following columns
StudentName, ClassName, Section, Marks1, Marks2, Marks3, TotalMarks
Using View - ViewClassStudents, Table – Marks and Function - TotalMarks
6. Write a Query which should display StudentName, ClassName, Section, TotalMarks
Who got 2 rank in entire School using View - ViewClassStudents
7. Write a Query which should display StudentName, ClassName, Section, TotalMarks
Who got 2 rank in the ClassName, Section using View - ViewClassStudents
8. Write a Stored Procedure USPRankClassSection
Using View - ViewClassStudents
Which should display following columns
StudentName, ClassName, Section, TotalMarks
By passing the @Rank, @ClassName, @Section
If I exec USPRankClassSection @Rank = 2
It should display the Student who got 2nd in Entire School
If I exec USPRankClassSection @Rank = 3, @ClassName = ‘First’
It should display the Student who got 3rd in ClassName = ‘First’
If I exec USPRankClassSection @Rank = 3, @ClassName = ‘First’, @Section =
‘A’
It should display the Student who got 3rd in ClassName = ‘First’ and
Section ‘A’

9. Write a Stored Procedure USPAddStudent


By passing @StudentName, @ClassID
Note:
If ClassID not found in Class Table Should Not Insert the Record in Student Table
Prompt message “ClassID is not Found”
If StudentName and ClassID exists in Student Should Not Insert the Record in Student
Table
Prompt message “Student Name is already exists”

10. Write down short notes on


With Encryption (Views)
With Schema binding (Views)
With Option (Views)

For above each give me one example.

--1Q. Create a View “ViewClassStudents” which should display


--StudentID, StudentName, ClassName, [Section] using Student, Class Table

--1A)
CREATE VIEW ViewClassStudents
AS
SELECT S.StudentID, S.StudentName,C.ClassName, C.[Section]
FROM STUDENT S
INNER JOIN CLASS C ON S.ClassID=C.ClassID
--SELECT * FROM ViewClassStudents

--2Q. Display the Students who are studying ClassName = First and
--Section “A” with the following columns StudentID, StudentName,
--ClassName, [Section] using ViewClassStudents
--2A)

SELECT StudentID, StudentName,ClassName, [Section]


FROM ViewClassStudents
WHERE ClassName='First' AND [Section]='A'

--3Q. Create a Stored Procedure USPClassStudents which should display


--the records from ViewClassStudents
--Add two Parameters @ClassName, @Section
--Note:
--When I run Exec USPClassStudents it should display all records irrespective of
--ClassName and Section

--When I run Exec USPClassStudents @ClassName = “First” it should


--display all records with respective to ClassName

--When I run Exec USPClassStudents @ClassName = “First”, @Section=”A” it


--should display all records with respective to ClassName and Section

--3A.

CREATE PROC USPClassStudents


(
@ClassName VARCHAR(10) = NULL,
@Section VARCHAR(1)= NULL
)
AS
BEGIN

SELECT * FROM ViewClassStudents


WHERE ClassName=ISNULL(@ClassName, ClassName)
AND [Section]=ISNULL(@Section, [Section])
END

--EXEC USPClassStudents
--EXEC USPClassStudents 'FIRST'
--EXEC USPClassStudents 'FIRST','A'

--OR

ALTER PROC USPClassStudents


(
@ClassName VARCHAR(10),
@Section VARCHAR(1)
)

AS
BEGIN
SELECT * FROM ViewClassStudents
WHERE ISNULL(ClassName,'')=ISNULL(@ClassName,
ISNULL(ClassName,''))
AND ISNULL([Section], '')=ISNULL(@Section, ISNULL([Section],''))
END

--EXEC USPClassStudents NULL,NULL


--EXEC USPClassStudents 'FIRST',NULL
--EXEC USPClassStudents 'FIRST','A'

--4Q. Write a function TotalMarks by passing three Parameters Marks1, Marks2, Marks3

--It should return TotalMarks = Marks1+Marks2+Marks3

--4A.
ALTER FUNCTION TOTALMARKS
(
@MARKS1 DECIMAL(4,2),
@MARKS2 DECIMAL(4,2),
@MARKS3 DECIMAL(4,2)
)
RETURNS DECIMAL(6,2)
AS
BEGIN

RETURN @Marks1+@Marks2+@Marks3
END
--SELECT DBO.TOTALMARKS (MARKS1,MARKS2,MARKS3) FROM Marks

--5Q. Display the records with the following columns

--StudentName, ClassName, Section, Marks1, Marks2, Marks3, TotalMarks

--Using View - ViewClassStudents, Table – Marks and Function - TotalMarks

--5A.
SELECT V.StudentName,V. ClassName,V. [Section],
M.MARKS1,M.MARKS2,M.MARKS3,
DBO.TOTALMARKS(MARKS1,MARKS2,MARKS3)TOTALMARKS
FROM ViewClassStudents V
INNER JOIN MARKS M ON (V.StudentID=M.StudentID)

--6Q. Write a Query which should display StudentName, ClassName, Section,


TotalMarks
--Who got 2 rank in entire School using View - ViewClassStudents

--6A.
--FIRST WE CREATE VIEW WITH FOUR COLUMNS SELECTING FROM
VIEWTotStud AND
--FUNCTION THIS IS THE VIEW VIEWTOTAL

ALTER VIEW VIEWTOTAL


AS
SELECT V.StudentName,V. ClassName,V.[Section],
DBO.TOTALMARKS(MARKS1,MARKS2,MARKS3)TOTAL
FROM ViewClassStudents V
INNER JOIN MARKS M ON (M.STUDENTID=V.STUDENTID)

---WE CAN FIND RANK BY USING THE VIEW:VIEWTOTAL THE FOLLOWING


QUERY
---IS

SELECT * FROM VIEWTOTAL V1


where
(
SELECT COUNT(*) FROM VIEWTOTAL V2
WHERE V1.Total <= V2.Total
)=3

--NOTE:-2 RANK STUDENTS ARE TWO MEMBERS THAT IS WHY


-- WE TAKE 3 RANK THIS DISPLAYS TWO MEMBERS

--7Q. Write a Query which should display StudentName, ClassName, Section,


TotalMarks

--Who got 2 rank in the ClassName, Section using View - ViewClassStudents


--7A.

SELECT * FROM VIEWTOTAL V1


where
(
SELECT COUNT(*) FROM VIEWTOTAL V2
WHERE V1.Total <= V2.Total
AND V1.ClassName = V2.ClassName
AND V1.Section = V2.Section
)=1

-- 8Q. Write a Stored Procedure USPRankClassSection Using View - ViewClassStudents


-- Which should display following columns StudentName, ClassName,
-- Section, TotalMarks
-- By passing the @Rank, @ClassName, @Section
-- If I exec USPRankClassSection @Rank = 2
-- It should display the Student who got 2nd in Entire School
-- If I exec USPRankClassSection @Rank = 3, @ClassName = ‘First’
-- It should display the Student who got 3rd in ClassName = ‘First’
-- If I exec USPRankClassSection @Rank = 3, @ClassName = ‘First’, @Section =
‘A’
-- It should display the Student who got 3rd in ClassName = ‘First’ and Section ‘A’

--8A.
ALTER PROC USPRankClassSection
(
@Rank INT,
@ClassName VARCHAR(10) = NULL,
@Section VARCHAR(1) = NULL

)
AS
BEGIN
IF @ClassName IS NULL AND @Section IS NULL
BEGIN
SELECT * FROM VIEWTOTAL V1
WHERE
(
SELECT COUNT(*) FROM VIEWTOTAL V2
WHERE V1.Total <= V2.Total
) = @Rank
END
ELSE IF @ClassName IS NULL AND @Section IS NOT NULL
BEGIN
SELECT * FROM VIEWTOTAL V1
WHERE
(
SELECT COUNT(*) FROM VIEWTOTAL V2
WHERE V1.Total <= V2.Total
AND V1.ClassName = V2.ClassName
) = @Rank
AND ISNULL(ClassName,'')=ISNULL(@ClassName,
ISNULL(ClassName,''))
END
ELSE
BEGIN
SELECT * FROM VIEWTOTAL V1
WHERE
(
SELECT COUNT(*) FROM VIEWTOTAL V2
WHERE V1.Total <= V2.Total
AND V1.ClassName = V2.ClassName
AND V1.Section = V2.Section
) = @Rank
AND ISNULL(ClassName,'')=ISNULL(@ClassName,
ISNULL(ClassName,''))
AND ISNULL([Section], '')=ISNULL(@Section,
ISNULL([Section],''))
END
END

--EXEC USPRankClassSection 1,'SECOND','A'

OR
CREATE PROC USPRankClassSectionALL
(
@Rank INT,
@ClassName VARCHAR(10) = NULL,
@Section VARCHAR(1) = NULL
)
AS
BEGIN
SELECT * FROM VIEWTOTAL V1
WHERE
(
SELECT COUNT(*) FROM VIEWTOTAL V2
WHERE V1.Total <= V2.Total
AND V1.ClassName = V2.ClassName
AND V1.Section = V2.Section
) = @Rank
AND ISNULL(ClassName,'')=ISNULL(@ClassName,
ISNULL(ClassName,''))
AND ISNULL([Section], '')=ISNULL(@Section, ISNULL([Section],''))
END
--EXEC USPRankClassSectionALL 1,'SECOND','A'

--9Q. Write a Stored Procedure USPAddStudent By passing @StudentName, @ClassID


-- Note:
-- If ClassID not found in Class Table Should Not Insert the Record in Student Table
-- Prompt message “ClassID is not Found”
-- If StudentName and ClassID exists in Student Should Not Insert
-- the Record in Student Table
-- Prompt message “Student Name is already exists”
--9A.
ALTER PROC USPAddStudent
(
@StudentID INT,
@StudentName VARCHAR(10),
@ClassID INT
)
AS
BEGIN
IF NOT EXISTS(SELECT * FROM CLASS WHERE ClassID = @CLASSID)
SELECT 'ClassID is not Found'
ELSE IF NOT EXISTS(SELECT * FROM STUDENT WHERE ClassID =
@CLASSID AND StudentName = @StudentName)
SELECT 'Student Name is already exists'
ELSE
INSERT INTO STUDENT(StudentID,StudentName, ClassID)
VALUES(@StudentID ,@StudentName, @ClassID)
END

--EXEC USPAddStudent 10,'STUD2',1


-- 10Q. Write down short notes on
--
-- With Encryption (Views)
--
-- With Schema binding (Views)
--
-- With Option (Views)
--
-- For above each give me one example.
--
-- 10A)
-- SCHEMABINDING OPTION IN VIEW:-
-- IF WE APPLY THIS OPTION WE CANNOT DROP TABLE WITHOUT DROPING
VIEW
-- EX:-
CREATE VIEW VIEWEMP12SCH
WITH SCHEMABINDING
AS
SELECT EmpID, EmpName FROM dbo.EMP12

--DROP VIEW VIEWEMP12SCH


--DROP TABLE EMP12

-- ENCRYPTION IN VIEW:-WE CANNOT SEE THE CONTENTS OF VIEW IN THE


RESULTS PAN
-- EX:-
CREATE VIEW VIEWEMP13ENCR
WITH ENCRYPTION
AS
SELECT EmpID, EmpName FROM EMP12
--WITHOUT ENCRYIPTION WE CAN SEE ALL THE ABOVE 4 LINES IN THE
RESULTS PAN

-- WITH CHECK OPTION IN VIEW:-DATA CANNOT BE MODIFIED IN THE VIEW


-- WITHOUT CHECK CONDITION
-- EX:-
ALTER VIEW VIEWEMP12CHEOPT
AS
SELECT EMPID, EMPNAME, DEPTNO FROM EMP13
WHERE DEPTNO =50
WITH CHECK OPTION
SELECT * FROM VIEWEMP12CHEOPT
--HERE WE CANNOT UPDATE OTHER RECORDS EXEPT DEPTNO=50 RECORD

Potrebbero piacerti anche