0% found this document useful (0 votes)
11 views45 pages

Dbms Lab Manual

The document is a laboratory manual for the DBMS course at Vidya Vikas Institute of Engineering & Technology, detailing the code of conduct for faculty, the institute's vision and mission, and the department's objectives. It outlines program outcomes and specific outcomes for computer science graduates, as well as the syllabus for the course, including SQL basics and commands. The manual emphasizes academic integrity, professional ethics, and the importance of fostering a conducive learning environment.

Uploaded by

kishu4725
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)
11 views45 pages

Dbms Lab Manual

The document is a laboratory manual for the DBMS course at Vidya Vikas Institute of Engineering & Technology, detailing the code of conduct for faculty, the institute's vision and mission, and the department's objectives. It outlines program outcomes and specific outcomes for computer science graduates, as well as the syllabus for the course, including SQL basics and commands. The manual emphasizes academic integrity, professional ethics, and the importance of fostering a conducive learning environment.

Uploaded by

kishu4725
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/ 45

VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

VIDYA VIKAS INSTITUTE OF ENGINEERING &


TECHNOLOGY
Department of Computer Science & Engineering

LABORATORY MANUAL

DBMS LABORATORY
BCS403

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

INDEX

1. Code of Conduct

2. Vision and Mission of the Institute

3. Vision and Mission of the Department

4. Program Outcomes

5. Program Specific Outcomes

6. Syllabus

7. Laboratory Manual

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

Code of Conduct

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

VidyaVikas Educational Trust®

1. Code of Conduct
Standards of Conduct
To,
Prof.Namithadevi
NN, Assistant
professor

Department of Computer Science


VVIET, Mysore

Towards the College, the Faculty shall:

a. Read and abide by the policies established by the college


b. Contribute to the college by participating in the committees and clubs at various levels. They may
include Program committees, student clubs, faculty projects etc.,
c. Be asked to serve in any of the Institutions run by The VVET®.
d. Avoid potential conflicts of interest
e. Not engage in outside professional activity that conflicts with their responsibilities and duties in
College.
f. Avoid misrepresentation of personal views as a statement of position of the College.
g. Strictly adhere to the dress code prescribed by the Institution. Women - Sari; Men – formal trousers, shirt
and shoes.
h. Not be found to indulge in smoking, drinking,or other unacceptable behavior within the campus or in its
immediate proximity
i. English should be the language of teaching and communication in class and at all times with the
students.
j. Each faculty member is responsible for the material provided in class during his/her presence inclass.
k. Subsequently, after the class, the room shall be locked and key returned to the department. Keyswill
only be handled by the Faculty and must not be given to students.
l. Faculty must use the infrastructure at their disposal, like video projectors, lap tops etc, as an aid in their
class room teaching.
m. Use of mobile phones is strictly prohibited in the class room and in the corridors of the college. They must
be switched off/ placed in silent mode during class.
n. Arrive at class on time and stay for the duration of time stipulated for the lecture in the time table

ll. Towards Students, The Faculty Shall:

a. Treat all students with fairness and respect.


b. Encourage the free exchange of ideas between themselves and students.
c. Any extra time after the lecture may be used for feedback and subject discussion with the students.
Students will not be allowed to leave before the completion of the time allotted.
d. Conscientiously strive to be fair and balanced in the evaluation of student work.
e. Should not exploit students for personal gain.
f. Should be fair and objective when providing references for students.
g. Should not disclose information about students to outside agencies.
h. Faculties are obliged to attend classes as scheduled and to maintain office hours
i. Faculty should help students and provide access to study material and information
j. Ragging in any form among students is strictly prohibited and such incidents must be brought tothe
notice of the Management for suitable action.

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

III. Towards Colleagues, Faculty Shall:

a. Treat each other with fairness and respect, and conduct themselves with dignity and restraint in all
exchanges with colleagues.
b. Be fair and objective when presenting a professional judgment on their colleagues’ work
c. Not use threatening or abusive behavior or language, verbal harassment or intimidation of another
member of the faculty, under any circumstance while on the college campus.
d. Not knowingly misrepresent the views/positions of colleagues to further their own position orpoint
of view.

IV. Towards their Disciplines, Faculty Shall:

a. Exhibit intellectual honesty and integrity in all scholarly endeavors.


b. Work toward improving the quality of instruction.
c. Participate in all training programs provided to them.
d. Update themselves regularly in the advancements in their academic fields

v. Course Plan and Delivery: Faculty Shall

a. Faculty shall discuss with their Principal/HOD and submit a semester wise Lesson Plan. This will be
followed-up and monitored on a regular basis by the respective Principal/HOD.
b. The HODs/Principals shall make periodic surprise visits to classes to provide feedback on the teaching
methods and materials used. They will also check the diaries the faculty are expected to maintain.

“I have read all the terms and conditions outlined in this document and agree to abide by them”

Name of the Faculty Signature of the Faculty

Prof.Namithadevi NN

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

Institute Vision and Mission

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

2. Institute Vision and Mission

Institute Vision

Our vision is to provide learning opportunities, ensuring excellence in education,


research and facilitate an inspiring world class environment to encourage creativity. The
Institute is committed to disseminating knowledge, and through itsingenuity, bring this
knowledge to bear on the world's great challenges. VVIET is dedicated to providing its
students with an education that combines academic study and the excitement of
discovery kindled by a diverse campus community.

Institute Mission

 Offer highest professional and academic standards in terms of personal growth


and satisfaction, and promote growth and value to our research sponsors.
 Provide students a platform where independent learning and scientific study are
encouraged with emphasis on latest engineering techniques.
 Encourage students to implement applications of engineering with a focus on
societal needs for the betterment of communities.
 Empower students with vast technical and life skills to raise their stakes of getting
placements in top reputed companies.
 Create a benchmark in the areas of research, education and public outreach.

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

Department Vision and


Mission

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

3. Department Vision and Mission

Department Vision

“To produce proficient computer professionals, having essential technical


knowledge and skills, with good work ethics”

Department Mission

M1: To promote growth of an individual by imparting comprehensive knowledge using latest


tools and technologies.

M2: To inculcate professionalism, social awareness and to promote creativity, research


aptitude by mentoring the students.

M3: To establish industry institute interaction, to enable students to cater the ever changing
industry demands and to nurture entrepreneurial qualities.

M4: To provide state-of-the-art environment and opportunities to enhance professional skills.

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

Program Educational
Objectives

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

4. Program Educational Objectives

PEO 1: To provide Graduates of computer science & engineering course with a solid
foundation in the principles and practices of computer science and engineering
enabling them to have successful professional career.

PEO 2: To encourage Graduates of computer science & engineering course to


pursue higher education.

PEO 3: To prepare Graduates of computer science & engineering course to adapt


to technological advancements by engaging in lifelong learning.

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

Program Outcome

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

5. Program Outcomes (POs)


1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,
and an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, research literature, and Analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis ofthe information
to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modelling to complex engineering activities with
an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant tothe professional
engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions
in societal and environmental contexts, and demonstrate the knowledge of, and needfor sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports
and design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering
and management principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

Program Specific
Outcomes

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

6. Program Specific Outcomes

PSO 1: Apply the appropriate programming constructs for solving


engineering problems.
PSO 2: Adapt to software development methodologies
PSO 3: Demonstrate the knowledge towards the domain specific
initiatives of Information Technology.

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

Specification of the Laboratory

SI.NO MAJOR EQUPIMENT/SYSTEM SPECIFICATION

1 Computer CORE I5 8GB RAM 160GB


HDD
17inchMONITOR
2 SOFTWARE Terminal

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

Syllabus

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

7. Syllabus

DATABASE MANAGEMENT
SYSTEM(BCS403)

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

BASIC CONCEPTS OF SQL

Introduction to SQL

SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel –
(Structured English Query Language)”. It is a query language used for accessing and modifying
information in the database. IBM first developed SQL in 1970s. Also, it is an ANSI/ISO
standard. It has become a Standard Universal Language used by most of the relational database
management systems (RDBMS). Some of the RDBMS systems are: Oracle, Microsoft SQL
server, Sybase etc. Most of these have provided their own implementation thus enhancing its
feature and making it a powerful tool. Few of the SQL commands used in SQL programming
are SELECT Statement, UPDATE Statement, INSERT INTO Statement, DELETE Statement,
WHERE Clause, ORDER BY Clause, GROUP BY Clause, ORDER Clause, Joins, Views,
GROUP Functions, Indexes etc.

SQL Commands

SQL commands are instructions used to communicate with the database to perform specific
task that work with data. SQL commands can be used not only for searching the database but
also to perform various other functions like, for example, you can create tables, add data to
tables, or modify data, drop the table, set permissions for users.

CREATE TABLE Statement

The CREATE TABLE Statement is used to create tables to store data. Integrity Constraints like
primary key, unique key and foreign key can be defined for the columns while creating the
table. The integrity constraints can be defined at column level or table level. The
implementation and the syntax of the CREATE Statements differs for different RDBMS.

The Syntax for the CREATE TABLE Statement is:

CREATE TABLE table_name


(column_name1 datatype constraint, column_name2 datatype,... column_nameNdatatype);

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

SQL Data Types:

char(size) Fixed-length character string. Size is specified in parenthesis. Max 255


bytes.
Varchar2(sizee) Variable-length character string. Max size is specified in parenthesis.

number(size)
Number value with a max number of column digits specified in
or int
parenthesis.

Date Date value in „dd-mon-yy‟. Eg., ‟07-jul-2004‟

number (size,d) Number value with a maximum number of digits of "size" total, with a

or real maximum number of "d" digits to the right of the decimal.

SQL Integrity Constraints:

Integrity Constraints are used to apply business rules for the database tables. The
constraintsavailable in SQL are Foreign Key, Primary key, Not Null, Unique, Check.
Constraints can be defined in two ways:
1. The constraints can be specified immediately after the column definition. This
is called column-leveldefinition.
2. The constraints can be specified after all the columns are defined. This is called table-
level definition.
1) Primary key:
This constraint defines a column or combination of columns which
uniquely identifies each row inthe table.
Syntax to define a Primary key at column level:

Column_namedatatype [CONSTRAINT constraint_name] PRIMARY KEY

Syntax to define a Primary key at table level:

[CONSTRAINT constraint_name] PRIMARY KEY(column_name1,

column_name2,..)

2) Foreign key or Referential Integrity:


This constraint identifies any column referencing the PRIMARY KEY in
another table. It establishes a relationship between two columns in the same table
or between different tables. For a column to be defined as a Foreign Key, it should
be a defined as a Primary Key in the table which it is referring. One or more
columns can be defined as foreign key.

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

Syntax to define a foreign key at column level:

[CONSTRAINT constraint_name] REFERENCES

referenced_table_name(column_name)

3) Not Null Constraint:


This constraint ensures all rows in the table contain a definite value
for the column which isspecified as not null. Which means a null value is
not allowed.
Syntax to define a Not Null constraint:
[CONSTRAINT constraint name] NOT NULL

4) Unique Key:
This constraint ensures that a column or a group of columns in each row have
a distinct value.
A column(s) can have a null value but the values cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE

Syntax to define a Unique key at table level:


[CONSTRAINT constraint_name] UNIQUE(column_name)

5) Check Constraint:
This constraint defines a business rule on a column. All the rows
must satisfy this rule. Theconstraint can be applied for a single column or a
group of columns.
Syntax to define a Check constraint:

[CONSTRAINT constraint_name] CHECK (condition)

ALTER TABLE Statement


The SQL ALTER TABLE command is used to modify the definition
structure) of a table by modifying the definition of its columns. The ALTER
command is used to perform the following functions.
1) Add, drop, modify table columns

2) Add and drop constraints

3) Enable and Disable constraints

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

The HAVING clause


The HAVING clause can be used to restrict the display of grouped rows.
The result of the groupedquery is passed on to the HAVING clause for
output filtration.

The INSERT INTO Statement


The INSERT INTO statement is used to insert a new row in a table.

The UPDATE Statement

The UPDATE statement is used to update existing records in a table.

The DELETE Statement

The DELETE statement is used to


delete rows in a table. SQLDELETE

Commit command

Commit command is used to permanently save any transaction into database

Rollback command
This command restores the database to last commited state. It is also use with
savepoint command tojump to a savepoint in a transaction.
Savepoint command

savepoint command is used to temporarily save a transaction so that you can


rollback to that pointwhenever necessary.

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

LAB EXPERIMENTS

1. Create a table called Employee & execute the following. Employee (EMPNO, ENAME,
JOB, MANAGER_NO, SAL, COMMISSION).
1. Create a user and grant all permissions to the user.
2. Insert the any three records in the employee table contains attributes EMPNO, ENAME
JOB, MANAGER_NO, SAL, COMMISSION and use rollback. Check the result.
3. Add primary key constraint and not null constraint to the employee table.
4. Insert null values to the employee table and verify the result.

$ sudo mysql -u root

mysql> CREATE DATABASE COMPANY;


Query OK, 1 row affected (0.14 sec)

mysql> USE COMPANY;


Database changed

1. Creating a table called Employee.

mysql> CREATE TABLE COMPANY.Employee (


-> EMPNO INT,
-> ENAME VARCHAR(255),
-> JOB VARCHAR(255),
-> MANAGER_NO INT,
-> SAL DECIMAL(10, 2),
-> COMMISSION DECIMAL(10, 2)
-> );

Query OK, 0 rows affected (0.91 sec)

mysql> SHOW TABLES;

+ +
| Tables_in_COMPANY |
+ +
| Employee |
+ +
1 row in set (0.00 sec)

mysql> DESC COMPANY.Employee;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| EMPNO | int | YES | | NULL | |
| ENAME | varchar(255) | YES | | NULL | |
| JOB | varchar(255) | YES | | NULL | |
| MANAGER_NO | int | YES | | NULL | |
| SAL | decimal(10,2) | YES | | NULL | |
| COMMISSION | decimal(10,2) | YES | | NULL | |
+ + + + + + +

6 rows in set (0.00 sec)

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

2. Create a user and grant all permissions to the user.

mysql> CREATE USER IF NOT EXISTS 'dbuser'@'localhost' IDENTIFIED BY 'T0p5E(RET';

mysql> GRANT ALL PRIVILEGES ON COMPANY.Employee TO 'dbuser'@'localhost';

3. Insert any three records in the employee table and use rollback.

-- Change the current database to COMPANY

mysql> USE COMPANY;

Database changed

mysql> SELECT * FROM Employee;

Query OK, 0 rows affected (0.00 sec)

-- START A TRANSACTION

mysql> START TRANSACTION;

Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,


COMMISSION)
-> VALUES (1, 'Kavana Shetty', 'Manager', NULL, 5000.00, 1000.00);
Query OK, 1 row affected (0.00 sec)

-- COMMIT DATABASE, db CONTENTS ARE WRITTEN TO THE DISK

mysql> COMMIT;

Query OK, 0 rows affected (0.06 sec)

-- DISPLAY TABLE CONTENTS

mysql> SELECT * FROM Employee;


+-----------+--------------------+------------+-----------------------+----------+ ----------------------+
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION |
+-----------+--------------------+------------+-----------------------+----------+ ----------------------+
| 1 | Kavana Shetty | Manager | NULL | 5000.00 | 1000.00 |
+-----------+--------------------+------------+-----------------------+----------+ ----------------------+
1 row in set (0.00 sec)

-- START ANOTHER TRANSACTION

mysql> START TRANSACTION;

-- INSERT MORE RECORDS

mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,


COMMISSION)
VALUES (2, 'Ram Charan', 'Developer', 1, 4000.00, NULL);

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,


COMMISSION)
VALUES (3, 'Honey Singh', 'Salesperson', 2, 3000.00, 500.00);

mysql> SELECT * FROM Employee;


+ + + + + + +
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION
|
+ + + + + + +
| 1 | Kavana Shetty | Manager | NULL | 5000.00 | 1000.00
|
| 2 | Ram Charan | Developer | 1 | 4000.00 | NULL
|
| 3 | Honey Singh | Salesperson | 2 | 3000.00 | 500.00
|
+ + + + + + +

3 rows in set (0.00 sec)

mysql> DELETE FROM Employee where ENAME = 'Kavana Shetty';


Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM Employee;

+ + + + + + +
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION |
+ + + + + + +
| 2 | Ram Charan | Developer | 1 | 4000.00 | NULL |
| 3 | Honey Singh | Salesperson | 2 | 3000.00 | 500.00 |
+ + + + + + +

2 rows in set (0.00 sec)

-- ROLLBACK 2 INSERTS AND 1 DELETE OPERATIONS

mysql> ROLLBACK;
Query OK, 0 rows affected (0.06 sec)

mysql> SELECT * FROM Employee;


+ + + + + + +
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION |
+ + + + + + +
| 1 | Kavana Shetty | Manager | NULL | 5000.00 | 1000.00 |
+ + + + + + +

4. Add primary key constraint and not null constraint to the employee table.

-- Add Primary Key Constraint

mysql> ALTER TABLE Employee


-> ADD CONSTRAINT pk_employee PRIMARY KEY (EMPNO);
Query OK, 0 rows affected (1.65 sec)

-- verify primary key constraint

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

mysql> DESC Employee;

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| EMPNO | int | NO | PRI | NULL | |
| ENAME | varchar(255) | YES | | NULL | |
| JOB | varchar(255) | YES | | NULL | |
| MANAGER_NO | int | YES | | NULL | |
| SAL | decimal(10,2) | YES | | NULL | |
| COMMISSION | decimal(10,2) | YES | | NULL | |
+ + + + + + +
6 rows in set (0.00 sec)

mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
-> VALUES (1, 'Ranjan', 'Manager', NULL, 5000.00, 1000.00);

ERROR 1062 (23000): Duplicate entry '1' for key 'Employee.PRIMARY'

5. Insert null values into the employee table and verify the result.

-- Add Not Null Constraints

mysql> ALTER TABLE Employee


-> MODIFY ENAME VARCHAR(255) NOT NULL,
-> MODIFY JOB VARCHAR(255) NOT NULL,
-> MODIFY SAL DECIMAL(10, 2) NOT NULL;
Query OK, 0 rows affected (1.08 sec)

mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
-> VALUES (4, 'Ranjan', 'Manager', NULL, 5000.00, 1000.00);
Query OK, 1 row affected (0.16 sec)

mysql>
mysql> SELECT * FROM Employee;
+ + + + + + +
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION |
+ + + + + + +
| 1 | Kavana Shetty | Manager | NULL | 5000.00 | 1000.00 |
| 4 | Ranjan | Manager | NULL | 5000.00 | 1000.00 |
+ + + + + + +
2 rows in set (0.00 sec)

mysql> INSERT INTO Employee (ENAME, JOB, MANAGER_NO, SAL, COMMISSION)


-> VALUES (NULL, 'Tester', NULL, 3500.00, NULL);
ERROR 1048 (23000): Column 'ENAME' cannot be null

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

2. Create a table called Employee that contain attributes EMPNO, ENAME, JOB,
MGR, SAL & execute the following.
1. Add a column commission with domain to the Employee table.
2. Insert any five records into the table.
3. Update the column details of job.
4. Rename the column of Employ table using alter command.
5. Delete the employee whose Empno is 105.

1. Create a table called Employee with attributes EMPNO, ENAME, JOB, MGR, SAL

mysql> CREATE DATABASE COMPANY02;


Query OK, 1 row affected (0.16 sec)

mysql> USE COMPANY02;


Database changed

mysql> CREATE TABLE Employee (


-> EMPNO INT,
-> ENAME VARCHAR(255),
-> JOB VARCHAR(255),
-> MGR INT,
-> SAL DECIMAL(10, 2)
-> );
Query OK, 0 rows affected (0.48 sec)

mysql> SHOW TABLES;


+ +
| Tables_in_COMPANY02 |
+ +
| Employee |
+ +
1 row in set (0.00 sec)

mysql> DESC Employee;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| EMPNO | int | YES | | NULL | |
| ENAME | varchar(255) | YES | | NULL | |
| JOB | varchar(255) | YES | | NULL | |
| MGR | int | YES | | NULL | |
| SAL | decimal(10,2) | YES | | NULL | |
+ + + + + + +
5 rows in set (0.00 sec)

2. Add a column commission with domain to the Employee table.

mysql> ALTER TABLE Employee


-> ADD COLUMN COMMISSION DECIMAL(10, 2);
Query OK, 0 rows affected (0.37 sec)

mysql> DESC Employee;

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| EMPNO | int | YES | | NULL | |
| ENAME | varchar(255) | YES | | NULL | |
| JOB | varchar(255) | YES | | NULL | |
| MGR | int | YES | | NULL | |
| SAL | decimal(10,2) | YES | | NULL | |
| COMMISSION | decimal(10,2) | YES | | NULL | |
+ + + + + + +
6 rows in set (0.00 sec)

3. Insert any five records into the table.

mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)
-> VALUES
-> (101, 'Radha Bai', 'Manager', NULL, 5000.00, 1000.00),
-> (102, 'Krishna Kumar', 'Developer', 101, 4000.00, NULL),
-> (103, 'Abdul Sattar', 'Salesperson', 102, 3000.00, 500.00),
-> (104, 'Bob Johnson', 'Accountant', 101, 4500.00, NULL),
-> (105, 'Amartya Sen', 'HR Manager', 101, 4800.00, 800.00);
Query OK, 5 rows affected (0.12 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM Employee;


+ + + + + + +
| EMPNO | ENAME | JOB | MGR | SAL | COMMISSION |
+ + + + + + +
| 101 | Radha Bai | Manager | NULL | 5000.00 | 1000.00 |
| 102 | Krishna Kumar | Developer | 101 | 4000.00 | NULL |
| 103 | Abdul Sattar | Salesperson | 102 | 3000.00 | 500.00 |
| 104 | Bob Johnson | Accountant | 101 | 4500.00 | NULL |
| 105 | Amartya Sen | HR Manager | 101 | 4800.00 | 800.00 |
+ + + + + + +
5 rows in set (0.00 sec)

4. Update the column details of job

mysql> UPDATE Employee


-> SET JOB = 'Senior Developer'
-> WHERE EMPNO = 102;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM Employee;


+ + + + + + +
| EMPNO | ENAME | JOB | MGR | SAL | COMMISSION
|
+ + + + + + +
| 101 | Radha Bai | Manager | NULL | 5000.00 | 1000.00
|
| 102 | Krishna Kumar | Senior Developer | 101 | 4000.00 | NULL
|
| 103 | Abdul Sattar | Salesperson | 102 | 3000.00 | 500.00
|
| 104 | Bob Johnson | Accountant | 101 | 4500.00 | NULL
|
| 105 | Amartya Sen | HR Manager | 101 | 4800.00 | 800.00
|
+ + + + + +

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

5 rows in set (0.00 sec)

5. Rename the column of Employee table using alter command.

mysql> ALTER TABLE Employee


-> CHANGE COLUMN MGR MANAGER_ID INT;
Query OK, 0 rows affected (0.30 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC Employee;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| EMPNO | int | YES | | NULL | |
| ENAME | varchar(255) | YES | | NULL | |
| JOB | varchar(255) | YES | | NULL | |
| MANAGER_ID | int | YES | | NULL | |
| SAL | decimal(10,2) | YES | | NULL | |
| COMMISSION | decimal(10,2) | YES | | NULL | |
+ + + + + + +
6 rows in set (0.00 sec)

6. Delete the employee whose Empno is 105.

mysql> DELETE FROM Employee


-> WHERE EMPNO = 105;
Query OK, 1 row affected (0.14 sec)

mysql> SELECT * FROM Employee;


+ + + + + + +
| EMPNO | ENAME | JOB | MANAGER_ID | SAL | COMMISSION
|
+ + + + + + +
| 101 | Radha Bai | Manager | NULL | 5000.00 | 1000.00
|
| 102 | Krishna Kumar | Senior Deve | 101 | 4000.00 | NULL
|
| 103 | Abdul Sattar | Salesperson | 102 | 3000.00 | 500.00
|
| 104 | Bob Johnson | Accountant | 101 | 4500.00 | NULL
|
+ + + + + + +
4 rows in set (0.00 sec)

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

3. Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM), Group by, Order by.
Employee (E_id, E_name, Age, Salary)
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
2. Count number of employee names from employee table
3. Find the Maximum age from employee table.
4. Find the Minimum age from employee table.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.

1. Create Employee table containing all Records E_id, E_name, Age, Salary.

mysql> CREATE DATABASE COMPANY03;


Query OK, 1 row affected (0.09 sec)

mysql> USE COMPANY03;


Database changed

mysql> CREATE TABLE Employee (


-> E_id INT PRIMARY KEY,
-> E_name VARCHAR(255),
-> Age INT,
-> Salary DECIMAL(10, 2)
-> );
Query OK, 0 rows affected (1.00 sec)

mysql> DESC Employee;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| E_id | int | NO | PRI | NULL | |
| E_name | varchar(255) | YES | | NULL | |
| Age | int | YES | | NULL | |
| Salary | decimal(10,2) | YES | | NULL | |
+ + + + + + +
4 rows in set (0.00 sec)

mysql> INSERT INTO Employee (E_id, E_name, Age, Salary)


-> VALUES
-> (1, 'Samarth', 30, 50000.00),
-> (2, 'Ramesh Kumar', 25, 45000.00),
-> (3, 'Seema Banu', 35, 60000.00),
-> (4, 'Dennis Anil', 28, 52000.00),
-> (5, 'Rehman Khan', 32, 58000.00),
-> (6, 'Pavan Gowda', 40, 70000.00),
-> (7, 'Shruthi Bhat', 27, 48000.00),
-> (8, 'Sandesh Yadav', 29, 51000.00),
-> (9, 'Vikram Acharya', 33, 62000.00),
-> (10, 'Praveen Bellad', 26, 46000.00),
-> (11, 'Sophia Mary', 31, 55000.00),
-> (12, 'Darshan Desai', 34, 63000.00);
Query OK, 12 rows affected (0.14 sec)
Records: 12 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM Employee;

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

+ + + + +
| E_id | E_name | Age | Salary |
+ + + + +
| 1 | Samarth | 30 | 50000.00 |
| 2 | Ramesh Kumar | 25 | 45000.00 |
| 3 | Seema Banu | 35 | 60000.00 |
| 4 | Dennis Anil | 28 | 52000.00 |
| 5 | Rehman Khan | 32 | 58000.00 |
| 6 | Pavan Gowda | 40 | 70000.00 |
| 7 | Shruthi Bhat | 27 | 48000.00 |
| 8 | Sandesh Yadav | 29 | 51000.00 |
| 9 | Vikram Acharya | 33 | 62000.00 |
| 10 | Praveen Bellad | 26 | 46000.00 |
| 11 | Sophia Mary | 31 | 55000.00 |
| 12 | Darshan Desai | 34 | 63000.00 |
+ + + + +
12 rows in set (0.00 sec)

2. Count number of employee names from employee table

mysql> SELECT COUNT(E_name) AS TotalEmployees


-> FROM Employee;
+ +
| TotalEmployees |
+ +
| 12 |
+ +
1 row in set (0.00 sec)

3. Find the Maximum age from employee table.

mysql> SELECT MAX(Age) AS MaxAge


-> FROM Employee;
+ +
| MaxAge |
+ +
| 40 |
+ +
1 row in set (0.01 sec)

4. Find the Minimum age from employee table.

mysql> SELECT MIN(Age) AS MinAge


-> FROM Employee;
+ +
| MinAge |
+ +
| 25 |
+ +
1 row in set (0.00 sec)

5. Find salaries of employee in Ascending Order.

mysql> SELECT E_name, Salary


-> FROM Employee
-> ORDER BY Salary ASC;

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

+ + +
| E_name | Salary |
+ + +
| Ramesh Kumar | 45000.00 |
| Praveen Bellad | 46000.00 |
| Shruthi Bhat | 48000.00 |
| Samarth | 50000.00 |
| Dennis Anil | 52000.00 |
| Sandesh Yadav | 52000.00 |
| Sophia Mary | 55000.00 |
| Rehman Khan | 58000.00 |
| Seema Banu | 62000.00 |
| Vikram Acharya | 62000.00 |
| Darshan Desai | 63000.00 |
| Pavan Gowda | 70000.00 |
+ + +
12 rows in set (0.00 sec)

6. Find grouped salaries of employees

mysql> SELECT Salary, COUNT(*) AS EmployeeCount


-> FROM Employee
-> GROUP BY Salary;
+ + +
| Salary | EmployeeCount |
+ + +
| 50000.00 | 1 |
| 45000.00 | 1 |
| 62000.00 | 2 |
| 52000.00 | 2 |
| 58000.00 | 1 |
| 70000.00 | 1 |
| 48000.00 | 1 |
| 46000.00 | 1 |
| 55000.00 | 1 |
| 63000.00 | 1 |
+ + +
10 rows in set (0.00 sec)

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

4. Create a row level trigger for the customers table that would fire for INSERT or UPDATE or
DELETE operations performed on the CUSTOMERS table. This trigger will display the
salary difference between the old & new Salary. CUSTOMERS (ID, NAME, AGE,
ADDRESS, SALARY)

1. Create the CUSTOMERS Table

Mysql> CREATE DATABASE COMPANY04;


Query OK, 1 Row Affected (0.14 Sec)

Mysql> USE COMPANY04;


Database Changed

Mysql> CREATE TABLE CUSTOMERS (


-> ID INT PRIMARY KEY AUTO_INCREMENT,
-> NAME VARCHAR(255),
-> AGE INT,
-> ADDRESS VARCHAR(255),
-> SALARY DECIMAL(10, 2)
-> );
Query OK, 0 Rows Affected (0.49 Sec)

2. Create Trigger for INSERT Operation

-- INSERT TRIGGER
DELIMITER //

CREATE TRIGGER After_Insert_Salary_Difference


AFTER INSERT ON CUSTOMERS
FOR EACH ROW
BEGIN
SET @My_Sal_Diff = CONCAT('Salary Inserted Is ', NEW.SALARY);
END;//

DELIMITER ;

3. Create Trigger for UPDATE Operation

-- UPDATE TRIGGER
DELIMITER //

CREATE TRIGGER after_update_salary_difference


AFTER UPDATE ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE old_salary DECIMAL(10, 2);
DECLARE new_salary DECIMAL(10, 2);

SET old_salary = OLD.SALARY;


SET new_salary = NEW.SALARY;
SET @my_sal_diff = CONCAT('salary difference after update is ', NEW.SALARY OLD.SALARY);
END;//

DELIMITER ;

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

4. Create Trigger for DELETE Operation

-- DELETE TRIGGER
DELIMITER //

CREATE TRIGGER after_delete_salary_difference


AFTER DELETE ON CUSTOMERS
FOR EACH ROW
BEGIN
SET @my_sal_diff = CONCAT('salary deleted is ', OLD.SALARY);
END;//

DELIMITER ;

5. Testing the Trigger:

mysql> -- test INSERT TRIGGER


mysql> INSERT INTO CUSTOMERS (NAME, AGE, ADDRESS, SALARY)
-> VALUES ('Shankara', 35, '123 Main St', 50000.00);
Query OK, 1 row affected (0.14 sec)

mysql>
mysql> SELECT @my_sal_diff AS SAL_DIFF;
+ -------------------------------- +
| SAL_DIFF |
+ -------------------------------- +
| salary inserted is 50000.00 |
+ -------------------------------- +
1 row in set (0.00 sec)

mysql> -- test UPDATE TRIGGER


mysql> UPDATE CUSTOMERS
-> SET SALARY = 55000.00
-> WHERE ID = 1;
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT @my_sal_diff AS SAL_DIFF;


+ ------------------------------------------------- +
| SAL_DIFF |
+ ------------------------------------------------- +
| salary difference after update is 5000.00 |
+ ------------------------------------------------- +
1 row in set (0.00 sec)

mysql> -- test DELETE TRIGGER


mysql> DELETE FROM CUSTOMERS
-> WHERE ID = 1;
Query OK, 1 row affected (0.13 sec)
mysql> SELECT @my_sal_diff AS SAL_DIFF;
+ ------------------------------ +
| SAL_DIFF |
+ ------------------------------ +
| salary deleted is 55000.00 |
+ ------------------------------- +
1 row in set (0.00 sec)

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

5. Create cursor for Employee table & extract the values from the table. Declare the variables
,Open the cursor & extrct the values from the cursor. Close the cursor. Employee(E_id,
E_name, Age, Salary)

1. Create the Employee table

CREATE DATABASE COMPANY05;

USE COMPANY05;

CREATE TABLE Employee (


E_id INT,
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);

2. Insert some sample data into the Employee table

INSERT INTO Employee (E_id, E_name, Age, Salary)


VALUES
(1, 'Samarth', 30, 50000.00),
(2, 'Ramesh Kumar', 25, 45000.00),
(3, 'Seema Banu', 35, 62000.00),
(4, 'Dennis Anil', 28, 52000.00),
(5, 'Rehman Khan', 32, 58000.00);

3. Create a Stored Procedure with Cursor

DELIMITER //

CREATE PROCEDURE fetch_employee_data()


BEGIN
-- Declare variables to store cursor values

DECLARE emp_id INT;


DECLARE emp_name VARCHAR(255);
DECLARE emp_age INT;
DECLARE emp_salary DECIMAL(10, 2);

-- Declare a cursor for the Employee table

DECLARE emp_cursor CURSOR FOR


SELECT E_id, E_name, Age, Salary
FROM Employee;

-- Declare a continue handler for the cursor

DECLARE CONTINUE HANDLER FOR NOT FOUND


SET @finished = 1;

-- Open the cursor

OPEN emp_cursor;

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

-- Initialize a variable to control cursor loop

SET @finished = 0;

-- Loop through the cursor results

cursor_loop: LOOP

-- Fetch the next row from the cursor into variables

FETCH emp_cursor INTO emp_id, emp_name, emp_age, emp_salary;

-- Check if no more rows to fetch

IF @finished = 1 THEN
LEAVE cursor_loop;
END IF;

-- Output or process each row (for demonstration, print the values)

SELECT CONCAT('Employee ID: ', emp_id, ', Name: ', emp_name, ', Age: ', emp_age, ', Salary:
',emp_salary) AS Employee_Info;
END LOOP;

-- Close the cursor

CLOSE emp_cursor;
END//

DELIMITER ;

4. Execute the Stored Procedure

mysql> CALL fetch_employee_data();

+ --------------------------------------------------------------------------- +
| Employee_Info |
+ --------------------------------------------------------------------------- +
| Employee ID: 1, Name: Samarth, Age: 30, Salary: 50000.00 |
+ --------------------------------------------------------------------------- +
1 row in set (0.07 sec)

+ ------------------------------------------------------------------------------------ +
| Employee_Info |
+ ------------------------------------------------------------------------------------ +
| Employee ID: 2, Name: Ramesh Kumar, Age: 25, Salary: 45000.00 |
+ ------------------------------------------------------------------------------------ +
1 row in set (0.07 sec)

+ ------------------------------------------------------------------------------- +
| Employee_Info |
+ ------------------------------------------------------------------------------- +
| Employee ID: 3, Name: Seema Banu, Age: 35, Salary: 62000.00 |
+ ------------------------------------------------------------------------------- +
1 row in set (0.07 sec)

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

+ ------------------------------------------------------------------------------ +
| Employee_Info |
+ ------------------------------------------------------------------------------ +
| Employee ID: 4, Name: Dennis Anil, Age: 28, Salary: 52000.00 |
+ ------------------------------------------------------------------------------ +
1 row in set (0.07 sec)

+ ---------------------------------------------------------------------------------- +
| Employee_Info |
+ ---------------------------------------------------------------------------------- +
| Employee ID: 5, Name: Rehman Khan, Age: 32, Salary: 58000.00 |
+ ---------------------------------------------------------------------------------- +
1 row in set (0.07 sec)

Query OK, 0 rows affected (0.07 sec)

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

6. Write a PL/SQL block of code using parameterized Cursor, that will merge the data available
in the newly created table N_RollCall with the data available in the table O_RollCall. If the
data in the first table already exist in the second table then that data should be skipped.

1. Create the Tables

CREATE DATABASE ROLLCALL;

USE ROLLCALL;

-- Create N_RollCall table


CREATE TABLE N_RollCall (
student_id INT PRIMARY KEY,
student_name VARCHAR(255),
birth_date DATE
);

-- Create O_RollCall table with common data


CREATE TABLE O_RollCall (
student_id INT PRIMARY KEY,
student_name VARCHAR(255),
birth_date DATE
);

2. Add Sample Records to both tables

mysql> -- Insert common data into O_RollCall


mysql> INSERT INTO O_RollCall (student_id, student_name, birth_date)
-> VALUES
-> (1, 'Shivanna', '1995-08-15'),
-> (3, 'Cheluva', '1990-12-10');
Query OK, 2 rows affected (0.17 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> -- Insert sample records into N_RollCall


mysql> INSERT INTO N_RollCall (student_id, student_name, birth_date)
-> VALUES
-> (1, 'Shivanna', '1995-08-15'), -- Common record with O_RollCall
-> (2, 'Bhadramma', '1998-03-22'),
-> (3, 'Cheluva', '1990-12-10'), -- Common record with O_RollCall
-> (4, 'Devendra', '2000-05-18'),
-> (5, 'Eshwar', '1997-09-03');
Query OK, 5 rows affected (0.21 sec)
Records: 5 Duplicates: 0 Warnings: 0

3. Define the Stored Procedure

DELIMITER //

CREATE PROCEDURE merge_rollcall_data()


BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE n_id INT;
DECLARE n_name VARCHAR(255);
DECLARE n_birth_date DATE;

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

-- Declare cursor for N_RollCall table


DECLARE n_cursor CURSOR FOR
SELECT student_id, student_name, birth_date
FROM N_RollCall;

-- Declare handler for cursor


DECLARE CONTINUE HANDLER FOR NOT FOUND
SET done = TRUE;

-- Open the cursor


OPEN n_cursor;

-- Start looping through cursor results


cursor_loop: LOOP
-- Fetch data from cursor into variables
FETCH n_cursor INTO n_id, n_name, n_birth_date;

-- Check if no more rows to fetch


IF done THEN
LEAVE cursor_loop;
END IF;

-- Check if the data already exists in O_RollCall


IF NOT EXISTS (
SELECT 1
FROM O_RollCall
WHERE student_id = n_id
) THEN
-- Insert the record into O_RollCall
INSERT INTO O_RollCall (student_id, student_name, birth_date)
VALUES (n_id, n_name, n_birth_date);
END IF;
END LOOP;

-- Close the cursor


CLOSE n_cursor;
END//

DELIMITER ;

4. Execute the Stored Procedure

mysql> CALL merge_rollcall_data();


Query OK, 0 rows affected (0.87 sec)

5. Verify Records in O_RollCall

mysql> -- Select all records from O_RollCall

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

mysql> SELECT * FROM O_RollCall;

+ + + +
| student_id | student_name | birth_date |
+ + + +
| 1 | Shivanna | 1995-08-15 |<- Common record,not duplicated
| 2 | Bhadramma | 1998-03-22 |<- New record from N_RollCall
| 3 | Cheluva | 1990-12-10 |<- Common record,not duplicated
| 4 | Devendra | 2000-05-18 |<- New record from N_RollCall
| 5 | Eshwar | 1997-09-03 |<- New record from N_RollCall
+ + + +
5 rows in set (0.00 sec)

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

7. Install an Open Source NoSQL Data base MangoDB & perform basic CRUD(Create, Read,
Update & Delete) operations. Execute MangoDB basic Queries using CRUD operations.

1. Installing Open Source NoSQL Data base MongoDB

Please refer to the link below which contains detailed procedure of installing Open Source NoSQL Data
base MongoDB.

https://moodle.sit.ac.in/blog/setting-up-mongodb-on-ubuntu/

2. Perform basic CRUD(Create, Read, Update & Delete) operations.

 Start MongoDB.

sudo systemctl start mongod

 Start the MongoDB Shell

Mongosh

 Switch to a Database (Optional):

test> use bookDB


switched to db bookDB
bookDB>

 Create the Programming Books Collection:

bookDB> db.createCollection("ProgrammingBooks")

 INSERT operations

a. Insert 5 Documents into the ProgrammingBooks Collection :

bookDB> db.ProgrammingBooks.insertMany([
{
title: "Clean Code: A Handbook of Agile Software Craftsmanship",
author: "Robert C. Martin",
category: "Software Development",
year: 2008
},
{
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
category: "JavaScript",
year: 2008
},
{
title: "Design Patterns: Elements of Reusable Object-Oriented Software",
author: "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides",
category: "Software Design",
year: 1994
},

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

{
title: "Introduction to Algorithms",
author: "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein",
category: "Algorithms",
year: 1990
},
{
title: "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",
author: "Eric Matthes",
category: "Python",
year: 2015
}
])

b. Insert a Single Document into ProgrammingBooks:

bookDB> db.ProgrammingBooks.insertOne({
title: "The Pragmatic Programmer: Your Journey to Mastery",
author: "David Thomas, Andrew Hunt",
category: "Software Development",
year: 1999
})

 Read (Query) Operations

a. Find All Documents

bookDB> db.ProgrammingBooks.find().pretty()
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e1'),
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Software Design',
year: 1994
},
{
_id: ObjectId('663eaaebae582498972202e2'),
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein',
category: 'Algorithms',
year: 1990
},

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

{
_id: ObjectId('663eaaebae582498972202e3'),
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
},
{
_id: ObjectId('663eab05ae582498972202e4'),
title: 'The Pragmatic Programmer: Your Journey to Mastery',
author: 'David Thomas, Andrew Hunt',
category: 'Software Development',
year: 1999
}
]

b. Find Documents Matching a Condition

bookDB> db.ProgrammingBooks.find({ year: { $gt: 2000 } }).pretty()


[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e3'),
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
}
]

 Update Operations

a. Update a Single Document

bookDB>db.ProgrammingBooks.updateOne(
{ title: "Clean Code: A Handbook of Agile Software Craftsmanship" },
{ $set: { author: "Robert C. Martin (Uncle Bob)" } }
)

//verify by displaying books published in year 2008


bookDB> db.ProgrammingBooks.find({ year: { $eq: 2008 } }).pretty()
[

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin (Uncle Bob)',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
}
]

b. Update Multiple Documents

bookDB> db.ProgrammingBooks.updateMany(
{ year: { $lt: 2010 } },
{ $set: { category: "Classic Programming Books" } }
)

//verify the update operation by displaying books published before year 2010
bookDB> db.ProgrammingBooks.find({ year: { $lt: 2010 } }).pretty()
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin (Uncle Bob)',
category: 'Classic Programming Books',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'Classic Programming Books',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e1'),
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Classic Programming Books',
year: 1994
},
{
_id: ObjectId('663eaaebae582498972202e2'),
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford
Stein',
category: 'Classic Programming Books',
year: 1990
},

VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

{
_id: ObjectId('663eab05ae582498972202e4'),
title: 'The Pragmatic Programmer: Your Journey to Mastery',
author: 'David Thomas, Andrew Hunt',
category: 'Classic Programming Books',
year: 1999
}
]

 Delete Operations

a. Delete a Single Document

bookDB> db.ProgrammingBooks.deleteOne({ title: "JavaScript: The Good Parts" })


{ acknowledged: true, deletedCount: 1 }

b. Delete Multiple Documents

bookDB> db.ProgrammingBooks.deleteMany({ year: { $lt: 1995 } })


{ acknowledged: true, deletedCount: 2 }

c. Delete All Documents in the Collection:

//delete all documents in a collection


bookDB> db.ProgrammingBooks.deleteMany({})
{ acknowledged: true, deletedCount: 3 }

//verify by displaying the collection


bookDB> db.ProgrammingBooks.find().pretty()

 Delete the Collection Using drop():

bookDB> show collections


ProgrammingBooks

bookDB> db.ProgrammingBooks.drop()
true

bookDB> show collections

bookDB>

VVIET, Mysuru

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy