Lab 4 - Help For Pes Students
Lab 4 - Help For Pes Students
Lab 4 - Help For Pes Students
Lab 4
DML – Insert, Update, Delete, Transactions - Commit,
Rollback, Savepoint
DML Commands:
1. INSERT
2. SELECT
3. UPDATE
4. DELETE
INSERT Statement
The INSERT INTO statement of SQL is used to insert a new row in a table. There
are two ways of using INSERT INTO statement for inserting rows:
1. Only values: First method is to specify only the value of data to be inserted
without the column names.
Syntax:
INSERT INTO table_name VALUES (value1, value2, value3,……,valueN);
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:
Syntax:
INSERT INTO table_name (column1, column2, column3,..) VALUES (value1,
value2, value3,..);
SELECT statement with INSERT INTO statement to copy rows from one table
and insert them into another table.
1. Inserting all columns of a table: We can copy all the data of a table and
insert into in a different table.
Syntax:
INSERT INTO first_table SELECT * FROM second_table;
2. Inserting specific columns of a table: We can copy only those columns of a
table which we want to insert into in a different table.
Syntax:
INSERT INTO first_table(names_of_columns1) SELECT
names_of_columns2 FROM second_table;
3. Copying specific rows from a table: We can copy specific rows from a table
to insert into another table by using WHERE clause with the SELECT
statement. We have to provide appropriate condition in the WHERE clause to
select specific rows.
Syntax:
INSERT INTO table1 SELECT * FROM table2 WHERE condition;
SELECT Statement
SELECT Command is used to retrieve the records from the table. It is one of the
most commonly used commands while working with the databases because at the
end-user wants to retrieve the results after any operation be it update, delete or
modify.
Syntax:
SELECT [column1, column2, column3, ……., columnN] from [table_name]
[where condition1 [AND [OR]] condition2..........;
Logical Operators:
Name Description
AND, && Logical AND
NOT, ! Negates value
OR, || Logical OR
XOR Logical XOR
UPDATE Statement
The UPDATE statement is used to modify data in a table.
Syntax:
UPDATE table_name SET column=value, column1=value1,... WHERE
someColumn=someValue;
DELETE Statement
The DELETE FROM statement is used to delete existing records from a database
table.
Syntax:
DELETE FROM tableName WHERE someColumn = someValue;
To delete all the records:
Syntax:
● DELETE FROM tablename;
● DELETE * FROM tablename;
DBMS LAB - UE20CS301
Parameters Used:
Privileges_name: These are the access rights or privileges granted to the user.
Object: It is the name of the database object to which permissions are being granted.
In the case of granting privileges on a table, this would be the table name.
User: It is the name of the user to whom the privileges would be granted.
DBMS LAB - UE20CS301
REVOKE:
Revoke command withdraw user privileges on database objects if any granted. When
a privilege is revoked from a particular user U, then the privileges granted to all other
users by user U will be revoked.
Syntax:
REVOKE privilege_name ON object_name FROM {user_name | public |
role_name}
COMMIT:
Syntax: COMMIT;
ROLLBACK:
Syntax: ROLLABACK;
SAVEPOINT:
Syntax:
SAVEPOINT SAVEPOINT_NAME;
INSERT Statements:
1.a. Using any form insert query, the above data can be inserted into respective
tables.
1.b. Mass insert can be done as below
i. Create a .csv file containing the data (for example create ‘example.csv’
containing the data for table ‘Example’
ii. When table ‘Example’ is created within in a database ‘My_DB’. Then,
we see a corresponding directory ‘My_DB’ under directory
‘/opt/lampp/var/mysql/’ (For Linux) or ‘\xampp\mysql\data’ (for
Windows) or ‘XAMPP/xamppfiles/var/mysql’ (for Mac)
iii. Place example.csv to the directory ‘My_DB’
iv. Run the command
LOAD DATA INFILE "example.csv" INTO TABLE Example
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
Note: When the csv file contains the column names on top, the top 1 line will
be ignored as per the above command
1.c Easier method would be to
1. In phpMyAdmin dashboard, select the table you want to insert
2. Select Import from the top menu and follow instructions to import the
respective .csv file into the table.
DBMS LAB - UE20CS301
Dealer Table:
1.a. Using any form of insert query
Dealer.csv file:
DBMS LAB - UE20CS301
Output: All the 10 tuples are inserted using LOAD DATA INFILE command
1.c. Easier method: Select Import from the top menus in PHPMYADMIN
1. In phpMyAdmin dashboard, select the table you want to insert
2. Select Import from the top menu and follow instructions to import the
respective .csv file into the table.
Customer Table:
Insert into Customer (Cust_ID,FName,LName,Address,Email) values
(201,’Sai’,’Shankar’,’Chennai’,’[email protected]’),
(202,’Apoorva’,’Kishore’,’Bangalore’,’[email protected]’),
(203,’Bala’,’Kumar’,’Chennai’,’[email protected]’),
(204,’Chethan’,’Kumar’,’Kerala’,’[email protected]’),
(205,’Gowtham’,’Raman’,’Bangalore’,’[email protected]’);
Bike Table:
Insert into Bike(VIN,Make,Model, Reg_No,Y_O_M,Chasis_No) values
(300,’Honda’,’CB500X’,’KA20H2010’,2015,’1GNCS18Z3M0115561’),
(301,’Kawasaki’,’KLX230’,’KA56H2344’,2017,’1HBGH41JXMN109186’),
(302,’Suzuki’,’GSX-R1000’,’TN70A1653’,2016,’JYA2UJE0X2A050036’),
(303,’Yamaha’,’Smax’,’MH04AT4351’,2014,’WYGZZZ5NZJM131396’),
(304,’TVS’,’Ntorq 125’,’TN30J5699’,2020,’JN3MS37A9PW202929’),
(305,’Mahindra’,’Duro’,’MH02BW1620’,2013,’MLHNC5110G5200064’);
Invoice Table:
Parts Table:
Insert into Parts(P_ID,Description,Qty,Price,Service_ID) values
(700,’Air Filter’,1,217.99,400),
(701,’Chain Set’,2,1987.99,401),
(702,’Clutch Plate’,517.99,404),
(703,’Handle Bar’,4,2519.99,405),
(704,’Horn’,5,1675.78,402),
(705,’Leg Guard’,101,6217.99,403);
SELECT Statements:
Retrieving records from the table with different conditions using Comparison
operators, Between, Membership operators, Pattern matching and Logical operators:
UPDATE Statements:
Update Customer set Address=’Mumbai’, Email=’[email protected]’
where Cust_ID=205;
Update Dealer set Pin=560001 where city=’Bangalore’;
Update Dealer set Pin=560001;
// Updates particular tuples by satisfying where clause condition
// Updates all the tuples in a relation when where clause is missing
DELETE Statements:
Delete from Sevice_Manager where M_emp_id =605;
Delete from Parts;
Delete from Dealer where Name like ‘%h’;
Delete from Bike where Y_O_M between 2012 and 2018;
Delete * from SalesPerson;
// Deletes particular tuples by satisfying where clause condition
// Deletes all the tuples in a relation when where clause is missing
DCL Statements:
GRANT:
GRANT SELECT, INSERT, UPDATE, DELETE ON Customer TO 'User_Name'@'localhost';
//Specified privileges are granted to the user
GRANT ALL ON Dealer TO 'User_Name'@'localhost';
// All the privileges are granted
GRANT SELECT ON Invoice TO '*'@'localhost';
//Read-only privilege granted
REVOKE:
REVOKE DELETE, UPDATE ON Bike FROM 'Username'@'localhost';
// Delete and Update privileges are cancelled / revoked from the user. So, the user cannot do any
write opeartion
DBMS LAB - UE20CS301
TCL Statements:
Ex1:
START TRANSACTION;
ALTER TABLE Service_Ticket ADD Service_Amt decimal(10,2) NOT NULL AFTER
Date_Del;
SELECT @A:=SUM(Price) FROM Parts WHERE Description in(‘Air Filter’,’Horn’,’Chain
Set);
UPDATE Service_Ticket SET Service_Amt=@A WHERE M_emp_id =602;
COMMIT;
// After issuing the Commit, the above changes in Service_Ticket relation has been saved.
Ex2:
mysql> CREATE TABLE Dealer_Address (Street_Name varchar(30),City varchar(30),Pincode
int);
Query OK, 0 rows affected (0.00 sec)
mysql> -- Do a transaction with autocommit turned on.
mysql> SET autocommit=1;
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO Dealer_Address(Street_Name,City,Pincode) SELECT Street,City,Pin
from Dealer;
Query OK, 1 row affected (0.00 sec)
//The above changes were saved automatically
mysql> -- Do another transaction with autocommit turned off.
mysql> SET autocommit=0;
DBMS LAB - UE20CS301
// The above code shows that, when Auto commit turned on, the changes were saved.
When the Auto commit turned off, no changes were saved. And, when we issue
Rollback, the changes were undone.
SAVEPOINT:
Ex:
SAVEPOINT SP1;
//Savepoint created.
DELETE FROM Customer WHERE Cust_ID= 203;
//delete operation is performed
SAVEPOINT SP2;
//Another Savepoint created.
ROLLBACK TO SP1;
//Rollback completed. So, it retains the customer tuple whose Cust_ID is 203
RELEASE SAVEPOINT:
This command is used to remove a SAVEPOINT that you have created.
Syntax:
RELEASE SAVEPOINT SAVEPOINT_NAME
Ex:
RELEASE SAVEPOINT SP2; // The above created Savepoint SP2 is removed