Vous êtes sur la page 1sur 18

DBMS ---- is all about retrieving data

Writing Basic #SQL SELECT Statements---- select means --- for retrieving data
3 capabilities
Projection ----- getting specific column
Selection --- getting specific rows sa table

---------------------------------------------------

After completing this lesson, you should be able to


do the following:

Describe each DML statement

Insert rows into a table

Update rows in a table

Delete rows from a table

Merge rows in a table

Control transactions

------------------------------------------------------------------
Data Manipulation Language (DML)
------------------------------------------------------------------
A DML statement is executed when you:
*Add new rows to a table
*Modify existing rows in a table
*Remove existing rows from a table
*A transaction consists of a collection of DML statements that form a logical unit
of work.

------------------------------------------------------------------
Adding a New Row to a Table - INSERT statement
------------------------------------------------------------------

+---------------+-----------------+------------+-------------+
| department_id | department_name | manager_id | location_id |
+---------------+-----------------+------------+-------------+
| 10 | Administration | 200 | 1700 |
| 20 | Marketing | 201 | 1800 |
| 50 | Shipping | 124 | 1500 |
| 60 | IT | 103 | 1400 |
| 80 | Sales | 149 | 2500 |
| 90 | Executive | 100 | 1700 |
| 110 | Accounting | 205 | 1700 |
| 190 | Contracting | NULL | 1700 |
+---------------+-----------------+------------+-------------+
8 rows in set (0.04 sec)

-----> to add below:


+---------------+-----------------+------------+-------------+
| 70 | Public Relations| 100 | 1700 |
+---------------+-----------------+------------+-------------+
Add new rows to a table by using the INSERT statement.

SYNTAX:
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);

ACTUAL:
INSERT INTO departments(department_id, department_name,
manager_id, location_id)
VALUES (70, 'Public Relations', 100, 1700);
1 row created.

Only one row is inserted at a time with this syntax.

------------------------------------------------------------------
Inserting New Rows - INSERT statement
------------------------------------------------------------------
Insert a new row containing values for each column.

List values in the default order of the columns in the table.

Optionally, list the columns in the INSERT clause.

INSERT INTO departments(department_id, department_name,


manager_id, location_id)
VALUES (70, 'Public Relations', 100, 1700);
1 row created.

Enclose character and date values within single quotation marks.

Adding a New Row to a Table (continued)


Because you can insert a new row that contains values for each column, the column
list is not required in the INSERT clause. However, if you do not use the column
list, the values must be listed according to the default order of the columns in
the table, and a value must be provided for each column.

DESCRIBE departments

For clarity, use the column list in the INSERT clause. Enclose character and date
values within single quotation marks; it is not recommended to enclose numeric
values within single quotation marks.
Number values should not be enclosed in single quotes, because implicit conversion
may take place for numeric values assigned to NUMBER data type columns if single
quotes are included.

------------------------------------------------------------------
Inserting Rows with Null Values - INSERT statement
------------------------------------------------------------------
Implicit method: Omit the column from the column list.

INSERT INTO departments (department_id,


department_name )
VALUES (30, 'Purchasing');
1 row created.

Explicit method: Specify the NULL keyword in the VALUES clause.


INSERT INTO departments
VALUES (100, 'Finance', NULL, NULL);
1 row created.

INSERT INTO departments


VALUES (100, 'Finance', NULL, NULL);

Query OK, 1 row affected (0.08 sec)

SELECT *
FROM departments' at line 1
SELECT *
FROM departments;
+---------------+-----------------+------------+-------------+
| department_id | department_name | manager_id | location_id |
+---------------+-----------------+------------+-------------+
| 10 | Administration | 200 | 1700 |
| 20 | Marketing | 201 | 1800 |
| 50 | Shipping | 124 | 1500 |
| 60 | IT | 103 | 1400 |
| 80 | Sales | 149 | 2500 |
| 90 | Executive | 100 | 1700 |
| 100 | Finance | NULL | NULL |
| 110 | Accounting | 205 | 1700 |
| 190 | Contracting | NULL | 1700 |
+---------------+-----------------+------------+-------------+
9 rows in set (0.00 sec)

------------------------------------------------------------------
Inserting Special Values - INSERT statement and VALUES statement
------------------------------------------------------------------
The CURDATE( ) function records the current date
and time.

INSERT INTO employees (employee_id,


first_name, last_name,
email, phone_number,
hire_date, job_id, salary,
commission_pct, manager_id,
department_id)
VALUES (113,
'Louis', 'Popp',
'LPOPP', '515.124.4567',
CURDATE(), 'AC_ACCOUNT', 6900,
NULL, 205, 100);
1 row created.
------------------------------------------------------------------
Copying Rows #from Another Table
------------------------------------------------------------------
Write your INSERT statement with a subquery.

INSERT INTO sales_reps(id, name, salary, commission_pct)


SELECT employee_id, last_name, salary, commission_pct
FROM employees
WHERE job_id LIKE '%REP%';

4 rows created.

Do not use the VALUES clause.

Match the number of columns in the INSERT clause to those in the subquery.

------------------------------------------------------------------
Changing Data in a Table - UPDATE statement
------------------------------------------------------------------
EMPLOYEES

+-------------+---------------+
| employee_id | department_id |
+-------------+---------------+
| 100 | 90 |
| 101 | 90 |
| 102 | 90 |
| 103 | 60 | <----------
| 104 | 60 | <----------
| 107 | 60 | <----------
| 124 | 50 |
| 141 | 50 |
| 142 | 50 |
| 143 | 50 |
| 144 | 50 |
| 149 | 80 |
| 174 | 80 |
| 176 | 80 |
| 178 | NULL |
| 200 | 10 |
| 201 | 20 |
| 202 | 20 |
| 205 | 110 |
| 206 | 110 |
+-------------+---------------+
20 rows in set (0.00 sec)

Update rows in the EMPLOYEES table:

+-------------+---------------+
| employee_id | department_id |
+-------------+---------------+
| 100 | 90 |
| 101 | 90 |
| 102 | 90 |
| 103 | 30 | <----------
| 104 | 30 | <----------
| 107 | 30 | <----------
| 124 | 50 |
| 141 | 50 |
| 142 | 50 |
| 143 | 50 |
| 144 | 50 |
| 149 | 80 |
| 174 | 80 |
| 176 | 80 |
| 178 | NULL |
| 200 | 10 |
| 201 | 20 |
| 202 | 20 |
| 205 | 110 |
| 206 | 110 |
+-------------+---------------+
20 rows in set (0.00 sec)

The UPDATE Statement Syntax

Modify existing rows with the UPDATE statement.

UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];

Update more than one row at a time, if required.

Updating Rows
You can modify existing rows by using the UPDATE statement.
In the syntax:
table is the name of the table
column is the name of the column in the table to populate
value is the corresponding value or subquery for the column
condition identifies the rows to be updated and is composed of column names
expressions, constants, subqueries, and comparison
operators

Confirm the update operation by querying the table to display the updated rows.

------------------------------------------------------------------
Updating Rows in a Table - UPDATE statement
------------------------------------------------------------------

Specific row or rows are modified if you specify the WHERE clause.

UPDATE employees
SET department_id = 70
WHERE employee_id = 113;
1 row updated.

All rows in the table are modified if you omit the WHERE clause.

UPDATE copy_emp
SET department_id = 110;
22 rows updated.

------------------------------------------------------------------
Updating Two Columns with a Subquery - UPDATE statement
------------------------------------------------------------------
Update employee 114�s job and salary to match that of employee 205.

UPDATE employees
SET job_id = (SELECT job_id
FROM employees
WHERE employee_id = 205),
salary = (SELECT salary
FROM employees
WHERE employee_id = 205)
WHERE employee_id = 114;
1 row updated.

Updating Two Columns with a Subquery


You can update multiple columns in the SET clause of an UPDATE statement by writing
multiple subqueries.
Syntax
UPDATE table
SET column =
(SELECT column
FROM table
WHERE condition)
[ ,
column =
(SELECT column
FROM table
WHERE condition)]
[WHERE condition ] ;
Note: If no rows are updated, a message �0 rows updated.� is returned.

------------------------------------------------------------------
Updating Rows Based on Another Table - UPDATE statement
------------------------------------------------------------------

Use subqueries in UPDATE statements to update


rows in a table based on values from another table.

UPDATE copy_emp
SET department_id = (SELECT department_id
FROM employees
WHERE employee_id = 100)
WHERE job_id = (SELECT job_id
FROM employees
WHERE employee_id = 200);
1 row updated.
------------------------------------------------------------------
Updating Rows: Integrity Constraint Error - UPDATE statement
------------------------------------------------------------------
UPDATE employees
SET department_id = 55
WHERE department_id = 110;

UPDATE employees
*
ERROR 1452(23000):
Cannot add or update a child row: a foreign key constraint fails

Department number 55 does not exist

------------------------------------------------------------------
Removing a Row from a Table - DELETE statement
------------------------------------------------------------------
DEPARTMENTS

+---------------+-----------------+------------+-------------+
| department_id | department_name | manager_id | location_id |
+---------------+-----------------+------------+-------------+
| 10 | Administration | 200 | 1700 |
| 20 | Marketing | 201 | 1800 |
| 50 | Shipping | 124 | 1500 |
| 60 | IT | 103 | 1400 |
| 80 | Sales | 149 | 2500 |
| 90 | Executive | 100 | 1700 |
| 110 | Accounting | 205 | 1700 |
| 190 | Contracting | NULL | 1700 |
+---------------+-----------------+------------+-------------+
8 rows in set (0.00 sec)

Delete a row from the DEPARTMENTS table.


DELETE FROM departments
WHERE department_name = 'Finance';
1 row deleted.

mysql> SELECT *
-> FROM departments;
+---------------+-----------------+------------+-------------+
| department_id | department_name | manager_id | location_id |
+---------------+-----------------+------------+-------------+
| 10 | Administration | 200 | 1700 |
| 20 | Marketing | 201 | 1800 |
| 50 | Shipping | 124 | 1500 |
| 60 | IT | 103 | 1400 |
| 80 | Sales | 149 | 2500 |
| 90 | Executive | 100 | 1700 |
| 100 | Finance | NULL | NULL | <-------------
| 110 | Accounting | 205 | 1700 |
| 190 | Contracting | NULL | 1700 |
+---------------+-----------------+------------+-------------+
9 rows in set (0.00 sec)

mysql> DELETE FROM departments


-> WHERE department_name = 'Finance';
Query OK, 1 row affected (0.11 sec)

mysql> SELECT * FROM departments;


+---------------+-----------------+------------+-------------+
| department_id | department_name | manager_id | location_id |
+---------------+-----------------+------------+-------------+
| 10 | Administration | 200 | 1700 |
| 20 | Marketing | 201 | 1800 |
| 50 | Shipping | 124 | 1500 |
| 60 | IT | 103 | 1400 |
| 80 | Sales | 149 | 2500 |
| 90 | Executive | 100 | 1700 |
| 110 | Accounting | 205 | 1700 |
| 190 | Contracting | NULL | 1700 |
+---------------+-----------------+------------+-------------+
8 rows in set (0.00 sec)

------------------------------------------------------------------
The DELETE Statement - DELETE statement
------------------------------------------------------------------
The DELETE Statement
You can remove existing rows from a table by using the DELETE statement.

DELETE [FROM] table


[WHERE condition];

Removing a Row from a Table


The slide graphic removes the Finance department from the DEPARTMENTS table
(assuming that there are no constraints defined on the DEPARTMENTS table).

Deleting Rows from a Table

Specific rows are deleted if you specify the WHERE clause.

DELETE FROM departments


WHERE department_name = 'Finance';
1 row deleted.

All rows in the table are deleted if you omit the WHERE clause.

DELETE FROM copy_emp;


22 rows deleted.

Deleting Rows (continued)


You can delete specific rows by specifying the WHERE clause in the DELETE
statement. The slide example deletes the Finance department from the DEPARTMENTS
table. You can confirm the delete operation by displaying the deleted rows using
the SELECT statement.

------------------------------------------------------------------
Deleting Rows Based on Another Table - DELETE statement
------------------------------------------------------------------
IMPORTANT LEARNED: SUBQUERY IS USED WHEN we want to compare two "magkalayong"
entities VIA a closer entity. Also comparison is for a compounded variable e.g.
"Abel's Salary".

IMPORTANT LEARNED: For DML - data manipulation language, subquery is used to add
row to a table, based on a variable on another table via an FKtoPK connection. Will
delete " deletes rows of data"

Use subqueries in DELETE statements to remove


rows from a table based on values from another table.

DELETE FROM employees


WHERE department_id =
(SELECT department_id
FROM departments
WHERE department_name LIKE '%Public%');
1 row deleted.

Deleting Rows Based on Another Table


You can use subqueries to delete rows from a table based on values from another
table. The example on the slide deletes all the employees who are in a department
where the department name contains the string �Public.� The subquery searches the
DEPARTMENTS table to find the department number based on the department name
containing the string �Public.� The subquery then feeds the department number to
the main query, which deletes rows of data from the EMPLOYEES table based on this
department number.

------------------------------------------------------------------
Deleting Rows: Integrity Constraint Error - DELETE statement
------------------------------------------------------------------
DELETE FROM departments
WHERE department_id = 60;

DELETE FROM departments


*
ERROR 1452(23000):
Cannot add or update a child row: a foreign key constraint fails

You cannot delete a row that contains a primary key that is used as a foreign key
in another table.
------------------------------------------------------------------
Using Explicit Default Values UPDATE statement AND DEFAULT
------------------------------------------------------------------
DEFAULT with INSERT:

INSERT INTO departments


(department_id, department_name, manager_id)
VALUES (300, 'Engineering', DEFAULT);

DEFAULT with UPDATE:

UPDATE departments
SET manager_id = DEFAULT WHERE department_id = 10;

Using Explicit Default Values


Specify DEFAULT to set the column to the value previously specified as the default
value for the column. If no default value for the corresponding column has been
specified, Oracle sets the column to null.
In the first example shown, the INSERT statement uses a default value for the
MANAGER_ID column. If there is no default value defined for the column, a null
value is inserted instead.

The second example uses the UPDATE statement to set the MANAGER_ID column to a
default value for department 10. If no default value is defined for the column, it
changes the value to null.

Summary
Statement Description
INSERT Adds a new row to the table
UPDATE Modifies existing rows in the table
DELETE Removes existing rows from the table

------------------------------------------------------------------
CREATE TABLE - 90% OF THE TIME MERON PRIMARY KEY - for columns
------------------------------------------------------------------

DROP TABLE IF EXISTS customer_contacts; ------------> always start with this to


avoid multiple tables

CREATE TABLE customer_contacts (


contact_id INT PRIMARY KEY AUTO_INCREMENT,
customer_code VARCHAR(10) NOT NULL,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email TEXT,
telephone TEXT
);

-----------------------------------------------------------------------------------
------------
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| contact_id | int(11) | NO | PRI | NULL | auto_increment |
| customer_code | varchar(10) | NO | | NULL | |
| first_name | varchar(30) | NO | | NULL | |
| last_name | varchar(30) | NO | | NULL | |
| email | text | YES | | NULL | |
| telephone | text | YES | | NULL | |
+---------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

-----------------------------------------------------------------------------------
------------
INSERT INTO customer_contacts
(customer_code, first_name, last_name, email)
VALUES ('PRESINC', 'Abraham', 'Lincoln', 'lincoln@presidentsinc.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('PRESINC', 'Richard', 'Nixon', 'nixon@presidentsinc.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('PRESINC', 'Franklin', 'Roosevelt', 'fdr@presidentsinc.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('PRESINC', 'Theodore', 'Roosevelt', 'roosevelt@presidentsinc.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('SCICORP', 'Albert', 'Einstein', 'einstein@sciencecorp.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('SCICORP', 'Charles', 'Darwin', 'darwin@sciencecorp.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('SCICORP', 'Marie', 'Curie', 'curie@sciencecorp.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('SCICORP', 'Benjamin', 'Franklin', 'franklin@sciencecorp.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('MUSGRP', 'George', 'Gershwin', 'hawking@musgrp.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('MUSGRP', 'Benjamin', 'Britten', 'britten@musgrp.com');

INSERT INTO customer_contacts


(customer_code, first_name, last_name, email)
VALUES ('MUSGRP', 'John', 'Lennon', 'lennon@musgrp.com');

-----------------------------------------------------------------------------------
----------------
+------------+---------------+------------+-----------
+-----------------------------+-----------+
| contact_id | customer_code | first_name | last_name | email
| telephone |
+------------+---------------+------------+-----------
+-----------------------------+-----------+
| 1 | PRESINC | Abraham | Lincoln | lincoln@presidentsinc.com
| NULL |
| 2 | PRESINC | Richard | Nixon | nixon@presidentsinc.com
| NULL |
| 3 | PRESINC | Franklin | Roosevelt | fdr@presidentsinc.com
| NULL |
| 4 | PRESINC | Theodore | Roosevelt | roosevelt@presidentsinc.com
| NULL |
| 5 | SCICORP | Albert | Einstein | einstein@sciencecorp.com
| NULL |
| 6 | SCICORP | Charles | Darwin | darwin@sciencecorp.com
| NULL |
| 7 | SCICORP | Marie | Curie | curie@sciencecorp.com
| NULL |
| 8 | SCICORP | Benjamin | Franklin | franklin@sciencecorp.com
| NULL |
| 9 | MUSGRP | George | Gershwin | hawking@musgrp.com
| NULL |
| 10 | MUSGRP | Benjamin | Britten | britten@musgrp.com
| NULL |
| 11 | MUSGRP | John | Lennon | lennon@musgrp.com
| NULL |
+------------+---------------+------------+-----------
+-----------------------------+-----------+
-----------------------------------------------------------------------------------
----------------

------------------------------------------------------------------

------------------------------------------------------------------

------------------------------------------------------------------

------------------------------------------------------------------
------------------------------------------------------------------

------------------------------------------------------------------

------------------------------------------------------------------

------------------------------------------------------------------
------------------------------------------------------------------

------------------------------------------------------------------

------------------------------------------------------------------

------------------------------------------------------------------

------------------------------------------------------------------

------------------------------------------------------------------

Vous aimerez peut-être aussi