MySql queries for update and delete operations

               Update is a SQL operation which updates a column or one or more column.

Let us create the query.The syntax is given below.

Syntax:

UPDATE tablename set value1,valu2,…,valuen where condition;’

Query: single field update

mysql> UPDATE Students SET age=15 WHERE name = 'Edward';

Query OK, 1 row affected (0.054 sec)

Rows matched: 1  Changed: 1  Warnings: 0

Display the table contents by SELECT Query:

mysql> SELECT * FROM students;

+----+--------+------+

| id | name   | age  |

+----+--------+------+

|  1 | ajay   |   12 |

|  2 | bob    |   15 |

|  3 | Edward |   15 |

|  4 | Jey    |   13 |

+----+--------+------+

4 rows in set (0.009 sec)

Update query : Multiple field update

mysql> UPDATE Students SET name='John',age=12 WHERE id =3;

Query OK, 1 row affected (0.072 sec)

Rows matched: 1  Changed: 1  Warnings: 0

Display the result.

mysql> SELECT * FROM Students;

+----+------+------+

| id | name | age  |

+----+------+------+

|  1 | ajay |   12 |

|  2 | bob  |   15 |

|  3 | John |   12 |

|  4 | Jey  |   13 |

+----+------+------+

4 rows in set (0.007 sec)

Delete query:

Deletion is the process of removing data or tuple.

Syntax:

DELETE FROM tablename where condition;’

Eg:

mysql> DELETE FROM Students where id = 2;

Query OK, 1 row affected (0.069 sec)

Display  the output as follows.

mysql> SELECT * FROM Students;

+----+------+------+

| id | name | age  |

+----+------+------+

|  1 | ajay |   12 |

|  3 | John |   12 |

|  4 | Jey  |   13 |

+----+------+------+

3 rows in set (0.007 sec)

This is the way of creating delete and update queries in mysql. Hope, this code is useful to you!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

Symmetric Encryption in java

Java NIO examples to illustrate channels and buffers.