Page 57 - SQL
P. 57

Chapter 14: DELETE




        Introduction



        The DELETE statement is used to delete records from a table.


        Syntax



            1.  DELETE FROM TableName [WHERE Condition] [LIMIT count]


        Examples


        DELETE certain rows with WHERE



        This will delete all rows that match the WHERE criteria.


         DELETE FROM Employees
         WHERE FName = 'John'


        DELETE all rows


        Omitting a WHERE clause will delete all rows from a table.


         DELETE FROM Employees


        See TRUNCATE documentation for details on how TRUNCATE performance can be better
        because it ignores triggers and indexes and logs to just delete the data.


        TRUNCATE clause


        Use this to reset the table to the condition at which it was created. This deletes all rows and resets
        values such as auto-increment. It also doesn't log each individual row deletion.


         TRUNCATE TABLE Employees



        DELETE certain rows based upon comparisons with other tables


        It is possible to DELETE data from a table if it matches (or mismatches) certain data in other tables.

        Let's assume we want to DELETEdata from Source once its loaded into Target.


         DELETE FROM Source
         WHERE  EXISTS ( SELECT 1 -- specific value in SELECT doesn't matter
                        FROM Target
                        Where Source.ID = Target.ID )


        https://riptutorial.com/                                                                               39
   52   53   54   55   56   57   58   59   60   61   62