Sql mutations: Difference between revisions

From wikinotes
m (Will moved page Sql: mutations to Sql mutations without leaving a redirect)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
Changing data in your database.
Changing data in your database.<br>
See [[sql schemas]] for instructions on how to change your database structure.


= Databases =
= INSERT =
<blockquote>
<syntaxhighlight lang="MySQL">
CREATE DATABASE [IF NOT EXISTS] pets_db  # Create Database
SHOW DATABASES;                          # Show All Databases
USE pets_db;                            # Select database to work with
DROP DATABASE pets_db;                  # Delete Database
</syntaxhighlight>
</blockquote><!-- Databases -->
 
= Tables =
<blockquote>
== CREATE ==
<blockquote>
<syntaxhighlight lang="MySQL">
CREATE TABLE users (
  id        INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  first_name VARCHAR(10),
  last_name  VARCHAR(10),
  age        TINYINT
);
</syntaxhighlight>
</blockquote><!-- CREATE -->
 
== ALTER ==
<blockquote>
<syntaxhighlight lang="MySQL">
ALTER TABLE users RENAME TO employees;
</syntaxhighlight>
</blockquote><!-- ALTER -->
 
== DROP ==
<blockquote>
<syntaxhighlight lang="MySQL">
DROP TABLE users;
</syntaxhighlight>
</blockquote><!-- DROP -->
</blockquote><!-- Tables -->
 
= Columns =
<blockquote>
<syntaxhighlight lang="MySQL">
ALTER TABLE pet ADD COLUMN alive INT;            # Add Column
ALTER TABLE pet ADD COLUMN alive INT  AFTER dead; # Add Column, and choose placement
ALTER TABLE pet MODIFY alive dead INT;            # Rename Column/modify datatype
ALTER TABLE pet DROP COLUMN alive;                # Delete Column
</syntaxhighlight>
</blockquote><!-- Columns -->
 
= Indexes =
<blockquote>
<syntaxhighlight lang="MySQL">
CREATE INDEX my_index ON my_table (first_name, last_name);
SHOW INDEX FROM table;
</syntaxhighlight>
</blockquote><!-- Indexes -->
 
= Rows =
<blockquote>
== INSERT ==
<blockquote>
<blockquote>
Add rows to a table.
Add rows to a table.
Line 83: Line 25:
</blockquote><!-- Insertion -->
</blockquote><!-- Insertion -->


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


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

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;