Sql mutations: Difference between revisions
From wikinotes
(Created page with "= Insertion = <blockquote> You create new rows using the <code>INSERT</code> statement. <syntaxhighlight lang="MySQL"> #### Basic Insert ## table ( columns...") |
m (Will moved page Sql: mutations to Sql mutations without leaving a redirect) |
||
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= | Changing data in your database.<br> | ||
See [[sql schemas]] for instructions on how to change your database structure. | |||
= INSERT = | |||
<blockquote> | <blockquote> | ||
Add rows to a table. | |||
Insert multiple rows. | |||
<syntaxhighlight lang="MySQL"> | <syntaxhighlight lang="MySQL"> | ||
# | # table ( columns ) | ||
# | | | |||
# | INSERT INTO tasks(subject, start_date, end_date, description) | ||
INSERT INTO tasks(subject,start_date,end_date,description) | |||
VALUES ('Task 1','2010-01-01','2010-01-02','Description 1'), | VALUES ('Task 1','2010-01-01','2010-01-02','Description 1'), | ||
('Task 2','2010-01-01','2010-01-02','Description 2'), | ('Task 2','2010-01-01','2010-01-02','Description 2'), | ||
('Task 3','2010-01-01','2010-01-02','Description 3'); | ('Task 3','2010-01-01','2010-01-02','Description 3'); | ||
</syntaxhighlight> | |||
Insert row if match does not exist. | |||
# | <syntaxhighlight lang="MySQL"> | ||
INSERT INTO | # Add 'Rupert' to users if does not exist | ||
INSERT INTO users (name, address, tele) | |||
SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp | SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp | ||
WHERE NOT EXISTS ( | WHERE NOT EXISTS (SELECT name FROM users WHERE name = 'Rupert') LIMIT 1; | ||
</syntaxhighlight> | |||
) LIMIT 1; | </blockquote><!-- Insertion --> | ||
= UPDATE = | |||
<blockquote> | |||
Update an existing row. | |||
<syntaxhighlight lang="MySQL"> | |||
UPDATE | UPDATE users | ||
set email = | set email = "user@domain.com" | ||
WHERE | WHERE id = 190; | ||
</syntaxhighlight> | |||
</blockquote><!-- UPDATe --> | |||
</blockquote><!-- | |||
= | = DELETE = | ||
<blockquote> | <blockquote> | ||
Delete an existing row. | |||
<syntaxhighlight lang="MySQL"> | <syntaxhighlight lang="MySQL"> | ||
DELETE FROM | DELETE FROM users | ||
WHERE | WHERE id = 100; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- | </blockquote><!-- DELETE --> |
Latest revision as of 18:37, 19 September 2021
Changing data in your database.
See sql schemas for instructions on how to change your database structure.
INSERT
Add rows to a table.
Insert multiple rows.
# table ( columns ) # | | INSERT INTO tasks(subject, start_date, end_date, description) VALUES ('Task 1','2010-01-01','2010-01-02','Description 1'), ('Task 2','2010-01-01','2010-01-02','Description 2'), ('Task 3','2010-01-01','2010-01-02','Description 3');Insert row if match does not exist.
# Add 'Rupert' to users if does not exist INSERT INTO users (name, address, tele) SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp WHERE NOT EXISTS (SELECT name FROM users WHERE name = 'Rupert') LIMIT 1;
UPDATE
Update an existing row.
UPDATE users set email = "user@domain.com" WHERE id = 190;
DELETE
Delete an existing row.
DELETE FROM users WHERE id = 100;