21.
Write SQL commands and their outputs based on the given tables of Database
‘Sport’:
Table: GAME
Table: COACH
1. Create the Table GAME with the following specificaons :
SCODE char(4) set as PRIMARY KEY,
SNAME varchar(15) set as NOT NULL AND UNIQUE,
VENUE varchar(10)
NO_OF_PARTI integer (maximum 20)
PRIZE integer default is 5000
SCH_DATE date
mysql> Create table GAME
-> (Scode char(4) primary key,
-> Sname varchar(15) not null unique,
-> Venue varchar(10),
-> No_of_parti int check No_of_parti < 20,
-> Prize int default 5000,
-> Sch_Date date);
Query OK, 0 rows affected (0.01 sec)
2. Add a record ‘C005’, ‘NISHAD’, ‘S003’ to the table COACH.
mysql> Insert into COACH values('C005','NISHAD','S003');
Query OK, 1 row affected (0.00 sec)
3. Display the structure of the table GAME.
mysql> desc GAME;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Scode | char(4) | NO | PRI | NULL | |
| Sname | varchar(15) | NO | UNI | NULL | |
| Venue | varchar(10) | YES | | NULL | |
| No_of_parti | int(11) | YES | | NULL | |
| Prize | int(11) | YES | | 5000 | |
| Sch_Date | date | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
4. Display the number of VENUEs without redundancy.
mysql> Select count(distinct venue) from GAME;
+-----------------------+
| count(distinct venue) |
+-----------------------+
| 3 |
+-----------------------+
1 row in set (0.01 sec)
5. Display the SNAME with NO_OF_PARTI > 10.
mysql> Select Sname from GAME
-> where No_of_parti > 10;
+-----------+
| Sname |
+-----------+
| RELAY |
| SHOT PUT |
| LONG JUMP |
+-----------+
3 rows in set (0.01 sec)
6. Display the SNAME with SCH_DATE after ‘September, 2024’ .
mysql> Select Sname from GAME
-> where Sch_Date > '2024-09-30';
+-----------+
| Sname |
+-----------+
| SHOT PUT |
| LONG JUMP |
+-----------+
2 rows in set (0.00 sec)
7. Display the SNAME with the letter ‘u’ in it.
mysql> Select Sname from GAME
-> where Sname like '%u%';
+-----------+
| Sname |
+-----------+
| HIGH JUMP |
| LONG JUMP |
| SHOT PUT |
+-----------+
3 rows in set (0.00 sec)
8. Display the SNAME in descending order of PRIZE.
mysql> Select Sname from GAME
-> order by Prize desc;
+-----------+
| Sname |
+-----------+
| HIGH JUMP |
| RELAY |
| LONG JUMP |
| SHOT PUT |
+-----------+
4 rows in set (0.00 sec)
9. Display the minimum & maximum PRIZE of the Games.
mysql> select min(Prize),max(Prize) from GAME;
+------------+------------+
| min(Prize) | max(Prize) |
+------------+------------+
| 8000 | 12000 |
+------------+------------+
1 row in set (0.00 sec)
10. Display the CNAME of SCODE ‘S001’.
mysql> Select Cname from COACH
-> where Scode = 'S001';
+-------+
| cname |
+-------+
| ARUN |
| ALI |
+-------+
2 rows in set (0.00 sec)
11. Count the total number of games under each VENUE.
mysql> select Venue,count(*) from GAME
-> group by Venue;
+---------+----------+
| Venue | count(*) |
+---------+----------+
| ANNEX 1 | 2 |
| ANNEX 2 | 1 |
| ANNEX 3 | 1 |
+---------+----------+
3 rows in set (0.00 sec)
12. Display the SNAME and the corresponding CNAME.
mysql> select Sname, Cname from GAME, COACH
-> where GAME.Scode = COACH.Scode;
+-----------+---------+
| Sname | Cname |
+-----------+---------+
| HIGH JUMP | ARUN |
| LONG JUMP | PRANAV |
| SHOT PUT | AHMED |
| RELAY | SANDEEP |
| HIGH JUMP | ALI |
| SHOT PUT | NISHAD |
+-----------+---------+
6 rows in set (0.01 sec)
13. Change the VENUE of ‘S002’ to ‘ANNEX 1’.
mysql> update GAME
-> set Venue = 'ANNEX 1'
-> where Scode = 'S002';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
14. Display the GAMES whose PRIZE > 10000.
mysql> Select Sname from GAME
-> where Prize > 10000;
+-----------+
| Sname |
+-----------+
| HIGH JUMP |
+-----------+
1 row in set (0.00 sec)
15. Add a column ‘PHNO’ of data type varchar(15) to the table COACH.
mysql> Alter table COACH
-> Add PHNO varchar(15);
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0
16. Delete the data from GAME table where SNAME is ‘SHOT PUT’.
mysql> Delete from GAME
-> Where Sname = "SHOT PUT";
Query OK, 1 row affected (0.01 sec)
17. Set the Column CCODE in the table COACH as Primary Key.
mysql> Alter table COACH
-> ADD Primary key (Ccode);
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0
18. Make SCODE as the foreign key of the table COACH with reference to GAME
table.
mysql> Alter table COACH
-> Add FOREIGN KEY (Scode) references GAME(Scode);
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0
19. Delete the table COACH.
mysql> Drop table COACH;
Query OK, 0 rows affected (0.01 sec)
20. Delete the database.
mysql> Drop database Sports;
Query OK, 1 row affected (0.05 sec)