Previous
Inserting Data 
Next
Joining Data From Multiple Tables 
SQL(Structured Query Language) Tutorial
Update/Delete Existing Data in a Table

Update/Delete Existing Data in a Table

Now that we know how to insert new data, let see how to edit the existing data. You can edit the data in a table using the UPDATE command. Here is the syntax...

Edit or UPDATE

UPDATE <Table> SET <Field>='<New_Value>' WHERE <Condition>

WHERE condition is not actually necessary - but in 99.99% of the cases you need it. If the where condition is absent, you will change all the rows of the table to the new value. OK - enough talking - let try some examples. Lets say Lisa had an IQ test - this test showed that her IQ is 170 instead of 159. We can use this query to make the change.

UPDATE Character SET iq=170 WHERE name='Lisa Simpson'

I hope you have understood this command. Lets see - try finding the SQL statements for the following problems...

You can check whether your query made a change in the database by running the select command. If you need help answering these questions, answers are available in the appendix page.

Delete

Deleting a row is even easier than updating - this is the syntax...

DELETE FROM <Table> WHERE <Condition>

Again, WHERE is optional - but usually needed. If the condition is not present, you will delete the data of the entire table.

Time for an example. If you have done a SELECT somewhere in this page, you may have noticed that there is a Peter Griffin character in out database. Hmm, that guy is not in Simpsons - he is in Family Guy. Let take him out...

DELETE FROM Character WHERE name='Peter Griffin'

Play around with the database - don't worry about deleting the data - I'll recreate it in the next page.

Previous
Inserting Data 
Next
Joining Data From Multiple Tables 
Subscribe to Feed