0% found this document useful (0 votes)
5 views2 pages

DBMS - Worksheet 5 - Answers

The document outlines SQL commands for creating and manipulating a Musician table, including creating the table, inserting records, handling errors related to null values and check constraints, altering the table to add a Country column, and updating the Country for various musicians. It also includes a series of SQL queries for retrieving distinct countries, filtering musicians based on rank and category, and aggregating data. The document serves as a guide for managing musician data in a database.
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)
5 views2 pages

DBMS - Worksheet 5 - Answers

The document outlines SQL commands for creating and manipulating a Musician table, including creating the table, inserting records, handling errors related to null values and check constraints, altering the table to add a Country column, and updating the Country for various musicians. It also includes a series of SQL queries for retrieving distinct countries, filtering musicians based on rank and category, and aggregating data. The document serves as a guide for managing musician data in a database.
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/ 2

Question 1--------

create table Musician(


MusicianID INT PRIMARY KEY,
MusicianName VARCHAR(50) NOT NULL,
Category VARCHAR(20) CHECK (Category IN ('Pop','Rock','Reggae','Classical')),
`Rank` INT DEFAULT 4,
Instrument VARCHAR(50));

Question 2---------

insert into Musician(MusicianID, MusicianName, Category, `Rank`, Instrument) values


(12, 'Selena', 'Pop', 4, 'Guitar'),
(14, 'Davis', 'Rock', 4, 'Piano'),
(15, 'Keanu', 'Reggae', 3, 'Guitar'),
(17, 'Ravi', 'Classical', 5, 'Flute'),
(24, 'Neela', 'Classical', Default, 'Piano'),
(25, 'Kevin', 'Pop', 2, 'Guitar'),
(27, 'Lucas', 'Reggae', 5, 'Flute'),
(29, 'Aiden', 'Classical', 3, 'Guitar');

Question 3---------

ERROR 1048 (23000): Column 'MusicianName' cannot be null


// SSMS
Msg 515, Level 16, State 2, Line 19
Cannot insert the value NULL into column 'MusicianName', table
'master.dbo.Musician'; column does not allow nulls. INSERT fails.

ERROR 3819 (HY000): Check constraint 'musician_chk_1' is violated.


//SSMS
Msg 547, Level 16, State 0, Line 19
The INSERT statement conflicted with the CHECK constraint
"CK__Musician__Catego__1293BD5E". The conflict occurred in database "master",
table "dbo.Musician", column 'Category'.

Question 4---------

alter table Musician add Country Varchar (50);

Question 5 ---------

update Musician
Set Country = case
when MusicianID = 12 then 'Sri Lanka'
when MusicianID = 14 then 'America'
when MusicianID = 15 then 'Brazil'
when MusicianID = 17 then 'Sri Lanka'
when MusicianID = 24 then 'India'
when MusicianID = 25 then 'America'
when MusicianID = 27 then 'Brazil'
when MusicianID = 29 then 'America'
end;

Question 6 -------------
a) select distinct Country from Musician;
b) select MusicianID, MusicianName from Musician where Country ='Sri Lanka'and
rank>3;
c) select * from Musician where Rank<4 or Category = 'Classical';
d) select * from Musician where MusicianName like '%n';
e) select * from Musician where MusicianName like '[G-S]%';
e) select * from Musician where MusicianName >='G%' and MusicianName <'T%';
f) select * from Musician where MusicianID<30 and MusicianID>20;
f) select * from Musician where MusicianID between 20 and 30;
g) select MusicianID, MusicianName from Musician where Instrument='Piano' or
Instrument='Guitar';
h) select avg(Rank) as AvrageRankOFMusician from Musician;
i) select max(Rank) as AmericaMaxRank from Musician where Country = 'America';
j) select count(distinct Category) as UniqueCategory from Musician;
k) select MusicianID, MusicianName, Country from Musician order by Country;
l) select Category, count(*) as NoofMusicians from Musician group by Category
having count(*)>1 order by Category desc;
m) select Instrument, count(*) as NoofPlayers from Musician group by Instrument
order by Instrument desc;

You might also like