Você está na página 1de 7

Oracle : DML - Presentation Transcript 1.

1 Using ORACLE Data Manipulation Language (Populating our sample database) and Transaction Control Language 2. 2 DATA DEFINATION LANGUAGE DML is used for manipulation the data contained in a table. Just as how DDL commands can be used to CREATE,ALTER and DROP objects DML can be used to INSERT , UPDATE and DELETE data from the objects.DML commands only affect the data and not the structure of the table. DML contains 3 commands: INSERT : Used to insert a new record into the table. UPDATE : Used to update the records already inserted into the table. DELETE : Used to delete records already inserted into the table. 3. 3 INSERT COMAND The INSERT command is used to insert records into the table. The basic syntax of the INSERT command is : INSERT INTO TABLE_NAME(colm1_name,[colmn_name]) VALUES ( colm1_val,[colmn_val]); EXAMPLE : INSERT INTO InfoTable(name,age,phone) VALUES (steve,50,9988776655); The above syntax is used when we only want to insert specified column values.If we are inserting a row containing all the columns value or intend to insert NULL values in remaining columns we can discard the column list after table name as: INSERT INTO TABLE_NAME VALUES(value[value]..[NULL]); The NULL keyword is used to insert a null values into the column. 4. 4 UPDATE COMAND The UPDATE command is used to modify the records that have been already inserted into the table. We use a WHERE clause to target a particular set of columns to be updated in absence of which all rows in the table will be updated. SYNTAX: UPDATE TABLE_NAME SET column = new_value,. We can update multiple columns at a time. ..[column = new value] WHERE [condition .]; WHERE condition limits the rows being updated. EXAMPLE : UPDATE InfoTable SET age = 40 , phone = 9988000000 WHERE name = steve;

5. 5 UPDATE USING SUBQUERIES We can update the value of a row to be set to values of another row using subqueries : SYNTAX: UPDATE TABLE_NAME SET column = [subquery] ..column = value WHERE (condition); EXAMPLE : UPDATE InfoTable SET phone= (SELECT phone FROM InfoTable WHERE name = bill) WHERE age = 40; 6. 6 DELETE COMAND The DELECT command can be used to delete a set of records that are inserted in a column. A WHERE clause is used to narrow the set of rows to be deleted in absence of which all the rows in the table will be deleted. To delete all rows we can also use the TRUNCATE command. SYNTAX: DELETE [FROM] TABLE_NAME WHERE (condition..); EXAMPLE: DELETE FROM InfoTable WHERE name = bill; DELETE FROM InfoTable;Entire table contents deleted 7. 7 TRANSACTION CONTROL LANGUAGE A TRANSACTION is a set of DML statements that cause a consistent change on data or it can be a single DDL / DCL command . The transaction begins when a DML command is entered and ends when COMMIT or ROLLBACK occurs. Since DCL and DDL commands are committed as soon as been entered only 1 such command is in a transaction. TCL statements are used to control transactions. There are three TL commands:

INSERT, UPDATE and DELETE statements have to comply with constraints such as NOT NULL, UNIQUE or CHECK. Also primary or foreign constraints can not be violated.

INSERT

INSERT is used to add data to the database. An INSERT statement using a VALUES clause inserts a single row as opposed to an INSERT statement with a subquery which can insert potentially multiple rows selected by the subquery. The INSERT command can also insert data into an updatable view's base table.
y y y y y y y

schema - owner. table_name or view_name. @dbl - database link for a remote table or view. subquery1 - a subquery that is identical to a view query. subquery2 - a subquery that returns rows or values to be inserted. column - column name(s) to be inserted into. If omitted, all the table or view columns must have a matching value or subquery2 item. expr - an expression or list of values corresponding to the inserted values.

UPDATE

y y y y y y y y y y

schema - owner. table_name, view_name or snapshot. @dbl - database link for a remote table or view. t_alias - object name alias. subquery1 - a subquery that is in the same format as a view definition. subquery2 - a subquery that returns values to be used to update the object. subquery3 - a subquery that returns a single value. column - column name(s) to be updated. Lists must be in parentheses. expr - new value(s) assigned to the subquery or column list of columns. WHERE - clause limiting rows to be updated. If no WHERE clause present then all rows are updated.

DELETE

The DELETE statement removes rows from tables allowing ROLLBACK or COMMIT. In contrast, TRUNCATE deletes rows without the possibility of ROLLBACK.
y y y y

schema - owner. table_name or view_name. @dbl - database link for a remote table or view. alias - table, view or subquery shorthand used in a WHERE clause, for instance,
SELECT a.FIELD, b.FIELD FROM TABLE1 a, TABLE2 b WHERE a.FIELD = b.FIELD;

y y

subquery - a subquery that selects data to be deleted. The subquery can not query a table which appears in the same FROM clause as the subquery. WHERE condition - deletion based on a condition.

SELECT

The SELECT statement is used to retrieve database values from a table/view or set of tables/views based on given selection criteria. An unrestricted SELECT retrieves all rows. Additionally, the SELECT statement is the basis for all subqueries in all other commands. Results are sorted in ascending order unless specified by DESC. The minimum form of the SELECT statement is as follows.
SELECT * FROM <table_name>;

Note that the DUAL table is required when doing non-directed SELECT statements such as from sequences or non-table related, single-row functions. If two or more tables have common column names in a SELECT statement then the table name or table alias must be used to differentiate between the columns. Column aliases can only be used in the ORDER BY clause. When the DISTINCT operator is used then the total number of bytes selected in all select list expressions is limited to the size of a data block less a small overhead.
y

DISTINCT - returns only one copy of any duplicate rows or of specific columns determined by the position of the DISTINCT and returned by the SELECT. If the DISTINCT is placed immediately following the SELECT, all

y y y y y y y y y y y

y y

combinations of the columns that follow will be distinct (unique with respect to all other rows selected). ALL - the default. Returns all rows including duplicates. * - all columns. expr - selects an expression. c_alias - column alias used in subsequent statements in the SELECT statement to refer to the aliased column. schema - owner. table_name, table, view_name, view or snapshot. @dblink - database link for a remote table or view. subquery - subquery which is specified identically to a view query definition. WITH READ ONLY - subquery can not be updated. Applies to views only since this is the only way in which a subquery can be updated. t_alias - table alias used in subsequent subqueries or WHERE clause conditions. WHERE - the WHERE clause is used to restrict the rows returned. Only WHERE clauses use indexes. Thus the WHERE clause must match an index or parts of an index in the order of the columns in an index beginning with the first column otherwise a full table-scan will result. A SELECT command between two tables without a WHERE clause will return the Cartesian Product of both tables. The Cartesian Product would contain each row in the first table matched with each row of the second table. ORDER BY - the ORDER BY clause forces ordering of returned rows with either ASC (ascending order - the default) or DESC (descending order). Note that the ORDER BY clause is only applied to the results of the SELECT statement after the WHERE clause has been applied. Thus it is not neccessary to include the contents of the WHERE clause into the ORDER BY clause since the WHERE clause has already ordered that set. The only case where the ORDER BY clause would contain columns contained in the WHERE clause would be when the WHERE clause must be over-ridden by the ORDER BY clause; this is extremely inefficient. It is best to use the WHERE clause, indexes and the ORDER BY clause in concert in order to achieve the most efficient return time of the result set of rows. START WITH CONNECT BY - returns rows in a hierarchical order. GROUP BY - the GROUP BY clause groups rows based on specified column values. The expression for the SELECT statement must include atleast one grouping function such as COUNT() and all columns not participating in the grouping functions must be included in the GROUP BY column list. The GROUP BY clause does not restrict values but simply groups them into coagulated sets of values. The GROUP BY clause can use a column name but not the column positional specification or column alias. Note the two examples

below. It should be stated that application of the WHERE clause would be faster than that of the HAVING clause since the HAVING clause is applied to the resulting grouped by set rather than the WHERE clause applying to the database on retrieval of the data. In general the WHERE clause will filter rows out of the result before those rows are grouped and functions are performed on the groups. Therefore the WHERE clause is faster than the HAVING clause.
SELECT DEPT, EMPLOYEE, AVG(SALARY) FROM EMPLOYEES WHERE DEPT IN (20, 30) GROUP BY DEPT, EMPLOYEE; SELECT DEPT, EMPLOYEE, AVG(SALARY) FROM EMPLOYEES GROUP BY DEPT, EMPLOYEE HAVING DEPT IN (20, 30); y

y y y y y

HAVING - the HAVING clause must restrict the GROUP BY for those groups for which the HAVING condition is TRUE. There can not be a HAVING clause without a GROUP BY clause. UNION, UNION ALL, INTERESECT and MINUS - SELECT statement set operation. ASC or DESC - ascending or descending order. FOR UPDATE - locks selected rows. OF - only locks rows of given table. NOWAIT - returns the statement initiator (user or program) the control if the SELECT statement attempts to lock a row which is locked by another user. Otherwise the SELECT statement will wait for any locked rows to be free and return the results when they are free.

Você também pode gostar