Transactions Concepts
Transactions Concepts
Ex 1:
start transaction;
update attendance_attendancetype set type=1 where id =1 ;
commit;
Ex 2:
start transaction;
update attendance_attendancetype set type=1 where id =1 ;
rollback;
Properties of Transactions:
Transactions have the following four standard properties, usually referred to by the acronym ACID.
Atomicity − ensures that all operations within the work unit are completed successfully.
Otherwise, the transaction is aborted at the point of failure and all the previous operations are rolled
back to their former state.
Consistency − ensures that the database properly changes states upon a successfully committed
transaction.
Searching Data:
The SQL Like is a logical operator that is used to determine whether a specific character
string matches a specified pattern. It is commonly used in a Where clause to search for a
specified pattern in a column. This operator can be useful in cases when we need to perform
pattern matching instead of equal or not equal.
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or Finds any values that have "or" in any position
%'
WHERE CustomerName LIKE '_r Finds any values that have "r" in the second position
%'
WHERE CustomerName LIKE 'a_ Finds any values that start with "a" and are at least 2
%' in length
WHERE CustomerName LIKE 'a__ Finds any values that start with "a" and are at least 3
%' in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
EG:
select * from attendance_attendancetype where emp_name like 'm%';
SQL – Expressions:
An expression is a combination of one or more values, operators and SQL functions that
evaluate to a value. These SQL EXPRESSIONs are like formulae and they are written in query
language. You can also use them to query the database for a specific set of data.
Syntax:
SELECT column1, column2, columnN
FROM table_name
WHERE [CONDITION|EXPRESSION];
EG:
select * from attendance_attendancetype where emp_name='abcd';
Subquery:
In SQL a Subquery can be simply defined as a query within another query. In other words we
can say that a Subquery is a query that is embedded in WHERE clause of another SQL query.
Syntax:
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
EG:
SELECT * FROM CUSTOMERS WHERE ID IN (SELECT ID FROM CUSTOMERS WHERE SALARY >
4500) ;
EG:
Consider a table CUSTOMERS_BKP with similar structure as CUSTOMERS table. Now to copy
the complete CUSTOMERS table into the CUSTOMERS_BKP table, you can use the following
syntax.
INSERT INTO CUSTOMERS_BKP SELECT * FROM CUSTOMERS WHERE ID IN (SELECT ID FROM
CUSTOMERS) ;
EG:
Assuming, we have CUSTOMERS_BKP table available which is backup of CUSTOMERS table.
The following example updates SALARY by 0.25 times in the CUSTOMERS table for all the
customers whose AGE is greater than or equal to 27.
UPDATE CUSTOMERS SET SALARY = SALARY * 0.25 WHERE AGE IN (SELECT AGE FROM
CUSTOMERS_BKP WHERE AGE >= 27 );
EG
Assuming, we have a CUSTOMERS_BKP table available which is a backup of the CUSTOMERS
table. The following example deletes the records from the CUSTOMERS table for all the
customers whose AGE is greater than or equal to 27.
DELETE FROM CUSTOMERS WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP WHERE
AGE >= 27 );
CREATE DEFINER=`root`@`localhost` PROCEDURE `new_data`(in input1 integer(10),in input2
integer(10))
BEGIN
start transaction;
update attendance_attendancetype set type=1 where id =1 ;
savepoint name1;
END