Relational Database 1
Relational Database 1
https://www.youtube.com/watch?v=GIRcpjg-3Eg
Install MySQL Connector Python on
Windows, MacOs, Linux, Unix and Ubuntu
https://pynative.com/install-mysql-connector-
python/
https://www.youtube.com/watch?v=dj5oOPaeIqI
CBSE Syllabus - Unit III: Database Management
Cardinality is
number of
rows/tuples in a
table.
For example:
i) In customer table, one row gives information about
one customer only.
ii) In student table, one row gives information about
one student only.
Column/ Attribute
A column is a set of data values of a particular
simple type, one for each row of the table. The
columns provide the structure according to which
the rows are composed.
For example: In student table, RollNo and Name
are the attributes of the table.
Table with 3 rows and 2 columns
Column 1 Column 2
1. Database,
2. Relations/tables,
3. Domain
4. Tuple/row,
5. Attributes/column,
6. Degree and cardinality.
7. Primary Key, Alternate keys, Candidate keys
8. Purpose of Primary key
9. Identifying these terms in a given table
Challenging questions:
1. Is it possible to retrieve data from two tables
Student and Teacher?
2. What should be the common key for linking
two table given above?
3. Is it possible to get meaningful data from two
tables without using a common key?
WS-4
Identify two tables which have a common keys.
Identify Primary keys, Degree, Cardinality for each table.
Hands on experience
1. Install MySQL on your laptops so that you can
create databases and table and execute MySQL
queries.
2. Create database
3. Use database
4. Create table
5. Insert records in table
Q: Using SQL commands create the
table given below and insert records.
Table: Players
Q: Write a statement to create database CBSE.
mysql> create database CBSE2022;
Output:
+-------------+---------------------------+--------------------------+---------------------------+-------------------------+
| count(*) | sum(prize_money) | max(prize_money) | min(prize_money) | avg(prize_money) |
+-------------+---------------------------+--------------------------+---------------------------+--------------------------+
| 5 | 59000 | 25000 | 5000 | 11800.0000 |
+-------------+---------------------------+--------------------------+---------------------------+--------------------------+
Q: Display the games which have Tennis in the
gname.
Ans.:
mysql> SELECT * FROM PLAYERS WHERE GNAME like
'%Tennis';
Output:
+---------+-----------------+--------+-------------------+----------------+
| gcode | gname | num | prize_money | s_date |
+---------+-----------------+---------+------------------+-----------------+
| 103 | Table Tennis | 4 | 8000 | 2004-02-14 |
| 108 | Lawn Tennis | 4 | 25000 | 2004-03-19 |
+--------+------------------+---------+------------------+-----------------+
Q: Display the game details which were enrolled
before 1st April 2004.
Ans.:
mysql> select * from players where s_date<'2004-04-01';
Output:
+---------+-------------------+--------+-------------------+----------------+
| gcode | gname | num | prize_money | s_date |
+---------+-------------------+--------+-------------------+----------------+
| 101 | Carom Board | 2 | 5000 | 2004-01-23|
| 102 | Badminton | 2 | 12000 | 2003-12-12 |
| 103 | Table Tennis | 4 | 8000 | 2004-02-14 |
| 105 | Chess | 2 | 9000 | 2004-01-01 |
| 108 | Lawn Tennis | 4 | 25000 | 2004-03-19 |
+---------+--------------------+-------+------------------+------------------+
5 rows in set (0.02 sec)
Q: Display the game details from table players
which have two words in gname.
Ans.:
mysql> select * from players where gname like '% %';
Output:
+---------+-------------------+--------+------------------+----------------+
| gcode | gname | num | prize_money | s_date |
+---------+-------------------+--------+------------------+----------------+
| 101 | Carom Board | 2 | 5000 | 2004-01-23 |
| 103 | Table Tennis | 4 | 8000 | 2004-02-14 |
| 108 | Lawn Tennis | 4 | 25000 | 2004-03-19 |
+---------+--------------------+--------+-------------------+----------------+
Q: Display the details from table players where
prize_money is divisible by 3
Ans.:
mysql> select * from players where prize_money%3=0;
Output:
+---------+----------------+------+-------------------+----------------+
| gcode | gname | num| prize_money | s_date |
+---------+----------------+-------+------------------+----------------+
| 102 | Badminton | 2 | 12000 | 2003-12-12 |
| 105 | Chess | 2 | 9000 | 2004-01-01 |
+---------+-----------------+------+------------------+-----------------+
Q: Display the prize_money in descending order
from table players.
Ans.:
mysql> select prize_money from players order by prize_money desc;
Output:
+------------------+
| prize_money |
+------------------+
| 25000 |
| 12000 |
| 9000 |
| 8000 |
| 5000 |
+------------------+
Q: Display the details of players table with
s_date sorted in latest to oldest date.
Ans.:
mysql> select * from players order by s_date desc;
Output:
+---------+-------------------+--------+------------------+----------------+
| gcode | gname | num | prize_money | s_date |
+---------+-------------------+--------+------------------+--------------- -+
| 108 | Lawn Tennis | 4 | 25000 | 2004-03-19 |
| 103 | Table Tennis | 4 | 8000 | 2004-02-14 |
| 101 | Carom Board | 2 | 5000 | 2004-01-23 |
| 105 | Chess | 2 | 9000 | 2004-01-01 |
| 102 | Badminton | 2 | 12000 | 2003-12-12 |
+---------+--------------------+-------+------------------+-----------------+
Q: Display game name and num from players table
where num is 2.
Ans.:
mysql> select gname, num from players where num=2;
Output:
+-------------------+--------+
| gname | num |
+-------------------+--------+
| Carom Board | 2 |
| Badminton | 2 |
| Chess | 2 |
+-------------------+--------+
Q: Display details from table players who were
enrolled after 1st January 2004
Ans.:
mysql> select * from players where s_date>'2004-01-01';
Output:
+---------+------------------+---------+------------------+-----------------+
| gcode | gname | num | prize_money | s_date |
+---------+-------------------+--------+-------------------+-----------------+
| 101 | Carom Board | 2 | 5000 | 2004-01-23 |
| 103 | Table Tennis | 4 | 8000 | 2004-02-14 |
| 108 | Lawn Tennis | 4 | 25000 | 2004-03-19 |
+---------+-------------------+--------+--------------------+----------------+
Q: Display highest prize money from table players
which are enrolled after 12th December 2003.
Ans.:
mysql> select max(prize_money) from players where
s_date>'2003-12-12';
Output:
+--------------------------+
| max(prize_money) |
+--------------------------+
| 25000 |
+--------------------------+
Q: Write a query to display the games which has
word ‘Tennis’ in it from table players.
Ans.:
mysql> select gname from players where gname like
'%tennis%';
Output:
+------------------+
| gname |
+------------------+
| Table Tennis |
| Lawn Tennis |
+------------------+
Worksheet: 1
Write statements to create database CBSE2022 and create
the following two tables: Identify the data type for each
column
Table: EMPLOYEES
ECODE NAME DESIGN SGRADE DOJ DOB
Ans.:
mysql> select * from employees;
Output:
+---------+--------------------+-----------------+-----------+---------------+-----------------+
| ECode | Name | Design | SGrade | DOJ | DOB |
+----------+--------------------+-----------------+----------+----------------+----------------+
| 101 | Abdul Ahmed | Executive | S03 | 2003-03-23 | 1980-01-13 |
| 102 | Ravi Chander | Head-IT | S02 | 2010-02-12 | 1987-07-22 |
| 103 | John Ken | Receptionist | S03 | 2009-06-24 | 1983-02-24 |
| 105 | Nazar Ameen | GM | S02 | 2006-08-11 | 1984-03-03 |
| 108 | Priyam Sen | CEO | S01 | 2004-12-29 | 1982-01-19 |
+----------+---------------------+-----------------+----------+----------------+----------------+
5 rows in set (0.08 sec)
Q.: Write statement to create SalGrad table:
Output:
+---------+--------------------+-----------------+----------+----------------+-----------------+
| ecode | name | design | sgrade | doj | dob |
+---------+--------------------+------------------+----------+----------------+-----------------+
| 101 | Abdul Ahmed | Executive | S03 | 2003-03-23 | 1980-01-13 |
| 102 | Ravi Chander | Head-IT | S02 | 2010-02-12 | 1987-07-22 |
| 103 | John Ken | Receptionist | S03 | 2009-06-24 | 1983-02-24 |
| 105 | Nazar Ameen | GM | S02 | 2006-08-11 | 1984-03-03 |
| 108 | Priyam Sen | CEO | S01 | 2004-12-29 | 1982-01-19 |
+---------+---------------------+------------------+---------+-----------------+----------------+
Q: Write the statement to display the
details of table salgrad.
Ans.:
mysql> select * from salgrad;
Output:
+----------+----------+----------+
| sgrade | salary | hra |
+----------+----------+----------+
| S01 | 56000 | 18000 |
| S02 | 32000 | 12000 |
| S03 | 24000 | 8000 |
+----------+----------+----------+
(i) Display the details of all EMPLOYEEs in
descending order of DOJ.
(i) Display the details of all EMPLOYEEs in
descending order of DOJ.
Ans.:
mysql> SELECT * FROM EMPLOYEES ORDER BY DOJ DESC;
Output:
+---------+--------------------+-----------------+---------+----------------+-----------------+
| ecode | name | design | sgrade | doj | dob |
+---------+--------------------+-----------------+----------+----------------+----------------+
| 102 | Ravi Chander | Head-IT | S02 | 2010-02-12 | 1987-07-22 |
| 103 | John Ken | Receptionist | S03 | 2009-06-24 | 1983-02-24 |
| 105 | Nazar Ameen | GM | S02 | 2006-08-11 | 1984-03-03 |
| 108 | Priyam Sen | CEO | S01 | 2004-12-29 | 1982-01-19 |
| 101 | Abdul Ahmed | Executive | S03 | 2003-03-23 | 1980-01-13 |
+---------+--------------------+------------------+--------+-----------------+----------------+
(ii) Display NAME and DESIGN of those
EMPLOYEEs, whose SGRADE is either S02 or
S03.
(ii) Display NAME and DESIGN of those
EMPLOYEEs, whose SGRADE is either S02 or
S03.
mysql> SELECT NAME, DESIGN FROM EMPLOYEES
WHERE SGRADE='S02' OR SGRADE='S03';
+-------------------+-----------------+
| NAME | DESIGN |
+--------------------+-----------------+
| Abdul Ahmed | Executive |
| Ravi Chander | Head-IT |
| John Ken | Receptionist |
| Nazar Ameen | GM |
+---------------------+-----------------+
(iii) To display the content of all employees from
the EMPLOYEEs table, whose DOJ is in between
’09-Feb-2006’ and ’08-Aug-2009’.
(iii) To display the content of all employees from
the EMPLOYEEs table, whose DOJ is in between
’09-Feb-2006’ and ’08-Aug-2009’.
mysql> SELECT * FROM EMPLOYEES WHERE DOJ BETWEEN
'2006-02-09' AND '2009-08-08';
OR
mysql> SELECT * FROM EMPLOYEES WHERE DOJ>='2006-02-
09' AND doj<='2009-08-08';
+---------+-------------------+-----------------+----------+---------------+---------------+
| ecode | name | design | sgrade | doj | dob |
+---------+-------------------+------------------+---------+----------------+---------------+
| 103 | John Ken | Receptionist | S03 | 2009-06-24 | 1983-02-24 |
| 105 | Nazar Ameen | GM | S02 | 2006-08-11 | 1984-03-03 |
+---------+-------------------+-------------------+---------+-----------------+------------+
(iv) To add a new row with the following:
109, ‘Harish Roy’, ‘HEAD-IT’, ‘S02’, '2007-09-09‘,
'1983-04-21'
(iv) To add a new row with the following:
109, ‘Harish Roy’, ‘HEAD-IT’, ‘S02’, '2007-09-09‘,
'1983-04-21'
Tables CUSTOMER
+-------------------+---------------+
| CNAME | CARNAME |
+-------------------+---------------+
| Hemant Sahu | A-STAR |
| Feroza Shah | INDIGO |
| Ketan Dhal | INNOVA |
| Raj Lal | SX4 |
+-------------------+---------------+
Q: (i) Find output:
mysql> SELECT COUNT(DISTINCT MAKE) FROM
CARDEN;
Q: (i) Find output:
mysql> SELECT COUNT(DISTINCT MAKE) FROM
CARDEN;
+----------------------------------+
| COUNT(DISTINCT MAKE) |
+----------------------------------+
| 4 |
+----------------------------------+
Q: (ii) Find output:
mysql> SELECT MAX(CHARGES), MIN(CHARGES)
FROM CARDEN;
Q: (ii) Find output:
mysql> SELECT MAX(CHARGES), MIN(CHARGES)
FROM CARDEN;
+----------------------+----------------------+
| MAX(CHARGES) | MIN(CHARGES) |
+----------------------+----------------------+
| 34 | 12 |
+----------------------+----------------------+
Q: (iii) Find output:
mysql> SELECT COUNT(*) , MAKE FROM
CARDEN GROUP BY MAKE;
Q: (iii) Find output:
mysql> SELECT COUNT(*) , MAKE FROM CARDEN
GROUP BY MAKE;
+------------- -+-----------------+
| COUNT(*) | MAKE |
+---------------+-----------------+
| 1 | MERCEDES |
| 2 | SUZUKI |
| 1 | TATA |
| 1 | TOYOTA |
+---------------+-----------------+
Q: (iv) Find Output:
mysql> SELECT CARNAME FROM CARDEN
WHERE CAPACITY=4;
Q: (iv) Find Output:
mysql> SELECT CARNAME FROM CARDEN
WHERE CAPACITY=4;
+---------------+
| CARNAME |
+---------------+
| SX4 |
| C CLASS |
+---------------+