0% found this document useful (0 votes)
166 views9 pages

Nimic

The document contains requests to create and alter tables in SQL Server. Request 1 creates a MyFaculty table with fields for faculty ID, name, and whether they have a master's degree. Request 2 creates a MyStudent table with fields for student ID, name, birthdate, admission and final grades, and scholarship status. Subsequent requests alter the tables by adding or modifying fields and indexes.

Uploaded by

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

Nimic

The document contains requests to create and alter tables in SQL Server. Request 1 creates a MyFaculty table with fields for faculty ID, name, and whether they have a master's degree. Request 2 creates a MyStudent table with fields for student ID, name, birthdate, admission and final grades, and scholarship status. Subsequent requests alter the tables by adding or modifying fields and indexes.

Uploaded by

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

Request 1 � create the �MyFaculty� table

having the following structure:


� FacultyID � primary key, integer data type,
automatically generated values, starting from 10,
incremented by 1
� FacultyName � 30 character string, mandatory
� HasMasters � mandatory logical values, the
default value is �True�

CREATE TABLE MyFaculty


(FacultyID BigInt
PRIMARY KEY IDENTITY (10,1),
FacultyName NVarChar(30) NOT NULL,
HasMasters Bit NOT NULL DEFAULT 0)

R02 � CREATING A TABLE IN T-SQL


�Request 2 � create the �MyStudent� table
having the following structure:
� StudentID � large integer data type
� FullName � 50 character string, mandatory
� BirthDate � temporal data, is allowed to be
missing, accepts values only before January
1
st
, 2002
� AdmGrade � mandatory decimal values (five
characters total, two decimal places)
� FinalGrade � mandatory decimal values (five
characters total, two decimal places)
� Scholarship � mandatory logical values, the
default value is �False�

CREATE TABLE MyStudent


(StudentID BigInt,
FullName NVarChar(50) NOT NULL,
BirthDate Date NULL
CHECK(BirthDate<'01.01.2002'),
AdmGrade Decimal(5,2) NOT NULL,
FinalGrade Decimal(5,2) NOT NULL,
ScholarshDip Bit NOT NULL DEFAULT 0)

Request 3 � alter the �MyFaculty�


table, changing the data type for the
FacultyName field from 30 characters
to 40 characters Unicode variable
character string, mandatory

ALTER TABLE MyFaculty


ALTER COLUMN FacultyName NvarChar(40) NOT NULL

�Request 4 � alter the �MyFaculty� table,


adding the fields HasFrench and
HasEnglish with mandatory logical values,
the default value being �False�
ALTER TABLE MyFaculty

ADD
HasFrench Bit NOT NULL DEFAULT 0,
HasEnglish Bit NOT NULL DEFAULT 0
�Request 5 � alter the �MyStudent�
table, setting the fields AdmGrade and
FinalGrade to accept only values
between 1 and 10

ALTER TABLE MyStudent


ADD
CONSTRAINT AdmGrade_1_10
CHECK (AdmGrade BETWEEN 1 AND 10),
CONSTRAINT FinalGrade_1_10
CHECK (FinalGrade>=1 AND FinalGrade<=10)

�Request 6 � alter the �MyStudent�


table, so as the FinalGrade field only
accepts values greater than the
AdmGrade field

ALTER TABLE MyStudent


ADD
CONSTRAINT FinalVsAdmission
CHECK (FinalGrade>=AdmGrade)

�Request 7 � Set the StudentID


field not to allow null values, then
set it as the primary key

ALTER TABLE MyStudent


ALTER COLUMN StudentID BigInt NOT NULL
GO
ALTER TABLE MyStudent
ADD CONSTRAINT MyStudent_IDStudent_PK
PRIMARY KEYD(StudentID)

�Request 8 � Alter the �MyStudent� table by


adding a new field, named FKFacultyID, and
set the new field as foreign key, in relation
with the primary key of the �MyFaculty� table

ALTER TABLE MyStudent


ADD FKFacultyID BigInt NOT NULL,
FOREIGN KEY (FKFacultyID)
REFERENCES MyFaculty(FacultyID)

�Request 9 � Add a nonclustered index to


the �MyStudent� table, so as the records
will be stored in descending order of the
final grade

CREATE NONCLUSTERED INDEX


FinalGrade_Index
ON MyStudent(FinalGrade DESC)

IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

CREATE TABLE MemberDetails


(
MemberId integer,
FirstName nvarchar(50),
LastName nvarchar(50),
DateOfBirth date,
Street varchar(100),
City varchar(75),
State varchar(75),
ZipCode varchar(12),
Email varchar(200),
DateOfJoining date
)
INSERT INTO MemberDetails
VALUES
(
1,
�Katie�,
�Smith�,
�1977-01-09�,
�Main Road�,
�Townsville�,
�Stateside�,
�123456�,
[email protected]�,
�2004-02-23�
);
UPDATE MemberDetails
SET
Street = �45 Upper Road�,
City = �New Town�,
State = �New State�,
ZipCode = �99112�
WHERE MemberId = 4;
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIII
5.
SELECT DISTINCT Country FROM Student;

SELECT TOP 10 PERCENT WITH TIES AverageGrade FROM Student


ORDER BY AverageGrade ASC;

13.
SELECT FirstName, LastName, Year, Country
FROM Student
WHERE (Country = 'Romania' OR Year = '2') AND StudentID NOT IN
(SELECT StudentID
FROM Student
WHERE Country = 'Romania' AND Year = '2');

33.
SELECT TOP 1 FirstName, LastName, BirthDate, Year
FROM Student
WHERE Year='2'
ORDER BY BirthDate DESC;

IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIII

1.a.
SELECT FirstName, LastName, [Year],
CASE [Year]
WHEN 1 THEN 'First'
WHEN 2 THEN 'Middle'
WHEN 3 THEN 'Last'
--ELSE 'Not a year'
END AS YearText
FROM Student;

1.b.
SELECT FirstName, LastName, [Year],
CASE
WHEN [Year]=1 THEN 'First'
WHEN [Year]=2 THEN 'Middle'
WHEN [Year]=3 THEN 'Last'
--ELSE 'Not a year'
END AS YearText
FROM Student;

29.v1.
SELECT CONCAT(FirstName, ' ', LastName) AS FullName,
LEN(CONCAT(FirstName, ' ', LastName)) AS Letters,
IIF(LEN(CONCAT(FirstName, ' ', LastName))>=12, 'Long Name', 'Short
Name') AS NameType
FROM Student;

29.v2.
SELECT CONCAT(FirstName, ' ', LastName) AS FullName,
LEN(CONCAT(FirstName, ' ', LastName)) AS Letters,
CASE
WHEN LEN(CONCAT(FirstName, ' ', LastName))>=12 THEN 'Long Name'
ELSE 'Short Name'
END AS NameType
FROM Student;

30.
SELECT FirstName, LastName, [Year],
CHOOSE([Year], 'First', 'Middle', 'Last') AS YearText
FROM Student;

31. (ROW_NUMBER)
SELECT
ROW_NUMBER() OVER(PARTITION BY Nationality ORDER BY AverageGrade DESC) AS
Ranking,
FirstName, LastName, Nationality, AverageGrade
FROM Student;

RANK.
SELECT
RANK() OVER (ORDER BY [Year] DESC) AS Ranking,
FirstName, LastName, [Year]
FROM Student;

DENSE_RANK.
SELECT
RANK() OVER (ORDER BY [Year] DESC) AS Ranking,
DENSE_RANK() OVER (ORDER BY [Year] DESC) AS DenseRanking,
FirstName, LastName, [Year]
FROM Student;
! QUARTILE = NTILE(4)
DECILE = NTILE(10)
PERCENTILE = NTILE(100)

32.
SELECT FirstName, LastName, AverageGrade,
NTILE(4) OVER (ORDER BY AverageGrade DESC)
FROM Student;

! RUNNING SUMS
MOVING AVERAGES

33.
SELECT FirstName, LastName, [Year], AverageGrade,
AVG(AverageGrade) OVER (PARTITION BY [Year] ORDER BY [Year] DESC) AS
YearAverage
FROM Student;

34.
SELECT FirstName, LastName, [Year], AverageGrade,
SUM(AverageGrade) OVER (PARTITION BY [Year] ORDER BY [AverageGrade] DESC) AS
SumOfGrades
FROM Student;

FUNCTIONS FOR STRINGS

9.
SELECT FirstName, CHARINDEX('e', FirstName) AS Occurence
FROM Student;

FUNCTIONS FOR NUMBERS

16.
SELECT FirstName, LastName, AverageGrade, CEILING(AverageGrade) AS RoundedUp,
FLOOR(AverageGrade) AS RoundedDown, ROUND(AverageGrade, 1) AS SimpleRound
FROM Student;

IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIII

10.
CREATE TRIGGER Student_INSERT_UPDATE
ON Student
AFTER INSERT, UPDATE
AS
UPDATE Student
SET Status = UPPER(Status)
WHERE StudentID IN (SELECT StudentID FROM Inserted);

UPDATE Student
SET AverageGrade = 8.50
WHERE StudentID = 105;

INSERT INTO Student


VALUES ('NFN', 'NLN', '2000-01-15', 'Arad', 'Romania', 'Romanian', 'scholarship',
'1', '0720123456', 7.70);
ALTER TRIGGER Student_INSERT_UPDATE
ON Student
AFTER INSERT
AS
UPDATE Student
SET Status=UPPER(Status)
WHERE StudentID IN (SELECT StudentID FROM Student);

UPDATE Student
SET AverageGrade = 7.66
WHERE StudentID = 104;

11. (WILL NOT WORK IN SQL SERVER 2014!)


CREATE TRIGGER Student_DELETE
ON Student
AFTER DELETE
AS
SELECT * INTO DelStudent
FROM Student
WHERE StudentID IN (SELECT StudentID FROM Deleted);

1.v1.
SELECT City
FROM Student
WHERE StudentID IN
(SELECT TOP 1 StudentID FROM Student ORDER BY BirthDate DESC);

1.v2.
SELECT TOP 1 City FROM Student ORDER BY BirthDate DESC;

8.
UPDATE Student
SET AverageGrade = AverageGrade - 1
WHERE Nationality = 'Romanian'
AND
AverageGrade >= 1.2 * (SELECT MIN(AverageGrade) FROM Student);

9.
SELECT FirstName + ' ' + LastName AS FullName, Nationality
FROM Student
WHERE Nationality = 'Romanian' AND DATEDIFF(DAY, BirthDate, GETDATE()) <
(SELECT AVG(DATEDIFF(DAY, BirthDate, GETDATE())) FROM Student WHERE Nationality =
'Romanian');

UPDATE Student
SET AverageGrade = AverageGrade + 1
WHERE Status = 'Scholarship' AND Year = 2 AND AverageGrade < 9;

IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIII

1.
CREATE VIEW MyFirstView
(FullName,Location,BirthYear,FinalGrade)
AS
SELECT FirstName+' '+LastName,
City+','+Country,
YEAR(BirthDate),
AverageGrade
FROM Student;

2.CREATE VIEW MySecondView


AS
SELECT * FROM Student INNER JOIN Faculty ON Faculty.FacultyID=Student.FKFacultyID
WHERE FacultyName IN ('Finance','Marketing')
3.CREATE VIEW MyThirdView
AS
SELECT FirstName,LastName,City,Country
FROM Student;

4.CREATE VIEW MyFourthView


(FullName,Year,Status,AverageGrade,FacultyName)
AS
SELECT FirstName+' '+LastName,Year,Status,AverageGrade,FacultyName
FROM Student AS Student INNER JOIN Faculty AS Faculty ON
Student.FKFacultyID=Faculty.FacultyID
Where FacultyName IN ('Accounting','Finance')

5.CREATE VIEW MyFifthView


(FirstName,LastName,StudentAge,RoomForImprovement)
AS
SELECT FirstName,LastName,
DateDiff(yy,BirthDate,GetDate()),
10-AverageGrade
FROM Student

6.CREATE VIEW MySixthView


(StudentName,FinalGrade,GeneralAverage,Difference)
AS
SELECT FirstName+' '+LastName,AverageGrade,
CAST(ROUND((SELECT AVG(AverageGrade) FROM Student),2) AS Numeric(5,2)),
AverageGrade-CAST(ROUND((SELECT AVG(AverageGrade) FROM Student),2) AS Numeric(5,2))
FROM Student

7.CREATE VIEW MySeventhView


AS
SELECT FacultyName
FROM Faculty INNER JOIN Student ON Faculty.FacultyID=Student.FKFacultyID
GROUP BY FacultyName
HAVING Count(StudentID)>=3

8.CREATE VIEW MyEightView


AS
SELECT FacultyName
FROM Faculty INNER JOIN Student ON Faculty.FacultyID= Student.FKFacultyID
WHERE Nationality='foreign'
GROUP BY FacultyName
Having Count(StudentID)>=1

9.CREATE VIEW MyNinthView


(FacultyName,NoOfStudents1StYear)
AS
SELECT FacultyName,Count(StudentID)
FROM Faculty INNER JOIN Student ON Faculty.FacultyID= Student.FKFacultyID
WHERE Year=1
GROUP BY FacultyName
Having Count(StudentID)>0
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIII
REQUESTS FOR T-SQL RECAP-VIEWS AND FUNCTIONS
19.
CREATE Trigger MyTrigger01
ON MyStudent AFTER DELETE
AS
IF EXISTS (SELECT * FROM Deleted WHERE Scholarship=1)
BEGIN
RAISERROR('YOU CAN NOT DELETE SCHOLARSHIP STUDENTS!',1,1)
ROLLBACK TRANSACTION
END

20.
CREATE Trigger MyTrigger02
ON MyStudent AFTER INSERT,UPDATE
AS
IF EXISTS (SELECT * FROM INSERTED WHERE Birthdate>'06-30-2001')
BEGIN
RAISERROR('All students must be born before June 2001',1,1)
ROLLBACK TRANSACTION
END

21.
CREATE Trigger MyTrigger03
ON MyStudent AFTER INSERT
AS
IF EXISTS (SELECT * FROM INSERTED WHERE FKFACULTYID IN
(SELECT FacultyId FROM MyFaculty WHERE FacultyName='Finance'))
BEGIN
RAISERROR('no new students can be added for the faculty of finance!',1,1)
ROLLBACK TRANSACTION
END

22.
CREATE Trigger MyTrigger04
ON MyStudent AFTER DELETE
AS
IF EXISTS (SELECT * FROM DELETED WHERE FKFACULTYID IN
(SELECT FacultyId FROM MyFaculty WHERE FacultyName='Marketing'))
BEGIN
RAISERROR('no new students can be added for the faculty of marketing!',1,1)
ROLLBACK TRANSACTION
END

23.
CREATE Trigger MyTrigger05
ON MyStudent AFTER UPDATE
AS
IF EXISTS (SELECT * FROM DELETED INNER JOIN INSERTED ON
Deleted.StudentID=Inserted.StudentID
WHERE Deleted.AdmGrade<>Inserted.AdmGrade)
BEGIN
RAISERROR('Admission grade can not be changed',1,1)
ROLLBACK TRANSACTION
END
24.
CREATE Trigger MyTrigger06
ON MyStudent AFTER UPDATE
AS
IF EXISTS (SELECT * FROM DELETED INNER JOIN INSERTED ON
Deleted.StudentID=Inserted.StudentID
WHERE Deleted.FinalGrade>Inserted.FinalGrade AND Inserted.FKFACULTYID IN
(SELECT FacultyId FROM MyFaculty WHERE FacultyName='Accounting'))
BEGIN
RAISERROR('New final grade shouldnt be smaller than the old one',1,1)
ROLLBACK TRANSACTION
END

ReqView15.

CREATE VIEW View15


(FacultyName,AverageAdmission,AverageFunction)
AS
SELECT Faculty.FacultyName,AVG(AdmGrade),AVG(FinalGrade) FROM MyStudent AS Student
INNER JOIN MyFaculty AS Faculty
ON Student.FKFacultyID=Faculty.FacultyID WHERE Faculty.FacultyName='Finance'
GROUP BY Faculty.FacultyName

You might also like