< Structured Query Language

Data Manipulation Language is used to modify the records in the database. It never modifies the schema of the database (table features, relationships, ...). It describes three statements: INSERT, UPDATE and DELETE.

INSERT statement

The exhaustive syntax of the INSERT statement is as follows:

INSERT INTO <table name>[ (<column name>[, <column name>]*)]
{
 VALUES (<value>[, <value>]*)
|
 SELECT [ALL | DISTINCT] <column name> [, [ALL | DISTINCT] <column name>]*
 FROM <table> [[AS | =] <alias> | [[FULL | LEFT | RIGHT] OUTER | INNER] JOIN <table> ON <expression>]
  [, <table> [[AS | =] <alias> | [[FULL | LEFT | RIGHT] OUTER | INNER] JOIN <table> ON <expression>]]*

 [WHERE <predicate> [{AND | OR} <predicate>]*]
 [GROUP BY <column name> [, <column name>]*
  [HAVING <predicate> [{AND | OR} <predicate>]]*]
 ]
 [ORDER BY <column name> [ASC | DESC] [, <column name> [ASC | DESC]]*]
 [LIMIT <count>]
};

The INSERT statement is used to add new records (rows) in a table. For instance, we want to add a new reunion:

  • Its primary key is 7,
  • Its name is "Job interview",
  • Its description is "Meeting with Mr. SPENCER",
  • Its priority is B,
  • Its planned,
  • Its date is on October 28, 2009,
  • Its hour is 18:30:00,
  • Its duration is 30,
  • Its office technical id is 23,
  • There is no pdf report.


  • The table before the statement:
reunion
id_reunionnamedescriptionpriorityplanneddatehourduration# id_officepdf_report
1PlanningWe need to plan the project.A12008-03-2410:30:00603548644...846348
2ProgressWhat we have done.C12008-05-1214:00:0030139862...15676
3ChangeWhat we need to change in the project.B12008-06-039:30:00904134876...4846548
4PresentationPresentation of the project.D02008-09-1115:30:0012027
5ReportingExplanation to the new beginner.B12009-03-1514:00:0060719739...37718
6LearningA new software version has been installed.B12009-09-2116:00:0012011785278...37528
  • Query:
INSERT INTO reunion (id_reunion, name, description, priority, planned, date, hour, duration, id_office, pdf_report)
VALUES (7, 'Job interview', 'Meeting with Mr. SPENCER', B, 1, 2009-10-28, 18:30:00, 30, 23, NULL);
  • The table after the statement:
reunion
id_reunionnamedescriptionpriorityplanneddatehourduration# id_officepdf_report
1PlanningWe need to plan the project.A12008-03-2410:30:00603548644...846348
2ProgressWhat we have done.C12008-05-1214:00:0030139862...15676
3ChangeWhat we need to change in the project.B12008-06-039:30:00904134876...4846548
4PresentationPresentation of the project.D02008-09-1115:30:0012027
5ReportingExplanation to the new beginner.B12009-03-1514:00:0060719739...37718
6LearningA new software version has been installed.B12009-09-2116:00:0012011785278...37528
7Job interviewMeeting with Mr. SPENCERB12009-10-2818:30:003023

The INTO clause contains the name of the table where the record needs to be inserted. It can be followed by a list of columns in brackets. The VALUES clause contains the values to insert in brackets. If the column names are omitted, the VALUES clause must contains as many values as the number of columns of the table. The values are inserted in the table columns in the same order that the order in which the columns have been declared in the table. If the column names are mentioned, there must be as many column names as values. The values are respectively inserted into the named columns. If a column in the table is omitted, a NULL value is inserted instead.

The VALUES clause can be replaced by an inner SELECT statement. In this case, the INSERT statement can insert several rows in the table. For example, we want to plan twice all the reunion with a B priority level, one year later:

  • The table before the statement:
reunion
id_reunionnamedescriptionpriorityplanneddatehourduration# id_officepdf_report
1PlanningWe need to plan the project.A12008-03-2410:30:00603548644...846348
2ProgressWhat we have done.C12008-05-1214:00:0030139862...15676
3ChangeWhat we need to change in the project.B12008-06-039:30:00904134876...4846548
4PresentationPresentation of the project.D02008-09-1115:30:0012027
5ReportingExplanation to the new beginner.B12009-03-1514:00:0060719739...37718
6LearningA new software version has been installed.B12009-09-2116:00:0012011785278...37528
7Job interviewMeeting with Mr. SPENCERB12009-10-2818:30:003023
  • Query:
INSERT INTO reunion (id_reunion, name, description, priority, planned, date, hour, duration, id_office)
SELECT id_reunion + max(id_reunion), name, description, priority, 1, date + 0001-00-00, hour, duration, id_office
FROM reunion
WHERE priority = 'B';
  • The table after the statement:
reunion
id_reunionnamedescriptionpriorityplanneddatehourduration# id_officepdf_report
1PlanningWe need to plan the project.A12008-03-2410:30:00603548644...846348
2ProgressWhat we have done.C12008-05-1214:00:0030139862...15676
3ChangeWhat we need to change in the project.B12008-06-039:30:00904134876...4846548
4PresentationPresentation of the project.D02008-09-1115:30:0012027
5ReportingExplanation to the new beginner.B12009-03-1514:00:0060719739...37718
6LearningA new software version has been installed.B12009-09-2116:00:0012011785278...37528
7Job interviewMeeting with Mr. SPENCERB12009-10-2818:30:003023
10ChangeWhat we need to change in the project.B12009-06-039:30:009041
12ReportingExplanation to the new beginner.B12010-03-1514:00:00607
13LearningA new software version has been installed.B12010-09-2116:00:0012011
14Job interviewMeeting with Mr. SPENCERB12010-10-2818:30:003023

UPDATE statement

The exhaustive syntax of the UPDATE statement is as follows:

UPDATE <table name>
SET <column name> = <value>[, <column name> = <value>]*
WHERE <predicate> [{AND | OR} <predicate>]*;

The UPDATE statement is used to modify already existent records in a table. The UPDATE clause is followed by the table name in which the rows need to be changed. The SET clause is followed by couples of column name and value. The values will be inserted in the given columns. The WHERE clause contains predicates. If the predicates are true for an existent row, this row will be modified.

For instance, we want to change the date, the hour and the description of the reunion with id 14:


  • The table before the statement:
reunion
id_reunionnamedescriptionpriorityplanneddatehourduration# id_officepdf_report
1PlanningWe need to plan the project.A12008-03-2410:30:00603548644...846348
2ProgressWhat we have done.C12008-05-1214:00:0030139862...15676
3ChangeWhat we need to change in the project.B12008-06-039:30:00904134876...4846548
4PresentationPresentation of the project.D02008-09-1115:30:0012027
5ReportingExplanation to the new beginner.B12009-03-1514:00:0060719739...37718
6LearningA new software version has been installed.B12009-09-2116:00:0012011785278...37528
7Job interviewMeeting with Mr. SPENCERB12009-10-2818:30:003023
10ChangeWhat we need to change in the project.B12009-06-039:30:009041
12ReportingExplanation to the new beginner.B12010-03-1514:00:00607
13LearningA new software version has been installed.B12010-09-2116:00:0012011
14Job interviewMeeting with Mr. SPENCERB12010-10-2818:30:003023
  • Query:
UPDATE reunion
SET description = 'Meeting with Ms. JOHNSON', date = '2010-02-11', hour = '08:00:00'
WHERE id_reunion = '14';
  • The table after the statement:
reunion
id_reunionnamedescriptionpriorityplanneddatehourduration# id_officepdf_report
1PlanningWe need to plan the project.A12008-03-2410:30:00603548644...846348
2ProgressWhat we have done.C12008-05-1214:00:0030139862...15676
3ChangeWhat we need to change in the project.B12008-06-039:30:00904134876...4846548
4PresentationPresentation of the project.D02008-09-1115:30:0012027
5ReportingExplanation to the new beginner.B12009-03-1514:00:0060719739...37718
6LearningA new software version has been installed.B12009-09-2116:00:0012011785278...37528
7Job interviewMeeting with Mr. SPENCERB12009-10-2818:30:003023
10ChangeWhat we need to change in the project.B12009-06-039:30:009041
12ReportingExplanation to the new beginner.B12010-03-1514:00:00607
13LearningA new software version has been installed.B12010-09-2116:00:0012011
14Job interviewMeeting with Ms. JOHNSONB12010-02-1108:00:003023

DELETE statement

The exhaustive syntax of the DELETE statement is as follows:

DELETE FROM <table name>
[WHERE <predicate> [{AND | OR} <predicate>]*];

The DELETE statement is used to remove specific rows in a table with conditions. The FROM clause is followed by the table name in which the rows need to be removed. The WHERE clause contains predicates. If the predicates are true for an row, this row will be removed. If the predicates are false for all the rows, the statement do nothing. A DELETE statement without WHERE clause empties the table.

For example, we want to remove all the reunions that last two hours:


  • The table before the statement:
reunion
id_reunionnamedescriptionpriorityplanneddatehourduration# id_officepdf_report
1PlanningWe need to plan the project.A12008-03-2410:30:00603548644...846348
2ProgressWhat we have done.C12008-05-1214:00:0030139862...15676
3ChangeWhat we need to change in the project.B12008-06-039:30:00904134876...4846548
4PresentationPresentation of the project.D02008-09-1115:30:0012027
5ReportingExplanation to the new beginner.B12009-03-1514:00:0060719739...37718
6LearningA new software version has been installed.B12009-09-2116:00:0012011785278...37528
7Job interviewMeeting with Mr. SPENCERB12009-10-2818:30:003023
10ChangeWhat we need to change in the project.B12009-06-039:30:009041
12ReportingExplanation to the new beginner.B12010-03-1514:00:00607
13LearningA new software version has been installed.B12010-09-2116:00:0012011
14Job interviewMeeting with Ms. JOHNSONB12010-02-1108:00:003023
  • Query:
DELETE FROM reunion
WHERE duration = 120;
  • The table after the statement:
reunion
id_reunionnamedescriptionpriorityplanneddatehourduration# id_officepdf_report
1PlanningWe need to plan the project.A12008-03-2410:30:00603548644...846348
2ProgressWhat we have done.C12008-05-1214:00:0030139862...15676
3ChangeWhat we need to change in the project.B12008-06-039:30:00904134876...4846548
5ReportingExplanation to the new beginner.B12009-03-1514:00:0060719739...37718
7Job interviewMeeting with Mr. SPENCERB12009-10-2818:30:003023
10ChangeWhat we need to change in the project.B12009-06-039:30:009041
12ReportingExplanation to the new beginner.B12010-03-1514:00:00607
14Job interviewMeeting with Ms. JOHNSONB12010-02-1108:00:003023

Now you can use an already existing database schema to handle your own data.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.