Your SlideShare is downloading. ×
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Sql   dml & tcl 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Sql dml & tcl 2

1,143

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,143
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×