INSERT INTO Statement

INSERT INTO statements are used to add rows into the database. If you are looking to start coding, here’s how

INSERT INTO table (column1, column2, …)
VALUES (value1, value2, …);

This is the table to be inserted in the output of the SELECT statement. You can change the name of the database if you put it in the parentheses. You need to use this statement after making a comma between each field in the set.

You’ll use the VALUES command next. It inserts a value into the table you’ve selected. There are two ways to do this—either write the comma first and then add the value, or just put it all in the middle like so

INSERT INTO employees (id, first_name, last_name)
VALUES (11, 'Obi-Wan', 'Kenobi');

To insert data into the table employees, this query changes the columns id, first_name, and last_name. The new employee Obi-Wan Kenobi, whose ID is 11, was just hired.

If you selected all the columns from this table, you’d get:

_idfirst_namelast_namesalarydepartment
1NancyChapman1,486.13Operations
2SteveMason2,886.25Accounting
3KateWilliams1,158.44Accounting
4FrankHasbeen5,711.49Operations
5HillaryAdailton3,066.52Operations
6DanielJackson3,066.52Sales
7SusanDeutsch5,039.13Operations
8DereckVans4,434.27Sales
9SamNelson2,044.19Accounting
10MariaLopez1,995.32Accounting
11Obi-WanKenobiNULLNULL

We didn’t set the salary and department values for the new employee because we don’t know their qualifications. The NULL is used as an indicator that no value has been entered for a particular field.

UPDATE Statement

UPDATE is used to modify data in the table. This command is usually run with the WHERE clause. It would be a lot easier to make the table smaller.

Syntax:

UPDATE table
SET column1 = value1, column2 = value2, …
WHERE …;

OK, you’ve finished the table update. Now you need to run the SET COMMAND to insert the values into the column. The set command takes in two optional parameters. If you want to be able to keep track of changes in a database, you’ll need to be able to write a query to see which of your columns need to be changed.

UPDATE employees
SET department = 'Sales'
WHERE id = 2;

To set up equations you need to enter an equals sign with each value and a comma separating the columns worth a new equation. The word WHERE is used to provide restriction on updates that are satisfied by a particular condition. Update the values in the table employees. For the department field, replace Sales

_idfirst_namelast_namesalarydepartment
1NancyChapman1,486.13Operations
2SteveMason2,886.25Sales
3KateWilliams1,158.44Accounting
4FrankHasbeen5,711.49Operations
5HillaryAdailton3,066.52Operations
6DanielJackson3,066.52Sales
7SusanDeutsch5,039.13Operations
8DereckVans4,434.27Sales
9SamNelson2,044.19Accounting
10MariaLopez1,995.32Accounting
11Obi-WanKenobiNULLNULL

DELETE Statement

This command is used for deleting data from the table. Again, the WHERE clause in this sentence is important. It allows deleting only certain data, i.e., that satisfies the condition in the WHERE clause. If you don’t include WHERE, DELETE deletes all records in a table.

Syntax:

DELETE
FROM table
WHERE …;

Last comes DELETE. It removes data from a given table. Unlike with the SELECT query, the DELETE statement does not return any records. Put the WHERE clause if you don’t wish to delete every single record in the table.

Example:

DELETE
FROM employees
WHERE id = 11;

This query removes all the rows that have the id = 11

_idfirst_namelast_namesalarysalary
1NancyChapman1,486.13Operations
2SteveMason2,886.25Sales
3KateWilliams1,158.44Accounting
4FrankHasbeen5,711.49Operations
5HillaryAdailton3,066.52Operations
6DanielJackson3,066.52Sales
7SusanDeutsch5,039.13Operations
8DereckVans4,434.27Sales
9SamNelson2,044.19Accounting
10MariaLopez1,995.32Accounting

Obi-Wan Kenobi was removed from the table.