Data Manipulation Language (DML)
DML commands are used to inert new values into a table, updating, deleting and retrieving data.
The most used commands are insert, update, delete and select.
However select is the most popular and widely used.
SQL Insert
insert command is used to add new rows to a table
Syntax
INSERT INTO Table_name (Col1, Col2, Col3, Col4, ... )
VALUES(value1,value2,value3,value4, ... )
Example:
INSERT INTO employees (emp_id, emp_name, salary,dept_id)
VALUES(1,'Ahmed',3000,1)
Insert a second record into employees table
Delete a record from employees table
Syntax:
DELETE FROM table_name;
Example: Delete employee with emp_id=2 from employees table
DELETE FROM employees WHERE emp_id = 2
Note:
If we use the delete command without where clause will result in deleting all the data from the table.
Update Records
Syntax:
UPDATE table_name
SET column_name1 = new_value1,
column_value1 = new_value2,
column_value N = new_value N
WHERE column_name = condition_value;
Example:
From the employees table above we would like to update the salary of the employee with emp_id =1 from 2000 to become 4000.
Note:
If we use the update command without where clause will result in updating all the data from the specified column.
Updating multiple columns with one update command:
Example:
From the employees table above we would like to update the salary to 5000 and dept_id to 2 for the employee with emp_id = 2.
UPDATE employees
SET salary = 5000,
dept_id = 2
WHERE emp_id = 2;
0 Comments