Update (SQL)

Update (SQL)

A SQL UPDATE statement that 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 [http://dev.mysql.com/doc/refman/5.0/en/update.html simplified from this page] :

: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 indexes, CHECK constraints, and NOT NULL constraints).

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'

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

Complex conditions are also possible:

UPDATE T SET A = 1 WHERE C1 = 1 AND C2 = 2

The standard does not support updates of a joined table. Therefore, the following method needs to be used. Note that the subselect in the SET clause must be a scalar subselect, i.e. it can return at most a single row.

UPDATE T1SET C1 = ( SELECT T2.C2 FROM T2 WHERE T1.ID = T2.ID )WHERE EXISTS ( SELECT 1 FROM T2 WHERE T1.ID = T2.ID )


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Update (SQL) — Правильный заголовок этой статьи  UPDATE. Он показан некорректно из за технических ограничений. Для улучшения этой статьи желательно …   Википедия

  • Update — may refer to: * Update (SQL), a SQL statement for changing database records * A patch (computing), also known as a software update * Update (university computer club), an academic computer society at Uppsala University in Sweden * Updation, a… …   Wikipedia

  • SQL-92 — SQL (das Kürzel für Structured Query Language; offizielle Aussprache [ɛskjuːˈɛl], häufig auch [ˈsiːkwəl] →SEQUEL), ist eine Datenbanksprache zur Definition, Abfrage und Manipulation von Daten in relationalen Datenbanken. SQL ist von ANSI und ISO… …   Deutsch Wikipedia

  • SQL-99 — SQL (das Kürzel für Structured Query Language; offizielle Aussprache [ɛskjuːˈɛl], häufig auch [ˈsiːkwəl] →SEQUEL), ist eine Datenbanksprache zur Definition, Abfrage und Manipulation von Daten in relationalen Datenbanken. SQL ist von ANSI und ISO… …   Deutsch Wikipedia

  • SQL — Desarrollador(es) IBM ISO/IEC 9075 1:2008 Información general Paradigma Multiparadigma …   Wikipedia Español

  • SQL — Класс языка: Мультипарадигмальный Появился в: 1974 Автор(ы): Дональд Чэмбэрлин Рэймонд Бойс Релиз: SQL:2008 (2008) Типизация данных …   Википедия

  • SQL-Injection — (dt. SQL Einschleusung) bezeichnet das Ausnutzen einer Sicherheitslücke in Zusammenhang mit SQL Datenbanken, die durch mangelnde Maskierung oder Überprüfung von Metazeichen in Benutzereingaben entsteht. Der Angreifer versucht dabei, über die… …   Deutsch Wikipedia

  • SQL-Injektion — SQL Injection (dt. SQL Einschleusung) bezeichnet das Ausnutzen einer Sicherheitslücke in Zusammenhang mit SQL Datenbanken, die durch mangelnde Maskierung oder Überprüfung von Metazeichen in Benutzereingaben entsteht. Der Angreifer versucht dabei …   Deutsch Wikipedia

  • SQL Injection — (dt. SQL Einschleusung) bezeichnet das Ausnutzen einer Sicherheitslücke in Zusammenhang mit SQL Datenbanken, die durch mangelnde Maskierung oder Überprüfung von Metazeichen in Benutzereingaben entsteht. Der Angreifer versucht dabei, über die… …   Deutsch Wikipedia

  • SQL Injektion — SQL Injection (dt. SQL Einschleusung) bezeichnet das Ausnutzen einer Sicherheitslücke in Zusammenhang mit SQL Datenbanken, die durch mangelnde Maskierung oder Überprüfung von Metazeichen in Benutzereingaben entsteht. Der Angreifer versucht dabei …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”