Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEvan N
3,244 PointsDo SQL statements actually modify the data within the table or overall database?
I know that this question is broad but do the SQL statements alter the table or database or does this depend entirely on the statement? Does this also differ between SQL variants (e.g. PostgreSQL, MS SQL, etc.)?
Some context: I am working through the "sql-basics" course and didn't see this mentioned.
2 Answers
Leslie Heinzen
26,662 PointsHi Evan,
I would like to expand a bit.
SQL generally involves the manipulation of data in a database through commands known as CRUD (Create, Read, Update, Delete). In SQL, these are the INSERT, SELECT, UPDATE, and DELETE commands, respectively.
Most database management systems are by default in auto-commit mode, so the changes are committed to disk when you issue a command that manipulates data in some way (creating, updating, or deleting). If you are simply reading data, then obviously no data will be changed.
In order to prevent changes from committing to disk as they are run, you need to declare BEGIN or BEGIN TRANSACTION at the beginning of your file (or wherever you would like to declare it), and declare COMMIT in the file when you are ready to write the changes to disk. BEGIN turns off auto-commit mode, so that you can make multiple changes in a file before writing them to disk. Basically, you can think of everything between a BEGIN and COMMIT statement as running as one big command all at the same time.
This is especially useful, for example, if you have a huge script that is initializing a database with tons of changes -- using BEGIN works as a safety net in case something disastrous happens in the middle of the script running (like an error or crash). If an error happen in your file before reaching the COMMIT line, then none of the changes are written to disk, so you don't have to deal with a half-finished database monstrosity the next time you come back to it.
The ROLLBACK command can also be used to undo changes that have occurred since the last BEGIN statement was issued.
Happy coding.
Leslie
Alexander Alegre
14,340 PointsHello, if you are just doing: SELECT * FROM table;
Will not affect the actual data. There are special UPDATE statements that will update the data but that is not covered in the SQL basics course. If you want to read about the UPDATE statement below is a link http://www.w3schools.com/sql/sql_update.asp
Hope this helps!