Download and Install MySQL 8 Version
Download and Install MySQL 8 Version
Click Next as given below and make a note of port number 3306 where MySql Server is running
Click Next as given below
Enter Root password as “root” and Retype Password as “root” (You can give password as you wish)
And Click Next
Click Next as given below
In Search, search for “MySQL 8.0 Command Line Client” and Click MySQL 8.0 Command Line Client to
open mysql> prompt
Using Database
mysql>use mydb;
syntax
CREATE TABLE tablename (Column-Name-1 Datatype(size), Column-Name-2 Datatype(size), ….
Column-Name-n Datatype(size));
EX:
CREATE TABLE Books (bno int(3),bname varchar(10),price float(4));
Syntax
DROP TABLE tablename;
Ex:
DROP TABLE Books;
DESC Command
Used to give the structure of the table
Syntax
DESC tablename;
Ex:
DESC Books;
INSERT Command
Used to insert the records into the table
Syntax
INSERT INTO tablename VALUES (value-1, value-2, … , value-n);
- Number of values should be equal to the number of columns of the table
Ex:
INSERT INTO Books VALUES (111,’java’,500);
INSERT INTO Books (bno,bname) VALUES (222,’xml’);
Inserting Multile Rows in one single insert command
Insert into Books values (333,’html’,400),(444,’css’,550),(555,’hibernate’,650);
UPDATE Command
Used to modify or update the existing records of the table
Syntax
UPDATE tablename SET column=newvalue,column=newvalue,…column=newvalue
[WHERE condition];
DELETE Command
Used to delete the records of the table
Syntax
DELETE FROM tablename [WHERE condition];
Where condition is optional
Ex:
DELETE FROM Books; //All records of Books table will be deleted
DELETE FROM Books WHERE bno=222;
DELETE FROM Books WHERE price>=500 AND price<=700;
TCL Commands
Commit
Used to save the transactions
mysql>commit
Rollback
Used to undo the uncommitted transactions
mysql>rollback
TRUNCATE Command
Used to delete all the records of the table
Syntax
TRUNCATE TABLE tablename;
Ex:
TRUNCATE TABLE Books;
Question: What is the difference between DELETE FROM Books and TRUNCATE TABLE Books?
Answer: In case of DELETE we can rollback the transaction where as in case of TRUNCATE we cannot
rollback.
SELECT Command
used to retrieve/read records of the table
syntax
SELECT */column-names FROM tablename
[WHERE condition]
[ORDER BY column-1, column-2, ... , column-n]
[GROUP BY Column]
[HAVING condition];
Ex:
SELECT * FROM Books;
SELECT bno,bname FROM Books;
WHERE clause
used to filter the records of the table based on the condition
ORDER BY Clause
used to arrage the records in ascending or descending order
syntax
SELECT * FROM tablename ORDER BY column-1, column-2, ..., column-n;
Ex:
SELECT * FROM Books ORDER BY price;//ascending
SELECT * FROM Books ORDER BY price DESC;//descending
SELECT * FROM Books ORDER BY price, bname;
Functions
• sum()
• avg()
• count()
• max()
• min()
•
Ex:
mysql>select sum(price) "Total Price" from Books;
Total Price
4550
Avg Price
568.75
No of Records
8
Max Price
800
GROUP BY Clause
used to group the records based on the given column
Ex:
Employee Table
--------------------------------------
eno ename salary dno
--------------------------------------
111 xxx 5000 10
222 yyy 6000 20
333 zzz 7000 30
444 ppp 5500 10
Ex:
mysql>select dno "Dept No",sum(salary) "Total Salary" from employee group by dno;
mysql>select dno "Dept No",count(*) "No of Emps" from employee group by dno;
Dept No No of Emps
-------------------------------
30 1
20 1
10 2
-------------------------------
HAVING Clause
- used to filter the records after grouping the records
- need to be used only with group by clause
Ex:
mysql>select dno "Dept No",sum(salary) "Total Salary" from employee group by dno having
sum(salary)<10000;
Constraints
➢ primary key
- which is unique and not null and used to identify the row uniquely
➢ not null
- does not accept null values (mandatory columns)
➢ unique
- does not allow duplicate values and accepts null values
➢ check
- used to check a condition for the column
➢ foreign key
- a primary key (pk) of one table acting as an ordinary key in another table and
mainly used to relate the tables is called as foreign key
Ex:
Department table
dno(pk) dname
--------------------------------------------
10 hr
20 finance
30 training
---------------------------------------------
Employee table
eno(pk) ename salary dno(fk)
------------------------------------------------------------------------------------
111 xxx 5000 10
222 yyy 6000 30
333 zzz 4000 40 (error)
Ex:
mysql>create table department (dno number(2) primary key, dname varchar(10) not null);
mysql>create table employee (eno number(3) primary key, ename varchar(10) not null,
salary number(4) check (salary >=5000 and salary <=8000), dno number(2),
constraint fk_dno foreign key (dno) references department(dno));