Q&A

Q1:Can I copy data from a table into itself using the INSERT command? In addition, I want to change the value of one column.
A1: By using the INSERT statement with a SELECT statement, you can copy all the rows from the table into itself. However, you must modify the column in question as a separate step. You cannot update columns while performing this type of INSERT. The following code shows how to perform these tasks:
Insert customer_demo
Select * from customer_demo
Update customer_demo
Set Addr2 = 'unavailable'

Q2:You said that caution should be taken when using the INSERT, UPDATE, DELETE statements, but there seem to be some simple fixes to correct whatever I did wrong. Is this correct?
A2: Yes. For example, a simple way to fix a misspelled name is to do an update on that row to fix the name. Or you could delete the row and redo the insert with the corrected spelling of the name.

However, what if you inserted a thousand rows and didn't notice that you misspelled something until a couple of weeks later? You would not want to simply delete the rows, which probably include many new rows or multiple updates from your users. In most cases, you would probably not even know what rows to fix.

To fix something this drastic, I would print all of the rows that have the misspelled name in it using a WHERE clause. Then, I would slowly make the changes one row at a time.

Q3:What is the difference between the DROP TABLE and the TRUNCATE TABLE statements?
A3: The DROP TABLE statement will completely remove a table and its definition from the database, whereas the TRUNCATE TABLE statement will only remove the data in the table (much like a DELETE statement), resetting the identity column.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset