To delete a record in the required way, the syntax is as follows −
delete from yourTableName where yourTableName .yourColumnName=yourValue;
Let us first create a table −
mysql> create table DemoTable ( StudentName varchar(100), StudentAge int ); Query OK, 0 rows affected (0.81 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris',21); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Robert',20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David',22); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Mike',24); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Sam',19); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+------------+ | StudentName | StudentAge | +-------------+------------+ | Chris | 21 | | Robert | 20 | | David | 22 | | Mike | 24 | | Sam | 19 | +-------------+------------+ 5 rows in set (0.00 sec)
Here’s how you can use tablename.columnname= value to get a value and delete it in MySQL −
mysql> delete from DemoTable where DemoTable.StudentName='Robert'; Query OK, 1 row affected (0.12 sec)
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+------------+ | StudentName | StudentAge | +-------------+------------+ | Chris | 21 | | David | 22 | | Mike | 24 | | Sam | 19 | +-------------+------------+ 4 rows in set (0.00 sec)