Sunteți pe pagina 1din 9

#SQL-Assignment1

#Kajal_Hardwaria
#19030141020

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.36-community-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database Programmer;


Query OK, 1 row affected (0.00 sec)

mysql> SHOW databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| employee |
| lms |
| mysql |
| php |
| rdbms_assignment_1 |
| rdbms_assignment_2 |
| rdbms_assignment_3 |
| rdbms_assignment_4 |
+--------------------+
9 rows in set (0.00 sec)

mysql> USE rdbms_assignment_4;


Database changed

mysql> CREATE TABLE Student (


-> StudID char(100),
-> StudName char(100),
-> StudAddr char(100),
-> City char(100));
Query OK, 0 rows affected (0.07 sec)

mysql> CREATE TABLE Professor (


-> ProfID int,
-> ProfName char(100),
-> ProfAge int,
-> City char(100));
Query OK, 0 rows affected (0.07 sec)

mysql> CREATE TABLE Courses (


-> CourseNo int,
-> CourseName char(100),
-> CourseCredit int);
Query OK, 0 rows affected (0.06 sec)
mysql> CREATE TABLE CourseTake (
-> StudID char(100),
-> CourseNo int,
-> ProfID int);
Query OK, 0 rows affected (0.15 sec)

mysql> CREATE TABLE Teach (


-> ProfID int,
-> CourseNo int);
Query OK, 0 rows affected (0.04 sec)

mysql> CREATE TABLE PreReq (


-> CourseNo int,
-> PRCNo int);
Query OK, 0 rows affected (0.07 sec)

mysql> SHOW Tables;


+------------------------------+
| Tables_in_rdbms_assignment_4 |
+------------------------------+
| courses |
| coursetake |
| prereq |
| professor |
| student |
| teach |
+------------------------------+
6 rows in set (0.00 sec)

mysql> DESC student;


+----------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| StudID | char(100) | YES | | NULL | |
| StudName | char(100) | YES | | NULL | |
| StudAddr | char(100) | YES | | NULL | |
| City | char(100) | YES | | NULL | |
+----------+-----------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> DESC Professor;


+----------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| ProfID | int(11) | YES | | NULL | |
| ProfName | char(100) | YES | | NULL | |
| ProfAge | int(11) | YES | | NULL | |
| City | char(100) | YES | | NULL | |
+----------+-----------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> DESC Courses;


+--------------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------+------+-----+---------+-------+
| CourseNo | int(11) | YES | | NULL | |
| CourseName | char(100) | YES | | NULL | |
| CourseCredit | int(11) | YES | | NULL | |
+--------------+-----------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> DESC CourseTake;


+----------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| StudID | char(100) | YES | | NULL | |
| CourseNo | int(11) | YES | | NULL | |
| ProfID | int(11) | YES | | NULL | |
+----------+-----------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> DESC Teach;


+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| ProfID | int(11) | YES | | NULL | |
| CourseNo | int(11) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> DESC PreReq;


+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| CourseNo | int(11) | YES | | NULL | |
| PRCNo | int(11) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> DESC student;


+----------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| StudID | char(100) | YES | | NULL | |
| StudName | char(100) | YES | | NULL | |
| StudAddr | char(100) | YES | | NULL | |
| City | char(100) | YES | | NULL | |
+----------+-----------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> ALTER TABLE Student


-> ADD PRIMARY KEY (StudID);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC Professor;
+----------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| ProfID | int(11) | YES | | NULL | |
| ProfName | char(100) | YES | | NULL | |
| ProfAge | int(11) | YES | | NULL | |
| City | char(100) | YES | | NULL | |
+----------+-----------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> ALTER TABLE Professor


-> ADD PRIMARY KEY (ProfID);
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC Courses;


+--------------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------+------+-----+---------+-------+
| CourseNo | int(11) | YES | | NULL | |
| CourseName | char(100) | YES | | NULL | |
| CourseCredit | int(11) | YES | | NULL | |
+--------------+-----------+------+-----+---------+-------+
3 rows in set (0.02 sec)

mysql> ALTER TABLE Courses


-> ADD PRIMARY KEY (CourseNo);
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC CourseTake;


+----------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| StudID | char(100) | YES | | NULL | |
| CourseNo | int(11) | YES | | NULL | |
| ProfID | int(11) | YES | | NULL | |
+----------+-----------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> ALTER TABLE CourseTake


-> ADD FOREIGN KEY (StudID) REFERENCES Students(StudID);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE CourseTake


-> ADD FOREIGN KEY (CourseNo) REFERENCES Courses(CourseNo);
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE CourseTake


-> ADD FOREIGN KEY (ProfID) REFERENCES Professors(ProfID);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE Teach


-> ADD FOREIGN KEY (ProfID) REFERENCES Professors(ProfID);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE Teach


-> ADD FOREIGN KEY (CourseNo) REFERENCES Courses(CourseNo);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE PreReq


-> ADD FOREIGN KEY (CourseNo) REFERENCES Courses(CourseNo);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE PreReq


-> ADD PRIMARY KEY (PRCNo);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> INSERT INTO Student VALUES ("s1" , "Stuti" , "Farukha Badi Bazar" ,
"Delhi");
Query OK, 1 row affected (0.11 sec)

mysql> INSERT INTO Student VALUES ("s4" , "Juhi" , "Ghanta Ghar" , "Pune");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Student VALUES ("s2" , "Puja" , "Lohat Bazar" , "Kanpur");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Student VALUES ("s3" , "Bharat" , "Ratan Lal Nagar" , "Pune");
Query OK, 1 row affected (0.02 sec)

mysql> INSERT INTO Professor VALUES (1 , "Stefen" , 32 , "Kanpur");


Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> INSERT INTO Professor VALUES (2 , "Harsh" , 45 , "Pune");


Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> INSERT INTO Professor VALUES (3 , "Samrat" , 35 , "Pune");


Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> INSERT INTO Professor VALUES (4 , "Jayesh" , 40 , "Delhi");


Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> ALter Table PreReq


-> ADD PreRequisite CHAR(100);
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> INSERT INTO Courses VALUES (1001 , "Java Enterprize Technology" , 4 );
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Courses VALUES (1002 , "PHP" , 2 );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Courses VALUES (1003 , "JavaScript" , 4 );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO PreReq VALUES (1002 , 201 , "C++" );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO PreReq VALUES (1002 , 202 , "HTML" );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO PreReq VALUES (1002 , 203 , "Java" );


Query OK, 1 row affected (0.08 sec)

mysql> INSERT INTO PreReq VALUES (1003 , 301 , "C++" );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO PreReq VALUES (1003 , 302 , "Java" );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO PreReq VALUES (1003 , 303 , "HTML" );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO PreReq VALUES (1002 , 204 , "SQL" );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Courses VALUES (1004 , "RDBMS" , 4 );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO CourseTake VALUES ("s3" , "1004" , 2 );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO CourseTake VALUES ("s2" , "1004" , 3 );


Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO CourseTake VALUES ("s1" , "1004" , 4 );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO CourseTake VALUES ("s3" , "1003" , 3 );


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Teach VALUES(4,1002);


Query OK, 1 row affected (0.00 sec)

1) What are the PIDs of the students whose name is Started from S?

mmysql> SELECT StudID , StudName


-> FROM Student
-> WHERE StudName LIKE "S%";
+--------+----------+
| StudID | StudName |
+--------+----------+
| s1 | Stuti |
| s4 | Juhi |
+--------+----------+
2 rows in set (0.00 sec)

2) Which pairs of students live at the same city with their professors.

mysql> SELECT sn.City , sn.StudName , pn.ProfName


-> FROM Student sn , Professor pn
-> WHERE sn.city = pn.city;
+--------+--------------+---------------+
| City | StudName | ProfName |
+--------+--------------+---------------+
| Kanpur | Puja | Stefen |
| Pune | Juhi | Harsh |
| Pune | Bharat | Harsh |
| Pune | Juhi | Samrat |
| Pune | Bharat | Samrat |
| Delhi | Stuti | Jayesh |
+--------+--------------+---------------+
6 rows in set (0.00 sec)

3) What are the prerequisite for course “Java Enterprise Tecnology”?

mysql> SELECT c.CourseName , p.PreRequisite


-> FROM Courses c , PreReq p
-> WHERE c.CourseNo = p.CourseNo AND c.CourseName = "Java Enterprize
Technology";
+----------------------------+---------------+
| CourseName | PreRequisite |
+----------------------------+---------------+
| Java Enterprize Technology | C |
| Java Enterprize Technology | C++ |
| Java Enterprize Technology | C# |
| Java Enterprize Technology | Basics of JVM |
+----------------------------+---------------+
4 rows in set (0.01 sec)

4) What are the names and addresses of the students who are taking course “Java “?

mysql> SELECT c.CourseName , s.StudName , s.StudAddr


-> From Courses c , Student s , CourseTake ct
-> WHERE s.StudID = ct.StudID AND c.CourseNo = ct.CourseNo AND c.CourseName
LIKE "Java %";
+----------------------------+--------------+-----------------+
| CourseName | StudName | StudAddr |
+----------------------------+--------------+-----------------+
| Java Enterprize Technology | Bharat | Ratan Lal Nagar |
| Java Enterprize Technology | Juhi | Ghanta Ghar |
+----------------------------+--------------+-----------------+
2 rows in set (0.00 sec)

5) What are the courses that the head of the CS department is teaching?

SOL :> There Is No Such Department column present in any of the table and i am not
going to add any column because then i have to deal with lots of problem in
inserting a data in the Table and i don't have that much of Time so i am selecting
the courses for each student

mysql> SELECT s.StudName , c.CourseName


-> From Courses c , Student s , CourseTake ct
-> WHERE s.StudID = ct.StudID AND c.CourseNo = ct.CourseNo;
+--------------+----------------------------+
| StudName | CourseName |
+--------------+----------------------------+
| Stuti | JavaScript |
| Puja | PHP |
| Bharat | Java Enterprize Technology |
| Juhi | Java Enterprize Technology |
+--------------+----------------------------+
4 rows in set (0.00 sec)

6) List the name and city of professors who teaches course “RDBMS”.

mysql> SELECT p.ProfName , p.City , c.CourseName


-> From Courses c , Professor p , CourseTake ct
-> WHERE p.ProfID = ct.ProfID AND c.CourseNo = ct.CourseNo AND c.CourseName
LIKE "%RDBMS%";
+---------------+-------+------------+
| ProfName | City | CourseName |
+---------------+-------+------------+
| Harsh | Pune | RDBMS |
| Samrat | Pune | RDBMS |
| Jayesh | Delhi | RDBMS |
+---------------+-------+------------+
3 rows in set (0.00 sec)

OR

mysql> SELECT p.ProfName , p.City , c.CourseName


-> From Courses c , Professor p , CourseTake ct
-> WHERE p.ProfID = ct.ProfID AND c.CourseNo = ct.CourseNo AND ct.CourseNo =
1004;
+---------------+-------+------------+
| ProfName | City | CourseName |
+---------------+-------+------------+
| Harsh | Pune | RDBMS |
| Samrat | Pune | RDBMS |
| Jayesh | Delhi | RDBMS |
+---------------+-------+------------+
3 rows in set (0.00 sec)

7) Are there any students who are taking at least two courses taught by Professor
“Samrat”

mysql> SELECT s.StudName , p.ProfName , c.CourseName


-> From Courses c , Professor p , CourseTake ct , Student s
-> WHERE s.StudID = ct.StudID AND p.ProfID = ct.ProfID AND c.CourseNo =
ct.CourseNo AND p.ProfID = 3;
+--------------+---------------+----------------------------+
| StudName | ProfName | CourseName |
+--------------+---------------+----------------------------+
| Bharat | Samrat | Java Enterprize Technology |
| Puja | Samrat | RDBMS |
| Bharat | Samrat | JavaScript |
+--------------+---------------+----------------------------+
3 rows in set (0.00 sec)

8) Update the name of professor for course “JavaScript”. The new name is “ jayesh

mysql> UPDATE Professor SET ProfName = "jayesh "


-> WHERE ProfID IN (SELECT Profid FROM Teach WHERE CourseNo IN (SELECT CourseNo
FROM Courses WHERE CourseName = "PHP"));
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from professor;


+--------+---------------+---------+--------+
| ProfID | ProfName | ProfAge | City |
+--------+---------------+---------+--------+
| 1 | Stefen | 32 | Kanpur |
| 2 | Harsh | 45 | Pune |
| 3 | Samrat | 35 | Pune |
| 4 | jayesh | 32 | Delhi |
+--------+---------------+---------+--------+
4 rows in set (0.00 sec)

S-ar putea să vă placă și