
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Does deleting row from view delete row from base table in MySQL?
Yes, deleting row from view delete row from base table. Let us understand this by creating a new table. The query to create a table is as follows
mysql> create table deleteFromBaseTableDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.83 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into deleteFromBaseTableDemo(Name) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into deleteFromBaseTableDemo(Name) values('Carol'); Query OK, 1 row affected (0.15 sec) mysql> insert into deleteFromBaseTableDemo(Name) values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into deleteFromBaseTableDemo(Name) values('Sam'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from deleteFromBaseTableDemo;
The following is the output
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Sam | +----+-------+ 4 rows in set (0.00 sec)
Let us create a view. The query to create a view is as follows
mysql> create view delete_view as select Id,Name from deleteFromBaseTableDemo; Query OK, 0 rows affected (0.17 sec)
Let us check all the records of view. The query is as follows −
mysql> select *from delete_view;
The following is the output
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Sam | +----+-------+ 4 rows in set (0.05 sec)
Now if you delete from view then it will automatically delete from base table as well.
The query to delete row from view is as follows
mysql> delete from delete_view where Id=4; Query OK, 1 row affected (0.26 sec)
Let us check all the records from view and parent table. The query is as follows −
mysql> select *from delete_view;
The following is the output
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | +----+-------+ 3 rows in set (0.00 sec)
The query to show all records from base table is as follows
mysql> select *from deleteFromBaseTableDemo;
The following is the output
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | +----+-------+ 3 rows in set (0.00 sec)
Look at both the sample outputs, Id with value 4 has been deleted from the parent table as well as from view.
- Related Articles
- How can we delete a single row from a MySQL table?
- What happens if I will delete a row from MySQL parent table?
- How to Delete a Row from Table using AngularJS?
- Display random row from a MySQL table
- How to delete a row from a table using jQuery?
- Deleting the nth row in MySQL?\n
- MySQL query to delete row
- How to exclude a specific row from a table in MySQL?
- How to delete duplicates and leave one row in a table in MySQL?
- How to delete a row from ResultSet object using JDBC?
- How to delete a row from an R data frame?
- Python Pandas - How to delete a row from a DataFrame
- How can we fetch a particular row as output from a MySQL table?
- Get the sum of multiple row (not all) values from a MySQL table?
- How to Add Edit and Delete Table Row in jQuery?
