Sql mutations: Difference between revisions

From wikinotes
No edit summary
No edit summary
Line 1: Line 1:
= INSERT =
= Rows =
<blockquote>
== INSERT ==
<blockquote>
<blockquote>
Add rows to a table.
Add rows to a table.
Line 22: Line 24:
</blockquote><!-- Insertion -->
</blockquote><!-- Insertion -->


= UPDATE =
== UPDATE ==
<blockquote>
<blockquote>
Update an existing row.
Update an existing row.
Line 34: Line 36:
</blockquote><!-- UPDATe -->
</blockquote><!-- UPDATe -->


= DELETE =
== DELETE ==
<blockquote>
<blockquote>
Delete an existing row.
Delete an existing row.
Line 43: Line 45:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- DELETE -->
</blockquote><!-- DELETE -->
</blockquote><!-- Rows -->

Revision as of 18:02, 19 September 2021

Rows

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;