0% found this document useful (0 votes)
13 views43 pages

Dbms Compelete Manual

Uploaded by

parth92.6000
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)
13 views43 pages

Dbms Compelete Manual

Uploaded by

parth92.6000
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/ 43

// Output:

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| customers |
| information_schema |
| mysql |
| performance_schema |
| persinsinfo |
| sakila |
| sys |
| teainds |
| world |
+--------------------+
9 rows in set (0.03 sec)
mysql> create database T3
-> ;
Query OK, 1 row affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| customers |
| information_schema |
| mysql |
| performance_schema |
| persinsinfo |
| sakila |
| sys |
| t3 |
| teainds |
| world |
+--------------------+
10 rows in set (0.00 sec)
mysql> use t3
Database changed
mysql> create table persons (id int, firstname varchar(255), lastname
varchar(255), city varchar(255), primary key (id));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+--------------+
| Tables_in_t3 |
+--------------+
| persons |
+--------------+
1 row in set (0.02 sec)
mysql> insert into persons values(1, 'NIlesh', 'sharma','Nashik');
Query OK, 1 row affected (0.01 sec)
mysql> insert into persons values(2, 'Anushka', 'Sharma','Mumbai');
Query OK, 1 row affected (0.00 sec)
mysql> insert into persons values(3, 'Gayatri', 'Jadhav','Chalisgaon');
Query OK, 1 row affected (0.01 sec)
mysq>linsert into persons values(3, 'Gayatri', 'Jadhav','Chalisgaon');
mysql> update persons set firstname='Aishwarya' where id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from persons;
+----+-----------+----------+------------+
| id | firstname | lastname | city |
+----+-----------+----------+------------+
| 1 | NIlesh | sharma | Nashik |
| 2 | Aishwarya | Sharma | Mumbai |
| 3 | Gayatri | Jadhav | Chalisgaon |
+----+-----------+----------+------------+
3 rows in set (0.00 sec)
mysql> delete from persons where id=1;
Query OK, 1 row affected (0.00 sec)
mysql> alter table persons add emailid varchar(255);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe persons;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| firstname | varchar(255) | YES | | NULL | |
| lastname | varchar(255) | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
| emailid | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table persons drop emailid;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table persons rename column id to rollno
-> ;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe persons;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| rollno | int | NO | PRI | NULL | |
| firstname | varchar(255) | YES | | NULL | |
| lastname | varchar(255) | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> truncate table persons;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from persons;
Empty set (0.00 sec)
mysql> drop table persons;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> create table persons (id int, firstname varchar(255), lastname
varchar(255), city varchar(255), primary key (id));
Query OK, 0 rows affected (0.01 sec)
5 rows in set (0.00 sec)
mysql> alter table persons add emailid varchar(255);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from persons;
+----+-----------+----------+------------+---------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+---------+
| 1 | NIlesh | sharma | Nashik | NULL |
| 2 | Anushka | Sharma | Mumbai | NULL |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
+----+-----------+----------+------------+---------+
5 rows in set (0.00 sec)
mysql> update persons set emailid="[email protected]"
where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from persons;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 2 | Anushka | Sharma | Mumbai | NULL |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> select * from persons order by firstname
-> ;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 2 | Anushka | Sharma | Mumbai | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> select * from persons order by firstname DESC
-> ;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 5 | Durgesh | Agrawal | Manmad | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 2 | Anushka | Sharma | Mumbai | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> select * from persons order by firstname;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 2 | Anushka | Sharma | Mumbai | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> select * from persons order by id DESC;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 5 | Durgesh | Agrawal | Manmad | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 2 | Anushka | Sharma | Mumbai | NULL |
| 1 | NIlesh | sharma | Nashik | [email protected] |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> create index i1 on persons (id, firstname);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> help
mysql> help auto_increment
Name: 'AUTO_INCREMENT'
Description:
The AUTO_INCREMENT attribute can be used to generate a unique identity
for new rows:
Examples:
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO animals (name) VALUES
('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
SELECT * FROM animals;
mysql> alter table persons modify column id int AUTO_INCREMENT;
Query OK, 5 rows affected (1.51 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from persons;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 2 | Anushka | Sharma | Mumbai | NULL |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> INSERT INTO Persons (FirstName,LastName)
values('rohit','sharma');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Persons (FirstName,LastName)
values('rohit','sharma');
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(id)
);
mysql> create table persons (id int, firstname varchar(255), lastname
varchar(255), city varchar(255), primary key (id));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into persons values(3, 'Gayatri', 'Jadhav','Chalisgaon');
Query OK, 1 row affected (0.00 sec)
mysql> select * from persons;
+----+-----------+----------+------------+
| id | firstname | lastname | city |
+----+-----------+----------+------------+
| 1 | NIlesh | sharma | Nashik |
| 2 | Anushka | Sharma | Mumbai |
| 3 | Gayatri | Jadhav | Chalisgaon |
+----+-----------+----------+------------+
3 rows in set (0.00 sec)
mysql> CREATE TABLE Orders (
-> OrderID int NOT NULL,
-> OrderNumber int NOT NULL,
-> PersonID int,
-> PRIMARY KEY (OrderID),
-> FOREIGN KEY (PersonID) REFERENCES Persons(id)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into orders values (1,22546,2);
mysql> select * from orders;
+---------+-------------+----------+
| OrderID | OrderNumber | PersonID |
+---------+-------------+----------+
| 1| 22546 | 2|
| 2| 22546 | 1|
| 3| 22546 | 3|
+---------+-------------+----------+
3 rows in set (0.00 sec)
mysql> select firstname , OrderID, OrderNumber from persons inner join
orders on persons.id=orders.personid
-> ;
+-----------+---------+-------------+
| firstname | OrderID | OrderNumber |
+-----------+---------+-------------+
| NIlesh | 2| 22546 |
| Anushka | 1| 12345 |
| Gayatri | 3| 53214 |
+-----------+---------+-------------+
3 rows in set (0.00 sec)
mysql> select firstname , OrderID, OrderNumber from persons inner join
orders on persons.id=orders.personid
-> ;
+-----------+---------+-------------+
| firstname | OrderID | OrderNumber |
+-----------+---------+-------------+
| NIlesh | 2| 22546 |
| Anushka | 1| 12345 |
| Gayatri | 3| 53214 |
+-----------+---------+-------------+
3 rows in set (0.00 sec)
mysql> select * from persons inner join orders on
persons.id=orders.personid;
+----+-----------+----------+------------+---------+-------------+----------+
| id | firstname | lastname | city | OrderID | OrderNumber | PersonID |
+----+-----------+----------+------------+---------+-------------+----------+
| 1 | NIlesh | sharma | Nashik | 2| 22546 | 1|
| 2 | Anushka | Sharma | Mumbai | 1| 12345 | 2|
| 3 | Gayatri | Jadhav | Chalisgaon | 3| 53214 | 3|
+----+-----------+----------+------------+---------+-------------+----------+
3 rows in set (0.00 sec)
mysql> create view v1 as select firstname, city from persons;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from v1;
+-----------+------------+
| firstname | city |
+-----------+------------+
| NIlesh | Nashik |
| Anushka | Mumbai |
| Gayatri | Chalisgaon |
+-----------+------------+
3 rows in set (0.00 sec)
//OUTPUT: SQL JOIN
mysql> use database_name
mysql> create table stud_info(Rno int, Name varchar(20), Address
varchar(20));
mysql> create table stud_marks(Rno int, DBMS int, TOC int, CN int);
mysql> insert into stud_info values (1,'Abhay','Nashik'), (2,'Sarika','Pune'),
(3,'Riya','Nashik'), (4,'Sachin','Manmad');
mysql> insert into stud_marks
values(1,50,60,55),(2,68,57,76),(3,45,76,70),(5,80,75,85);
mysql> select * from stud_info;
| Rno | Name | Address |
| 1 | Abhay | Nashik |
| 2 | Sarika | Pune |
| 3 | Riya | Nashik |
| 4 | Sachin | Manmad |
mysql> select * from stud_marks;
| Rno | DBMS | TOC | CN |
| 1 | 50 | 60 | 55 |
| 2 | 68 | 57 | 76 |
| 3 | 45 | 76 | 70 |
| 5 | 80 | 75 | 85 |
mysql> select stud_info.Rno, Name,DBMS,TOC,CN from stud_info inner
join stud_marks
on stud_info.Rno=stud_marks.Rno;
| Rno | Name | DBMS | TOC | CN |
| 1 | Abhay | 50 | 60 | 55 |
| 2 | Sarika | 68 | 57 | 76 |
| 3 | Riya | 45 | 76 | 70 |
3 rows in set (0.00 sec)
mysql> select stud_info.Rno, Name,DBMS,TOC,CN from stud_info left join
stud_marks on
stud_info.Rno=stud_marks.Rno;
| Rno | Name | DBMS | TOC | CN |
| 1 | Abhay | 50 | 60 | 55 |
| 2 | Sarika | 68 | 57 | 76 |
| 3 | Riya | 45 | 76 | 70 |
| 4 | Sachin | NULL | NULL | NULL |
4 rows in set (0.00 sec)
mysql> select stud_info.Rno, Name,DBMS,TOC,CN from stud_info right
join stud_marks
on stud_info.Rno=stud_marks.Rno;
| Rno | Name | DBMS | TOC | CN |
| 1 | Abhay | 50 | 60 | 55 |
| 2 | Sarika | 68 | 57 | 76 |
| 3 | Riya | 45 | 76 | 70 |
| NULL | NULL | 80 | 75 | 85 |
4 rows in set (0.00 sec)
mysql> select stud_marks.Rno, Name,DBMS,TOC,CN from stud_info right
join
stud_marks on stud_info.Rno=stud_marks.Rno;
| Rno | Name | DBMS | TOC | CN |
| 1 | Abhay | 50 | 60 | 55 |
| 2 | Sarika | 68 | 57 | 76 |
| 3 | Riya | 45 | 76 | 70 |
| 5 | NULL | 80 | 75 | 85 |
4 rows in set (0.00 sec)
//Sub queries.
// Create table EMP
mysql> create table Emp(Eid int, Ename varchar(20), City varchar(20),
Post
varchar(15),Salary int, deptno int);
//Insert 10 Rows in the same
mysql> insert into Emp values
(1,'John','Nashik','Clerk',5000,10),
(2,'Seema','Aurangabad','Developer',20000,20),
(3,'Amita','Nagar','Manager',70000,20),
(4,'Rakesh','Pune','Analyst',8000,10),
(5,'Samata','Nashik','Tester',20000,10),
(6,'Anita','Chandwad','Developer',30000,30),
(7,'Bhavika','Pune','Team-LR',50000,30),
(8,'Deepa','Mumbai','CEO',90000,10),
(9,'Nitin','Nagpur','Clerk',8000,30),
(10,'Pooja','Pune','Analyst',45000,20);
mysql> select * from Emp;
| Eid | Ename | City| Post| Salary | deptno |
| 1 | John | Nashik| Clerk| 5000 | 10 |
| 2 | Seema | Aurangabad | Developer | 20000 | 20 |
| 3 | Amita | Nagar| Manager | 70000 | 20 |
| 4 | Rakesh | Pune| Analyst | 8000 | 10 |
| 5 | Samata | Nashik| Tester | 20000 | 10 |
| 6 | Anita | Chandwad | Developer | 30000 | 30 |
| 7 | Bhavika | Pune| Team-LR | 50000 | 30 |
| 8 | Deepa | Mumbai| CEO| 90000 | 10 |
| 9 | Nitin | Nagpur| Clerk | 8000 | 30 |
| 10 | Pooja | Pune| Analyst | 45000 | 20 |
//Display the information of employees, paid more than ‘pooja' from emp
table
mysql> select * from Emp where salary>(select Salary from Emp where
Ename='Pooja');
+------+---------+--------+---------+--------+--------+
| Eid | Ename | City | Post | Salary | deptno |
+------+---------+--------+---------+--------+--------+
| 3 | Amita | Nagar | Manager | 70000 |20 |
| 7 | Bhavika | Pune | Team-LR | 50000 |30 |
| 8 | Deepa | Mumbai | CEO | 90000 | 10 |
//List the name of the employees, who live in the same city as of ‘Rakesh’
mysql> select * from Emp where City=(select City from Emp where
Ename='Rakesh');
+------+---------+------+---------+--------+--------+
| Eid | Ename | City | Post | Salary | deptno |
+------+---------+------+---------+--------+--------+
| 4 | Rakesh | Pune | Analyst | 8000 | 10 |
| 7 | Bhavika | Pune | Team-LR | 50000 | 30 |
| 10 | Pooja | Pune | Analyst | 45000 | 20 |
// Display the information of employees, paid greater salary than average
salary throughout the company.
mysql> select * from Emp where Salary>=(select avg(Salary) from Emp);
+------+---------+--------+---------+--------+--------+
| Eid | Ename | City | Post | Salary | deptno |
+------+---------+--------+---------+--------+--------+
| 3 | Amita | Nagar | Manager | 70000 | 20 |
| 7 | Bhavika | Pune | Team-LR | 50000 | 30 |
| 8 | Deepa | Mumbai | CEO| 90000 | 10 |
| 10 | Pooja | Pune | Analyst | 45000 | 20 |
// Display the information of employees, paid less salary than average
salary throughout the company.
mysql> select * from Emp where Salary<(select avg(Salary) from Emp);
+------+--------+------------+-----------+--------+--------+
| Eid | Ename | City
| Post
| Salary | deptno |
+------+--------+------------+-----------+--------+--------+
| 1 | John | Nashik | Clerk | 5000 | 10 |
| 2 | Seema | Aurangabad | Developer | 20000 | 20 |
| 4 | Rakesh | Pune| Analyst | 8000 |10 |
| 5 | Samata | Nashik| Tester | 20000 |10 |
| 6 | Anita | Chandwad | Developer | 30000 | 30 |
| 9 | Nitin | Nagpur | Clerk | 8000 | 30 |
//Display the information of employees having maximum salary in
company
mysql> select * from Emp where Salary=(select max(Salary) from Emp);
+------+-------+--------+------+--------+--------+
| Eid | Ename | City | Post | Salary | deptno |
+------+-------+--------+------+--------+--------+
| 8 | Deepa | Mumbai | CEO | 90000 | 10 |
//Display the information of employees having minimum salary in
company
mysql> select * from Emp where Salary=(select min(Salary) from Emp);
+------+-------+--------+-------+--------+--------+
| Eid | Ename | City | Post | Salary | deptno |
+------+-------+--------+-------+--------+--------+
| 1 | John | Nashik | Clerk | 5000 | 10 |
// IN Example- Display the employee name ,salary and department no of
those
employees whose salary is the minimum salary of that department.
mysql> SELECT Ename, salary, deptno FROM EMP WHERE salary IN
( SELECT MIN(salary) FROM emp GROUP BY deptno );
+--------+--------+--------+
| Ename | salary | deptno |
+--------+--------+--------+
| John | 5000 | 10 |
| Seema | 20000 | 20 |
| Rakesh | 8000 | 10 |
| Samata | 20000 | 10 |
| Nitin | 8000 | 30 |
// All Example- Display the employee name, salary and department no of
those employees whose salary is higher than all developers salary.
mysql> SELECT Ename, salary, deptno FROM EMP WHERE salary > All
( SELECT salary FROM emp Where post= 'Developer');
+---------+--------+--------+
| Ename | salary | deptno |
+---------+--------+--------+
| Amita | 70000 | 20 |
| Bhavika | 50000 | 30 |
| Deepa | 90000 | 10 |
| Pooja | 45000 | 20 |
+---------+--------+--------+
4 rows in set (0.00 sec)
// All Example- Display the employee name, salary and department no of
those employees whose salary is lower than all developers salary
mysql> SELECT Ename, salary, deptno FROM EMP WHERE salary < All
( SELECT salary FROM emp Where post= 'Developer'
+--------+--------+--------+
| Ename | salary | deptno |
+--------+--------+--------+
| John | 5000 | 10 |
| Rakesh | 8000 | 10 |
| Nitin | 8000 | 30 |
+--------+--------+--------+
3 rows in set (0.00 sec)
//Any Example- Display the employee name, salary and department no of
those employees whose salary is higher than salary of any developers
salary
mysql> SELECT Ename, salary, deptno FROM EMP WHERE salary >any
( SELECT salary FROM emp Where post= 'Developer')
+---------+--------+--------+
| Ename | salary | deptno |
+---------+--------+--------+
| Amita | 70000 |20 |
| Anita | 30000 |30 |
| Bhavika | 50000 |30 |
| Deepa | 90000 |10 |
| Pooja | 45000 |20 |
+---------+--------+--------+
5 rows in set (0.00 sec)
//Any Example- Display the employee name, salary and department no of
those employees whose salary is less than salary of developers salary.
mysql> SELECT Ename, salary, deptno FROM EMP WHERE salary <any
( SELECT salary FROM emp Where post= 'Developer')
+--------+--------+--------+
| Ename | salary | deptno |
+--------+--------+--------+
| John | 5000 | 10 |
| Seema | 20000 | 20 |
| Rakesh | 8000 | 10 |
| Samata | 20000 | 10 |
| Nitin | 8000 | 30 |
+--------+--------+--------+
//Output:
> show dbs
admin (empty)
local 0.078GB
> use admin
switched to db admin
> db
admin
> db.dropDatabase()
{ "dropped" : "admin", "ok" : 1 }
> db.createCollection('stud')
{ "ok" : 1 }
> show collections
stud
system.indexes
> db.emp.insert({rno:1,name:'Bhavana'})
WriteResult({ "nInserted" : 1 })
> db.emp.insert({name:'Amit',rno:2})
WriteResult({ "nInserted" : 1 })
> db.emp.insert({rno:3, email_id:'[email protected]'})
WriteResult({ "nInserted" : 1 })
> db.emp.find()
{ "_id" : ObjectId("6321a6f722267f027ba09604"), "rno" : 1, "name" :
"Bhavana" }
{ "_id" : ObjectId("6321a70922267f027ba09605"), "name" : "Amit", "rno" :
2}
{ "_id" : ObjectId("6321a71422267f027ba09606"), "rno" : 3, "email_id" :
"[email protected]" }
> db.emp.insert({_id:1,rno:4,name:"Akash"})
WriteResult({ "nInserted" : 1 })
> db.emp.find()
{ "_id" : ObjectId("6321a6f722267f027ba09604"), "rno" : 1, "name" :
"Bhavana" }
{ "_id" : ObjectId("6321a70922267f027ba09605"), "name" : "Amit", "rno" :
2}
{ "_id" : ObjectId("6321a71422267f027ba09606"), "rno" : 3, "email_id" :
"[email protected]" }
{ "_id" : 1, "rno" : 4, "name" : "Akash" }
> db.emp.insert({_id:1,rno:5,name:"Reena"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000
duplicate key error index: admin.emp.$_id_ dup key: { : 1.0 }"
}
})
> E11000 duplicate key error index: db1.emp.$_id_ dup key: { : 1.0 }
2022-09-14T15:36:35.402+0530 SyntaxError: Unexpected identifier
> db.emp.insert ([{rno:7,name:'a'},{rno:8,name:'b'},{rno:8,name:'c' } ] )
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
db.emp.insert({rno:10,name:'Ankit',hobbies:['singing','cricket','swimming']
,age:21})
WriteResult({ "nInserted" : 1 })
>
> db.emp.insert({rno:11, Name: {Fname:"Bhavana", Mname:"Amit",
Lname:"Khivsara"}})
WriteResult({ "nInserted" : 1 })
> db.emp.insert({rno:12, Name: "Janvi", Address:{Flat:501, Building:"Sai
Appart", area:"Tidke colony", city: "Nashik", state:"MH", pin:423101},
age:22})
WriteResult({ "nInserted" : 1 })
> db.emp.insert({rno:15, name:'Ravina', dob: ISODate("2019-09-14")})
WriteResult({ "nInserted" : 1 })
> db.emp.insert(
... {rno:17, name:"Ashika",
... date:Date(),
... awards:[{
... name:"Best c -Designer", year:2010, prize:"winner"},
... {name:"Wen site competition",year:2012,prize:"Runner-up"},
... {name:"Fashion show", year:2015,prize:"winner"
... }],
... city:"Nashik"})
WriteResult({ "nInserted" : 1 })
> db.emp.find().pretty()
{
"_id" : ObjectId("6321a6f722267f027ba09604"),
"rno" : 1,
"name" : "Bhavana"
}
{ "_id" : ObjectId("6321a70922267f027ba09605"), "name" : "Amit", "rno" :
2}
{
"_id" : ObjectId("6321a71422267f027ba09606"),
"rno" : 3,
"email_id" : "[email protected]"
}
{ "_id" : 1, "rno" : 4, "name" : "Akash" }
{ "_id" : ObjectId("6321a7b822267f027ba09607"), "rno" : 7, "name" : "a" }
{ "_id" : ObjectId("6321a7b822267f027ba09608"), "rno" : 8, "name" : "b" }
{ "_id" : ObjectId("6321a7b822267f027ba09609"), "rno" : 8, "name" : "c" }
{
"_id" : ObjectId("6321a7c422267f027ba0960a"),
"rno" : 10,
"name" : "Ankit",
"hobbies" : [
"singing",
"cricket",
"swimming"
],
"age" : 21
}
{
"_id" : ObjectId("6321a7d722267f027ba0960b"),
"rno" : 11,
"Name" : {
"Fname" : "Bhavana",
"Mname" : "Amit",
"Lname" : "Khivsara"
}
}
{
"_id" : ObjectId("6321a7e022267f027ba0960c"),
"rno" : 12,
"Name" : "Janvi",
"Address" : {
"Flat" : 501,
"Building" : "Sai Appart",
"area" : "Tidke colony",
"city" : "Nashik",
"state" : "MH",
"pin" : 423101
},
"age" : 22
}
{
"_id" : ObjectId("6321a7e922267f027ba0960d"),
"rno" : 15,
"name" : "Ravina",
"dob" : ISODate("2019-09-14T00:00:00Z")
}
{
"_id" : ObjectId("6321a7fe22267f027ba0960e"),
"rno" : 17,
"name" : "Ashika",
"date" : "Wed Sep 14 2022 15:37:58 GMT+0530 (IST)",
"awards" : [
{
"name" : "Best c -Designer",
"year" : 2010,
"prize" : "winner"
},
{
"name" : "Wen site competition",
"year" : 2012,
"prize" : "Runner-up"
},
{
"name" : "Fashion show",
"year" : 2015,
"prize" : "winner"
}
],
"city" : "Nashik"
}
> db.stud.insert([{rno:1, name:'Ashiti'}, {rno:2,name:'Savita'},
{rno:3,name:'Sagar'}, {rno:4,name:'Reena'},{rno:5,name:'Jivan'}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 5,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.stud.find()
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
{ "_id" : ObjectId("6321a81a22267f027ba09610"), "rno" : 2, "name" :
"Savita" }
{ "_id" : ObjectId("6321a81a22267f027ba09611"), "rno" : 3, "name" :
"Sagar" }
{ "_id" : ObjectId("6321a81a22267f027ba09612"), "rno" : 4, "name" :
"Reena" }
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
> db.stud.find({rno:5})
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
> db.stud.find({rno:5},{name:1})
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "name" : "Jivan" }
> { "_id" : ObjectId("5d83af5aa44331f62bcd836d"), "name" : "Jivan" }
>
> db.stud.find({rno:5},{name:1,_id:0})
{ "name" : "Jivan" }
> { "name" : "Jivan" }
2022-09-14T15:39:21.534+0530 SyntaxError: Unexpected token :
> db.stud.find({rno:5},{name:1,_id:0})
{ "name" : "Jivan" }
> { "name" : "Jivan" }
2022-09-14T15:40:35.559+0530 SyntaxError: Unexpected token :
> db.stud.find({rno:4},{name:,id:0}){"name":"Akash"}
2022-09-14T15:41:37.704+0530 SyntaxError: Unexpected token ,
> db.stud.find({},{name:1,_id:0})
{ "name" : "Ashiti" }
{ "name" : "Savita" }
{ "name" : "Sagar" }
{ "name" : "Reena" }
{ "name" : "Jivan" }
> { "name" : "Ashiti" }
> db.stud.find({rno:{$gt:2}})
{ "_id" : ObjectId("6321a81a22267f027ba09611"), "rno" : 3, "name" :
"Sagar" }
{ "_id" : ObjectId("6321a81a22267f027ba09612"), "rno" : 4, "name" :
"Reena" }
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
> db.stud.find({rno:{$lte:2}})
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
{ "_id" : ObjectId("6321a81a22267f027ba09610"), "rno" : 2, "name" :
"Savita" }
> db.stud.find({rno:{$ne:2}})
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
{ "_id" : ObjectId("6321a81a22267f027ba09611"), "rno" : 3, "name" :
"Sagar" }
{ "_id" : ObjectId("6321a81a22267f027ba09612"), "rno" : 4, "name" :
"Reena" }
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
> db.stud.find({rno:{$in:[1,3,5]}})
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
{ "_id" : ObjectId("6321a81a22267f027ba09611"), "rno" : 3, "name" :
"Sagar" }
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
> db.stud.find().sort({rno:-1})
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
{ "_id" : ObjectId("6321a81a22267f027ba09612"), "rno" : 4, "name" :
"Reena" }
{ "_id" : ObjectId("6321a81a22267f027ba09611"), "rno" : 3, "name" :
"Sagar" }
{ "_id" : ObjectId("6321a81a22267f027ba09610"), "rno" : 2, "name" :
"Savita" }
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
> db.stud.find().sort({name:1})
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
{ "_id" : ObjectId("6321a81a22267f027ba09612"), "rno" : 4, "name" :
"Reena" }
{ "_id" : ObjectId("6321a81a22267f027ba09611"), "rno" : 3, "name" :
"Sagar" }
{ "_id" : ObjectId("6321a81a22267f027ba09610"), "rno" : 2, "name" :
"Savita" }
> db.stud.find({rno:{$gt:2}},{_id:0}).sort({rno:-1})
{ "rno" : 5, "name" : "Jivan" }
{ "rno" : 4, "name" : "Reena" }
{ "rno" : 3, "name" : "Sagar" }
> db.stud.find()
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
{ "_id" : ObjectId("6321a81a22267f027ba09610"), "rno" : 2, "name" :
"Savita" }
{ "_id" : ObjectId("6321a81a22267f027ba09611"), "rno" : 3, "name" :
"Sagar" }
{ "_id" : ObjectId("6321a81a22267f027ba09612"), "rno" : 4, "name" :
"Reena" }
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
> db.stud.distinct("rno")
[ 1, 2, 3, 4, 5 ]
> [ 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5 ]
> db.stud.find().limit(2)
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
{ "_id" : ObjectId("6321a81a22267f027ba09610"), "rno" : 2, "name" :
"Savita" }
> db.stud.find().skip(2)
{ "_id" : ObjectId("6321a81a22267f027ba09611"), "rno" : 3, "name" :
"Sagar" }
{ "_id" : ObjectId("6321a81a22267f027ba09612"), "rno" : 4, "name" :
"Reena" }
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
> db.stud.find({name:/^A/})
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
> db.stud.find({name:/i$/})
{ "_id" : ObjectId("6321a81a22267f027ba0960f"), "rno" : 1, "name" :
"Ashiti" }
> db.stud.find({name:/a/})
{ "_id" : ObjectId("6321a81a22267f027ba09610"), "rno" : 2, "name" :
"Savita" }
{ "_id" : ObjectId("6321a81a22267f027ba09611"), "rno" : 3, "name" :
"Sagar" }
{ "_id" : ObjectId("6321a81a22267f027ba09612"), "rno" : 4, "name" :
"Reena" }
{ "_id" : ObjectId("6321a81a22267f027ba09613"), "rno" : 5, "name" :
"Jivan" }
> db.stud.findOne()
{
"_id" : ObjectId("6321a81a22267f027ba0960f")
"rno" : 1,
"name" : "Ashiti"
}
> db.stud.find().count()
5
> db.stud.find({rno:{$gt:2}}).count()
3
> db.stud.insert({rno:8,address:{area:"College
Road",city:"Nashik",state:"MH"},name:"Arya"})
WriteResult({ "nInserted" : 1 })
> db.stud.find({"address.city":"Nashik"})
{ "_id" : ObjectId("6321a99722267f027ba09614"), "rno" : 8, "address" :
{ "area" : "College Road", "city" : "Nashik", "state" : "MH" }, "name" :
"Arya" }
> db.stud.insert({rno:9,hobbies:['singing','dancing','cricket']})
WriteResult({ "nInserted" : 1 })
> db.stud.find({hobbies:'dancing'})
{ "_id" : ObjectId("6321a9a822267f027ba09615"), "rno" : 9, "hobbies" :
[ "singing", "dancing", "cricket" ] }
> db.stud.update({rno:1},{$unset:{rno:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.stud.update({rno:2},{$set:{rno:22}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.stud.update({rno:50},{$set:{rno:55}},{upsert:true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("6321a9c92cb29a58d3264181")
})
> //multi:true used to update in multiple documents
> db.stud.update({rno:5},{$set:{rno:15}},{multiple:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.stud.remove({rno:4})
WriteResult({ "nRemoved" : 1 })
> db.stud.remove({rno:4},1)
WriteResult({ "nRemoved" : 0 })
> db.stud.remove({})
WriteResult({ "nRemoved" : 7 })
//OUTPUT:

mysql> use neha;


Database changed
mysql> show tables;
+----------------+
| Tables_in_neha |
+----------------+
| borrower |
| fine |
| persons |
| student |
| student1 |
+----------------+
5 rows in set (0.03 sec)
mysql> select * from borrower;
+---------+-----------+--------------+------------+-------+
| Roll_no | Name | DateofoIssue | NameofBook | Satus |
+---------+-----------+--------------+------------+-------+
| 1 | Neha | 2017-06-25 | Java |I |
| 2 | Shantanu | 2017-07-10 | Networking | I |
| 3 | Nandini | 2017-05-22 | MySql |I |
| 4 | Vaishnavi | 2017-06-10 | DBMS |I |
| 5 | Aniket | 2017-07-05 | WT |I |
| 6 | Komal | 2017-06-30 | AI |I |
+---------+-----------+--------------+------------+-------+
6 rows in set (0.34 sec)
mysql> select * from fine;
Empty set (0.13 sec)
mysql>delimiter $
mysql>create procedure p1(In rno1 int(3),name1 varchar(30))
->begin
->Declare i_date date
->Declare diff int;
->select DateofoIssue into i_date from borrower where Roll_no=rno1 and
NameofBook=name1;
->SELECT DATEDIFF(CURDATE(),i_date)into diff;
->End;
->$
Query OK, 1 row affected (0.13 sec)
mysql> delimiter ;
mysql> call p1(1,'Java');
Query OK, 1 row affected (0.13 sec)
mysql>delimiter $
mysql>create procedure p1(In rno1 int(3),name1 varchar(30))
->begin
->Declare i_date date
->Declare diff int;
->select DateofoIssue into i_date from borrower where Roll_no=rno1 and
NameofBook=name1;
->SELECT DATEDIFF(CURDATE(),i_date)into diff;
->if diff>15 then
->Update borrower
->set satus='R'
->where Roll_no=rno1 and NameofBook=name1;
->End if;
->End;
->End;
->$
Query OK, 1 row affected (0.13 sec)
mysql> delimiter ;
mysql> call p1(1,'Java');
Query OK, 1 row affected (0.13 sec)
mysql> delimiter ;
mysql> call p1(2,'Networking');
Query OK, 1 row affected (0.13 sec)
mysql> select * from borrower;
+---------+-----------+--------------+------------+-------+
| Roll_no | Name | DateofoIssue | NameofBook | Satus |
+---------+-----------+--------------+------------+-------+
| 1 | Neha | 2017-06-25 | Java |R |
| 2 | Shantanu | 2017-07-10 | Networking | R |
| 3 | Nandini | 2017-05-22 | MySql |I |
| 4 | Vaishnavi | 2017-06-10 | DBMS |I |
| 5 | Aniket | 2017-07-05 | WT |I |
| 6 | Komal | 2017-06-30 | AI |I |
+---------+-----------+--------------+------------+-------+
6 rows in set (0.34 sec)
mysql>delimiter ;
mysql>create procedure p1(In rno1 int(3),name1 varchar(30))
->begin
->Declare i_date date
->Declare diff int;
->Declare fine_amt int;
->select DateofoIssue into i_date from borrower where Roll_no=rno1 and
name=name1;
->SELECT DATEDIFF(CURDATE(),i_date)into diff;
->if(diff>=15 and diff<=30)then
->SET fine_amt=diff*5;
->insert into fine values(rno1,CURDATE(),fine_amt);
->elseif(diff>30)then
->SET fine_amt=diff*50;
->insert into fine values(rno1,CURDATE(),fine_amt);
->End if;
->Update borrower set status='R' where Roll_no=rno1 and name=name1;
->End;
Query OK, 1 row affected (0.13 sec)
mysql> delimiter ;
mysql> call P3(1,'Java');
Query OK, 1 row affected (0.00 sec)
mysql> select * from fine;
Empty set (0.00 sec)
//code:
Example 1.
PL/SQL Block :
set autoprint on;
set serveroutput on;
set verify off;
declare cursor cu1 is
select Roll,Name from Student;
cursor cu2 is
select Roll from CompDep;
rno int;
nm varchar(20); rno2 int;
begin
open cu1; open cu2;
loop
fetch cu1 into rno,nm; fetch cu2 into rno2;
exit when cu1%found = false; if rno2 <> rno then
insert into CompDep values(rno,nm); end if;
end loop; close cu1; close cu2;
end;
Output:
SQL> create table CompDep(Roll int,Name varchar(20));
Table created.
SQL> create table Student(Roll int,Name varchar(20));
Table created.
SQL> insert into Student values(1,'a');
1 row created.
SQL> insert into Student values(2,'b');
1 row created.
SQL> insert into Student values(3,'c');
1 row created.
SQL> insert into Student values(4,'d');
1 row created.
SQL> insert into CompDep values(2,'b');
1 row created.
SQL> insert into CompDep values(5,'e');
1 row created.
SQL> insert into CompDep values(6,'f');
1 row created.
SQL> @C:\Users\sitrc\assign6.txt // call PL/SQL block code created above
OR
SQL> delimiter /
PL/SQL procedure successfully completed
SQL> select * from CompDep;
ROLL NAME
2b
e
f
a
b
c
d
7 rows selected.
—-----------------------------------------------------------------------------------------------------
Example 2.
mysql> use Vedant;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database
changed
mysql> createtable o_rollcall(roll_no int,name varchar(20),address
varchar(20));
Query OK, 0 rows affected (0.28 sec)
mysql> createtable n_rollcall(roll_no int,namevarchar(20),address
varchar(20));
Query OK, 0 rows affected (0.27 sec)
mysql> insert into o_rollcall values('1','Hitesh','Nandura');
Query OK, 1 row affected (0.05 sec)
mysql> insert into o_rollcall values('2','Piyush','MP');
Query OK, 1 row affected (0.06 sec)
mysql> insert into o_rollcall values('3','Ashley','Nsk');
Query OK, 1 row affected (0.05 sec)
mysql> insert into o_rollcall values('4','Kalpesh','Dhule');
Query OK, 1 row affected (0.05 sec)
mysql> insert into o_rollcall values('5','Abhi','Satara');
Query OK, 1 row affected (0.04 sec)
mysql> delimiter //
mysql> create procedure p3(in r1 int)
-> begin
-> declare r2 int;
-> declare exit_loop boolean;
-> declare c1 cursor for select roll_no from o_rollcall where roll_no>r1;
-> declare continue handler for not found set exit_loop=true;
-> open c1;
-> e_loop:loop
-> fetch c1 into r2;
-> if not exists(select * from n_rollcall where roll_no=r2)
-> then
-> insert into n_rollcall select * from o_rollcall where roll_no=r2;
-> end if;
-> if exit_loop
-> then
-> close c1;
-> leave e_loop;
-> end if;
-> end loop e_loop;-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> call p3(3);
-> //
Query OK, 0 rows affected (0.10 sec)
mysql> select * from n_rollcall;
-> //
+_______+_______+________+
| roll_no | name | address |
+_______+_______+________+
| 4 | Kalpesh | Dhule |
| 5 | Abhi | Satara |
+_______+_______+________+
2 rows in set (0.00 sec)
mysql> call p3(0);
-> //
Query OK, 0 rows affected (0.22 sec)
mysql> select * from n_rollcall;
-> //
+_______+_______+________+
| roll_no | name | address |
+_______+_______+________+
| 4 | Kalpesh | Dhule |
| 5 | Abhi | Satara |
| 1 | Hitesh | Nandura |
| 2 | Piyush | MP |
| 3 | Ashley | Nsk |
+_______+_______+________+
5 rows in set (0.00 sec)
mysql> insert into o_rollcall values('6','Patil','Kolhapur');
-> //
Query OK, 1 row affected (0.04 sec)
mysql> call p3(4);
-> //
Query OK, 0 rows affected (0.05 sec)
mysql> select * from n_rollcall;
-> //
+_______+_______+________+
| roll_no | name | address |
+_______+_______+________+
| 4 | Kalpesh | Dhule |
| 5 | Abhi | Satara |
| 1 | Hitesh | Nandura |
| 2 | Piyush | MP |
| 3 | Ashley | Nsk |
| 6 | Patil | Kolhapur |
+_______+_______+________+
6 rows in set (0.00 sec)
Code:
config1.php
<?php
define('DB_SERVER', 'localhost'); // Name of Serevr
define('DB_USERNAME', 'root'); // User of Database
define('DB_PASSWORD', '');
// Password
define('DB_DATABASE', 'ndd'); // Datanase Name
$db =
mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABAS
E);
// Check connection
if (!$db)
{
die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected successfully";
?>
simple.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<center> <br> <br> <h3>Registration Form </h3>
<form name="myForm" action="<?php
method="post" enctype="multipart/form-data" >
echo($_SERVER["PHP_SELF"]);?>"
<table>
<tr> <td> <label for="firstName">First Name:</label> </td> <td>
<input
type="text" name="first_name" id="firstName"> </td> <td> <input
type="submit"
name="add" value="search"></td> </tr>
<tr> <td> <label for="lastName">Last Name:</label>
type="text" name="last_name" id="lastName" > </td> </tr>
</td>
<td> <input
<tr> <td> <label for="emailAddress">Email Address:</label> </td>
<td> <input
type="text" name="email" id="emailAddress" > </td></tr>
<tr> <td> <label> Upload Image </label> </td> <td> <input
type="file"
name="fileToUpload" id="fileToUpload"> </td></tr>
</table>
<table>
</tr><td><input type="submit" name="add" value="Submit"> </td>
<td><input
type="submit" name="add" value="delete"></td> <td><input
type="submit"
name="add"
value="update"></td>
<td><input
type="submit"
name="add"
value="display"></td></tr>
</table>
</form>
</center>
<br>
<?php
include("config1.php"); /* file contain the database name user name and
connection
details */
if(isset($_POST['add']))
{
$first_name = mysqli_real_escape_string($db,$_POST['first_name']);
$last_name = mysqli_real_escape_string($db,$_POST['last_name']);
$email = mysqli_real_escape_string($db,$_POST['email']);
$ch = mysqli_real_escape_string($db,$_POST['add']);
$file =
@addslashes(file_get_contents($_FILES["fileToUpload"]["tmp_name"]));
switch ($ch)
{
case "display":
$sql = "SELECT * from persons";
$result = mysqli_query($db, $sql);
?>
<center><h2> Registration </h2> </center>
<center> <table border="2">
<thead>
<tr>
<th>First Name</th>
<th>Last Name </th>
<th>Email Id</th>
<th>Photo</th>
</tr>
</thead>
<tbody>
<?php if (mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
$im= '<img
src="data:image/jpeg;base64,'.base64_encode($row['image']).'"
height="100" width="100"/>'; //store image in varibale
echo
"<tr><td>{$row['first_name']}</td><td>{$row['last_name']}</td><td
>{$row['email']}<
/td><td> $im</td></tr>\n";
}
}
else
{
echo '<tr><td colspan="4">No Rows Returned</td></tr>';
} ?>
</tbody>
</table>
<?php
break;
case "Submit":
$sql = "INSERT INTO persons ". "(first_name,last_name,email,image) ". "
VALUES('$first_name','$last_name','$email','$file')";
$result = mysqli_query($db, $sql);
if ($result !=0)
{
echo "Recored is inserted : $result";
}
break;
case "delete":
$sql = " delete from persons where first_name='$first_name' ";
$result = mysqli_query($db, $sql);
if ($result !=0)
{
echo "Recored is delete : $first_name ";
}
//echo "delete button click ";
break;
case "update":
$sql = "update persons set first_name='$first_name',
last_name='$last_name',email='$email',image='$file' where
first_name='$first_name' ";
$result = mysqli_query($db, $sql);
if ($result !=0)
{
echo "Recored is update : $first_name ";
}
break;
case "search":
$sql = "SELECT * from persons where first_name= '".$first_name."'";
$result = mysqli_query($db, $sql) ;
?>
<center><h2> Registration </h2> </center>
<center> <table border="2">
<thead>
<tr>
<th>First Name</th>
<th>Last Name </th>
<th>Email Id</th>
<th>Photo</th>
</tr>
</thead>
<tbody>
<?php if (mysqli_num_rows($result)!==0)
{
while($row = mysqli_fetch_assoc($result))
{
$im= '<img
src="data:image/jpeg;base64,'.base64_encode($row['image']).'"
height="100" width="100"/>'; //store image in varibale
echo
"<tr><td>{$row['first_name']}</td><td>{$row['last_name']}</td><td
>{$row['email']}<
/td><td> $im</td></tr>\n";
//$im= '<img
src="data:image/jpeg;base64,'.base64_encode($row['image']).'"
height="10" width="50"/>';
}
}
else
{
echo '<tr><td colspan="4">No Record Found ! </td></tr>';
}
?>
</tbody>
</table>
<?php
break;
default:
echo $ch;
}
} ?>
</body>
</html> <!-- end of form -->

You might also like