MySQL tutorial on insert, update and delete Free Web Design Tutorials
Home   HTML CSS JavaScript PHP MySQL Usability Glossary
MySQLBasicsCreate and DropShow and DescribeMySQL insert, update and deleteInsert, Update and DeleteQueryingJoin QueriesFunctionsTable LockingPHP/MySQL FunctionsAccessing a DatabaseQuerying with PHPCreate and Drop with PHPInsert & Update with PHPFrequently Used Functions

Home > MySQL > Insert, Update and Deleteprinter version

MySQL Insert, Update and Delete

This tutorial assumes that you have access to the MySQL command prompt and uses the people table created in the Create Tutorial.

Insert

Inserting data into a table can be achieved in different ways and two of the methods are covered below.

INSERT INTO table_name VALUES ()

With this method the attributes for a table row are listed in parenthesis, separated by commas, and must conform to the following rules.



First Method of Inserting Data into a Table
mysql> INSERT INTO people VALUES
(NULL, "Dave", "Clark", "Main Street", "West Lothian", "UK", 40);
Query OK, 1 row affected (0.05 sec)

mysql>


If a string contains single quotes then double quotes can be used to enclose it or vice versa.

Alternatively quotes can be escaped using the \ character as covered in the section on escape sequences in the PHP Strings Tutorial.

INSERT INTO table_name SET

This method doesn't require all the attributes to be listed and those that are can be in any order.

Each attribute to be inserted is listed by name followed by = then the value to be assigned.

Second Method of Inserting Data into a Table
mysql> INSERT INTO people SET
firstname = "Dave",
lastname = "Clark",
address = "Main Street",
county = "West Lothian",
country = "UK",
age = 40;
Query OK, 1 row affected (0.05 sec)

mysql>


Notice that there's no need to mention the id attribute above since it will auto_increment.

Update

Updating an attribute is straightforward and uses syntax similar to the previous INSERT example.

Updating an Attribute
mysql> UPDATE people SET country = "US" WHERE id = 1;
Query OK, 1 row affected (0.05 sec)

mysql>


Note: If the WHERE clause wasn't used above then all country attributes in every row of the table would be updated.

Any attribute can be used in a WHERE clause to selectively update, but only the id attribute guarantees that only one row will be affected (because we made it a primary key and therefore unique).


Delete

The DELETE statement is used to remove rows from a table. Even if all rows are removed from a table, the table itself is not removed - as it would be with the DROP statement.

Deleting a Row
mysql> DELETE FROM people WHERE id = 1;
Query OK, 1 row affected (0.05 sec)

mysql>


Note: As with the UPDATE statement, if the WHERE clause wasn't used above then all rows in the table would be affected i.e. deleted.

Any attribute can be used in a WHERE clause to selectively delete rows, but only the id attribute guarantees that only one row will be affected (because we made it a primary key and therefore unique).


Previous - MySQL Show and Describe Previous - Show and Describe     Next - Querying Next - MySQL Querying


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2009 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2009
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Recommended Reading
PHP6 and MySQL5

PHP and MySQL Web Development

Build Your Own Database Driven Website using PHP and 

MySQL

How to do Everything with PHP and MySQL