Computer Science

SQL DELETE

SQL DELETE is a command used to remove one or more rows from a table in a database. It is used to delete specific data from a table based on certain conditions. The DELETE statement can be used with or without a WHERE clause to specify the rows to be deleted.

Written by Perlego with AI-assistance

7 Key excerpts on "SQL DELETE"

  • Book cover image for: MCSA Guide to Microsoft SQL Server 2012 (Exam 70-462)
    • Faisal Akkawi, Kayed Akkawi, Schofield, , Faisal Akkawi, Kayed AkkawiSchofield(Authors)
    • 2013(Publication Date)
    A DELETE statement is used to delete rows from a table, and it uses the following syntax: DELETE FROM [Table Name] WHERE [Criteria]; Because the scope of a DELETE statement is at the row level, no columns need to be specified in the DELETE clause. The filter criteria syntax is identical to the SELECT statement, and it is used to instruct the database engine which records to delete. Be extremely careful when executing the DELETE statement without a WHERE clause because this will result in all the rows from the referenced table being deleted. It is a good idea before running a DELETE statement to first execute the equivalent SELECT statement with an asterisk to validate the filter criteria and then replace the SELECT command with the DELETE keyword. An alternative option is to nest the statement inside BEGIN TRANSACTION and ROLLBACK TRANSACTION statements that will execute the transaction without committing any changes to the database. Once the query has been thoroughly tested, the ROLLBACK TRANSACTION can be replaced with a COMMIT TRANSACTION to write the changes to the database. An INSERT statement is used to insert a new row into a table. To insert a single row into a table, use the VALUES clause and type the individual column values into a comma-separated list as follows: INSERT INTO [Table Name] (Column1, Column2, Column3) VALUES (Value1, Value2, Value3) A SELECT statement can be used in place of the VALUES clause to insert multiple rows into a table at once: INSERT INTO [Table Name] (Column1, Column2, Column3) SELECT Value1, Value2, Value3 FROM [Table Name2] WHERE [Criteria]; Keep in mind the following key requirements when creating an INSERT statement: • The column values in the VALUES or SELECT clause must match the order and data types of the columns specified in the INSERT clause. • The column values in the VALUES or SELECT clause must respect any constraints present in the table, for example foreign keys or NOT NULL constraints.
  • Book cover image for: Understanding Databases
    eBook - PDF

    Understanding Databases

    Concepts and Practice

    • Suzanne W. Dietrich(Author)
    • 2021(Publication Date)
    • Wiley
      (Publisher)
    For the set null or set default actions, an update to a primary key results in setting its corresponding foreign keys to either null or its default value, respectively. Delete The delete statement removes tuples from a table with an optional where clause. The delete statement removes tuples from the table specified. The optional where clause restricts the delete operation to the rows that satisfy the DELETE-CONDITION. If the where clause is not specified, then all rows of the table are deleted. SYNTAX 6.9 Delete Statement Summary: delete from TABLE-NAME [where DELETE-CONDITION] Example: delete from takes where eID = '123' 160 SQL: BEYOND THE QUERY LANGUAGE Recall that there is an on delete clause specification when declaring a referential integrity constraint. The no action option means that a delete statement will fail if the delete removed the value of a primary key referenced by a foreign key. For the options set null or set default, the referencing foreign key value would automatically be set to null or the attribute’s default value, respectively. The cascade option cascades the delete by removing all rows having a foreign key referencing the deleted primary key. This is a dangerous option to use and should only be used when warranted by the application. A delete of a primary key value uses the referential integrity specification to determine the action to take. Self Check 4. For the EMPLOYEE TRAINING database, insert a new employee with attribute values of your choice. 5. Assuming the dept table defined earlier with schema dept(deptid, dname, mgrid), update the manager of the 'Research' department to the employee with id '123'. 6. For the EMPLOYEE TRAINING database, write a statement to remove all rows in the takes table. 6.3 Database User Privileges Use grant and revoke statements to manage privileges on a database table.
  • Book cover image for: Fundamentals of Database Management Systems
    • Mark L. Gillenson(Author)
    • 2012(Publication Date)
    • Wiley
      (Publisher)
    Data definition, which is operationalized with a data definition language (DDL), involves instructing the DBMS software on what tables will be in the database, what attributes will be in the tables, which attributes will be indexed, and so forth. Data manipulation refers to the four basic operations that can and must be performed on data stored in any DBMS (or in any other data storage arrangement, for that matter): data retrieval, data update, insertion of new records, and deletion of existing records. Data manipulation requires a special language with which users can communicate data manipulation commands to the DBMS. Indeed, as a class, these are known as data manipulation languages (DMLs). A standard language for data management in relational databases, known as Structured Query Language or SQL, was developed in the early 1980s. SQL incorporates both DDL and DML features. It was derived from an early IBM research project in relational databases called ‘‘System R.’’ SQL has long since been declared a standard by the American National Standards Institute (ANSI) and by the International Standards Organization (ISO). Indeed, several versions of the standards have been issued over the years. Using the standards, many manufacturers have produced versions of SQL that are all quite similar, at least at the level at which we will look at SQL in this book. These SQL versions are found in such mainstream DBMSs as DB2, Oracle, MS Access, Informix, and others. SQL in its various imple- mentations is used very heavily in practice today by companies and organizations of every description, Advance Auto Parts being one of countless examples. SQL is a comprehensive database management language. The most interesting aspect of SQL and the aspect that we want to explore in this chapter is its rich data retrieval capability.
  • Book cover image for: Beginning Visual Basic 2015
    • Bryan Newsome(Author)
    • 2015(Publication Date)
    • Wrox
      (Publisher)
    DELETE statement looks like this:
    DELETE FROM Teacher Or this: DELETE Teacher
    This statement deletes every row in the table. You may want to delete just one row or maybe only certain rows. In this case, you just add the WHERE clause—the same statement that you learned using the SELECT statement. The FROM keyword is optional; use it if you think the SQL is more readable.
    DELETE FROM Teacher WHERE TeacherID = 4
    The previous code deletes the row that has the TeacherID of 4 from the table.
    You can summarize the basic DELETE syntax in the following way:
    DELETE [FROM] table-name [WHERE search-condition]

    Using the INSERT Statement

    To insert data in the database, you use the SQL INSERT statement. The basic INSERT statement looks like this:
    INSERT INTO Teacher(Name, Address, Zip) VALUES ('Bryan', '123 Main Street', '20211') Or this: INSERT Teacher(Name, Address, Zip) VALUES ('Bryan', '123 Main Street', '20211')
    This statement inserts one row in the table. As you can see, the INTO keyword is optional, and you should use it when it makes your code more readable. Sometimes, you need to insert more than one row at a time. You do this a lot when loading data into a temp. To load data like this, you can SELECT data from another table and INSERT it into a table:
    INSERT INTO Teacher_Temp(Name, Address, Zip) (SELECT Name, Address, Zip from Teacher WHERE Zip = '20211')
    The previous code inserts all rows that have a Zip of 20211 from Teacher into Teacher_temp .
    You may find that you need to enter data that already exists in a table, but one column must be changed to a new value. INSERT INTO Teacher(ID, Name, Address, Zip, InsertUser) (SELECT 'AX0001', Name, Address, Zip, 'Bryan' from Teacher_Temp WHERE ID = 'QX1278')
    The previous SQL code inserts a row into Teacher and copies data from the row with ID QX1278 . The difference is that you assign a new ID of AX0001 and the InsertUser of Bryan
  • Book cover image for: Beginning Visual Basic 2012
    • Bryan Newsome(Author)
    • 2012(Publication Date)
    • Wrox
      (Publisher)
    DELETE statement looks like this:
      DELETE FROM Teacher Or this:   DELETE Teacher
    This statement deletes every row in the table. You may want to delete just one row or maybe only certain rows. In this case, you just add the WHERE clause—the same statement that you learned using the SELECT statement. The FROM keyword is optional; use it if you think the SQL is more readable.
      DELETE FROM Teacher WHERE TeacherID = 4
    The previous code deletes the row that has the TeacherID of 4 from the table.
    You can summarize the basic DELETE syntax in the following way:
      DELETE [FROM] table-name [WHERE search-condition]

    Using the INSERT Statement

    To insert data in the database, you use the SQL INSERT statement. The basic INSERT statement looks like this:
      INSERT INTO Teacher(Name, Address, Zip) VALUES ('Bryan', '123 Main Street', '20211') Or this:   INSERT Teahcer(Name, Address, Zip) VALUES ('Bryan', '123 Main Street', '20211')
    This statement inserts one row in the table. As you can see, the INTO keyword is optional, and you should use it when it makes your code more readable. Sometimes, you need to insert more than one row at a time. You do this a lot when loading data into a temp. To load data like this, you can SELECT data from another table and INSERT it into a table:
      INSERT INTO Teacher_Temp(Name, Address, Zip) (SELECT Name, Address, Zip from Teacher WHERE Zip = '20211')
    The previous code inserts all rows that have a zip of 20211 from Teacher into Teacher_temp .
    You may find that you need to enter data that already exists in a table, but one column must be changed to a new value.   INSERT INTO Teacher(ID, Name, Address, Zip, InsertUser) (SELECT 'AX0001', Name, Address, Zip, 'Bryan' from Teacher_Temp WHERE ID = 'QX1278')
    The previous SQL code inserts a row into Teacher
  • Book cover image for: Automated Physical Database Design and Tuning
    • Nicolas Bruno(Author)
    • 2011(Publication Date)
    • CRC Press
      (Publisher)
    For that purpose, SQL introduces data manipulation primi-tives, which can insert, delete, or update values in tables. As an example, to ∗ When there are aggregate expressions in the SELECT clause but no GROUP BY clause, the result implicitly considers all tuples in the result as belonging to the same group. Declarative Query Processing 11 increase the salary of all employees based in Seattle by 10%, we can use the following UPDATE query: UPDATE Emp SET Salary = 1.1 * Salary WHERE Emp.DId IN ( SELECT Dept.DId FROM Dept WHERE Dept.City = ’Seattle’ ) UPDATE queries are defined by the updated table, the set of assignment operations, and a predicate that constrains which rows in the table to actually modify. Similarly, DELETE queries are specified by a table and a predicate that identifies which rows to delete, and INSERT queries are specified by a table and either an explicit set of tuples or another SQL query that returns the tuples to insert. 1.3 Processing SQL Queries We now briefly explain how SQL queries are actually evaluated in a database system. Our exposition is necessarily high level and omits many details and special cases. The objective of this section is to introduce the reader to con-cepts that are needed in later chapters—mostly by example—but it is by no means comprehensive. References at the end of this chapter provide many additional details. In a database system, tables are stored on disk as a sequence of pages, which typically range from 4 KB to 64 KB in size. Tuples in a table are laid out in pages sequentially, using a variety of encodings that balance tuple size and processing efficiency. Each tuple in a table is given a record id (or RID for short), which identifies both the page that contains the tuple and the offset inside the page. Pages are the unit of input/output (I/O) transfer and can be cached in memory (in what is called a buffer pool). The execution environment in a relational database system can be seen as a virtual machine.
  • Book cover image for: Wiley Pathways Introduction to Database Management
    • Mark L. Gillenson, Paulraj Ponniah, Alex Kriegel, Boris M. Trukhnov, Allen G. Taylor, Gavin Powell, Frank Miller(Authors)
    • 2012(Publication Date)
    • Wiley
      (Publisher)
    You will learn about basic language components through simple command exam- ples. The chapter introduces representative DDL and DML command statements through the standard command syntax and gives some insight into how some DBMS providers have modified these statements to meet their particular needs and design visions. 6.1 Introducing the SQL Language There are two aspects of data management: data definition and data manipula- tion. Data definition requires a data definition language (DDL) that instructs the DBMS software as to what tables will be in the database, what attributes will be in the tables, which attributes will be indexed, and so forth. Data manipula- tion refers to the four basic operations performed on data stored in any DBMS: data retrieval, data update, insertion of new records, and deletion of existing records. Data manipulation requires a special language with which users can communicate data manipulation commands to the DBMS. As a class, these com- mands are known as data manipulation language (DML). Data retrieval state- ments are sometimes placed in a different category, separate from DML, as data query language (DQL) statements. For the purposes of this chapter, retrieval statements are treated as part of the DML set. The current relational database management system language, Structured Query Language (SQL), was developed in the early 1980s and incorporates both DDL and DML features. SQL has been declared a standard by the American National Standards Institute (ANSI) and by the International Organization for Standardization (ISO), with several standard versions issued since its initial development. Many manufacturers have produced versions of SQL, most quite similar, including mainstream DBMSs such as DB2, Oracle, MySQL, Informix, and Microsoft SQL Server. 6.1.1 Understanding SQL Features The SQL standard defines the features supported by “standard” SQL implemen- tations. Most vendor implementations, however, go beyond the SQL standard.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.