Sql Tutorial (Structured Query Language): Four types of sql queries
In this article we are going to learn sql queries which are the most important part of every part every websites which uses databases on their site such as dynamic websites.
After making connection to database via script or manually, Usually four types of work is done on database,
Queries that do such modification in database are written below:
/* Insert query */ " INSERT INTO table_name (attribute1, attribute2) VALUES ('value1', 'value2')" /*insert query ends */ /* retrieve Query */ " SELECT * FROM table_name WHERE attribute='value_stored_in_table' ORDER BY table_attribute LIMIT starting_value, No_of_rows_to_be_displayed " /* retrieve query ends */ /* update query */ " UPDATE table_name SET attribute1='any value', attribute2='any value' WHERE attribute='value_stored_in_table' " /* update query ends */ /* Delete query */ " DELETE FROM table name WHERE attribute='value_stored_in_table' " /* Delete query ends */
Insert query is used to Insert new row into the database, Retrieve Query to retrieve/select data from database, Update query for inserting new data by replacing the older values and Delete query to delete a row from the database table. Some of the important points are displayed below:
- "WHERE" clause specifies the condition or select the row in database tables on which data modifications is to be done,
- "ORDER BY" clause specifies the condition in what order data should be modified or selected according to a particular attribute(this attribute of table must be set to auto-increment in database while creating table) of database table,
- We can use "ASC" and "DESC" in retrieve query to order retrieved rows in ascending and descending order, like this: ORDER BY table_attribute ASC.
- "LIMIT" clause set the number of rows that should be modified in database tables while setting the starting value Like this "LIMIT 0, 30" - Here "0" is the starting value and data starting from zero until 30 rows reached will be selected and then modifications will be done on it.
- "value_stored_in_table" refer to the value that is already stored in a particular row of the database table, You should change this according to your needs.
- "SET" clause in update query is used to set the new values of attribute of a particular row and will replace the already stored values in row of database table.