DEV Community

Adam
Adam

Posted on • Edited on

SQL CRUD Operations

(C)REATE

Creating is for inserting information in your tables, this is where you specify how your data is going to represent in table or "entity". It starts with the column names that is going to be inserted, followed by the values that need to be inserted. the end results it enter a new row in your table

`INSERT INTO tbl_name (column_name_one, column_name_two)
VALUES (column_value_one, column_value_two)`
Enter fullscreen mode Exit fullscreen mode

(R)ETREIVE

this is used for quering, or selecting data from a particular table you are looking to retrieve. In your select statement you can specify what the data is stored by selecting what columns you want to display.

`SELECT * FROM tbl_name`
Enter fullscreen mode Exit fullscreen mode

the "*" selector you see shown above will display all columns that is structured in that entity. You can specify what columns you want to see just by specifying a particular column shown below

`SELECT column_name FROM tbl_name`
Enter fullscreen mode Exit fullscreen mode

Conditional Statements

You can also retrieve data from your database by using conditional statements. Using conditional statements, it returns records from your tables that will display records that have a match from what condition you are specifying

Shown below is an example using the "WHERE" clause to specify quering your data based on a condition.

There are also cases when you need to use aggregated functions in your SELECT statement, in this case the WHERE clause won't recognize how to run conditions off of what aggregated function you are using.
This example shown below, this query is using the COUNT() aggregate function, and its using the "HAVING" clause to execute this condition correctly.

There will also be cases when there are going to be similarities in your data, and you need to find a way to group and associate those similarities. The example shown below, this query is grouping the similarities of "column_name" and grouping them by that column while displaying "column_name_two" with that group

Another case to similiaries in your data. if you need to retrieve data (such as text, string) it would be beneficial querying data if a sub strings contains a certain text. Shown below is a query executing a condition if "column_name" contains a substring. In return it will return a list of records where it finds a match.

Using the LIKE clause in this query, you specify your wild card (%).
In this scenario its telling the LIKE clause that "column_name" has a pattern of "substring"

(U)PDATE

This is used to modify current records in your table. When updating records, you can run a query to update all records, or update records based on a condition (i.e. WHERE clauses). It is all based on your business needs on what functionality are you trying to achieve. Shown below this query is updating "tble_name" using a condition to specify what record to update.

(D)ELETE

This is where you are removing tables from a table, you can remove all records from a table

Top comments (0)