4 DML
4 DML
The SQL commands that deals with the manipulation of data present in the database belong
to DML or Data Manipulation Language and this includes most of the SQL statements.
DML is responsible for all forms data modification in a database.
INSERT:
The insert statement is used to add new row to a table.
Syntax
INSERT INTO TABLE_NAME
(col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);
Or
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3, .... valueN);
Example
Insert the values into the following table (person).
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| ID | int | NO | | NULL | |
| NAME | varchar(20) | YES | | NULL | |
| AGE | int | YES | | NULL | |
| ADDRESS | varchar(20) | YES | | NULL | |
| SALARY | int | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
SELECT:
SELECT statement is used to fetch the data from a database table which returns this data in the
form of a result table. These result tables are called result-sets.
Syntax
SELECT column1, column2, columnN FROM table_name;
Example
To fetch all the fields of the table,
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | ram | 32 | Chennai | 2000 |
| 2 | sam | 28 | Hyd | 2500 |
| 3 | Henry | 30 | Bangalore | 5000 |
| 4 | Adam | 29 | Pune | 6500 |
| 5 | Alice | 25 | Delhi | 8500 |
+----+----------+-----+-----------+----------+
+----+----------+
| ID | NAME |
+----+----------+
| 1 | ram |
| 2 | sam |
| 3 | Henry |
| 4 | Adam |
| 5 | Alice |
+----+----------+
UPDATE
UPDATE Query is used to modify the existing records in a table. The WHERE clause with
the UPDATE query to update the selected rows, otherwise all the rows would be affected.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Example
Update the ADDRESS for a customer whose ID number is 5 in the table.
To modify all the ADDRESS and the SALARY column values in the CUSTOMERS table, not
need to use the WHERE clause as the UPDATE query would be enough as shown in the
following code block.
UPDATE person SET ADDRESS = “LONDON” , SALARY = 5000;
DELETE
DELETE Query is used to delete the existing records from a table.
You can use the WHERE clause with a DELETE query to delete the selected rows, otherwise all
the records would be deleted.
Syntax
Example
The following code has a query, which will DELETE a customer, whose ID is 5.
To DELETE all the records from the CUSTOMERS table, no need to use the WHERE
clause and the DELETE query would be as follows –