Académique Documents
Professionnel Documents
Culture Documents
CREATE TABLE table_one ( col_one NUMBER, col_two CHAR(1) ); CREATE TABLE table_two ( col_one NUMBER, col_two CHAR(1) ); INSERT INTO table_one VALUES (1, 'a'); INSERT INTO table_one VALUES (2, 'b'); INSERT INTO table_one VALUES (3, 'c'); INSERT INTO table_two VALUES (2, 'B'); INSERT INTO table_two VALUES (3, 'C'); INSERT INTO table_two VALUES (4, 'D'); SELECT * FROM table_one t1 left outer join table_two t2 ON t1.col_one = t2.col_one; COL_ONE ---------2 3 1 C COL_ONE C - ---------- b 2 B c 3 C a
SELECT * FROM table_one t1 right outer join table_two t2 ON t1.col_one = t2.col_one; COL_ONE ---------2 3 C COL_ONE C - ---------- b 2 B c 3 C 4 D
SELECT * FROM table_one t1 full outer join table_two t2 ON t1.col_one = t2.col_one; COL_ONE ---------2 3 1 C COL_ONE C - ---------- b 2 B c 3 C a 4 D
);
txt VARCHAR2(10)
CREATE TABLE right_tbl ( id NUMBER, txt VARCHAR2(10) ); INSERT INSERT INSERT --insert INSERT INSERT --insert INSERT INSERT INSERT ----INTO INTO INTO into INTO INTO into INTO INTO INTO left_tbl left_tbl left_tbl left_tbl left_tbl right_tbl right_tbl right_tbl right_tbl right_tbl VALUES VALUES VALUES values VALUES VALUES values VALUES VALUES VALUES (1, (2, (3, (4, (5, (1, (2, (3, (4, (5, 'one' 'two' 'three' 'four' 'five' ); ); ); ); );
A full join returns the records of both tables (that satisfy a [potential] where condition). In the following example, 4 cuatro and 2 two are returned, although the ids 4 and 2 are not present in both tables: id, l.txt, r.txt left_tbl l full join right_tbl r using(id) id; TXT ---------one two three five TXT ---------uno tres cuatro cinco
SELECT
FROM
ID ---------1 2 3 4 5
'one'); 'two');
INSERT INTO table_two VALUES (10, 'ten'); INSERT INTO table_two VALUES (20, 'twenty'); INSERT INTO table_two VALUES ( 5, 'five'); SELECT * FROM table_one cross join table_two; -- Each row from table_one is returned together -- with each row from table_two: COL_ONE ---------1 1 1 2 2 2 COL_TWO COL_THREE COL_FOUR ---------- ---------- ---------one 10 ten one 20 twenty one 5 five two 10 ten two 20 twenty two 5 five
-- Note: col_two is only returned once here instead of twice -- when using is used instead of on. This is because it must -- be the same value: COL_TWO COL_ONE COL_ONE ---------- ---------- ---------5 3 4 5 3 5
FROM
-- for example: SELECT Person.LastName, Person.FirstName, Sales.OrderNo FROM Person FULL JOIN Sales ON Person.P_Id=Sales.P_Id ORDER BY Person.LastName -- alternate syntax: SELECT * FROM employee LEFT JOIN department ON employee.DepartmentID = department.DepartmentID UNION SELECT * FROM employee RIGHT JOIN department ON employee.DepartmentID = department.DepartmentID WHERE employee.DepartmentID IS NULL
SELECT * FROM employee LEFT OUTER JOIN department ON employee.DepartmentID = department.DepartmentID -- for example: SELECT Person.LastName, Person.FirstName, Sales.OrderNo FROM Person LEFT JOIN Sales ON Person.P_Id=SalesP_Id ORDER BY Person.LastName
Left Outer Join SELECT p.last_name, t.title_name FROM person p, role r WHERE p.title_1 = r.title(+); Returns ALL records FROM role TABLE AND ALL matching records FROM person TABLE
Right Outer Join SELECT p.last_name, t.title_name FROM person p, role r WHERE p.title_1 (+)= r.title; Returns ALL records FROM person TABLE AND ALL matching records FROM role TABLE
Having Clause
column column column column column owner format a10 table_name format a32 index_name format a32 maxext format 99999 numext format 99999
prompt CHECK Tables SELECT t.owner, t.table_name, MAX(t.max_extents) maxext, COUNT(extent_id) numext FROM sys.dba_tables t, sys.dba_extents e WHERE t.table_name = e.segment_name AND t.owner = e.owner AND e.segment_type = 'TABLE' GROUP BY t.owner, t.table_name HAVING COUNT(extent_id)/ MAX(t.max_extents) > .8 / prompt CHECK Indexes SELECT t.owner, t.index_name, MAX(t.max_extents) maxext, COUNT(extent_id) numext FROM sys.dba_indexes t, sys.dba_extents e
WHERE t.index_name = e.segment_name AND t.owner = e.owner AND e.segment_type = 'INDEX' GROUP BY t.owner, t.index_name HAVING COUNT(extent_id)/ MAX(t.max_extents) > .8 /
oracle supports the user defined datatypes,where as mysql not support all these types of datatypes, like user defined , and cursors like these stuff;;
TRUNCATE
TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a DELETE.
SQL> TRUNCATE TABLE emp; Table truncated. SQL> SELECT COUNT(*) FROM emp; COUNT(*) ---------0
DROP
The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back.
SQL> DROP TABLE emp; Table dropped. SQL> SELECT * FROM emp; SELECT * FROM emp * ERROR at line 1: ORA-00942: table or view does not exist
DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. Therefore DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.
PS: DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. As such, DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.
What are the difference between DDL, DML and DCL commands? up How does one escape special characters when writing SQL queries?
1>TRUNCATE is a DDL command whereas DELETE is a DML command. 2>TRUNCATE is much faster than DELETE. Reason:When you type DELETE.all the data get copied into the Rollback Tablespace first.then delete operation get performed.Thatswhy when you type ROLLBACK after deleting a table ,you can get back the data(The system get it for you from the Rollback Tablespace).All this process take time.But when you type TRUNCATE,it removes data directly without copying it into the
Rollback Tablespace.Thatswhy TRUNCATE is faster.Once you Truncate you cann't get back the data. 3>You cann't rollback in TRUNCATE but in DELETE you can rollback.TRUNCATE removes the record permanently. 4>In case of TRUNCATE ,Trigger doesn't get fired.But in DML commands like DELETE .Trigger get fired. 5>You cann't use conditions(WHERE clause) in TRUNCATE.But in DELETE you can write conditions using WHERE clause
DELETE,DROP,TRUNCATE
Submitted by shoaib on Thu, 2007-01-18 00:04.
DELETE Delete is the command that only remove the data from the table. It is DML statement. Deleted data can be rollback. By using this we can delete whole data from the table(if use without where clause).If ew want to remove only selected data then we should specify condition in the where clause SQL>delete from employee;(this command will remove all the data from table) SQL>delete from employee where employee_name='JOHN';(This command will remove only that row from employee table where employee_name is JOHN'); DROP: Drop command remove the table from data dictionary. This is the DDL statement. We can not recover the table before Oracle 10g. But Oracle 10g provide the command to recover it by using the command (FLASHBACK) TRUNCATE: This is the DML command. This command delete the data from table. But there is one difference from ordinary delete command. Truncate command drop the storage held by this table. Drop storage can be use by this table again or some other table. This is the faster command because it directly drop the storage
DELETE,DROP,TRUNCATE
Submitted by Sandip Mitra (not verified) on Fri, 2007-01-19 01:36.
DROP command is a DDL command. It removes the information along with structure. It also removes all information about the table from data dictionary. TRUNCATE command is DDL command. It removes all the information of the table. Regarding performance if you have to delete all the rows of a table you should perform TRUNCATE command with DROP STORAGE option.
DELETE is a DML command. It provides the facility of conditional-based deletion. It also generates REDO information.
NOTE:The WHERE clause in the sql delete command is optional and it identifies the rows in the column that gets deleted. If you do not include the WHERE clause all the rows in the table is deleted, so be careful while writing a DELETE query without WHERE clause. For Example: To delete an employee with id 100 from the employee table, the sql delete query would be like,
DELETE FROM employee WHERE id = 100;
To delete all the rows from the employee table, the query would be like,
DELETE FROM employee;
For Example: To delete all the rows from employee table, the query would be like,
TRUNCATE TABLE employee;
Difference between DELETE and TRUNCATE Statements: DELETE Statement: This command deletes only the rows from the table based on the condition given in the where clause or deletes all the rows from the table if no condition is specified. But it does not free the space containing the table. TRUNCATE statement: This command is used to delete all the rows from the table and free the space containing the table.
For Example: To drop the table employee, the query would be like
DROP TABLE employee;
Difference between DROP and TRUNCATE Statement: If a table is dropped, all the relationships with other tables will no longer be valid, the integrity constraints will be dropped, grant or access privileges on the table will also be dropped, if want use the table again it has to be recreated with the integrity constraints, access privileges and the relationships with other tables should be established again. But, if a table is truncated, the table structure remains the same, therefore any of the above problems will not exist.
DELETE
and
DELETE 1. DELETE is a DML Command. 2. DELETE statement is executed using a row lock, each row in the table is locked for deletion. 3. We can specify filters in where clause 4. It deletes specified data if where condition exists. 5. Delete activates a trigger because the operation are logged individually. 6. Slower than truncate because, it keeps logs. 7. Rollback is possible. TRUNC ATE 1. TRUNCATE is a DDL command. 2. TRUNCATE TABLE always locks the table and page but not each row. 3. Cannot use Where Condition. 4. It Removes all the data. 5. TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions. 6. Faster in performance wise, because it doesn't keep any logs. 7. Rollback is not possible. DELETE and TRUNCATE both can be rolled back when used with TRANSACTION. If Transaction is done, means COMMITED, then we can not rollback TRUNCATE command, but we can still rollback DELETE command from LOG files, as DELETE
write records them in Log file in case it is needed to rollback in future from LOG files. 1) DELETE IS AN DML COMMAND 2) DELETE CAN ROLLBACK 3) DELETE CAN USE WHERE CONDITION 4) DELETE IS SLOWER THAN TRUNCATE TRUNCATE: 1) TRUNCATE CANNOT ROLLBACK. 2) CANNOT USE WHERE CONDITION 3) Cannot use Where Condition. 4) It Removes all the data. 5) TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions. 6) Faster in performance wise, because it doesn't keep any logs. 7) Rollback is not possible. DELETE -Removes some or all rows from a table. - A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. - Causes all DELETE triggers on the table to fire. - It deallocated records row-by-row in transaction logs and thus is slower than TRUNCATE. - According to MS BOL, if a table is a heap or no clustered index is defined than the row-pages emptied are not deallocated and remain allocated in the heap. Thus no other object can reuse this associated space. Thus to deallocate the space a Clustered index is required or TABLOCK hint should be applied in the DELETE statement. - Thus it requires more locks and database resources. - This is a DML command as its is just used to manipulate/modify the table data. It does not change the property of a table.
TRUNCATE - Removes all rows from a table. - Does not require a WHERE clause, not allowed here. - Identity columns are re-seeded on this operation, if no seed was defined then the default value 1 is used. - No triggers are fired on this operation because it does not log individual rows. - It deallocates data pages instead of rows in transaction logs, thus is faster than DELETE.
- Thus it also requires less number of locks. - TRUNCATE is not possible when a table is reference by a Foreign Key or tables used in replication or with Indexed views. - This is a DDL command as its resets identity columns, deallocates data pages and empty them for use of other objects in the database.
TRUNCATE faster and uses fewer system and transaction log resources than DELETE. removes the data by deallocating the data pages used to store the tables data, and only the page deallocations are recorded in the transaction log. removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column. You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint; Because TRUNCATE TABLE is not logged, it cannot activate a trigger. No Roll back DDL Command
DELETE removes rows one at a time and records an entry in the transaction log for each deleted row. If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement. use DELETE statement without a WHERE clause Activates Trigger Can Rollback DML Command
If Transaction is done, means COMMITED, then we can not rollback TRUNCATE command, but we can still rollback DELETE command from LOG files, as DELETE write records them in Log file in case it is needed to rollback in future from LOG files.
DELETE statement features : The DELETE statement deletes table rows and returns number of rows deleted.we can delete selected no of records from table using DELETE command.DELETE statement has two modifiers namely LOW_PRIORITY and QUICK. When we have to use DELETE Command then the data deleted can be retrieved when you ROLLBACK . QUICK modifier is specified then the table handler does not merge index leaves during delete, this may cause to speed up certain kind of deletes.
LIMIT clause can also be used to set a limit on the number of rows to be deleted. A DELETE is issued with no WHERE clause, all rows are deleted. ORDER BY clause can be used in DELETE statement. In this case, the rows are deleted in the specified order.
Note : With LOW_PRIORITY keyword, DELETE execution is delayed until no other clients are reading from the table and If WHERE clause is specified then rows are deleted satisfying the given conditions and finally returns the number of rows deleted. With no WHERE clause in AUTOCOMMIT mode, DELETE works as TRUNCATE and does not return affected rows. For Example :
TRUNCATE statement features : TRUNCATE TABLE differs from DELETE in the following ways : TRUNCATE drops the table and re-create it. It is much faster than deleting rows one by one. Once u use TRUNCATE command then u cannot retrieve the data again from the table. TRUNCATE is not transaction-safe; an error occurs when attempting one in the course of an active transaction or active table lock. TRUNCATE removes all the rows from the Table. TRUNCATE does not return number of deleted rows. TRUNCATE functionality is an Oracle SQL extension adopted in MySQL.
TRUNCATE is probably better thought of as a shortcut for DROP TABLE/CREATE TABLE rather than a quirky kind of DELETE.
When TRUNCATE is not transaction-safe,an error occurs when attempting one in the course of an active transaction or active table lock.With TRUNCATE TABLE, a table can be recreated (as an empty table) when only the table format file tbl_name.frm is valid, and its data or index files have become corrupted.With TRUNCATE TABLE, the table handler reset the AUTO_INCREMENT value starting from the beginning. This is true even for MyISAM and InnoDB engines. Note: In case of partitioned tables, TRUNCATE TABLE drops the data and index files and recreates but preserves the partition definitions (.par) file.TRUNCATE TABLE works similar to DELETE if there are foreign key constraints that reference the table; otherwise it simply drops and re-creates the table. The AUTO_INCREMENT value is reset by TRUNCATE TABLE even ignoring foreign key constraint, if there is. For Example :
Note : After truncate this table do not contains any fields but it show in our database by using the following command.
DROP Statement Features : Drop - deletes the data as well as structure. The difference between DROP and DELETE table is that, after executing DELETE statement the contents of table are removed but the structure remains same, but in case of DROP statement both the contents and structure are removed. Syntax : mysql>DROP TABLE table_name;