Unit Iiii (RDBMS)
Unit Iiii (RDBMS)
Oracle Tables: DDL - Working with tables: Data Manipulation and Retrieval – Working with
Tables: Function and Grouping - Multiple tables: join and set operators
ALTER Command
ALTER is a DDL command which changes or modifies the existing structure of the database,
and it also changes the schema of database objects.
Example 1: This example shows how to add a new field to the existing table.
Suppose, you want to remove the Age and Marks column from the existing Student table. To
do this, you have to write the following DDL command:
Suppose, you want to change the character size of the Last_Namefield of the Student table.
To do this, you have to write the following DDL command:
TRUNCATE Command
TRUNCATE is another DDL command which deletes or removes all the records from the
table.
This command also removes the space allocated for storing the table records.
Example
Suppose, you want to delete the record of the Student table. To do this, you have to write the
following TRUNCATE DDL command:
RENAME Command
RENAME is a DDL command which is used to change the name of the database table.
Example
RENAME TABLE Student TO Student_Details ;
Data Manipulation Language (DML) in SQL
The SQL commands that deal with the manipulation of data present in the database
belong to DML or Data Manipulation Language and this includes most of the SQL
statements.
SQL INSERT INTO Statement
The INSERT INTO statement is a fundamental SQL command used to add new rows of data
into a table in a database. It is one of the most commonly used SQL statements for
manipulating data and plays a key role in database management.
This article will explore the SQL INSERT INTO statement in detail, showing how to use it
effectively with multiple examples. We’ll cover both basic and advanced usage, including
inserting individual rows, multiple rows, and even copying data between tables.
SQL INSERT INTO Statement
The INSERT INTO statement in SQL is used to add new rows of data to a table in a
database.
There are two main ways to use the INSERT INTO statement by specifying the columns
and values explicitly or by inserting values for all columns without specifying them.
There are two primary syntaxes of INSERT INTO statements depending on the
requirements. The two syntaxes are:
Syntax 1. Only Values
The first method is to specify only the value of data to be inserted without the column names.
INSERT INTO table_name
VALUES (value1, value2, value);
Parameters:
table_name: name of the table.
value1, value2: value of first column, second column,… for the new record
Syntax 2. Column Names And Values Both
In the second method we will specify both the columns which we want to fill and their
corresponding values as shown below:
INSERT INTO table_name (column1, column2, column3)
VALUES ( value1, value2, value);
Parameters:
table_name: name of the table.
column1, column2..: name of first column, second column.
value1, value2, value..: the values for each specified column of the new record.
Examples of SQL INSERT INTO
For better understanding, let’s look at the SQL Server INSERT statement with examples.
Let us first create a table named ‘Student‘.
CREATE DATABASE StudentDB;
USE StudentDB;
CREATE TABLE Student (
ROLL_NO INT PRIMARY KEY,
NAME VARCHAR(50),
ADDRESS VARCHAR(100),
PHONE VARCHAR(15),
AGE INT
);
INSERT INTO Student (ROLL_NO, NAME, ADDRESS, PHONE, AGE) VALUES
(1, 'Ram', 'Delhi', 'XXXXXXXXXX', 18),
(2, 'Ramesh', 'Gurgaon', 'XXXXXXXXXX', 18),
(3, 'Sujit', 'Rohtak', 'XXXXXXXXXX', 20),
(4, 'Suresh', 'Rohtak', 'XXXXXXXXXX', 18);
Inserting Only New Values Using INSERT INTO Example
If we want to insert only values then we use the following query.
Query:
INSERT INTO Student
VALUES ('5','HARSH','WEST BENGAL', 'XXXXXXXXXX','19');
Insert Values to Specified Columns Using INSERT INTO Example
If we want to insert values in the specified columns then we use the following query.
Query:
INSERT INTO Student (ROLL_NO, NAME, Age)
VALUES ('5','PRATIK','19');
UPDATE Statement in SQL
The UPDATE statement in SQL is used to update the data of an existing table in the
database. We can update single columns as well as multiple columns using the UPDATE
statement as per our requirement.
Syntax:
The syntax for SQL UPDATE Statement is :
UPDATE table_name SET column1 = value1, column2 = value2,…
WHERE condition;
Where,
table_name: name of the table
column1: name of first, second, third column….
value1: new value for first, second, third column….
condition: condition to select the rows for which the
Parameter Explanation
1. UPDATE: Command is used to update the column value in the table.
2. WHERE: Specifies the condition which we want to implement on the table.
Example 1: Update Single Column Using UPDATE Statement
Update the column NAME and set the value to ‘Nitin’ in the rows where the Age is 22.
Query:
UPDATE Customer
SET CustomerName = 'Nitin'
WHERE Age = 22;
UPDATE Customer
SET
CustomerName = 'Satyam',
Country = 'USA'
WHERE CustomerID = 1;
Omitting WHERE Clause in UPDATE Statement
If we omit the WHERE clause from the update query then all of the rows will get updated.
Query:
UPDATE Customer SET CustomerName = 'Shubham';
SQL DELETE Statement
The SQL DELETE statement is one of the most commonly used commands in SQL
(Structured Query Language). It allows you to remove one or more rows from the table
depending on the situation. Unlike the DROP statement, which removes the entire table,
the DELETE statement removes data (rows) from the table retaining only the table
structure, constraints, and schema
SQL DELETE Statement
The SQL DELETE statement removes one or more rows from a database table based on a
condition specified in the WHERE clause. It’s a DML (Data Manipulation Language)
operation that modifies the data within the table without altering its structure.
Syntax:
DELETE FROM table_name
WHERE some_condition;
Parameter Explanation
Some_condition: condition to choose a particular record.
table_name: name of the table
Example 1: Deleting Single Record
We can use the DELETE statement with a condition to delete a specific row from a table.
The WHERE clause ensures only the intended record is removed. We can delete the records
named Rithvik by using the below query:
Query:
DELETE FROM GFG_Employees WHERE NAME = 'Rithvik';
Example 2: Deleting Multiple Records
Delete the rows from the table GFG_Employees where the department is “Development”.
This will delete 2 rows(the first row and the seventh row).
Query
DELETE FROM GFG_Employees
WHERE department = 'Development';
Example 3: Delete All of the Records
To remove all the entries from the table, you can use the following query:
Query:
DELETE FROM GFG_EMPLOyees;
Or
DELETE * FROM GFG_EMPLOyees;
Output:
All of the records in the table will be deleted, there are no records left to display. The table
GFG_EMPLOyees will become empty.
Rolling Back DELETE Operations
Since the DELETE statement is a DML operation, it can be rolled back when executed in
a statement. If you accidentally delete records or need to repeat the process, you can use
the ROLLBACK command.
Query:
START TRANSACTION;
DELETE FROM GFG_Employees WHERE department = 'Development';
-- If needed, you can rollback the deletion
ROLLBACK;