Professional Documents
Culture Documents
UNIT2
UNIT2
UNIT2
Constraints can be specified when the table is created with the CREATE TABLE statement, or
after the table is created with the ALTER TABLE statement.
Syntax
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
);
UNIQUE CONSTRAINT:
SQL UNIQUE Constraint
However, you can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.
PRIMARY KEY CONSTRAINT:
PRIMARY KEY Constraint:
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single
or multiple columns (fields).
ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID);
Drop Constraint:
ALTER TABLE Persons DROP CONSTRAINT PK_Person;
FOREIGN KEY CONSTRAINT:
The FOREIGN KEY constraint is used to prevent actions that would destroy links between
tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY
KEY in another table.
The table with the foreign key is called the child table, and the table with the primary key is
called the referenced or parent table.
Persons Table
1 Hansen Ola 30
2 Svendson Tove 23
3 Pettersen Kari 20
Orders Table
1 77895 3
2 44678 3
3 22456 2
4 24562 1
Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column in the
"Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key
column, because it has to be one of the values contained in the parent table.
ALTER TABLE Orders
ADD CONSTRAINT FK_PersonOrderFOREIGN KEY (PersonID) REFERENCES Persons(Per
sonID);
Drop Statement:
ALTER TABLE Orders DROP CONSTRAINT FK_PersonOrder;
CHECK CONSTRAINT:
The CHECK constraint is used to limit the value range that can be placed in a column.
ALTER TABLE Persons ADD CHECK (Age>=18);
ALTER TABLE Persons
ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes');
Drop Constraint:
ALTER TABLE Persons DROP CONSTRAINT CHK_PersonAge;
DEFAULT CONSTRAINT
The default value will be added to all new records, if no other value is specified.
Drop Constraint:
ALTER TABLE Persons MODIFY City DEFAULT null ;
A strong entity set with only simple attributes will require only one table in relational model.
Attributes of the table will be the attributes of the entity set.
The primary key of the table will be the key attribute of the entity set.
Example-
Roll_no Name Gender
A strong entity set with any number of multi valued attributes will require two tables in relational
model.
One table will contain all the simple attributes with the primary key.
Other table will contain the primary key and all the multi valued attributes.
Example-
Roll_no City
Roll_ Mobile_
no no
NOTE-
If we consider the overall ER diagram, three tables will be required in relational model-
One table for the entity set “Employee”
One table for the entity set “Department”
One table for the relationship set “Works in”
NOTE- Here, combined table will be drawn for the entity set B and relationship set R.
NOTE- Here, combined table will be drawn for the entity set A and relationship set R.
Case-04: For Binary Relationship With Cardinality Ratio 1:1
Here, two tables will be required. Either combine ‘R’ with ‘A’ or ‘B’
Way-01: Way-02:
1. A ( a1 , a2 ) 1.AR ( a1 , a2 , b1 )
2. BR ( a1 , b1 , b2 ) 2. B ( b1 , b2 )
Case-01: For Binary Relationship With Cardinality Constraint and Total Participation
Constraint From One Side-
Because cardinality ratio = 1 : n , so we will combine the entity set B and relationship set R.
Then, two tables will be required-
1. A ( a1 , a2 )
2. BR ( a1 , b1 , b2 )
Because of total participation, foreign key a1 has acquired NOT NULL constraint, so it can’t be
null now.
Case-02: For Binary Relationship With Cardinality Constraint and Total Participation
Constraint From Both Sides-
Ifthere is a key constraint from both the sides of an entity set with total participation, then that
binary relationship is represented using only single table.
Sol: -ARB(a1,a2,b1,b2)
Rule-07: For Binary Relationship With Weak Entity Set -
Weak entity set always appears in association with identifying relationship with total
participation constraint.
View in DBMS
Views act as a proxy or virtual table created from the original table.
Views simplify SQL queries and allow secure access to underlying tables. Views in
DBMS can be visualized as virtual tables that are formed by original tables from the
database.
Sample Table
Employee Table:
This table contains details of employees in a particular company and has data fields such
as EmpID, EmpName, Address, Contact. We have added 6 records of employees for our
purpose.
EmpRole Table:
This table contains details of employees' roles in a particular company and has data fields
as EmpID, Role, Dept. We have stored all the records particular to the EmpID of each employee.
1 Intern Engineering
2 Trainee IT
3 Executive HR
4 SDE-1 Engineering
5 SDE-2 Engineering
Creating View
The view can be created by using the CREATE VIEW statement, Views can be simple or
complex depending on their usage.
Syntax:
Here, viewName is the Name for the View we set, tableName is the Name of the table
FROM table Name
Simple view is the view that is made from a single table, It takes only one table and just the
conditions, It also does not take any inbuilt SQL functions like AVG(), MIN(), MAX() etc,
or GROUP BY clause.
While creating a simple view, we are not creating an actual table, we are just projecting the data
from the original table to create a view table.
Example 1: In this example, we are creating a view table from Employee Table for getting
the EmpID and EmpName. So the query will be:
Now to see the data in the EmpView1 view created by us, We have to simply use the SELECT
statement.
Output:
The view table EmpView1 that we have created from Employee Table contains EmpID and
EmpName as its data fields.
EmpID EmpName
1 Alex
2 Adolf
3 Aryan
4 Bhuvan
5 Carol
6 Steve
The complex view is the view that is made from multiple tables, It takes multiple tables in which
data is accessed using joins or cross products, It contains inbuilt SQL functions
like AVG(), MIN(), MAX() etc, or GROUP BY clause. So, whenever we need to access data
from multiple tables we can make use of Complex Views.
Example:
In this example, we are creating a view table using Employee Table and EmpDept table for
getting the EmpName from Employee table and Dept from EmpDept. So the query will be:
Output:
The view table CompView that we have created from Employee Table and EmpRole Table
contains EmpName and Dept as its data fields. We have used the cross join to get our necessary
data.
EmpName Dept
Alex Engineering
Adolf IT
Aryan HR
Bhuvan Engineering
Carol Engineering
Steve Engineering
Deleting view
To delete the view so as we DROP a table in SQL, similarly, we can delete or drop a view using
the DROP statement. The DROP statement completely deletes the structure of the view.
Syntax:
.Example:
Output:
For updating the views we can use CREATE OR REPLACE VIEW statement, new columns will
replace or get added to the view.
Syntax:
Example 1:
Output:
The data fields of view table EmpView1 have been updated to EmpID, EmpName, and Address.
1 Alex London
3 Aryan Delhi
4 Bhuvan Hyderabad
6 Steve California
Updateable Views are views that allow for data manipulation but there are certain
conditions needed to be taken care of while manipulating the data of the view:
Inserting view a row in the view takes the same syntax as we use to insert a row in a simple table
Syntax:
Here, viewName is the view in which we have to insert data and we add values according to the
columns in the view table.
Example: let's take the above created simple view EmpView2 and we want to insert a new row.
Now to see the data in the EmpView2, We have to simply use the SELECT statement.
Output:
Now the updated table EmpView2 has one more row of EmpID 4 and Role Intern.
EmpID Role
1 Intern
2 Trainee
3 Executive
4 Intern
Syntax:
Here, viewName is the view from which data has been deleted, condition is the Condition by
which we select rows to be deleted.
Example: let's take the above created simple view EmpView2 and we want to delete the row
having EmpID = 1.
Now to see the data in the EmpView2, We have to simply use the SELECT statement.
Output:
Now 1 record with data EmpID 1 and Role Intern has been deleted.
EmpID Role
2 Trainee
3 Executive
4 Intern
Conclusion
Now that we have seen many examples of views in DBMS, let us note down a few points:
Views are some kind of virtual tables created by original tables from the database.
Views actually do not hold the actual data and just have the definition of the original
Views act as a proxy or virtual table created from the original table.
Relational Algebra
The purpose of a query language is to retrieve data from database or perform various operations
such as insert, update, delete on the data.
procedural query language, it means that it tells what data to be retrieved and how to be
retrieved.
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Derived Operations:
Select Operator is denoted by sigma (σ) and it is used to find the tuples (or rows) in a relation (or
table) which satisfy the given condition.
\
Syntax of Select Operator (σ)
σ Condition/Predicate(Relation/Table name)
Table: CUSTOMER
---------------
σ Customer_City="Agra" (CUSTOMER)
Output:
Project operator is denoted by ∏ symbol and it is used to select desired columns (or attributes)
from a table (or relation).
In this example, we have a table CUSTOMER with three columns, we want to fetch only two
columns of the table, which we can do with the help of Project Operator ∏.
Table: CUSTOMER
Customer_Name Customer_City
------------- -------------
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Carl Delhi
Union Operator (∪)
Union operator is denoted by ∪ symbol and it is used to select all the rows (tuples) from two
tables (relations).
table_name1 ∪ table_name2
Union Operator (∪) Example
Table 1: COURSE
Student_Name
------------
Aditya
Carl
Paul
Lucy
Rick
Steve
Note: As you can see there are no duplicate names present in the output even though we had few
common names in both the tables, also in the COURSE table we had the duplicate name itself.
Intersection operator is denoted by ∩ symbol and it is used to select common rows (tuples) from
two tables (relations).
Note: Only those rows that are present in both the tables will appear in the result set.
table_name1 ∩ table_name2
Intersection Operator (∩) Example
Student_Name
------------
Aditya
Steve
Paul
Lucy
Set Difference (-)
Set Difference is denoted by – symbol. Lets say we have two relations R1 and R2 and we want to
select all those tuples(rows) that are present in Relation R1 but not present in Relation R2, this
can be done using Set difference R1 – R2.
table_name1 - table_name2
Set Difference (-) Example
Query:.
Student_Name
------------
Carl
Rick
Cartesian product (X)
Cartesian Product is denoted by X symbol. Lets say we have two relations R1 and R2 then the
cartesian product of these two relations (R1 X R2) would combine each tuple of first relation R1
with the each tuple of second relation R2. I know it sounds confusing but once we take an
example of this, you will be able to understand this.
Table 1: R
Col_A Col_B
----- ------
AA 100
BB 200
CC 300
Table 2: S
Col_X Col_Y
----- -----
XX 99
YY 11
ZZ 101
Query:
Lets find the cartesian product of table R and S.
RXS
Output:
Rename (ρ)
Table: CUSTOMER
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
----------
Steve
Raghu
Chaitanya
Ajeet
Carl
RELATIONAL CALCULUS:
UNIT-3
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Derived Operations:
1. Natural Join (⋈)
2. Left, Right, Full outer join (⟕, ⟖, ⟗)
3. Intersection (∩)
4. Division (÷)
σ Customer_City="Agra" (CUSTOMER)
Output:
σ (CUSTOMER)
Customer_Name Customer_City
------------- -------------
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Carl Delhi
∏ Customer_Name (CUSTOMER)
Output:
Note: The rows (tuples) that are present in both the tables will only appear once in
the union set. In short you can say that there are no duplicates present after the
union operation.
table_name1 ∪ table_name2
Union Operator (∪) Example
Table 1: COURSE
Student_Name
------------
Aditya
Carl
Paul
Lucy
Rick
Steve
Note: As you can see there are no duplicate names present in the output even
though we had few common names in both the tables, also in the COURSE table
we had the duplicate name itself.
Note: Only those rows that are present in both the tables will appear in the result
set.
table_name1 ∩ table_name2
Intersection Operator (∩) Example
Lets take the same example that we have taken above.
Table 1: COURSE
Student_Name
------------
Aditya
Steve
Paul
Lucy
table_name1 - table_name2
Set Difference (-) Example
Lets take the same tables COURSE and STUDENT that we have seen above.
Query:
Lets write a query to select those student names that are present in STUDENT
table but not present in COURSE table.
Student_Name
------------
Carl
Rick
R1 X R2
Cartesian product (X) Example
Table 1: R
R
Col_A Col_B
----- ------
AA 100
BB 200
CC 300
Table 2: S
S
Col_X Col_Y
----- -----
XX 99
YY 11
ZZ 101
Query:
Lets find the cartesian product of table R and S.
RXS
Output:
Rename (ρ)
Rename (ρ) operation can be used to rename a relation or an attribute of a relation.
Rename (ρ) Syntax:
ρ(new_relation_name, old_relation_name)
Rename (ρ) Example
Lets say we have a table customer, we are fetching customer names and we are
renaming the resulted relation to CUST_NAMES.
Table: CUSTOMER
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
----------
Steve
Raghu
Chaitanya
Ajeet
Carl
Join Operations
Types of JOIN:
Inner Joins:
Theta join
EQUI join
Natural join
Outer join:
Inner Join:
In an inner join, only those tuples that satisfy the matching criteria are included, while
the rest are excluded. Let's study various types of Inner Joins:
Theta Join:
The general case of JOIN operation is called a Theta join. It is denoted by symbol θ
Example : A ⋈θ B
Table A Table B
1 1 1 1
1 2 1 3
column 1 column 2
1 2
EQUI JOIN:
When a theta join uses only equivalence condition, it becomes a equi join.
column 1 column 2
1 1
Natural join can only be performed if there is a common attribute (column) between
the relations. The name and type of the attribute must be same.
Example :
Num Square
2 4
3 9
Num Cube
2 8
3 27
C⋈D
C⋈D
2 4 4
3 9 27
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria, we also include some or all
tuples that do not match the criteria.
In the left outer join, operation allows keeping all tuple in the left relation.
if there is no matching tuple is found in right relation, then the attributes of right relation
in the join result are filled with null values.
Num Square
2 4
3 9
4 16
Num Cube
2 8
3 18
5 75
2 4 4
3 9 9
4 16 -
In the right outer join, operation allows keeping all tuple in the right relation.
if there is no matching tuple is found in the left relation, then the attributes of the left
relation in the join result are filled with null values.
A B Right outer join
A⋈B
2 8 4
3 18 9
5 75 -
In a full outer join, all tuples from both relations are included in the result, irrespective of
the matching condition.
Result of outer join contains all rows of bpth tables
A⋈B
3 9 18
4 16 -
5 - 75
SQL supports few Set operations which can be performed on the table data. These are used to get
meaningful results from data stored in the table, under different special conditions.
UNION
UNION ALL
INTERSECT
MINUS
UNION OPERATION
The First table,
ID Name
1 abhi
2 adam
The Second table,
ID Name
2 adam
3 Chester
ID NAME
1 abhi
2 adam
3 Chester
UNION ALL
This operation is similar to Union. But it also shows the duplicate rows.
The First table,
ID NAME
1 abhi
2 adam
The Second table,
ID NAME
2 adam
3 Chester
ID NAME
1 abhi
2 adam
2 adam
3 Chester
INTERSECT
Intersect operation is used to combine two SELECT statements.
it only retuns the records which are common from both SELECT statements.
In case of Intersect the number of columns and datatype must be same.
Example of Intersect
The First table,
ID NAME
1 abhi
2 adam
The Second table,
ID NAME
2 adam
3 Chester
ID NAME
2 adam
MINUS
The Minus operation combines results of two SELECT statements and return only those in the
final result, which belongs to the first set of the result.
Example of Minus
The First table,
ID NAME
1 abhi
2 adam
The Second table,
ID NAME
2 adam
3 Chester
ID NAME
1 abhi
SQL aggregation function is used to perform the calculations on multiple rows of a single
column of a table.
It returns a single value.
It is also used to summarize the data.
COUNT function is used to Count the number of rows in a database table. It can work on
both numeric and non-numeric data types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and Null.
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example: COUNT( )
SELECT COUNT(*) FROM PRODUCT_MAST;
Output: 10
1. SELECT COUNT(*) FROM PRODUCT_MAST WHERE RATE>=20;
Output: 7
SELECT COUNT(DISTINCT COMPANY) FROM PRODUCT_MAST;
Output: 3
SUM FUNCTION
Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.
Example: SUM()
SELECT SUM(COST) FROM PRODUCT_MAST;
Output: 670
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3;
Output: 320
AVG FUNCTION (AVERAGE)
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Example: SELECT AVG(COST) FROM PRODUCT_MAST;
Output: 67.00
MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column.
Syntax : MAX() or MAX( [ALL|DISTINCT] expression )
Example: SELECT MAX(RATE) FROM PRODUCT_MAST;
OUTPUT:30
MIN function is used to find the minimum value of a certain column. This function determines
the smallest value of all selected values of a column.
Syntax :MIN() or MIN( [ALL|DISTINCT] expression )
Example: SELECT MIN(RATE) FROM PRODUCT_MAST;
Output: 10
The GROUP BY clause is a SQL command that is used to group rows that have the
same values.
The GROUP BY clause is used in the SELECT statement.
it is used in conjunction with aggregate functions to produce summary reports from the
database.
The queries that contain the GROUP BY clause are called grouped queries and only
return a single row for every grouped item.
gender
Female
Female
Male
Female
Male
Male
Male
Male
Male
Suppose we want to get the unique values for genders. We can use a following query -
gender
Female
Male
Note :
Only two results have been returned. This is because we only have two gender types Male and
Female. The GROUP BY clause in SQL grouped all the "Male" members together and returned
only a single row for it. It did the same with the "Female" members.
Suppose that we want to get a list of movie category_id and corresponding years in which they
were released.
category_id year_released
1 2011
2 2008
NULL 2008
NULL 2010
8 2007
6 2007
6 2007
8 2005
NULL 2012
7 1920
8 NULL
8 1920
Executing the above script in MySQL workbench against the myflixdb gives us the
following results shown below.
category_id year_released
NULL 2008
NULL 2010
NULL 2012
1 2011
2 2008
6 2007
7 1920
8 1920
8 2005
8 2007
The GROUP BY clause operates on both the category id and year released to
identify unique rows in our above example.
If the category id is the same but the year released is different, then a row is treated as a
unique one .If the category id and the year released is the same for more than one row, then
it's considered a duplicate and only one row is shown.
Suppose we want total number of males and females in our database. We can use the following
script shown below to do that.
gender COUNT('membership_number')
Female 3
Male 5
The results shown below are grouped by every unique gender value posted and the number of
grouped rows is counted using the COUNT aggregate function.
It's not always that we will want to perform groupings on all the data in a given table.
There will be times when we will want to restrict our results to a certain given criteria.
In such cases , we can use the HAVING clause
Suppose we want to know all the release years for movie category id 8. We would use the
following script to achieve our results.
SELECT * FROM `movies` GROUP BY `category_id`,`year_released` HAVING
`category_id` = 8;
Note only movies with category id 8 have been affected by our GROUP BY clause.
Points To Remember
The GROUP BY Clause SQL is used to group rows with same values.
The GROUP BY Clause is used together with the SQL SELECT statement.
The SELECT statement used in the GROUP BY clause can only be used contain column
names, aggregate functions, constants and expressions.
SQL Having Clause is used to restrict the results returned by the GROUP BY clause.
SQL GROUP BY Clause is used to collect data from multiple records and returned
record set by one or more columns.
The GROUP BY clause is a SQL command that is used to group rows that have the
same values.
The GROUP BY clause is used in the SELECT statement.
it is used in conjunction with aggregate functions to produce summary reports from the
database.
The queries that contain the GROUP BY clause are called grouped queries and only
return a single row for every grouped item.
gender
Female
Female
Male
Female
Male
Male
Male
Male
Male
Suppose we want to get the unique values for genders. We can use a following query -
gender
Female
Male
Note :
Only two results have been returned. This is because we only have two gender types Male and
Female. The GROUP BY clause in SQL grouped all the "Male" members together and returned
only a single row for it. It did the same with the "Female" members.
category_id year_released
1 2011
2 2008
NULL 2008
NULL 2010
8 2007
6 2007
6 2007
8 2005
NULL 2012
7 1920
8 NULL
8 1920
Executing the above script in MySQL workbench against the myflixdb gives us the
following results shown below.
category_id year_released
NULL 2008
NULL 2010
NULL 2012
1 2011
2 2008
6 2007
7 1920
8 1920
8 2005
8 2007
The GROUP BY clause operates on both the category id and year released to
identify unique rows in our above example.
If the category id is the same but the year released is different, then a row is treated as a
unique one .If the category id and the year released is the same for more than one row, then
it's considered a duplicate and only one row is shown.
Suppose we want total number of males and females in our database. We can use the following
script shown below to do that.
gender COUNT('membership_number')
Female 3
Male 5
The results shown below are grouped by every unique gender value posted and the number of
grouped rows is counted using the COUNT aggregate function.
It's not always that we will want to perform groupings on all the data in a given table.
There will be times when we will want to restrict our results to a certain given criteria.
In such cases , we can use the HAVING clause
Suppose we want to know all the release years for movie category id 8. We would use the
following script to achieve our results.
Points To Remember
The GROUP BY Clause SQL is used to group rows with same values.
The GROUP BY Clause is used together with the SQL SELECT statement.
The SELECT statement used in the GROUP BY clause can only be used contain column
names, aggregate functions, constants and expressions.
SQL Having Clause is used to restrict the results returned by the GROUP BY clause.
SQL GROUP BY Clause is used to collect data from multiple records and returned
record set by one or more columns.
A Subquery or Nested Query is a query within another SQL query and embedded within
the WHERE clause.
IMPORTANT RULE:
A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.
You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
A subquery is a query within another query. The outer query is known as the main query,
and the inner query is known as a subquery.
Subqueries are on the right side of the comparison operator.
A subquery is enclosed in parentheses.
In the Subquery, ORDER BY command cannot be used. But GROUP BY command can
be used to perform the same function as ORDER BY command.
SQL subqueries are most frequently used with the Select statement.
SYNTAX:
SELECT column_name FROM table_name WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );
Example:
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
SELECT * FROM EMPLOYEE WHERE ID IN (SELECT ID FROM EMPLOYEE W
HERE SALARY > 4500);
4 Alina 29 UK 6500.00
SQL subquery can also be used with the Insert statement. In the insert statement, data
returned from the subquery is used to insert into another table.
In the subquery, the selected data can be modified with any of the character, date
functions.
Syntax:
INSERT INTO table_name (column1, column2, column3....) SELECT * FROM table_nam
e
WHERE VALUE OPERATOR
Example
Consider a table EMPLOYEE_BKP with similar as EMPLOYEE.
Now use the following syntax to copy the complete EMPLOYEE table into the EMPLOYEE_BKP table.
INSERT INTO EMPLOYEE_BKP SELECT * FROM EMPLOYEE WHERE ID IN
(SELECT ID FROM EMPLOYEE);
The subquery of SQL can be used in conjunction with the Update statement.
When a subquery is used with the Update statement, then either single or multiple columns in a
table can be updated.
Syntax
UPDATE table SET column_name = new_value WHERE VALUE OPERATOR (SELE
CT COLUMN_NAME FROM TABLE_NAME WHERE condition);
Example
UPDATE EMPLOYEE SET SALARY = SALARY * 0.25 WHERE AGE IN
(SELECT AGE FROM CUSTOMERS_BKP WHERE AGE >= 29);
This would impact three rows, and finally, the EMPLOYEE table would have the following records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
The subquery of SQL can be used in conjunction with the Delete statement just
like any other statements mentioned above.
Syntax :
DELETE FROM TABLE_NAME WHERE VALUE OPERATOR (SELECT COLUMN_
NAME FROM TABLE_NAME WHERE condition);
Example
DELETE FROM EMPLOYEE WHERE AGE IN
(SELECT AGE FROM EMPLOYEE_BKP WHERE AGE >= 29 );
This would impact three rows, and finally, the EMPLOYEE table would have the following records.
1 John 20 US 2000.00