XAMPP PL/SQL INSERT, UPDATE, DELETE & SELECT INTO [ SET 1]

7 Likes Comment

In this tutorial, we are going to learn how to use SQL in PL/SQL. SQL is the actual component that takes care of fetching and updating of data in the database whereas PL/SQL is the component that processes these data. Further, in this article, we will also discuss how to combine the SQL within the PL/SQL block.

DML Transactions in PL/SQL

DML stands for Data Manipulation Language. These statements are mainly used to perform the manipulation activity. It deals with the below operations.

  • Data Insertion
  • Data Update
  • Data Deletion
  • Data Selection

In PL/SQL, we can do the data manipulation only by using the SQL commands.

Data Insertion

In PL/SQL, we can insert the data into any table using the SQL command INSERT INTO. This command will take the table name, table column and column values as the input and insert the value in the base table.

The INSERT command can also take the values directly from another table using ‘SELECT’ statement rather than giving the values for each column. Through ‘SELECT’ statement, we can insert as many rows as the base table contains.

Syntax:

  • The above syntax shows the INSERT INTO command. The table name and values are a mandatory fields, whereas column names are not mandatory if the insert statements have values for all the column of the table.
  • The keyword ‘VALUES’ is mandatory if the values are given separately as shown above.

Syntax:

  • The above syntax shows the INSERT INTO command that takes the values directly from the <table_name2> using the SELECT command.
  • The keyword ‘VALUES’ should not be present in this case as the values are not given separately.

Data Update

Data update simply means an update of the value of any column in the table. This can be done using ‘UPDATE’ statement. This statement takes the table name, column name and value as the input and updates the data.

Syntax:

  • The above syntax shows the UPDATE. The keyword ‘SET’ instruct that PL/SQL engine to update the value of the column with the value given.
  • ‘WHERE’ clause is optional. If this clause is not given, then the value of the mentioned column in the entire table will be updated.

Data Deletion

Data deletion means to delete one full record from the database table. The ‘DELETE’ command is used for this purpose.

Syntax:

  • The above syntax shows the DELETE command. The keyword ‘FROM’ is optional and with or without ‘FROM’ clause the command behaves in the same way.
  • ‘WHERE’ clause is optional. If this clause is not given, then the entire table will be deleted.

Data Selection

Data projection/fetching means to retrieve the required data from the database table. This can be achieved by using the command ‘SELECT’ with ‘INTO’ clause. The ‘SELECT’ command will fetch the values from the database, and ‘INTO’ clause will assign these values to the local variable of the PL/SQL block.

Below are the points that need to be considered in ‘SELECT’ statement.

  • ‘SELECT’ statement should return only one record while using ‘INTO’ clause as one variable can hold only one value. If the ‘SELECT’ statement returns more than one value than ‘TOO_MANY_ROWS’ exception will be raised.
  • ‘SELECT’ statement will assign the value to the variable in the ‘INTO’ clause, so it needs to get at least one record from the table to populate the value. If it didn’t get any record, then the exception ‘NO_DATA_FOUND’ is raised.
  • The number of columns and their datatype in ‘SELECT’ clause should match with the number of variables and their datatypes in the ‘INTO’ clause.
  • The values are fetched and populated in the same order as mentioned in the statement.
  • ‘WHERE’ clause is optional that allows to having more restriction on the records that are going to be fetched.
  • ‘SELECT’ statement can be used in the ‘WHERE’ condition of other DML statements to define the values of the conditions.
  • The ‘SELECT’ statement when using ‘INSERT’, ‘UPDATE’, ‘DELETE’ statements should not have ‘INTO’ clause as it will not populate any variable in these cases.

EXAMPLE SET 1 :

Create the following table

  1. Student {roll, name, address, ph_no, DOB, MA, HS, DOA, Stream, C_num}

Write the following queries

i) Describe Student table

ii) Insert 6 values into these table. (Two values using method1, method2, method3)

iii) Add a column name FEES

iv) Insert 20,000 for BCA students and 18000 for BBA students.

v) Remove C_num attribute from student Database.

vi) Make Roll As Primary Key

vii) Change the database range varchar(10) of name attribute to varchar(20).

viii) Change the FIELD name “MA” to “CLASS 10”.

Syntax in xampp:

ALTER TABLE tableName CHANGE “oldcolname” “newcolname” datatype(length);

ix) Find the details of the student who are from Burdwan.

x) Show the students with marks who admitted in BCA course.

xi) Show the address field, not show duplicate address.

xii) Delete the student roll_num 70

xiii) Delete all values from Student table

xiv) Remove student table

YOU MIGHT LIKE :

You might like

Saif

About the Author: Saif

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami