Académique Documents
Professionnel Documents
Culture Documents
Writing Basic #SQL SELECT Statements---- select means --- for retrieving data
3 capabilities
Projection ----- getting specific column
Selection --- getting specific rows sa 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)
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.
------------------------------------------------------------------
Inserting New Rows - INSERT statement
------------------------------------------------------------------
Insert a new row containing values 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.
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.
4 rows created.
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)
+-------------+---------------+
| 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)
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
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 Rows Based on Another Table - UPDATE statement
------------------------------------------------------------------
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
------------------------------------------------------------------
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)
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)
------------------------------------------------------------------
The DELETE Statement - DELETE statement
------------------------------------------------------------------
The DELETE Statement
You can remove existing rows from a table by using the DELETE statement.
All rows in the table are deleted if you omit the WHERE clause.
------------------------------------------------------------------
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"
------------------------------------------------------------------
Deleting Rows: Integrity Constraint Error - DELETE statement
------------------------------------------------------------------
DELETE FROM departments
WHERE department_id = 60;
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:
UPDATE departments
SET manager_id = DEFAULT WHERE department_id = 10;
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
------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------
+---------------+-------------+------+-----+---------+----------------+
| 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');
-----------------------------------------------------------------------------------
----------------
+------------+---------------+------------+-----------
+-----------------------------+-----------+
| 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 |
+------------+---------------+------------+-----------
+-----------------------------+-----------+
-----------------------------------------------------------------------------------
----------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------