Sql mutations: Difference between revisions

From wikinotes
No edit summary
Line 3: Line 3:
Add rows to a table.
Add rows to a table.


Insert
Insert multiple rows.
<syntaxhighlight lang="MySQL">
<syntaxhighlight lang="MySQL">
#          table (          columns                        )
#          table (          columns                        )
Line 13: Line 13:
</syntaxhighlight>
</syntaxhighlight>


Insert where not exist.
Insert row if match does not exist.
<syntaxhighlight lang="MySQL">
<syntaxhighlight lang="MySQL">
# Add 'Rupert' to users if does not exist
# Add 'Rupert' to users if does not exist
Line 20: Line 20:
WHERE NOT EXISTS (SELECT name FROM users WHERE name = 'Rupert') LIMIT 1;
WHERE NOT EXISTS (SELECT name FROM users WHERE name = 'Rupert') LIMIT 1;
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Insertion -->
= UPDATE =
<blockquote>
Update an existing row.


Update where column already exists
<syntaxhighlight lang="MySQL">
<syntaxhighlight lang="MySQL">
UPDATE users
UPDATE users
Line 27: Line 31:
WHERE  id = 190;
WHERE  id = 190;
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Insertion -->


= Deletion =
</blockquote><!-- UPDATe -->
 
= DELETE =
<blockquote>
<blockquote>
Delete an existing row.
<syntaxhighlight lang="MySQL">
<syntaxhighlight lang="MySQL">
DELETE FROM userDepartmentTable          ##Deleting Rows works just like adding them
DELETE FROM users
WHERE project_Id = 43
WHERE id = 100;
AND  user_Id    = 190;
 
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Deletion -->
</blockquote><!-- DELETE -->

Revision as of 17:53, 19 September 2021

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;