0% found this document useful (0 votes)
8 views

Relational Database 1

Uploaded by

sfake8693
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Relational Database 1

Uploaded by

sfake8693
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 110

Relational Database

Terms and Definitions


How to install MYSQL
https://www.sqlshack.com/how-to-install-mysql-
database-server-8-0-19-on-windows-10/

How to Install MySQL on Windows10? | MySQL


Tutorial for Beginners | MySQL Training | Edureka

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/

#1 Install mysql connector in python | Interface


Python with MySQL | Class 12 CBSE Computer
Science
https://www.youtube.com/watch?v=j6Pxrw5bnJ
U
How to install MySql connector in Windows 10 |
Interface Python with MYSQL Class 12
https://www.youtube.com/watch?v=mt-5FGkw2zY

[Solved] python/pip/pip3 is not recognized as an


internal or external command | python command
error

https://www.youtube.com/watch?v=dj5oOPaeIqI
CBSE Syllabus - Unit III: Database Management

• Database concepts: introduction to database concepts and its


need
• Relational data model: relation, attribute, tuple, domain,
degree, cardinality, keys (candidate key, primary key, alternate
key, foreign key)
• Structured Query Language: introduction, Data Definition
Language and Data Manipulation Language, data type (char(n),
varchar(n), int, float, date), constraints (not null, unique,
primary key), create database, use database, show databases,
drop database, show tables, create table, describe table, alter
table (add and remove an attribute, add and remove primary
key), drop table, insert, delete, select, operators (mathematical,
relational and logical), aliasing, distinct clause, where clause, in,
between, order by, meaning of null, is null, is not null, like,
update command, delete command, aggregate functions (max,
min, avg, sum, count), group by, having clause, joins: cartesian
product on two tables, equi-join and natural join
• Interface of python with an SQL database:
connecting SQL with Python, performing
insert, update, delete queries using cursor,
display data by using connect(), cursor(),
execute(), commit(), fetchone(), fetchall(),
rowcount, creating database connectivity
applications, use of %s format specifier or
format() to perform queries
Objectives:
Students should be able to understand:
1. Database concepts: Relations/tables,
Domain, Tuple/row, Attributes/column,
2. Degree and cardinality.
3. Primary Key ,Alternate keys, Candidate keys
4. Purpose of Primary key
Some warm-up questions:
(i) What have you used earlier to store data in rows and
columns?
(ii) Can you identify anything similar in your classroom
which has data in rows and columns?
(iii) On what basis do you arrange the above data?
(iv) Should the data be in a sequence?
(v) What happens if the data is not in order or sequence?
(vi) What do you call huge collections of data?
(vii) What is degree in a table / relation?
(viii)What is cardinality a table / relation?
Ans.
(i) What have used earlier to store data in rows and columns?
(Matrix/Tables/2-D arrays)
(ii) Can you identify anything similar in your classroom which has data in
rows and columns?: (Time Table/ Class Lists)
(iii) On what basis do you arrange the above data? (Roll number, Days,
Account Number/ Board Exam roll number/ Computer Number)
(iv) Should the data be in a sequence? (Yes, for easier processing)
(v) What happens if the data is not in order or sequence? (Searching or
processing becomes difficult and time consuming)
(vi) What do you call huge collections of data? ( Databases, Database is
collection of related tables or relations)

(vii) What is degree in a table / relation? (Number of columns or attributes


in a table/relation)
(viii) What is cardinality a table / relation? (Number of rows/tuples in a table
or relation)
Define Relational Database
A relational database is database that has
a collection of tables of data items, all of
which is formally described and organized
according to the relational model.
Each table is termed as relation and has its
unique name in the relational data model.
Tables are formed by using rows and
columns.
Examples of database management systems :
1. MS-Access,
2. MySQL,
3. PostgreSQL,
4. SQLite,
5. Microsoft SQL Server,
6. Oracle,
7. SAP,
8. dBASE,
9. FoxPro etc.
Relation/ Table in which data are organized in the
form of rows and columns.
Degree is number of
columns/attributes
in a table.

Cardinality is
number of
rows/tuples in a
table.

Find degree and


cardinality in the
given table.
Relation/ Table
In database, a relation means a 'table', in
which data are organized in the form of rows
and columns. Therefore, in database,
relations are equivalent to tables.
Example: Table: Student
Domain
A domain is the original sets of atomic values used
to model data. In data base management and
database a domain refers to all the possible unique
values of a particular column.
For example:
i) The domain of gender column has a set of two
possible values i.e. Male or Female.
ii) The domain of marital status has a set of four
possible values i.e. Married, Unmarried, Widows
and Divorced.
Row/Tuple
Horizontal subset/information in a table is called
tuple. The tuple is also known as a 'record', which
gives particular information of the relation (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

Row 1 Row 1, Column 1 Row 1, Column 2

Row 2 Row 2, Column 1 Row 2, Column 2

Row 3 Row 3, Column 1 Row 3, Column 2


Degree:
Degree is the number of columns / attributes in a
table.
Cardinality:
Cardinality is the number of rows / tuples in a tables.

Q. Find degree and cardinality in Table Student:


WS-1
Q.: Identify Fields(Attributes /columns), Records
(tuples / rows), Domain values of Section column,
degree and cardinality in the given table Student:
Primary Key:
A column or set of columns that uniquely identifies a
row within a table is called primary key.
Candidate Key:
Candidate keys are set of fields (columns with
unique values) in the relation that are eligible to act
as a primary key.
Alternate Key:
Out of the candidate keys, after selecting a key as
primary key, the remaining keys are called alternate
key.
Foreign Key:

A foreign key is a field (or collection of fields) in one


table that uniquely identifies a row of another table.
In other words, a foreign key is a column or a
combination of columns that is used to establish a
link between two tables.
WS-2
Q. 1 Give examples of databases.
Q. 2 Identify tuples/rows, attributes, degree,
cardinality and Primary key in the following table
also give the reason for selecting the Primary Key
Table: Players
Gcode GameName Number PrizeMoney ScheduleDate

101 Carom Board 2 5000 23-Jan-2004


102 Badminton 2 12000 12-Dec-2003
103 Table Tennis 4 8000 14-Feb-2004
105 Chess 2 9000 01-Jan-2004
108 Lawn Tennis 4 25000 19-Mar-2004
WS-3
Q.: Identify Primary Key, Candidate key and Alternate
key in the given table Student:
Candidate Key: In the above table, AdNo and Name has unique
values. Therefore, AdNo and Name are candidate keys.
Primary Key: Out of the AdNo and Name, AdNo is the primary
key.
Alternate Key: In the candidate key, AdNo is the primary key
and the Name is the Alternate key.
Recap

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;

Q: Write a statement to open the database CBSE


mysql> use CBSE2022;

Q: Write a statement to create table players.


mysql> create table players (gcode int(3) primary
key, gname varchar(20), num int(1), prize_money
int(5), s_date date);
Q: Write statement to display the structure of the table players.
Ans.:

mysql> desc players;


+-------------------+---------------+--------+------+-----------+--------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------+--------+------+-----------+--------+
| gcode | int(3) | NO | PRI | | |
| gname | varchar(20) | YES | | NULL | |
| num | int(1) | YES | | NULL | |
| prize_money | int(5) | YES | | NULL | |
| s_date | date | YES | | NULL | |
+-------------------+----------------+-------+------+-----------+--------+
5 rows in set (0.01 sec)
Q. Write a query to display the
contents of table players.
Ans.:
mysql> select * from players;
Empty set (0.00 sec)

Q: Give the reason for the above output.


Ans.:
Q: Write statements to insert data in table
players.
Ans.:
mysql> insert into players values(101,'Carom Board',2, 5000, '2004-01-23');

mysql> insert into players values(102,'Badminton',2, 12000, '2003-12-12');

mysql> insert into players values(103,'Table Tennis',4, 8000, '2004-02-14');

mysql> insert into players values(105,'Chess',2, 9000, '2004-01-01');

mysql> insert into players values(108,'Lawn Tennis',4, 25000, '2004-03-19');


Q. Write a query to display the contents of table
players.
Ans.:
mysql> select * from players;
+---------+-------------------+--------+-------------------+----------------+
| 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: Write a query to display the number of rows, total,
highest, lowest and average prize money from table
players.
Ans.:
mysql> select count(*), sum(prize_money), max(prize_money),
min(prize_money), avg(prize_money) from players;

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

101 Abdul EXECUTIVE S03 2003-03-23 1980-01-13


Ahmed
102 Ravi Head-IT S02 2010-02-12 1987-07-22
Chander
103 John Ken RECEPTIONIST S03 2009-06-24 1983-02-24

105 Nazar GM S02 2006-08-11 1984-03-03


Ameen
108 Priyam CEO S01 2004-12-29 1982-01-19
Sen
Q: Create the following table Salgrad
Table SALGRAD

SGRADE SALARY HRA

S01 56000 18000

S02 32000 12000

S03 24000 8000


Q.: Write statement to create Employees table:
mysql> create table Employees (ECode int(3), Name Varchar(15), Design
Varchar(15), SGrade char(3),DOJ Date, DOB date);

Q.: Write MySQL statements to insert 5 employee records in Employees


table.
mysql> insert into employees values (101,'Abdul
Ahmed','Executive','S03','2003-03-23','1980-01-13');

mysql> insert into employees values (102,'Ravi Chander','Head-


IT','S02','2010-02-12','1987-07-22');

mysql> insert into employees values (103,'John


Ken','Receptionist','S03','2009-06-24','1983-02-24');

mysql> insert into employees values (105,'Nazar Ameen','GM','S02','2006-


08-11','1984-03-03');

mysql> insert into employees values (108,'Priyam Sen','CEO','S01','2004-12-


29','1982-01-19');
Q: Write a query to display all employees details from Table
Employee.

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:

mysql> create table SalGrad (SGrade char(3), Salary Decimal(9,2), HRA


Decimal(9,2));

Q.: Write MySQL statements to insert 3 records in SalGrad table.


mysql> insert into salgrad values ('S01',56000, 18000);

mysql> insert into salgrad values ('S02',32000, 12000);

mysql> insert into salgrad values ('S03',24000, 8000);

mysql> select * from salgrad;


+-----------+--------------+-------------+
| SGrade | Salary | HRA |
+-----------+--------------+--------------+
| S01 | 56000.00 | 18000.00 |
| S02 | 32000.00 | 12000.00 |
| S03 | 24000.00 | 8000.00 |
+-----------+--------------+--------------+
Structure of the table: Employees
Q: Write the query to display the structure of Employees table.
Ans.:
mysql> desc employees;
+----------+----------------+-------+------+-----------+--------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------+-------+-----+-----------+--------+
| ECode | int | YES | | NULL | |
| Name | varchar(15) | YES | | NULL | |
| Design | varchar(15) | YES | | NULL | |
| SGrade | char(3) | YES | | NULL | |
| DOJ | date | YES | | NULL | |
| DOB | date | YES | | NULL | |
+-----------+-----------------+-------+------+----------+--------+
Structure of the table: Salgrad
Q: Write the query to display the structure of
Salgrad table.
Ans.:
mysql> desc salgrad;
+----------+-----------------+-------+-----+------------+--------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-----------------+-------+-----+-----------+--------+
| SGrade | char(3) | YES | | NULL | |
| Salary | decimal(9,2) | YES | | NULL | |
| HRA | decimal(9,2) | YES | | NULL | |
+-----------+------------------+------+------+-----------+-------+
Q: Write statements to create database
CBSE2020 and open it for creating tables.
Ans.:
mysql> create database cbse2020;

mysql> use cbse2020;


Q: Write statement to create table
employees.
Ans.:
mysql> create table employees (ecode int(3)
primary key, name varchar(15), design
varchar(15), sgrade char(3),doj date, dob
date);
Q: Write statements to insert five records
or tuples in table employees.
Ans.:
mysql> insert into employees values(101, 'Abdul Ahmed',
'Executive','S03', '2003-03-23', '1980-01-13');

mysql> insert into employees values(102, 'Ravi Chander', 'Head-


IT','S02', '2010-02-12', '1987-07-22');

mysql> insert into employees values(103, 'John Ken',


'Receptionist','S03', '2009-06-24', '1983-02-24');

mysql> insert into employees values(105, 'Nazar Ameen', 'GM','S02',


'2006-08-11', '1984-03-03');

mysql> insert into employees values(108, 'Priyam Sen', 'CEO','S01',


'2004-12-29', '1982-01-19');
Statement to create table SALGRAD.
mysql> create table salgrad (sgrade char(3), salary
int(7), hra int(6));

mysql> insert into salgrad values ('S01',56000, 18000);

mysql> insert into salgrad values ('S02',32000, 12000);

mysql> insert into salgrad values ('S03',24000, 8000);


Q: Write statements to insert three
records or tuples in table salgrade.
Ans.:
mysql> insert into salgrad values ('S01',56000,
18000);

mysql> insert into salgrad values ('S02',32000,


12000);

mysql> insert into salgrad values ('S03',24000,


8000);
Q: Write the statement to display the
details of table employees.
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 |
+---------+---------------------+------------------+---------+-----------------+----------------+
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'

mysql> INSERT INTO EMPLOYEES VALUES


(109,'Harish Roy','HEAD-IT','S02','2007-09-
09','1983-04-21' );
(v) Delete the row inserted above.
(v) Delete the row inserted above.
mysql> delete from employees where ecode=109;
(i) Find Output:
SELECT COUNT(SGRADE), SGRADE FROM
EMPLOYEES GROUP BY SGRADE;

SELECT COUNT(*), SGRADE FROM EMPLOYEES


GROUP BY SGRADE;
(i) Find Output:
SELECT COUNT(SGRADE), SGRADE FROM
EMPLOYEES GROUP BY SGRADE;
OR
SELECT COUNT(*), SGRADE FROM
EMPLOYEES GROUP BY SGRADE;
Ans.:
+------------------------+------------+
| COUNT(SGRADE) | SGRADE |
+------------------------+------------+
| 1 | S01 |
| 2 | S02 |
| 2 | S03 |
+------------------------+------------+
(ii) Find Output:
SELECT MIN(DOB), MAX(DOJ) FROM
EMPLOYEES;
(ii) Find Output:
SELECT MIN(DOB), MAX(DOJ) FROM
EMPLOYEES;
Ans.:
+----------------+-----------------+
| MIN(DOB) | MAX(DOJ) |
+-----------------+----------------+
| 1980-01-13 | 2010-02-12 |
+-----------------+----------------+
(iii) Find Output:
SELECT NAME, SALARY FROM
EMPLOYEES E, SALGRAD S WHERE
E.SGRADE=S.SGRADE AND
E.ECODE<103;
OR
SELECT NAME, SALARY FROM
EMPLOYEES , SALGRAD WHERE
EMPLOYEES.SGRADE =
SALGRAD.SGRADE AND ECODE<103;
(iii) Find Output: SELECT NAME,
SALARY FROM EMPLOYEE E, SALGRAD
S WHERE E.SGRADE=S.SGRADE AND
E.ECODE<103;
Ans.:
+--------------------+------------+
| NAME | SALARY |
+--------------------+------------+
| Abdul Ahmed | 24000 |
| Ravi Chander | 32000 |
+--------------------+------------+
(iv) Find Output: SELECT SGRADE,
SALARY+HRA FROM SALGRAD WHERE
SGRADE='S02';
(iv) Find Output: SELECT SGRADE,
SALARY+HRA FROM SALGRAD WHERE
SGRADE='S02';
Ans.:
+------------+-------------------+
| SGRADE | SALARY+HRA |
+------------+-------------------+
| S02 | 44000 |
+------------+-------------------+
Cartesian Product: Degrees are added
and Cardinalities are multiplied
mysql> select * from employees, salgrad;
Cartesian Product: Degrees are added
and Cardinalities are multiplied
mysql> select * from employees, salgrad;
Worksheet: 2
Write statements to create database and create
the following two tables: Identify the data type for
each column
Table CARDEN

Ccode CarName Make Color Capacity Charges


501 A-STAR SUZUKI RED 3 14
503 INDIGO TATA SILVER 3 12
502 INNOVA TOYOTA WHITE 7 15
509 SX4 SUZUKI SILVER 4 14
510 C CLASS MERCEDES RED 4 35
Q: Write Statement to create table
CARDEN.
mysql> CREATE TABLE CARDEN (CCODE
INT(3),CARNAME VARCHAR(10),MAKE
VARCHAR(10),COLOR VARCHAR(10),CAPACITY
INT(1), CHARGES INT(2));
Q: Write statements to insert five records
in table CARDEN
mysql> INSERT INTO CARDEN VALUES(501,'A-STAR', 'SUZUKI',
'RED', 3,14);

mysql> INSERT INTO CARDEN VALUES(503,'INDIGO','TATA',


'SILVER', 3,12);

mysql> INSERT INTO CARDEN VALUES(502,'INNOVA','TOYOTA',


'WHITE',7,15);

mysql> INSERT INTO CARDEN VALUES(509,'SX4','SUZUKI',


'SILVER', 4,14);

mysql> INSERT INTO CARDEN VALUES(510,'C CLASS','MERCEDES',


'RED',4,34);
Q: Write statement to display contents of table
CARDEN.

mysql> SELECT * FROM CARDEN;


+----------+---------------+---------------+-----------+--------------+--------------+
| CCODE | CARNAME | MAKE | COLOR | CAPACITY | CHARGES |
+----------+---------------+----------------+-----------+--------------+-------------+
| 501 | A-STAR | SUZUKI | RED | 3 | 14 |
| 503 | INDIGO | TATA | SILVER | 3 | 12 |
| 502 | INNOVA | TOYOTA | WHITE | 7 | 15 |
| 509 | SX4 | SUZUKI | SILVER | 4 | 14 |
| 510 | C CLASS | MERCEDES | RED | 4 | 34 |
+----------+----------------+----------------+------------+-------------+------------+
Q: Write statement to create the
following table.
mysql> SELECT 1+1; # This comment continues to the end of line
mysql> SELECT 1+1; -- This comment continues to the end of line
mysql> SELECT 1 /* this is an in-line comment */ + 1;
mysql> SELECT 1+ 1/* this is a multiple-line comment */;

Tables CUSTOMER

CODE CNAME CCODE


1001 Hemant Sahu 501
1002 Raj Lal 509
1003 Feroza Shah 503
1004 Ketan Dhal 502
Statement to create table Customer.
mysql> CREATE TABLE CUSTOMER (CODE INT(4),
CNAME VARCHAR(15), CCODE INT(3));
Q: Write statements to insert 4 customers
details in table Customer.
mysql> INSERT INTO CUSTOMER VALUES (1001,
'Hemant Sahu', 501);

mysql> INSERT INTO CUSTOMER VALUES (1002, 'Raj


Lal', 509);

mysql> INSERT INTO CUSTOMER VALUES (1003, 'Feroza


Shah', 503);

mysql> INSERT INTO CUSTOMER VALUES (1004, 'Ketan


Dhal', 502);
Q: Display the contents of table
CARDEN.
Q: Display the contents of table
CARDEN.
mysql> SELECT * FROM CARDEN;
+----------+----------------- +---------------+-----------+--------------+--------------+
| CCODE | CARNAME | MAKE | COLOR | CAPACITY | CHARGES |
+--------+-----------------+----------------+-----------+--------------+--------------+
| 501 | A-STAR | SUZUKI | RED | 3 | 14 |
| 503 | INDIGO | TATA | SILVER | 3 | 12 |
| 502 | INNOVA | TOYOTA | WHITE | 7 | 15 |
| 509 | SX4 | SUZUKI | SILVER | 4 | 14 |
| 510 | C CLASS | MERCEDES | RED | 4 | 34 |
+-------+------------------+----------------+----------+--------------+--------------+
Q: Display the contents of table
CUSTOMER.
Q: Display the contents of table
CUSTOMER.
mysql> SELECT * FROM CUSTOMER;
+---------+-------------------+----------+
| CODE | CNAME | CCODE |
+---------+-------------------+----------+
| 1001 | Hemant Sahu | 501 |
| 1002 | Raj Lal | 509 |
| 1003 | Feroza Shah | 503 |
| 1004 | Ketan Dhal | 502 |
+---------+-------------------+----------+
Q: Show the structure of the table
CARDEN
Q: Show the structure of the table
CARDEN
mysql> DESC CARDEN;
+----------------+----------------+-------+------+-----------+--------+
| Field | Type | Null | Key | Default | Extra |
+----------------+----------------+-------+------+-----------+--------+
| CCODE | int(3) | YES | | NULL | |
| CARNAME | varchar(10) | YES | | NULL | |
| MAKE | varchar(10) | YES | | NULL | |
| COLOR | varchar(10) | YES | | NULL | |
| CAPACITY | int(1) | YES | | NULL | |
| CHARGES | int(2) | YES | | NULL | |
+---------------+-----------------+-------+------+-----------+-------+
Q: Show the structure of the table
CUSTOMER
Q: Show the structure of the table
CUSTOMER
mysql> DESC CUSTOMER;
+-----------+--------------+-------+------+-----------+-------+
| Field | Type | Null | Key| Default | Extra |
+-----------+---------------+-------+-----+-----------+-------+
| CODE | int(4) | YES | | NULL | |
| CNAME| varchar(15)| YES | | NULL | |
| CCODE | int(3) | YES | | NULL | |
+-----------+----------------+------+-----+-----------+-------+
Q: (i) To display the names of all the
silver colored Cars.
Q: (i) To display the names of all the
silver colored Cars.
mysql> SELECT CARNAME, COLOR FROM
CARDEN WHERE COLOR='SILVER';
+---------------+-----------+
| CARNAME | COLOR |
+---------------+-----------+
| INDIGO | SILVER |
| SX4 | SILVER |
+---------------+-----------+
Q: (ii) To display name of car, make and
capacity of cars in descending order of
their sitting capacity.
Q: (ii) To display name of car, make and
capacity of cars in descending order of
their sitting capacity.
mysql> SELECT CARNAME, MAKE, CAPACITY FROM
CARDEN ORDER BY CAPACITY DESC;
+---------------+---------------+--------------+
| CARNAME | MAKE | CAPACITY |
+---------------+---------------+--------------+
| INNOVA | TOYOTA | 7 |
| SX4 | SUZUKI | 4 |
| C CLASS | MERCEDES | 4 |
| A-STAR | SUZUKI | 3 |
| INDIGO | TATA | 3 |
+---------------+----------------+-------------+
Q: (iii) To display the highest charges at
which a vehicle can be hired from
CARDEN.
Q: (iii) To display the highest charges at
which a vehicle can be hired from
CARDEN.
mysql> SELECT MAX(CHARGES) "HIGHEST CHARGES" FROM CARDEN;
+--------------------------+
| HIGHEST CHARGES |
+--------------------------+
| 34 |
+--------------------------+
OR
mysql> SELECT MAX(CHARGES) AS HIGHEST_CHARGES FROM CARDEN;
+--------------------------+
| HIGHEST_CHARGES |
+--------------------------+
| 34 |
+--------------------------+
OR
mysql> SELECT MAX(CHARGES) HIGHEST_CHARGES FROM CARDEN;
Q: (iv) To display the customer name
and the corresponding name of the
cars hired by them.
Q: (iv) To display the customer name and the
corresponding name of the cars hired by them.

mysql> SELECT CNAME, CARNAME FROM CUSTOMER, CARDEN WHERE


CUSTOMER.CCODE=CARDEN.CCODE;
OR
mysql> SELECT CNAME, CARNAME FROM CUSTOMER A, CARDEN B WHERE
A.CCODE=B.CCODE;

+-------------------+---------------+
| 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 |
+---------------+

You might also like