DML OPERATIONS:
Data Manipulation Language is a one of the Subset of SQL.
These SQL commands are used to store, modify, and delete data from database tables. In this category we have INSERT, UPDATE, and DELETE commands. Different DML statements are
• Select
• Insert
• Update
• Delete
Select: The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set.
The SELECT command is the most commonly used command in SQL. It allows database users to retrieve the specific information they desire from an operational database.
Syntax:
SELECT column_name(s)
FROM table_name
and
SELECT * FROM table_name
Example: consider the table student_details. To select the first name of all the students the query would be
SELECT first_name FROM student_details;
If we want to select all the columns from the table, we use the following SELECT statement:
SELECT * FROM student_details;
Insert: The INSERT INTO statement is used to insert new records in a table. We can insert data to a table in two ways,
Inserting the data directly to a table:
Syntax:
INSERT INTO TABLE_NAME
[(col1, col2, col3,...colN)]
VALUES (value1, value2, value3,...valueN);
col1, col2,...colN are the names of the columns in the table into which you want to insert data.
While inserting a row, if you are adding value for all the columns of the table you need not specify the column(s) name in the sql query. But you need to make sure the order of the values is in the same order as the columns in the table. The sql insert query will be as follows
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3,...valueN);
Example: If you want to insert a row to the employee table, the query would be like,
INSERT INTO employee (id, name, dept, age, salary) VALUES (105, 'Srinath', 'Aeronautics', 27, 33000);
Inserting data to a table through a select statement:
Syntax:
INSERT INTO table_name
[(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN
FROM table_name [WHERE condition];
Example: To insert a row into the employee table from a temporary table, the sql insert query would be like,
INSERT INTO employee (id, name, dept, age, salary location) SELECT emp_id, emp_name, dept, age, salary, location
FROM temp_employee;
Update: The UPDATE statement is used to update existing records in a table.
Syntax:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated.
Example: To update the location of an employee, the sql update query would be,
UPDATE employee
SET location ='Mysore'
WHERE id = 101;
Delete: The SQL DELETE Query is used to delete the existing records from a table. You can use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted.
Syntax: DELETE FROM table_name [WHERE condition];
Example: To delete an employee with id 100 from the employee table, the sql delete query would be
DELETE FROM employee WHERE id = 100;
To delete all the rows from the employee table, the query would be,
DELETE FROM employee;
Comments
Post a Comment