Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/LeThaoHuyen/CS468_Team12 in…
Browse files Browse the repository at this point in the history
…to main
  • Loading branch information
LeThaoHuyen committed Aug 14, 2021
2 parents 683cab9 + e03b31d commit c960a8a
Showing 1 changed file with 293 additions and 70 deletions.
363 changes: 293 additions & 70 deletions CS486-Team12-SQL2019.sql
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,297 @@ insert into songs(songID, songName) VALUES
(15, 'My Homeland');
go

-- CREATE OR ALTER procedure sp_selectMoiNhat(@playlistid INT, @genre NVARCHAR(50))
-- AS
-- BEGIN
-- SELECT s.songName, sng.singerName, s.streamCount, s.dateAdded, sp.favorites
-- FROM playlists p JOIN SongsOfPlaylist sp ON p.playlistID = sp.playlistID
-- JOIN Songs s ON sp.songID = s.songID JOIN SingerSongs ss ON s.songID = ss.songID
-- JOIN Singers sng ON ss.singerID = sng.singerID
-- WHERE p.playlistID = @playlistid AND s.genre = @genre
-- END
-- GO
--select pricipal
CREATE OR ALTER procedure sp_selectprincipal
AS
BEGIN
select * from Singers s
where s.principles=1
END
GO

--exec sp_selectprincipal

--Randomly select 6 singers from the reserved team
create or alter procedure sp_selectRandomReserved
as
BEGIN
SELECT TOP 6 * FROM Singers s
WHERE s.principles=0
order by NEWID()
END
GO

-- exec sp_selectRandomReserved

create or alter trigger tg_singerPairTrial
on SingerPairSongsTrial
for insert, update
as
begin
if(update(singer1ID) OR update(singer2ID))
BEGIN
if(NOT EXISTS(SELECT * FROM INSERTED I INNER JOIN Singers S ON I.singer1ID=S.singerID WHERE S.principles=1)
OR (NOT EXISTS(SELECT * FROM INSERTED I INNER JOIN Singers S ON I.singer2ID=S.singerID WHERE S.principles=0))
OR (EXISTS(SELECT * FROM INSERTED I INNER JOIN SingerPairSongsTrial SPST ON I.singer1ID=SPST.singer1ID))
OR (EXISTS(SELECT * FROM INSERTED I INNER JOIN SingerPairSongsTrial SPST ON I.singer2ID=SPST.singer2ID))
)
begin
ROLLBACK;
THROW 50001, 'Trial Round: Singer 1 must be a principal, singer 2 must be a reserve', 1
end
end
END
go


create or alter trigger tg_singerPairPremain_onPair
on SingerPairSongsPreMain
for insert, update
AS
BEGIN
if(UPDATE(singer1ID) or update(singer2ID))
BEGIN
if(exists(
select * from SingerPairSongsPreMain p join Singers s on (singer1ID=singerID)
where principles=1))
begin
ROLLBACK;
THROW 50002, 'PreMain Round: Singer 1 must be a reserved', 1
end

if(exists(
select * from SingerPairSongsPreMain p join Singers s on (singer2ID=singerID)
where principles=1))
begin
ROLLBACK;
THROW 50003, 'PreMain Round: Singer 2 must be a reserved', 1
end
end
END

go
create or alter trigger tg_singerPairPremain_singers
on Singers
for insert, update
AS
BEGIN
if(UPDATE(singerID))
BEGIN
if(exists(
select * from SingerPairSongsPreMain p join Singers s on (singer1ID=singerID)
where principles=1))
begin
ROLLBACK;
THROW 50004, 'PreMain Round: Singer 1 must be a reserve', 1
end

if(exists(
select * from SingerPairSongsPreMain p join Singers s on (singer2ID=singerID)
where principles=1))
begin
ROLLBACK;
THROW 50005, 'PreMain Round: Singer 2 must be a reserve', 1
end
end
END
go



create or alter procedure sp_select3SongsRandomly
as
begin
SELECT TOP 3 * FROM Songs s
order by NEWID()
end
go

create or alter procedure sp_inserttrial (@singer1Name nvarchar(50), @singer2Name nvarchar(50), @songName nvarchar(50), @result bit)
as
begin TRANSACTION
begin try
if not exists(
select *
from Songs s
where s.songName = @songName
)
BEGIN
THROW 50006, 'There is no such song', 1
END

if not exists(
select *
from Singers sg
where sg.name = @singer1Name
)
BEGIN
THROW 50007,'There is no such singer 1', 1;
END


if not exists(
select *
from Singers sg
where sg.name = @singer2Name
)
BEGIN
THROW 50008,'There is no such singer 2', 1;
END

declare @singer1ID int
set @singer1ID=(select singerID from Singers s where s.name=@singer1Name)

declare @singer2ID int
set @singer2ID=(select singerID from Singers s where s.name=@singer2Name)


declare @songID INT
set @songID = (select songID from Songs where songName = @songName)

insert into SingerPairSongsTrial
values (@singer1ID, @singer2ID,@songID, @result);
commit transaction
end TRY
begin CATCH
rollback transaction;
throw
end catch
go


create or alter procedure sp_insertpremain (@singer1Name nvarchar(50), @singer2Name nvarchar(50), @songName nvarchar(50), @result bit)
as
begin TRANSACTION
begin try
if (@songName <> 'Happy Heart' and @songName <> 'Quest' and @songName <> 'To the Horizon With You')
BEGIN
THROW 50010, 'Invalid song for this round', 1
END

if not exists(
select *
from Singers sg
where sg.name = @singer1Name
)
BEGIN
THROW 50011,'There is no such singer 1', 1;
END


if not exists(
select *
from Singers sg
where sg.name = @singer2Name
)
BEGIN
THROW 50012,'There is no such singer 2', 1;
END

declare @singer1ID int
set @singer1ID=(select singerID from Singers s where s.name=@singer1Name)

declare @singer2ID int
set @singer2ID=(select singerID from Singers s where s.name=@singer2Name)


declare @songID INT
set @songID = (select songID from Songs where songName = @songName)

insert into SingerPairSongsPreMain
values (@singer1ID, @singer2ID,@songID, @result);
commit transaction
end TRY
begin CATCH
rollback transaction;
throw
end catch
go

--exec dbo.sp_inserttrial 'abc', 'xyz', 'That Man', 0

--exec dbo.sp_inserttrial 'Jin Shengquan', 'Zhai Lishuotian', 'That Man', 0
go

--//sau trial thì gán lại principles cho những căp thắng?
create or alter PROCEDURE sp_updatePrinciplesAfterTrial
as
begin
update singers
set principles = 0;

update Singers
set principles = 1
where singerID in
(
select singer1ID
from SingerPairSongsTrial
where result = 1
union
select singer2ID
from SingerPairSongsTrial
where result = 1
)
end
go

exec sp_updatePrinciplesAfterTrial
select * from Singers
select * from Songs
select * from SingerPairSongsTrial
insert into SingerPairSongsTrial (singer1id, singer2ID, songID, result) values
(1, 2, 1, 1),
(3, 4, 1, 0);
go

create or alter procedure sp_viewMainResullt
as
BEGIN
(SELECT SPST.singer1ID, SPST.singer2ID, S.songName
FROM SingerPairSongsTrial SPST INNER JOIN Songs S ON SPST.songID=S.songID
WHERE SPST.result=1)
UNION
(SELECT SPST.singer1ID, SPST.singer2ID, S.songName
FROM SingerPairSongsPreMain SPST INNER JOIN Songs S ON SPST.songID=S.songID
WHERE SPST.result=1);
end
go

create or alter procedure sp_viewMembersTwoGroups
as
BEGIN
begin TRANSACTION
SELECT S.name as PrincipleList FROM Singers S WHERE principles=1;

SELECT S.name as ReservedList FROM Singers S WHERE principles=0;
commit transaction
end
go


create or alter procedure sp_updateSingerPrinciples(@singerid int, @principles bit)
as
BEGIN TRY
begin TRANSACTION
if not exists
(
select * from Singers
where singerID = @singerid
)
throw 60000, 'There is no such singerid', 1;

update Singers
set principles = @principles
where singerID = @singerid

commit transaction
end try
begin CATCH
ROLLBACK;
throw;
end catch
go
-- exec sp_updateSingerPrinciples 1, 0

-- CREATE OR ALTER PROCEDURE sp_updatePlaylist(@playlistid INT, @songid INT, @favorites BIT)
-- AS
Expand Down Expand Up @@ -169,64 +450,6 @@ go
-- END CATCH
-- GO

-- CREATE OR ALTER FUNCTION fn_count_songs()
-- RETURNS INT
-- AS
-- BEGIN
-- DECLARE @count INT;
-- SET @count =
-- (SELECT COUNT(*) FROM Songs)
-- RETURN @count
-- END
-- GO

-- select dbo.fn_count_songs()

-- Select * from playlists

-- go

-- create or alter procedure sp_count_songs1(@result INT out)
-- AS
-- BEGIN
-- SET @result =
-- (SELECT COUNT(*) FROM Songs)
-- END
-- GO

-- declare @result INT
-- exec sp_count_songs1 @result out
-- print(@result)


-- -- select * from SongsOfPlaylist where playlistID = 1
-- -- exec sp_updatePlaylist 1, 3, 1

-- -- delete from SongsOfPlaylist where songid = 3

--select pricipal
CREATE OR ALTER procedure sp_selectprincipal
AS
BEGIN
select * from Singers s
where s.principles=1
END
GO

--exec sp_selectprincipal

--Randomly select 6 singers from the reserved team
create or alter procedure sp_selectRandomReserved
as
BEGIN
SELECT TOP 6 * FROM Singers s
WHERE s.principles=0
order by NEWID()
END
GO

-- exec sp_selectRandomReserved

-- EXEC sp_select3SongsRandomly
-- USE MASTER
-- DROP DATABASE CS486_Team12_DB

-- DROP DATABASE CS486_Team12_DB

0 comments on commit c960a8a

Please sign in to comment.