My-Sql: Learning Objectives
My-Sql: Learning Objectives
CHAPTER 8
LEARNING OBJECTIVES:
Relational Database: A database in which the data is stored in the form of relations
(also called tables) is called a Relational Database. In other words a Relational Database is a
collection of one or more tables which are related to each other.
RDBMS: A DBMS used to manage Relational Databases is called an RDBMS (Relational Data
Base Management System). Some popular RDBMS software available are: Oracle, MySQL,
Sybase, Ingress.EVISION TOUR
For eg. In a student’s database there will be Students, Teachers and Office staff tables for
storing respective data.
RDBMS TERMINOLOGY
Cardinality: Total number of rows in a table is called the cardinality of the table.
Arity/Degree: Total number of columns of a table is called the Degree of the table.
Primary Key: The group of one or more columns used to uniquely identify each row of a
relation is called its Primary Key.
Candidate Key: A column or a group of columns which can be used as the primary key of a
relation is called a candidate key because it is one of the candidates available to be the primary
key of the relation.
Alternate Key: A candidate key of a table which is not made its primary key is called its
Alternate Key. H
Table: STUDENT
SQL (Structured Query Language): It is the language used to manipulate and manage
databases and tables within them using an RDBMS.
CATEGORIES OF SQL COMMANDS
DDL (Data Definition Language): All the commands which are used to create, destroy, or
restructure databases and tables come under this category. Examples of DDL commands are -
CREATE, DROP, ALTER.
DML (Data Manipulation Language): All the commands which are used to manipulate data
within tables come under this category. Examples of DML commands are - INSERT, UPDATE,
DELETE.
DCL (Data Control Language): All the commands which are used to control the access to
databases and tables fall under this category. Examples of DCL commands are - GRANT,
REVOKE.
Show tables: This command is used to view the list of tables present in a current
database.
Q. Write a command to view tables in the currently opened database student.
Ans. Show tables;
Select database(): This command is used to show the name of the current database.
Q. Write a command to show the name of the current database.
Ans. select database();
CREATING A TABLE
Q Write a command to create a table STUDENT having columns roll number as integer,
name as character(20), class as integer, date of birth as date and total as float.
Ans. create table STUDENT( roll_no int(2), name char(20), dob date, total
decimal(5,2));
To verify that tables table has been created , give the command:
Show tables;
We can view the structure of the table STUDENT by giving the command:
Desc student;
Q. Insert a record with values 1, Rajat Kumar, 6-12-2005, 450 into the table STUDENT
Ans. insert into STUDENT values(1, ‘Rajat Kumar’, ‘2015-12-6’,450);
or
insert into STUDENT (roll_no, name, dob, total) values (1, “Rajat Kumar”,
‘2015/12/6’,450);
Note:
In the first case the data values for each column must match exactly the default
order in which they appear in the table.
The values for char and date data types must be enclosed in quotes. Standard
date format is "yyyy-mm-dd".
Consider the tables EMP and DEPT to show the results of all further queries.
DISPLAYING RECORDS
Select command: The select command is used to display data stored in a table.
To display all attributes of all records:
Q. Write a command to display all records from the table emp.
Ans. Select * from emp;
To display selected attributes of all records:
Q. Write a command to display employee number and name of all employees.
Ans. Select empno, ename from emp;
Note: Please use the column names as given in the table in the sql command.
Q. Write a command to display the name and annual salary of all employees from the
table emp.
Ans. Select name, sal*12 from emp;
Q. Write a command to display employee number and bonus of all employees where
bonus is calculated as 10% of salary.
Ans. Select eno, sal*10/100 from emp;
Q. Write a command to display the result of the expression: 24*5/2
Ans. Select 24*5/2;
Column alias lets different name to appear for a column than the actual one in the
output. Column alias does not rename the column. It simply displays a different column
name in the output. You can use AS keyword to specify the alias name. But As keyword
is optional.
Q. Write a command to display employee name and salary and display salary as Monthly
salary.
Ans. Select ename, sal as “Monthly Salary” from emp;
or
Select ename , sal from emp;
SELECT COMMAND
Various clauses/keywords/aggregate functions associated with SELECT command are as follows:
KEYWORDS/CLAUSES IN SELECT COMMAND
CLAUSE/KEYWORD USAGE
WHERE Used to specify the condition based on which rows of a table are
displayed.
BETWEEN Used to define the range of values within which the column
values must fall to make a condition true. Range includes both
the upper and the lower values.
IN Used to select values that match any value in a list of Specified
values.
LIKE Used for pattern matching of string data using wildcard
characters % and _ .
IS NULL Used to select rows in which the specified column is NULL
(or is NOT NULL).
ORDER BY Used to display the selected rows in ascending or in descending
order of the specified column/expression.
Q. Write a command (WAC) to display name and salary of those employees whose
salary is greater than and equal to 10000.
Ans. Select ename, sal from emp where sal>=10000;
Q. Write a command to display employee number, department number of those
employees whose department number is not 20.
Ans. Select empno, deptno from emp where deptno != 20;
LOGICAL OPERATORS IN WHERE CLAUSE
NOT Negates a condition
The keyword NOT LIKE is used to select the rows that do not match the specified
pattern.
UPDATE STATEMENT
The UPDATE statement is used to modify the data present in the table.
Syntax:
UPDATE <table_name>
SET <column name> = <value>, [ <column name> = <value>, …] [WHERE <condn>];
Q. WAC to increase the salary of all employees by Rs 2000.
Ans. Update emp set sal=sal +2000;
Q. WAC to increase the salary by 20% of all MANAGER.
Ans. Update emp set sal=sal +sal*0.20 where job= ‘MANAGER’;
Q. WAC to increase the salary by 2000Rs and change department number to 20 for
employee whose employee number is 7521.
Ans. Update emp set sal=sal +2000, deptno=20 where empno=7521 ;
DELETE STATEMENT
DELETE is used to delete rows from the table.
Syntax:
DELETE FROM < tablename> [ Where < condn>];
Q. WAC to delete data of those employee whose salary is less than 6000.
Ans. Delete from emp where sal<6000;
Q. WAC to delete all rows of table employee.
Ans. Delete from emp;
AGGREGATE(Group) FUNCTIONS
Aggregate functions work on multiple rows. There are 5 types of aggregate functions:
Aggregate function Purpose
To find the highest salary Select max(sal) from emp; Max (sal)
paid to an employee
95700
To find the highest salary Select max(sal) from emp where Max (sal)
paid to MANAGERS job=‘MANAGER’;
60000
To find the highest salary + Select max(sal+comm*sal) from Max(sal+comm)
commision paid to emp;
95700
employee
To find the lowest salary paid Select min(sal) from emp; Min (sal)
to an employee
9600
To find the lowest salary paid Select min(sal) from emp Min (sal)
to MANAGERS where job=‘MANAGER’;
15000
To find the lowest salary + Select min(sal+comm*sal) from Min(sal+comm)
commision paid to employee emp;
9600
GROUP BY CLAUSE
Group by clause is used we need to group the data of the table based on certain type.
Q WAC to display the maximum salary paid to employee of each type of job.
Ans. Select job, max(sal) from emp group by job;
We can include more than one aggregate function in one command.
Q WAC to display maximum, minimum, sum of salary paid to employee of each type of
department.
Ans. Select deptno, max(sal), min(sal), sum(sal) from emp group by deptno;
Note: Please make sure to include the column name on which we are grouping in the
select command.
HAVING CLAUSE
Having clause is used to apply condition on groups ie when condition is applied on
aggregate function. We can include more than one aggregate function in one command.
Q WAC to display the maximum salary paid to employee of each type of job where
maximum salary is greater than 10000.
Ans. Select job, max(sal) from emp group by job having max(sal)>10000;
Q WAC to display maximum, minimum, sum of salary paid to employee of each type of
department for departments 10 and 20.
Ans. Select deptno, max(sal), min(sal), sum(sal) from emp group by deptno having
deptno=10 or deptno=20;
Note: WHERE is used to put a condition on individual row of a table whereas HAVING
is used to put condition on individual group formed by GROUP BY clause in a SELECT
statement.
EQUI JOIN
When we extract data from two tables they must have one column which is present in
both the tables. An equi join of two tables is obtained by putting an equality condition
on the Cartesian product of two tables.
This equality condition is put on the common column of the tables and is called equi join
condition.
It is mandatory to give equi join condition when we want to extract data from two
tables. Let us consider two tables: emp and dept
Q. WAC to display employee number, job, salary, department name and loction from
the tables emp and dept.
Ans. Select empno, job, sal, dname, loc from emp, dept where emp.deptno =
dept.deptno;
Note: When a column exist with same name in both the tables it is preceded by table
name and dot to avoid ambiguity.
Q. WAC to display employee name, salary and depart name of all MANAGERS from emp
and dept tables.
Ans. Select ename, sal, dname from emp, dept where emp.deptno = dept.deptno and
job = “MANAGER”;
Q. WAC to display employee name, salary and depart name of all employee in ascending
order of salary from emp and dept tables.
Ans. Select ename, sal, dname from emp, dept where emp.deptno = dept.deptno
order by sal;
Note: Equi join is nothing but a Cartesian product of two tables in which a condition of
equality is enforced on the column which is present in both the tables.
UNION
Union of two tables is a table in which the number of columns is same as the number of
columns of the two tables and number of rows is the sum of the number of rows of both
the tables.
Union operation can be applied on two tables only if the tables have same number and
type of columns. The syntax of union is:
SELECT <select_list>
FROM <tablename>
[WHERE <condition> ]
UNION [ALL]
SELECT <select_list>
FROM <tablename>
[WHERE <condition> ];
Union does not display any duplicate rows unless ALL is specified with it.
Q. WAC to produce a combined list of all students from class XI and XII eliminating the
duplicate rows.
Ans. Select * from classxi union select * from classxii;
Q WAC to produce a combined list of all students from class XI and XII without
eliminating the duplicate rows.
Ans. Select * from classxi union all select * from classxii;
Note: In the first question roll no 2, which is present in both the tables will appear only
once but in the second question it will appear twice.
REVIEW
Following are the clauses which can be used with SELECT command:
b. WHERE Used to specify the condition based on which rows of a table are displayed.
c. BETWEEN Used to define the range of values within which the column values must fall
to make a condition true. Range includes both the upper and the lower values.
d. IN Used to select values that match any value in a list of Specified values.
e. LIKE Used for pattern matching of string data using wildcard characters % and _ .
f. IS NULL Used to select rows in which the specified column is NULL (or is NOT NULL NOT
NULL).
g. ORDER BY Used to display the selected rows in ascending or in descending order of the
specified column/expression.
AGGREGATE(Group) FUNCTIONS
Aggregate functions work on multiple rows. There are 5 types of aggregate functions:
Aggregate function Purpose