You are on page 1of 12

The Data Manipulation Language (DML) is used to retrieve, insert and modify database information.

These commands will be used by all database users during the routine operation of the database. DML-Data Manipulation Language: DML statements, INSERT, DELETE, UPDATE AND SELECT rows, deleting unwanted rows and changing data in the rows. These are known as SQL-change statements. DML does not change the schema of database.
2009)

INTO are used for data manipulation, adding new

A data manipulation language (DML) is a family of syntax elements similar to a computer programming language used for inserting, deleting and updating data in a database. Performing read-only queries of data is sometimes also considered a component of DML. Data manipulation languages have their functional capability organized by the initial word in a statement, which is almost always a verb. In the case of SQL, these verbs are:
y y y y SELECT INSERT UPDATE DELETE ... FROM ... WHERE ... INTO ... VALUES ... ... SET ... WHERE ... FROM ... WHERE ...

The SQL SELECT statement returns a result set of records from one or more tables.[1][2] A SELECT statement retrieves zero or more rows from one or more database tables or database views. In most applications, SELECT is the most commonly used Data Manipulation Language (DML) command. As SQL is a declarative programming language, SELECT queries specify a result set, but do not specify how to calculate it. The database translates the query into a "query plan" which may vary between executions, database versions and database software. This functionality is called the "query optimizer" as it is responsible for finding the best possible execution plan for the query, within applicable constraints.

The SELECT statement has many optional clauses:


y y y y WHERE specifies which rows to retrieve. GROUP BY groups rows sharing a property so that an aggregate function can be applied to each group. HAVING selects among the groups defined by the GROUP BY clause. ORDER BY specifies an order in which to return the rows. y y y y y y y y y y y
P_Id 1 2 3

The SQL SELECT Statement


The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set.

SQL SELECT Syntax


SELECT column_name(s) FROM table_name and SELECT * FROM table_name Note: SQL is not case sensitive. SELECT is the same as select.

An SQL SELECT Example


The "Persons" table: LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

y y y y

Now we want to select the content of the columns named "LastName" and "FirstName" from the table above. We use the following SELECT statement: SELECT LastName,FirstName FROM Persons The result-set will look like this: FirstName Ola Tove

LastName Hansen Svendson

Pettersen

Kari

y y y y y y y y
P_Id 1 2 3

SELECT * Example
Now we want to select all the columns from the "Persons" table. We use the following SELECT statement: SELECT * FROM Persons Tip: The asterisk (*) is a quick way of selecting all columns! The result-set will look like this: LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

y y y y y y y y y y y y

INSERT INTO Statement


The INSERT INTO statement is used to insert a new row in a table.

SQL INSERT INTO Syntax


It is possible to write the INSERT INTO statement in two forms. The first form doesn't specify the column names where the data will be inserted, only their values: INSERT INTO table_name VALUES (value1, value2, value3,...) The second form specifies both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

y y
P_Id 1 2 3

SQL INSERT INTO Example


We have the following "Persons" table: LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

y y y y
P_Id 1 2 3 4

Now we want to insert a new row in the "Persons" table. We use the following SQL statement: INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger') The "Persons" table will now look like this: LastName Hansen Svendson Pettersen Nilsen FirstName Ola Tove Kari Johan Address Timoteivn 10 Borgvn 23 Storgt 20 Bakken 2 City Sandnes Sandnes Stavanger Stavanger

y y y y y y y
P_Id 1 2 3

Insert Data Only in Specified Columns


It is also possible to only add data in specific columns. The following SQL statement will add a new row, but only add data in the "P_Id", "LastName" and the "FirstName" columns: INSERT INTO Persons (P_Id, LastName, FirstName) VALUES (5, 'Tjessem', 'Jakob') The "Persons" table will now look like this: LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

4 5

Nilsen Tjessem

Johan Jakob

Bakken 2

Stavanger

The DELETE Statement


In the database structured query language (SQL), the DELETE statement removes one or more records from a table. A subset may be defined for deletion using a condition, otherwise all records are removed.[1]

The DELETE statement is used to delete rows in a table.

SQL DELETE Syntax


DELETE FROM table_name WHERE some_column=some_value Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

SQL DELETE Example


The "Persons" table: P_Id 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

4 5

Nilsen Tjessem

Johan Jakob

Bakken 2 Nissestien 67

Stavanger Sandnes

Now we want to delete the person "Tjessem, Jakob" in the "Persons" table. We use the following SQL statement: DELETE FROM Persons WHERE LastName='Tjessem' AND FirstName='Jakob' The "Persons" table will now look like this: P_Id 1 2 3 4 LastName Hansen Svendson Pettersen Nilsen FirstName Ola Tove Kari Johan Address Timoteivn 10 Borgvn 23 Storgt 20 Bakken 2 City Sandnes Sandnes Stavanger Stavanger

Delete All Rows


It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact: DELETE FROM table_name or DELETE * FROM table_name Note: Be very careful when deleting records. You cannot undo this statement!

Examples

Delete rows from table pies where the column flavour equals Lemon Meringue:
DELETE FROM pies WHERE flavour='Lemon Meringue';

Delete rows in trees, if the value of height is smaller than 80.


DELETE FROM trees WHERE height < 80;

Delete all rows from mytable:


DELETE FROM mytable;

Delete rows from mytable using a subquery in the where condition:


DELETE FROM mytable WHERE id IN ( SELECT id FROM mytable2 )

An SQL UPDATE statement changes the data of one or more records in a table. Either all the rows can be updated, or a subset may be chosen using a condition. The UPDATE statement has the following form:[1]
UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE condition]

For the UPDATE to be successful, the user must have data manipulation privileges (UPDATE privilege) on the table or column, the updated value must not conflict with all the applicable constraints (such as primary keys, unique index CHECK constraintsNOT [2] NULLPostgreSQL
The UPDATE statement is used to update existing records in a table.

SQL UPDATE Syntax


UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

SQL UPDATE Example


The "Persons" table: P_Id 1 LastName Hansen FirstName Ola Address Timoteivn 10 City Sandnes

2 3 4 5

Svendson Pettersen Nilsen Tjessem

Tove Kari Johan Jakob

Borgvn 23 Storgt 20 Bakken 2

Sandnes Stavanger Stavanger

Now we want to update the person "Tjessem, Jakob" in the "Persons" table. We use the following SQL statement: UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE LastName='Tjessem' AND FirstName='Jakob' The "Persons" table will now look like this: P_Id 1 2 3 4 5 LastName Hansen Svendson Pettersen Nilsen Tjessem FirstName Ola Tove Kari Johan Jakob Address Timoteivn 10 Borgvn 23 Storgt 20 Bakken 2 Nissestien 67 City Sandnes Sandnes Stavanger Stavanger Sandnes

SQL UPDATE Warning


Be careful when updating records. If we had omitted the WHERE clause in the example above, like this: UPDATE Persons SET Address='Nissestien 67', City='Sandnes' The "Persons" table would have looked like this: P_Id 1 2 3 4 5 LastName Hansen Svendson Pettersen Nilsen Tjessem FirstName Ola Tove Kari Johan Jakob Address Nissestien 67 Nissestien 67 Nissestien 67 Nissestien 67 Nissestien 67 City Sandnes Sandnes Sandnes Sandnes Sandnes

Examples
Set the value of column C1 in table T to 1, only in those rows where the value of column C2 is "a".
UPDATE T SET C1 = 1 WHERE C2 = 'a'

In table T, set the value of column C1 to 9 and the value of C3 to 4 for all rows for which the value of column C2 is "a".
UPDATE T SET C1 = 9,

C3 = 4 WHERE C2 = 'a'

Increase value of column C1 by 1 if the value in column C2 is "a".


UPDATE T SET C1 = C1 + 1 WHERE C2 = 'a'

Prepend the value in column C1 with the string "text" if the value in column C2 is "a".
UPDATE T SET C1 = 'text' || C1 WHERE C2 = 'a'

Set the value of column C1 in table T1 to 2, only if the value of column C2 is found in the sub list of values in column C3 in table T2 having the column C4 equal to 0.
UPDATE T1 SET C1 = 2 WHERE C2 IN ( SELECT C3 FROM T2 WHERE C4 = 0)

You may also update multiple columns in a single update statement:


UPDATE T SET C1 = 1, C2 = 2

You might also like