DBMS LAB 2
2019-CS-682
Muhammad Bilal
Section:B
TASK 1:
Create the following table using SQL and using the INSERT INTO
command, insert the following values in the table created.
Name Reg_No Courses Course_Code Offered_By
Ali 01 DIP 1001 Mr. A
Basit 02 DBMS 1002 Mr. X
Akram 03 OS 1003 Mr. Y
Asad 04 DBMS 1002 Mr. X
Zeeshan 05 DIP 1001 Mr. A
Muneer 06 OS 1003 Mr. Y
Shafqat 07 NM 1004 Mr. H
Ahsan 08 OS 1003 Mr. Y
Ikram 09 DIP
Hassan 10
Answer:
Commands:
1) For creating database
CREATE DATABASE labtask3
2) For creating table given
CREATE TABLE Persons (
Name varchar(50),
RegNo int,
Courses varchar(50),
CourseCode int,
OfferedBy varchar(50)
);
3) For inserting values in the table
INSERT INTO Persons(
Name,
RegNo,
Courses,
CourseCode,
OfferedBy
)
VALUES
(
'Ali',
01,
'DIP',
1001,
'Mr.A'
),
(
'Basit',
02,
'DBMS',
1002,
'Mr. X'
),
(
'Akram',
03,
'OS',
1003,
'Mr.Y'
),
(
'Asad',
04,
'DBMS',
1002,
'Mr.X'
),
(
'Zeeshan',
05,
'DIP',
1001,
'Mr.A'
),
(
'Muneer',
06,
'OS',
1003,
'Mr.Y'
),
(
'Shafqat',
07,
'NM',
1004,
'Mr.H'
),
(
'Ahsan',
08,
'OS',
1003,
'Mr.Y'
),
(
'Ikram',
09,
'DIP',
null,
''
),
(
'Hassan',
10,
'',
null,
''
);
Output
TASK 2:
Using the UPDATE statement, update the above table for the following values:
Name Reg_No Courses Course_Code Offered_By
Ali 01 DIP 1001 Mr. A
Basit 02 DBMS 1002 Mr. X
Akram 03 OS 1003 Mr. Y
Asad 04 DBMS 1002 Mr. X
Zeeshan 05 DIP 1001 Mr. A
Muneer 06 OS 1003 Mr. Y
Shafqat 07 NM 1004 Mr. H
Ahsan 08 OS 1003 Mr. Y
Ikram 09 DIP 1001 Mr. A
Hassan 10 DSP 1005 Mr. Z
Answer:
Command used to update table named persons
UPDATE Persons
SET Courses = 'DSP', CourseCode = 1005,
OfferedBy='Mr.Z'
WHERE RegNo = 10;
UPDATE dbo.Persons
SET CourseCode=1001,OfferedBy='Mr.A'
WHERE RegNo = 9;
TASK 3: Using the DELETE statement,
delete the record for the student having
name Akram and Ahsan in the above
table. Also delete the record for the
course having course code=1001.
Answer:
Commands Used to delete records name
Akram and Ahsan for coursecode=1001
DELETE FROM Persons WHERE Name='Akram';
DELETE FROM Persons WHERE Name='Ahsan';
DELETE FROM Persons WHERE CourseCode=1001;
TASK 4: Select distinct values from the
above table for the last three columns.
Answer:
Commands
SELECT DISTINCT Courses FROM Persons;
SELECT DISTINCT CourseCode FROM Persons;
SELECT DISTINCT OfferedBy FROM Persons;
TASK 6: For the table in task 2,
generate a query for updating the
table with fully qualified names
and update the following values:
Ali 01 SE 1001 Mr. Z
Basit 02 CG 1002 Mr. X
Answer:
Commands:
UPDATE Persons
SET Courses = 'SE', CourseCode = 1005
WHERE RegNo = 1;
UPDATE dbo.Persons
SET Courses='CG'
WHERE RegNo = 2;
END