Sql dml & tcl 2


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Sql dml & tcl 2

  1. 1. <ul><li> DML </li></ul><ul><li>[Data Manipulation Language] </li></ul>
  2. 2. Insert command <ul><li>It is used to add one or more rows to a table. </li></ul><ul><li>Values are separated by commas </li></ul><ul><li>Data types varchar2, char, raw, long and date are enclosed in single quotes. </li></ul><ul><li>The value must be entered in the same order as they are defined in the table. </li></ul><ul><li>Data can be entered for a specific column also. </li></ul>
  3. 3. Insert – Syntax and Example <ul><li>Syntax: </li></ul><ul><li>Insert into <table_name> values (a list of data values); </li></ul><ul><li>Example: </li></ul><ul><li>[Empno N(4), EName v2(15 ), Sal N(7,2), Comm N(8), Design v(10 ), Deptno N(2)] </li></ul><ul><li>Insert into Emp values(101, ’Ravi’, 5000,1000, ’Clerk’, 10); </li></ul>
  4. 4. To insert values in interactive mode <ul><li>Example: </li></ul><ul><li>Insert into Emp values (&Empno, ‘&Ename’, &Sal, &Comm, ‘&Design’, &Deptno); </li></ul><ul><li>Oracle prompts the user to enter values for all columns in the table as shown below: </li></ul><ul><li>Enter value for Empno: </li></ul><ul><li>Enter value for Ename: </li></ul><ul><li>Enter value for Sal: </li></ul><ul><li>Enter value for Comm: </li></ul><ul><li>Enter value for Design: </li></ul><ul><li>Enter value for Deptno: </li></ul><ul><li>To repeat type / at SQL> prompt </li></ul>
  5. 5. To insert values for specific columns <ul><li>Syntax: </li></ul><ul><li>Insert into <table_name(Col_names)> values (list of values); </li></ul><ul><li>Example: </li></ul><ul><li>Insert into Emp ( Empno, Ename, Deptno ) </li></ul><ul><li> values ( 102, ‘Anitha’, 20 ); </li></ul>
  6. 6. To insert values for specific columns – using null <ul><li>Syntax: </li></ul><ul><li>Insert into <table_name> values (list of values); </li></ul><ul><li>Example: </li></ul><ul><li>Insert into Emp values ( 102, ‘Anitha’,null,null,null,20 ); </li></ul><ul><li>SQL will prompt a message </li></ul><ul><li>1 row inserted </li></ul>
  7. 7. Inserting date values <ul><li>Date values must be enclosed within single quotes . </li></ul><ul><li>Standard format for date data types is “DD-MON-YY”. </li></ul><ul><li>Assuming a column DOJ – Date of Joining in Emp table </li></ul><ul><li>Example: </li></ul><ul><li>Insert into Emp values(103,’Harathi’,15000,2000,’Engineer’, </li></ul><ul><li>40, ’04-May-02’ ); </li></ul>
  8. 8. Inserting values from the other table <ul><li>Replace the values clause with appropriate query. </li></ul><ul><li>Columns returned by the query must be of the same type. </li></ul><ul><li>Target-table-name is Employee1. </li></ul><ul><li>Source table-name is Employee. </li></ul><ul><li>For all columns </li></ul><ul><li>Syntax: </li></ul><ul><li>Insert into <target-table-name> </li></ul><ul><li>select * from <source-table-name> <where condition> </li></ul><ul><li>Example: </li></ul><ul><li>Insert into Employee1 </li></ul><ul><li>select * from Employee; </li></ul>
  9. 9. For few columns <ul><li>Syntax: </li></ul><ul><li>Insert into <target table-name> (column-name1,column-name2,…) select (source table col1, col2, …) from Source table <where condtion>; </li></ul><ul><li>Example: </li></ul><ul><li>Insert into Employee1 (Empno, Ename, Doj) </li></ul><ul><li>select Empno, Ename, Doj from Employee; </li></ul>
  10. 10. Select command <ul><li>To retrieve information from a table. </li></ul><ul><li>This is called as Querying . </li></ul><ul><li>Syntax: </li></ul><ul><li>Select column_name1,column-name2, . .. From table_name .. ; </li></ul><ul><li>Example: </li></ul><ul><li>Select * from Emp; </li></ul><ul><li>The * in the query displays all the columns from the table. </li></ul><ul><li>The output can be obtained for selected columns. </li></ul><ul><li>The order of the column names in the query can be as required. </li></ul>
  11. 11. Querying for a particular columns <ul><li>Example: </li></ul><ul><li>Select Empno, Ename, Deptno from Emp ; </li></ul><ul><li>It will display the only the selected columns for all the employees in the table. </li></ul>
  12. 12. Select command with ‘Where’ clause <ul><li>WHERE clause in the SELECT statement, user can still filter the records based on the requirements </li></ul><ul><li>It is used to obtain the output based on some condition[s]. </li></ul><ul><li>Syntax: </li></ul><ul><li>Select <columnname1>,< columnname3 >,< columnname3 >, from <table name> where <condition> ; </li></ul><ul><li>Example: </li></ul><ul><li>Select * from Emp where Empno=101; </li></ul><ul><li>The where clause retrieves specific row or rows based on the condition from a table. </li></ul>
  13. 13. Updating Column(s) values of the Table <ul><li>UPDATE command is used to update the columns in a table. </li></ul><ul><li>Values of a single column or a group of columns can be updated. </li></ul><ul><li>Update statement can affect a single row or a set of rows depending on the WHERE clause in the update statement. </li></ul>
  14. 14. Updating a single column: <ul><li>Syntax: </li></ul><ul><ul><li>UPDATE <TABLE-NAME> </li></ul></ul><ul><ul><ul><li>SET <COLUMN-NAME> = <NEW-VALUE> </li></ul></ul></ul><ul><ul><ul><ul><li><WHERE CONDITION>; </li></ul></ul></ul></ul><ul><li>Example: </li></ul><ul><li>The following example updates the COMM column to 1000 in EMP table. </li></ul><ul><li>UPDATE EMP </li></ul><ul><ul><ul><ul><li>SET COMM = 1000; </li></ul></ul></ul></ul><ul><li>Without a WHERE clause in the above example, it will update all the records in the EMP table. </li></ul><ul><li>  </li></ul>
  15. 15. Update using WHERE clause <ul><li>The following example will update the COMM column to 1000 in the EMP table for the Employee 7788 using WHERE clause. </li></ul><ul><li>UPDATE EMP SET COMM = 1000 WHERE EMPNO = 7788; </li></ul><ul><li>Updating multiple columns: </li></ul><ul><li>Syntax: </li></ul><ul><ul><li>UPDATE <TABLE-NAME> </li></ul></ul><ul><ul><ul><li>SET <COLUMN1> = < VALUE1>, <COLUMN2> = <VALUE2> . . . <WHERE CONDITION>; </li></ul></ul></ul><ul><li>Example: </li></ul><ul><li>UPDATE EMP </li></ul><ul><ul><ul><ul><li>SET SAL = 2500, COMM = 500, MGR = 7900 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>WHERE EMPNO = 7934; </li></ul></ul></ul></ul><ul><li>Updating a single row or multiple rows is Subject to the WHERE clause. </li></ul><ul><li>  </li></ul>
  16. 16. Deleting Row(s) from a Table <ul><li>DELETE command is used to delete a row(s) from the table. </li></ul><ul><li>All the rows will be deleted with a single DELETE command if you do not give WHERE clause. </li></ul><ul><li>DELETE command used to delete a single row or a set of rows from the table is subject to the WHERE clause. </li></ul><ul><li>Syntax: </li></ul><ul><li>DELETE <TABLE-NAME> ; </li></ul><ul><li>  Example: </li></ul><ul><li>DELETE EMP (OR) DELETE FROM EMP; </li></ul><ul><li>  </li></ul><ul><li>The FROM keyword between DELETE and EMP is optional only, which doesn’t make any difference in the DELETE action. </li></ul><ul><li>  </li></ul>
  17. 17. Deleting a single record <ul><li>Syntax: </li></ul><ul><li>  Delete <Table-name> <Where Condition>; </li></ul><ul><li>  </li></ul><ul><li>Note: WHERE clause in the above syntax should identify a single record in the table, otherwise there is a chance to delete more than one record. </li></ul><ul><li>  </li></ul><ul><li>The following example will delete an employee from EMP table whose EMPNO = 7788; </li></ul><ul><li>  </li></ul><ul><li>Example: </li></ul><ul><li>Delete Emp Where Empno = 7788; </li></ul><ul><li>  </li></ul>
  18. 18. Deleting group of records <ul><li>Syntax: </li></ul><ul><li>  </li></ul><ul><li>Delete <Table-name> <Where Condition>; </li></ul><ul><li>  </li></ul><ul><li>Example: </li></ul><ul><li>Delete Emp Where Deptno = 10 ; </li></ul><ul><li>  </li></ul>
  19. 19. On Delete Cascade <ul><li>   </li></ul><ul><li>Row(s) cannot be deleted from a table when there is matching records in the child table. </li></ul><ul><li>It can be made possible with ON DELETE CASCADE option. </li></ul><ul><li>Whenever you delete a row(s) from parent table, then all the corresponding child rows will be deleted from the child table without any warning. </li></ul><ul><li>  </li></ul>
  20. 20. <ul><li>Example: </li></ul><ul><li>The following example is to create an EMPLOYEE table with ON DELETE CASCADE option. DEPT is a Parent Table for EMPLOYEE table. </li></ul><ul><li>  </li></ul><ul><li>CREATE TABLE EMPLOYEE (EMPNO NUMBER(4), ENAME VARCHAR2(20), DEPTNO NUMBER(2) CONSTRAINT EMP_DNO_FK REFERENCES DEPT(DEPTNO) ON DELETE CASCADE , SAL NUMBER(5)); </li></ul><ul><li>  </li></ul><ul><li>Now whenever you delete a row from DEPT table, then all the corresponding rows will be deleted from EMPLOYEE table. </li></ul><ul><li>  </li></ul><ul><li>   </li></ul>
  21. 21. <ul><li>The previous example illustrates the definition of ON DELETE CASCADE in column level creation. ON DELETE CASCADE can be created in the table level as well as using ALTER TABLE (DDL) command. </li></ul><ul><li>  </li></ul><ul><li>ON DELETE CASCADE option should come along with declaration of foreign key constraint only . It cannot be given separately after creating the foreign key constraint. </li></ul><ul><li>  </li></ul>
  22. 22. Transaction Control Language (TCL) <ul><li>Commit </li></ul><ul><li>Rollback and </li></ul><ul><li>Savepoint </li></ul><ul><li>The COMMIT command is used to make changes to data (inserts, updates, deletes) permanently. </li></ul><ul><li>The ROLLBACK command is used to discard parts of work or all the work the user has done in the current transaction. </li></ul><ul><li>The Savepoint statements are used discard or commit all the changes upto a point. </li></ul>
  23. 23. TCL contd.. <ul><li>The changes made by the user are not physically written to the table. </li></ul><ul><li>  The user has only a view of his/her work. </li></ul><ul><li>Other users have access to these tables to continue to work. </li></ul><ul><li>The COMMIT command is used to make the changes permanent to the database. </li></ul><ul><li>The user can continue with any number of inserts, updates and/or deletion, and still can undo the work by issuing the ROLLBACK command. </li></ul><ul><li>This is important in a case when an error id detected during these operations. </li></ul>
  24. 24. <ul><li>SQL has the facility to automatically commit all the work, without explicitly issuing the COMMIT command: </li></ul><ul><li>Example: </li></ul><ul><li>SET AUTOCOMMIT ON :Enables autocommit feature. </li></ul><ul><li>SET AUTOCOMMIT OFF :Is the default and disables the automatic committing. </li></ul><ul><li>Set Autocommit command can be issued with value. </li></ul><ul><li>Example: SET AUTOCOMIT 3; </li></ul><ul><li>The above Example will determine the 3 DML commands after which Oracle will issue a commit automatically. </li></ul><ul><li>SET AUTOCOMMIT ON, without any value, by default is takes 1 as a value. </li></ul><ul><li> Oracle will issue an automatic commit after each DML operation . </li></ul>
  25. 25. Implicit Commit <ul><li>  </li></ul><ul><li>The actions that force a commit to occur, even without issuing the COMMIT command are: </li></ul><ul><li>  </li></ul><ul><li>Creating any objects like Table, Views, Synonym etc.,(DDL operation). </li></ul><ul><li>Altering any objects through ALTER TABLE command (DDL operation). </li></ul><ul><li>Dropping any objects like Tables, Views, Synonym etc.,(DDL operation). </li></ul><ul><li>Granting and Revoking rights (DCL operations). </li></ul><ul><li>Connecting or Disconnecting from ORACLE. </li></ul><ul><li>  Quit or Exit from SQL. </li></ul>
  26. 26. Rollback <ul><li>Auto Rollback </li></ul><ul><li>  </li></ul><ul><li>After completion of any DML statement, if the user experiences serious difficulty such as hardware failure and no explicit commit is made, ORACLE will automatically ROLLBACK all </li></ul><ul><li>un-committed work. </li></ul>
  27. 27. <ul><li>1. SELECT * FROM STUDENT; </li></ul><ul><li> SID SNAME COURSE </li></ul><ul><li> ----- ----------- ------------ </li></ul><ul><li> 120 RAJIV ORACLE </li></ul><ul><li> 121 PRIYANKA JAVA </li></ul><ul><li> 122 ALEX D2K </li></ul><ul><li>2. INSERT INTO STUDENT VALUES(123, 'SALIM', 'VB6'); </li></ul><ul><li>3. INSERT INTO STUDENT VALUES(124, 'LAXMAN', 'ASP'); </li></ul><ul><li>4. SELECT * FROM STUDENT; </li></ul><ul><li>  </li></ul><ul><li>SID SNAME COURSE </li></ul><ul><li>--- ---------- ------------- </li></ul><ul><li> 120 RAJIV ORACLE </li></ul><ul><li>121 PRIYANKA JAVA </li></ul><ul><li> 122 ALEX D2K </li></ul><ul><li> 123 SALIM VB6 </li></ul><ul><li> 124 LAXMAN ASP </li></ul><ul><li>5. ROLLBACK; </li></ul>
  28. 28. <ul><li>6. SELECT * FROM STUDENT; </li></ul><ul><li>  </li></ul><ul><li>SID SNAME COURSE </li></ul><ul><li>------ ----------- -------------- </li></ul><ul><li>120 RAJIV ORACLE </li></ul><ul><li>121 PRIYANKA JAVA </li></ul><ul><li>122 ALEX D2K </li></ul><ul><li>ROLLBACK command in the 5 th statement discards the two-insert statement, which are issued in 2nd, and 3 rd statement. </li></ul>
  29. 29. <ul><li>7. UPDATE STUDENT SET COURSE = 'DEVELOPER 2000' WHERE SID = 122; </li></ul><ul><li>8.   SELECT * FROM STUDENT; </li></ul><ul><li>  </li></ul><ul><li>SID SNAME COURSE </li></ul><ul><li>----- ----------- ------------- </li></ul><ul><li>120 RAJIV ORACLE </li></ul><ul><li>121 PRIYANKA JAVA </li></ul><ul><li>122 ALEX DEVELOPER 2000 </li></ul><ul><li>7)           </li></ul><ul><li>9.   COMMIT; </li></ul>
  30. 30. <ul><li>10. SELECT * FROM STUDENT; </li></ul><ul><li>SID SNAME COURSE </li></ul><ul><li>---- -------- ---------- </li></ul><ul><li>120 RAJIV ORACLE </li></ul><ul><li>121 PRIYANKA JAVA </li></ul><ul><li>122 ALEX DEVELOPER 2000 </li></ul><ul><li>Update Student Set Sname = 'Rajiv Roy' Where Sid = 120; </li></ul><ul><li>12. SELECT * FROM STUDENT; </li></ul><ul><li>  </li></ul><ul><li>SID SNAME COURSE </li></ul><ul><li>---- ---------- ------------- </li></ul><ul><li>120 RAJIV ROY ORACLE </li></ul><ul><li>121 PRIYANKA JAVA </li></ul><ul><li>122 ALEX DEVELOPER 2000 </li></ul><ul><li>13. ROLLBACK; </li></ul><ul><li>  </li></ul>
  31. 31. <ul><li>14. Select * from Student’ </li></ul><ul><li>SID SNAME COURSE </li></ul><ul><li>----- ------------ -------------- </li></ul><ul><li>120 RAJIV ORACLE </li></ul><ul><li>121 PRIYANKA JAVA </li></ul><ul><li>122 ALEX DEVELOPER 2000 </li></ul><ul><li>ROLLBACK command in the 13 th statement has discarded the 11 th statement. </li></ul><ul><li>ROLLBACK command will undo only the statements, which are found after the latest COMMIT command (it could be implicit, explicit or autocommit). </li></ul>
  32. 32. Savepoint <ul><li>Suppose the user has </li></ul><ul><li>inserted a few rows into the EMP table </li></ul><ul><li>updated a row and </li></ul><ul><li>deleted a row </li></ul><ul><li>Then he/she realize that they have deleted a wrong row. </li></ul><ul><li>Normally, the tendency is to discard the entire transaction even though the insertions and updations are valid. </li></ul><ul><li>Because if commit is applied, a valid row entry will be lost. </li></ul><ul><li>This can be handled with the use of SAVEPOINTS. </li></ul>
  33. 33. What is a SavePoint? <ul><li>Savepoint is a point within a single logical transaction identified by a name and it’s purpose is to group or partition statements in a single logical statement. </li></ul><ul><li>So that, one can ROLLBACK TO A SPECIFIC POINT IN THE TRANSACTION. </li></ul>
  34. 34. Points to be noted while naming savepoints are: <ul><li>Savepoint names must be unique to a transaction. </li></ul><ul><li>If the name of a new savepoint within a transaction is same as the earlier one then new one replaces the earlier one. </li></ul><ul><li>Once a COMMIT or an un-conditional ROLLBACK is issued, all the savepoints are erased. </li></ul><ul><li>Implicit commit too erases all the Savepoints. </li></ul><ul><li>Savepoint name must start with Alphabet. </li></ul><ul><li>Savepoint name is not case sensitive . </li></ul>