SQL CRUD Commands
In SQL there is an acronym know as CRUD. This stands for create, read, update, and delete. These 4 terms are the 4 basic actions that are used to manipulate your database. I am going to briefly touch each one because there is so much to write about in each CRUD command. One you understand these 4 commands you have a good basic understand of how to write SQL queries.
We are going to start with create. The create command is when a user inserts new data(row) into a database. This is done using the INSERT INTO key. The way the format goes is you do INSERT INTO <table_name>(column_1, column_2) VALUES ('value_1',value_2'). After table name you designate the name of the columns that you are inserting data into and you must have the same amount of values after the VALUE keyword. In SQL every command also ends with a ; symbol just like in java. Your values must also have the same data type as the columns you are selecting or else this will cause an error.
The next command we are going to talk about is read. Read is just retrieving information from your database. This is done by using the SELECT command. The syntax would be SELECT * FROM <table_name>. In this command you are selecting everything from the table you are selecting. The * is what signifies that you want every column in that table. If you did not want to select every piece of column from your table then instead of the * you would just put the names of the columns in the table separated by commas. You can also do a deeper search by using the WHERE command after the table name but I will touch on that in a future blog.
The next command in our CRUD is update. Update is just what is sounds like. You are updating data that is already inside of your table. Maybe a name was misspelled or an employee got a raise. This is where update would come in. The syntax for update is UPDATE <table_name> SET <column_name> = value WHERE <column_name> = value. First we call the key UPDATE and select the table we are updating from. After that we use SET to select the name of the column we are updating and the value we are passing. The WHERE key is selecting the column and the value that is being replaced.
The last command in our CRUD is delete. Delete is one of the most powerful commands in SQL but also very dangerous because once you delete a table or a row there is no getting it back. This means when using the delete action we have to make sure we are selecting the correct values that we are deleting. The syntax for the delete command is DELETE FROM <table_name> WHERE <column_name> = value. We are calling the table name and then just selecting what row we are going to delete from my calling a specific value. This deletes your entire row. You can also delete an entire table by simply using DELETE FROM <table_name>. This will delete everything inside of your table but your table will still remain as a shell with no values in it.
Comments
Post a Comment