
Adding and Modifying Data
Introduction
So far, we have seen the following:
CREATE TABLE: create a table.DROP TABLE: delete a table.
The table we have created so far did not contain any data.
We will now look at how we can: * Add data to a table. * Modify data. * Delete data.
First, we will create the trainees table again:
CREATE TABLE trainees (
first_name VARCHAR (50) NOT NULL,
last_name VARCHAR (50) NOT NULL,
email_address VARCHAR (255) UNIQUE NOT NULL
);
| first_name | last_name | email_address |
|---|---|---|
1. Adding a Row
With INSERT INTO and VALUES, you add a row to a table.
You do the following:
INSERT INTO- Table name.
- Column names in parentheses.
- Table name.
VALUES- Values in parentheses.
In the example below, we add a row to the trainees table:
INSERT INTO
trainees (
first_name,
last_name,
email_address
)
VALUES
(
'Adriana',
'Goswin',
'a.goswin@mail.com'
);
- We check the result:
| first_name | last_name | email_address |
|---|---|---|
| Adriana | Goswin | a.goswin@mail.com |
- If you try to add the same row again, you will get an error:
- This is because the
email_addresscolumn has theUNIQUEconstraint.
2. Adding Multiple Rows
We have now added 1 row.
With INSERT INTO and VALUES, we can also add multiple rows.
In the example below, we add multiple rows to the trainees table:
INSERT INTO
trainees (
first_name,
last_name,
email_address
)
VALUES
(
'Mirthe',
'Duuk',
'm.duuk@mail.com'
),
(
'Thijmen',
'Jansen',
't.jansen@mail.com'
),
(
'Paulien',
'Spronk',
'p.spronk@mail.com'
);
- We check the result:
| first_name | last_name | email_address |
|---|---|---|
| Adriana | Goswin | a.goswin@mail.com |
| Mirthe | Duuk | m.duuk@mail.com |
| Thijmen | Jansen | t.jansen@mail.com |
| Paulien | Spronk | p.spronk@mail.com |
3. Deleting Rows
We now have a table with 3 columns and 4 rows.
It may be that a participant has unsubscribed and needs to be removed.
With DELETE FROM and WHERE, we can delete rows.
DELETE FROMallows you to delete a selection.- With
WHERE, you filter a selection to be deleted.
In the example below, we delete a selection from the trainees table:
- All rows filtered with the
WHEREstatement are deleted. - 1 row is affected.
- We check the result:
| first_name | last_name | email_address |
|---|---|---|
| Adriana | Goswin | a.goswin@mail.com |
| Mirthe | Duuk | m.duuk@mail.com |
| Thijmen | Jansen | t.jansen@mail.com |
- If you want to delete all rows from a table, use
TRUNCATE TABLE <table_name>;- Always think carefully before doing this.
4. Modifying Values
We now have a table with 3 columns and 3 rows.
It may be that, for example, an email address was entered incorrectly.
In that case, we need to modify it.
With UPDATE, SET, and WHERE, we can make modifications.
UPDATEallows you to make a modification.- From
SET, you specify the intended modification. - With
WHERE, you filter a selection to be modified.
In the example below, we modify an email address:
't.jansen@mail.com'->'t.jansen@mail.nl'.
- All rows filtered with the
WHEREstatement are modified. - 1 row is affected.
- We check the result:
| first_name | last_name | email_address |
|---|---|---|
| Adriana | Goswin | a.goswin@mail.com |
| Mirthe | Duuk | m.duuk@mail.com |
| Thijmen | Jansen | t.jansen@mail.nl |
- Here, you can specify what you want to modify using comparison (
=,>, etc.) and logical operators (AND,OR, etc.).
5. Adding a Column
We now have a table with 3 columns and 3 rows.
It may be that additional details need to be added.
In that case, we need to add a column.
With ALTER TABLE and ADD, we can add columns.
ALTER TABLEallows you to make a modification to the table structure.- From
ADD, you specify the new column(s).
In the example below, we add a new column:
- Named
date_of_birth. - With datatype
DATE.
- We check the result:
| first_name | last_name | email_address | date_of_birth |
|---|---|---|---|
| Adriana | Goswin | a.goswin@mail.com | (NULL) |
| Mirthe | Duuk | m.duuk@mail.com | (NULL) |
| Thijmen | Jansen | t.jansen@mail.nl | (NULL) |
- The new column currently contains no values.
- You can add these with
UPDATE,SET, andWHERE.
6. Deleting a Column
We now have a table with 4 columns and 3 rows.
It may be that we made a mistake and the new column date_of_birth needs to be deleted.
With ALTER TABLE and DROP COLUMN, we can delete columns.
ALTER TABLEallows you to make a modification to the table structure.- From
DROP COLUMN, you specify the column to be deleted.
In the example below, we delete a column:
- The column named
date_of_birth.
- We check the result:
| first_name | last_name | email_address |
|---|---|---|
| Adriana | Goswin | a.goswin@mail.com |
| Mirthe | Duuk | m.duuk@mail.com |
| Thijmen | Jansen | t.jansen@mail.nl |
- The column is deleted.
Summary
- Adding rows:
- With
INSERT INTOandVALUES, you add rows to a table.
- With
- Deleting rows:
- With
DELETE FROMandWHERE, you delete a filtered selection of rows. - With
TRUNCATE TABLE, you delete all rows.
- With
- Modifying values:
- With
UPDATE,SET, andWHERE, you make modifications to a filtered selection of rows.
- With
- Adding a column:
- With
ALTER TABLEandADD, you add a column.
- With
- Deleting a column:
- With
ALTER TABLEandDROP COLUMN, you delete a column.
- With