MySQL delete data records in the table

Article Directory


foreword

        Deleting data records is a common operation in data operations, which can delete existing data records in the table. In MySQL, data records can be deleted through the DELETE statement. This SQL statement can be used in the following ways: delete specific data records, delete all data records.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Delete data records in the table?

1. Delete specific data records

        Deleting a specific data record in MySQL can be achieved through the SQL statement DELETE, and its syntax is as follows:

        DELETE FROM tablename WHERE CONDITION;   

        In the above statement, the parameter tablename indicates the table name of the data record to be deleted, and the parameter CONDITION specifies to delete a specific data record that meets the condition.

        For example: The class led by Martin has graduated, so delete it from the class table!

mysql>   create database school;   #create database school 

mysql>   use school;   #select database school 

mysql> create table class(id int UNIQUE AUTO_INCREMENT, name varchar(128) UNIQUE, teacher varchar(64));       #Create table class, specify id field self-growth 

mysql>  insert into class(id, name, teacher) values(1, ' Class One ', 'Martin'),(2,' Class Two ', 'Rock'),(3, ' Class Three ', 'Janny' );   #insert multiple records

mysql> delete from class where teacher = 'Martin';  #Delete records through the teacher field or      

mysql> delete from class where id = 1;  #Delete matching records through the id field

2. Delete all data records

        Deleting all data records in MySQL can also be achieved through the SQL statement DELETE, and its syntax is as follows:

        DELETE FROM tablename WHERE CONDITION;          

        DELETE FROM tablename

        In the above statement, in order to delete all data records, the parameter CONDITION needs to satisfy all data records in the table tablename, such as id>0; or no keyword WHERE statement.

        For example: all the classes led by teachers have graduated, delete them from the class table!

mysql>   create database school;   #create database school 

mysql>   use school;   #select database school 

mysql> create table class(id int UNIQUE AUTO_INCREMENT, name varchar(128) UNIQUE, teacher varchar(64));       #Create table class, specify id field self-growth 

mysql>  insert into class(id, name, teacher) values(1, ' Class One ', 'pass'),(2, ' Class Two ', 'wang'),(3, ' Class Three ', 'gou' );   #insert multiple records

mysql> delete from class ;  #Delete all records directly or      

mysql> delete from class where id > 0;  #Delete all matching records through the id field


Summarize

as above

Guess you like

Origin blog.csdn.net/m0_65635427/article/details/127937787