MySQL Pratical Notes
MySQL Pratical Notes
Now, to check your database is created or not, we can use the below command.
mysql> show databases;
Output:
To switched to a database
mysql>use Employees;
Output:
After that, we can create tables name “Employee” within open database “Employees”.
#Creating Employee table
create table Employee
(
Empno char(4),
Ename varchar(30),
Job varchar(20),
Hire_Date date,
Salary decimal(6,2),
Commision decimal(6,2),
Deptno char(2)
1
);
# To insert data or values in the created table 1st we have to know the data type of all those
fields of “Employee” table by execute the below command.
mysql> desc Employee;
or
mysql> describe Employee;
Output:
2
insert into Employee
values('8900','JATIN','CLERK','1991-12-03',950,NULL,'30');
To check or display all records that are inserted into the table, by execute the below
command:
mysql> select * from Employee;
OUT PUT
3. Display the employee name, job type and salary of those employee whose salary range
2000-3000.
mysql> select ename,job,salary from employee where salary between 2000 and 3000;
OUTPUT:
3
4. To display the type of job category available in the employee table.
mysql> select distinct(job) from employee;
OUTPUT:
5. Write a SQL query to display total no. of employees working in each job type.
mysql> select count(*) as ‘Department Wise’, job from employee group by job;
OUT PUT
6. Write a SQL query to display earliest and latest hire date of each department.
OUTPUT:
7. Write a SQL query to display average salary of clerk and managers of each department.
Pre-condition:- In this, first the condition is checked and only those records which
match the conditions are included in group by.
It is applied using where clause.
4
8. Write a SQL query to display the total salary of those departments where total salary is more
than seven thousand.
Post-condition: In this, first the records are grouped and after grouping the
condition is applied on the combined records and then checked.
It is applied using having clause.
mysql> select sum(salary), deptno from employee group by deptno having
sum(salary)>7000;
OUT PUT
9. Create two tables named Items, Salesperson and insert values into these tables as below:
Table: Items
create table Items
(
ITCode int not null primary key,
ItemType varchar(50),
Turnover float(10,2)
);
OUT PUT
View all records from the salesperson table, use the below code
OUT PUT
6
10. Write a query to display ITCode, Code, Turnover from both salesperson and Items table.
mysql> select salesperson.itcode,code,turnover from salesperson, items
where salesperson.itcode=items.itcode;
OUT PUT
11. Integrate MySQL with Python to insert data in the database’s table Student.
mysql> desc student;
OUT PUT
import mysql.connector as c
con=c.connect(host="localhost", user="root",passwd="123456789",database="om_shanti")
cursor=con.cursor()
name=input("Enter student name:")
class1=input("Enter Class")
mark=int(input("Enter mark"))
OUTPUT:
Enter student name:Mohit Kumar
Enter Class:XII
Enter mark:97
Data inserted successfully
7
# Display all records from student table after inserting the data.
mysql> select * from student;
OUT PUT
12. Integrate MySQL with Python to update data in the database’s table Student.
import mysql.connector as c
con=c.connect(host="localhost", user="root",passwd="123456789",database="om_shanti")
cursor=con.cursor()
name=input("Enter student name:")
OUTPUT:
Enter student name:ram
Enter New Mark:77
Data update successfully
# Display all records from student table after updating the data.
mysql> select * from student;
OUT PUT
cursor=con.cursor()
name=input("Enter student name:")
8
cursor.execute(query)
con.commit()
print("Data deleted successfully")
OUT PUT
Enter student name:raju
Data deleted successfully
# Display all records from student table after deleting the data.
mysql> select * from student;
14. Code to display record from the salesperson table based on the Code enter by the user.
# import required modules
import mysql.connector as c
# connect python with mysql
con=c.connect(host="localhost",user="root",Password="123456789",database="Employees"
)
# Create a query
qry="SELECT * FROM salesperson where code='{}'".format(data)
OUTPUT:
Enter the code:1003
Record Found…
(1003, 'Rahul Raj', 4500.0, 12)
9