You are on page 1of 11
vatawsase Management system (333/03) PRACTICAL - 5 AIM: Implement various DML (Data Manipulation Language) commands (INSERT, DELETE, UPDATE) and one DDL command (TRUNCATE) + DML abbreviation of “Data Manipulation Language”. It is a language that provides a set of operations to support the basic data manipulation operations on the data held in the databases. It affects the records of database. ‘Tt allows users to insert, update, delete and retrieve data from the database and its objects. Examples of DML commands. Usage = It is used to insert data into a table. UPDATE = Itis used to update existing data within a table. DELETE © Teis used to delete records from a database table. ‘SELECT = It is used to retrieve data from the database. W® Note: SELECT command will be discussed in next practical. + INSERT (inserting new data into the table) ¢ The INSERT command is used to insert the data into a table or creates a new row in table. + To insert user data into tables, “INSERT INTO ..." SQL statement is used, and stores the inserted values into respective columns, ‘* _ Itis possible to write the INSERT INTO statement in two ways. The first way specifies both the column names and the values to be inserted: Syntax: { INSERT INTO tablename (columnl, column2, VALUES (valucl, value2, valuc3...}s In the second way when you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. ts Pe vatawsase Management system (333/03) Syntax: INSERT INTO tablename VALUES (valuel, valuc?, valuc3...)5 © At the time of inserting values, characters and date constants are enclosed within single quotes (* 9), There is no eed Uf quotes in ease of numbers. + If you omit any column at the time of inserting values (in the first way explained above), by default ‘NULL’ will be inserted. Example: Insert data th the rable rest. SQL> INSERT INTO test (eno, name,age) Input command —e ["°>" vacues (i, “BHAGYA's 4033 output —* fh row er oa. Input command —> [squ> xNseRT INTO text VALUES (2, “AARUSH', 535 output — /1 row crearea. Isqu> seLect = From tests % (Note: here we are using SELECT command to check the data items of the table) Inserting NULL values To insert NULL values into the table for any column, there are three different ways by which we can insert NULL. Example: Insert anew record in ‘est’ table that contains NULL value in the gender field. 1 way: Input is > INSERT INTO test VALUES (3/ ASMITA’36,NULL); 20 way: Input is > INSERT INTO test VALUES (3/ ASMITA’,36,"); 3st way: is PR vataBase Management System (3350/03) Input is > INSERT INTO test (eno, name, age) VALUES (3, ASMITA’,36); + UPDATE (updating rows of a table) + The UPDATE command can be used to change or modify the data values in a table. ‘* Itcanbe used to update either all rows or a set of rows from a table. + The SEY clause specities which column data to modity. * You can update specific rows by the WHERE clause, and displays message regarding how many rows have been updated. Syntax: UPDATE tablename SET columat = valuel, column2=value2,.. WHERE condition; (Note: Be careful when updating records in a table, if we don’t specify WHERE condition, then all the records from the table will be updated.) Example: Change the age of AARUSH in table ‘test’ from 5 to 10. SQL> select * Fron testy ENO NAME 2 AARUSH Input command —® |squ> upoare test ser output —* # row updated. lsou> =: If we write command > UPDATE test SET age= 5; Here we are not provi will be set to 15, ing WHERE condition, so in output ‘age’ field of every record “ DELETE (delete records from a table) ¢ The DELETE command can be used to remove either or all the rows of a table, or a set of rows froma table. a vataBase Management System (3350/03) * The DELETE command deletes all rows from the table, and displays message regarding how many rows have been deleted, = You can also use WHERE condition with DELETE statement so that only selected rows will be deleted that satisfy the condition, DELETE FROM tablename WHERE condition; °®Note: Be careful when using DELETE, if we don’t specify WHERE condition, then all the records from the table will be deleted.) Syntax: Example: Delete the record from the table ‘test’ whose enrolment number (eno) is 1 SQL> select * from test; ENO NAME AGE 1 BHAGYA 10 2 AARUSH 15 Input command —*'sQL> DELETE FROM test WHERE eno=1; Output —") pow deleted. SQL> select * from test; ENO NAME AGE 2 AARUSH 15 If we write command > DELETE FROM test; Here we are not providing WHERE condition, so in output all the records from the table will be deleted and the table will be empty. We can recover the deleted rows using COMMIT whereas we cannot recover them in case of TRUNCATE. 4 TRUNCATE (deleting all the rows from table) © The TRUNCATE TABLE command deletes the data inside a table, but not the table itself. It empties the table. ee i. vatawsase Management system (333/03) + Logically, this is equivalent to DELETE statement that deletes all rows without using WHERE clause. SO we can say TRUNCATE command deletes all the records ‘unconditionally. + TRUNCATE operation drops and re-creates the table. This is much faster than deleting all rows Une by one. ‘+ The deleted records cannot be recovered in truncate operation. While in delete operation, deleted records can be recovered using ROLLBACK statement. Syntax: ‘TRUNCATE TABLE tablename Example: Delete all the rows from the table ‘test’. ceteris SQL> TRUNCATE TABLE te=t;| Output —* Table truncated. SQL> select * from te: no rows selected Y% (Note: here we used SELECT command to check the data items of table test. But after using TRUNCATE command all the data of the table test is deleted.) vatawsase Management system (333/03) Write SOL queries to insert the data in tables using INSERT command: %. ( Note: Consider the tables created in practical no. 3 and insert following data) Table name: Test NO NAME GE 2 aarush 5 2 bhagya 0 3 vihaan 4 4 virat 35 5 vijay 33 6 sachin 25 7 avani. 5 B 2 P_SURNAME P_BDATE bhagya — parikh © -15-JAN-1@ surendranagar 15000 vihan sagar 22-FEB-12 rangpur 10000 shreeja parikh ——-22-JUL-1@ rajkot 5000 samay Saxena -—-1@-AUG-10 ahmedabad 1000 parmar —-13-NOV-9@ palanpur 19000 aarush = parikh =©—-15-JAN-15 surendranagar 22000 avani solanki 15-MAY-1 ahmedabad 1000 misha sutariya 22-DEC-12 ahmedabad 55000 parth parikh 15-SEP-95 rajkot see aarav solanki _11-SEP-1@ surendranagar 15000 e i i Table Name: Student STEN ST_NANE ST_SURNAME ST_G ST_DEPT | ST_CITY ‘ST_SPI 1 vijay saxena._—=—sm_~—computer—_ ahmedabad 9.3 ja hevang = eutariya om mechanical ahmedabad 4 jal hitesh = parmar_ mato rajkot 72 2 parimal = parikh =m ~—computer—rajkot 8.7 sd ghata zaveri «=f cd surendranagar 6.9 fet jal sagar = m_— electrical. rangpur 9.6 fz chirag = Sagar = m_~—s mechanical surendranagar 6.2 jaz kaushal «= zaveri_ «smut ‘ahmedabad 49 3 rutva —parmar «= #_~—scomputer kalo 5.5 Jez yogendrasagar_—m__ electrical. rangpur 75 sv PR vatawsase Management system (333/03) ‘Table Name: Faculty F_ID F_NAME F_GE F_DEPT F_SUBJECT 2 unp m computer 2 akp m computer 3 kep m — auto 4 srb m civil 5 bbg f — cddm 6 cck m electrical nma 7 rit m civil ens 8 bdl m — mechanical acp 9 nnd * computer cmt 10 pbp m auto php Table Name: Employee E_ID E_NAME E_BDATE E_DESG E_SALARY 1 dinesh @1-JUN-68 lecturer 25000 2 ashok 12-JUL-64 manager 3500 3 gita 13-JAN-72 clerk 12500 4 naresh 21-DEC-7@ clerk 1500 5 kamlesh — 17-NOV-85 peon 500 6 haresh 11-FEB-84 peon 3000 7 babulal = @5-JUL-88 lecturer 5500 8 akash @7-AUG-90 manager 33500 9 jitu 12-Nov-98 clerk 5500 1@ piyush 22-SEP-82 peon 3000 Write SQL statements to insert data values as shown in the above tables. DataBase Management System (3330703) DataBase Management System (3330703) Write SQL queries to perform following tasks: (Consider the tables created above) 1. WAQ to insert NULL value in the age column of test table. 2. WAQ to change the serial no of aarush to 15 in person table. DataBase Management System (3330703) 3. WAQ to make the salary as 1000 of every employee in employee table. 4, WAQ to remove the female faculties from faculty table. 5. WAQ to increase the salary of clerk by 10% in employee table. 6. WAQ to transfer the faculties from Auto department to Computer dept in faculty table. 7. WAQ to remove all the persons whose balance is greater than 10000 from person table. 8. WAQ to delete all the data from test table. 9. WAQ to delete the students who are living in Rajkot from student table. 10. WAQ to truncate the table person.

You might also like