Vous êtes sur la page 1sur 21

ANSI Joins: OUTER JOIN

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

ANSI Joins: FULL JOIN


CREATE TABLE left_tbl ( id NUMBER,

);

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' ); ); ); ); );

'uno' ); 'dos' ); 'tres' ); 'cuatro'); 'cinco' );

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

DROP TABLE left_tbl; DROP TABLE right_tbl;

ANSI Joins: CROSS JOIN


CREATE TABLE table_one ( col_one NUMBER, col_two VARCHAR2(10) ); CREATE TABLE table_two ( col_three NUMBER, col_four VARCHAR2(10) );

INSERT INTO table_one VALUES ( 1, INSERT INTO table_one VALUES ( 2,

'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

ANSI Joins: INNER JOIN


CREATE TABLE table_one ( col_one NUMBER, col_two NUMBER ); CREATE TABLE table_two ( col_one NUMBER, col_two NUMBER ); INSERT INTO table_one VALUES ( 1, 1); INSERT INTO table_one VALUES ( 3, 5); INSERT INTO table_one VALUES ( 5, 9); INSERT INTO table_two VALUES ( 4, 5); INSERT INTO table_two VALUES ( 6, 3); INSERT INTO table_two VALUES ( 5, 5); SELECT * FROM table_one t1 inner join table_two t2 ON t1.col_one = t2.col_two; COL_ONE COL_TWO COL_ONE COL_TWO ---------- ---------- ---------- ---------5 9 4 5 3 5 6 3 5 9 5 5 SELECT * FROM table_one t1 inner join table_two t2 using (col_two);

-- 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

Self-join example and syntax


SELECT F.EmployeeID, F.LastName, S.EmployeeID, S.LastName, F.Country FROM Employee F, Employee S WHERE F.Country = S.Country AND F.EmployeeID < S.EmployeeID ORDER BY F.EmployeeID, S.EmployeeID; -- FOR this example, note that: -- F and S are aliases FOR the first and second copies of the -- employee table. -- The condition F.Country = S.Country excludes pairings between -- employees in different countries. The example question only -- wanted pairs of employees in the same country. -- The condition F.EmployeeID < S.EmployeeID excludes pairings -- where the EmployeeIDs are the same. -- F.EmployeeID < S.EmployeeID also excludes duplicate pairings. -- The effect of outer joins can also be obtained using -- correlated subqueries. FOR example: SELECT employee.LastName, employee.DepartmentID, department.DepartmentName FROM employee LEFT OUTER JOIN department ON employee.DepartmentID = department.DepartmentID -- this can also be written AS: SELECT employee.LastName, employee.DepartmentID, (SELECT department.DepartmentName FROM department WHERE employee.DepartmentID = department.DepartmentID ) FROM employee

FULL JOIN example and syntax


SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name -- or SELECT *

FROM

employee FULL OUTER JOIN department ON employee.DepartmentID = department.DepartmentID

-- 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

RIGHT JOIN example and syntax


SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name -- or SELECT * FROM employee RIGHT OUTER JOIN department ON employee.DepartmentID = department.DepartmentID -- for example: SELECT Person.LastName, Person.FirstName, Sales.OrderNo FROM Person RIGHT JOIN Sales ON Persons.P_Id=Sales.P_Id ORDER BY Person.LastName

LEFT JOIN example and syntax


SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name -- or

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

INNER JOIN example and syntax


SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name -- for example: SELECT Person.LastName, Person.FirstName, Sales.OrderNo FROM Person INNER JOIN Sales ON Person.P_Id=Sales.P_Id ORDER BY Person.LastName

INNER JOIN example and syntax


SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name -- for example: SELECT Person.LastName, Person.FirstName, Sales.OrderNo FROM Person INNER JOIN Sales ON Person.P_Id=Sales.P_Id ORDER BY Person.LastName

WHERE Clause - Joins


SELECT p.last_name, t.title_name FROM person p, title t WHERE p.title_1 = t.title_abbrev; SELECT p.last_name, r.role_name FROM person p, person_role_ie i, person_role r WHERE p.person_id = i.person_id AND i.role_id = r.role_id;

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 /

Differences between SQL and MySQL


SQL stands for Structured Query Language. It's a standard language for accessing and manipulating databases. MySQL is a database management system, like SQL Server 2005, Oracle, Informix, Postgres etc. MySQL is a RDMS (Relational Database Management System). All types of RDMB use SQL. SQL is used to manipulate database or to create a database. It's actually a common language. MySQL is an actual computer application. You must need to download or collect it and install it. MySQL is one of the most popular open source database management system. MySQL has an SQL interpreter. MySQL can be used as the back engine database for several kinds of applications and it's one of the best choice for many web programmers for web-base application. All these are the differences between SQL and MySQL. If you want to mention further differences, you may add them to the comment section.

What are the differences between MySQL and SQL?


SQL is short for Structured Query Language, and MySQL is a database management system, like SQL Server 2005, Oracle. An example would be other programming languages like BASIC and QBASIC where BASIC is the generic term for the BASIC programming language and QBASIC is Microsoft's version (quick BASIC) and TURBO BASIC was Borlands version.

oracle supports the user defined datatypes,where as mysql not support all these types of datatypes, like user defined , and cursors like these stuff;;

Difference between TRUNCATE, DELETE and DROP commands


DELETE
The DELETE command is used to remove 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. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire.
SQL> SELECT COUNT(*) FROM emp; COUNT(*) ---------14 SQL> DELETE FROM emp WHERE job = 'CLERK'; 4 rows deleted. SQL> COMMIT; Commit complete. SQL> SELECT COUNT(*) FROM emp; COUNT(*) ---------10

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.

From Oracle 10g a table can be "undropped". Example:


SQL> FLASHBACK TABLE emp TO BEFORE DROP; Flashback complete.

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?

Login to post comments

Difference between TRUNCATE and DELETE commands


Submitted by Dipal havsar (not verified) on Tue, 2006-09-19 07:39.

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.

List 10 difference between TRUNCATE & DELETE statement in SQL Server?


TRUNCATE V/s DELETE
Delete DELETE is a DML command DELETE statement is executed using a row lock, TRUNCATE TABLE always locks the table and each row in the table is locked for page but not each row deletion Cannot use Where Condition We can specify filters in where clause It Removes all the data It deletes specified data if where condition exists. TRUNCATE TABLE cannot activate a trigger Delete activates a trigger because the operation because the operation does not log individual row are logged individually. deletions. Faster in performance wise, because it Slower than truncate because, it maintain logs for every is minimally logged in transaction log. record Truncate TRUNCATE is a DDL command Drop all objects statistics and marks like High keeps objects statistics and all allocated space. After a Water Mark free extents and leave the object DELETE statement is executed,the table can really empty with the first extent. zero pages are still contain empty pages. left in the table TRUNCATE TABLE removes the data by The DELETE statement removes rows one at a time deallocating the data pages used to store the table and records an entry in the transaction log for data and records only the page deallocations in the each deleted row transaction lo If the table contains an identity column, the counter for that column is reset to the seed value DELETE retain the identity that is defined for the column Restrictions on using Truncate Statement 1. Are referenced by a FOREIGN KEY constraint. 2. Participate in an indexed view. Delete works at row level, thus row level constrains apply 3. Are published by using transactional replication or merge replication.

SQL Delete Statement


The DELETE Statement is used to delete rows from a table. The Syntax of a SQL DELETE statement is:

DELETE FROM table_name [WHERE condition];

table_name -- the table name which has to be updated.

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;

SQL TRUNCATE Statement


The SQL TRUNCATE command is used to delete all the rows from the table and free the space containing the table.

Syntax to TRUNCATE a table:


TRUNCATE TABLE table_name;

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.

SQL DROP Statement:


The SQL DROP command is used to remove an object from the database. If you drop a table, all the rows in the table is deleted and the table structure is removed from the database. Once a table is dropped we cannot get it back, so be careful while using DROP command. When a table is dropped all the references to the table will not be valid.

Syntax to drop a sql table structure:


DROP TABLE table_name;

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.

What is the difference between TRUNCATE in SQL Server

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.

What is difference between TRUNCATE & DELETE


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
TRUNCATE is a DDL command and cannot be rolled back. All of the memory space is released back to the server. DELETE is a DML command and can be rolled back. Both commands accomplish identical tasks (removing all data from a table), but TRUNCATE is much faster. TRUNCATE : You can't use WHERE clause DELETE : You can use WHERE clause truncate = delete+commit -so we cant roll back delete = delete- so it can be rolled back Delete - delete deletes the records from table it can be rollbacked also you can give the where condiition to it. Truncate - delete all records from table There is no rollback it always commit without givening the commit

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

Difference between delete and truncate


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. TRUNCATE 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.

Difference between delete, truncate and drop statements of MySQL


In MySQL table if i have to modify and delete then we have to use some statements on table. But these commands are different to each other and his working so different.Thus I have to explain the differences between DELETE ,DROP and TRUNCATE statements of MySQL. For Example : Use the following table 'student' and apply these commands.

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;

Vous aimerez peut-être aussi