Upload
aldan
View
90
Download
0
Embed Size (px)
DESCRIPTION
SQL 結構化查詢語言. SQL 語言種類. 資料處理型語言( DML Data Manipulation Language) SELECT,INSERT,UPDATE,DELETE 交易控制型語言( Transaction Control) COMMIT,ROLLBACK,SAVEPOINT. SQL 語言種類(續). 資料定義型語言( DDL Data Definition Language) CREATE,ALTER,DROP 資料控制型語言( DCL Data Control Language) GRANT,REVOKE,SET ROLE. - PowerPoint PPT Presentation
Citation preview
2001/03/21 1
SQL 結構化查詢語言
2001/03/21 2
SQL 語言種類 資料處理型語言 (DML Data
Manipulation Language) SELECT,INSERT,UPDATE,DELETE
交易控制型語言 (Transaction Control) COMMIT,ROLLBACK,SAVEPOINT
2001/03/21 3
SQL 語言種類(續) 資料定義型語言 (DDL Data Definition
Language) CREATE,ALTER,DROP
資料控制型語言 (DCL Data Control Language) GRANT,REVOKE,SET ROLE
2001/03/21 4
一、資料處理型語言 (DML)
SELECT Syntax( 語法 ) : SELECT [DISTINCT] {* , column [alias]..} FROM table ; * : selects all column column: select the name of column alias : give a select
2001/03/21 5
Example : 1. SELECT * FROM emp ;
2. SELECT emp_no , emp_name FROM emp ;
2001/03/21 6
Arithmetic Expressions
Operators Description
+ add
- subtract
* multiply
/ divide
先乘除後加減,可以用括號改變次序
2001/03/21 7
Example : SELECT emp_no , emp_name ,
emp_salary * 5 FROM emp ;
2001/03/21 8
Column Aliases
Example : SELECT emp_no AS 序號 ,
emp_name “ 姓名” FROM emp ;
2001/03/21 9
Concatenation Operator
Example : SELECT emp_no || emp_name AS
“序號 姓名” FROM emp ;
2001/03/21 10
NVL Function
Data type Conversion Example
Number NVL(number_column,9)
Date NVL(date_column,’15-JAN-98’)
CHAR orVARCHAR2
NVL(character_column,’NULL’)
2001/03/21 11
Example : SELECT emp_no,emp_name,emp_dept FROM emp ;
Example : SELECT
emp_no,emp_name,NVL(emp_dept,’NO_WRITE)
FROM emp ;
2001/03/21 12
Duplicate Rows
Example : SELECT emp_no,emp_name FROM emp ;
Example : SELECT DISTINCT
emp_no,emp_name FROM emp ;
2001/03/21 13
Limit Condition and Order
Syntax : SELECT expr FROM table WHERE condition ORDER BY expr ;
2001/03/21 14
Comparison and Logical Operators
Logical comparison operators= > >= < <=
SQL comparison operators BETWEEN ... AND... IN(list) LIKE IS NULL
Logical operators AND OR NOT
2001/03/21 15
Negating Expressions
Logical Operators!= <> ^=
SQL Operators NOT BETWEEN NOT IN NOT LIKE IS NOT NULL
2001/03/21 16
BETWEEN and IN SQL Operators
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_no BETWEEN ‘1001'emp_no BETWEEN ‘1001' 4 4 AND ‘1025';AND ‘1025';
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_no BETWEEN ‘1001'emp_no BETWEEN ‘1001' 4 4 AND ‘1025';AND ‘1025';
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_dept IN (‘D01’,’D02’);emp_dept IN (‘D01’,’D02’);
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_dept IN (‘D01’,’D02’);emp_dept IN (‘D01’,’D02’);
2001/03/21 17
LIKE SQL Operator You can use the LIKE operator to
perform wildcard searches of valid search string values.
Search conditions can contain either literal characters or numbers. "%" denotes none or many characters. "_" denotes one character.
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_name LIKE ‘USER%5';emp_name LIKE ‘USER%5';
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_name LIKE ‘USER%5';emp_name LIKE ‘USER%5';
2001/03/21 18
LIKE SQL Operator
SQL> SELECTSQL> SELECT emp_name, emp_deptemp_name, emp_dept 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_salary LIKE '%9_';emp_salary LIKE '%9_';
SQL> SELECTSQL> SELECT emp_name, emp_deptemp_name, emp_dept 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_salary LIKE '%9_';emp_salary LIKE '%9_';
SQL> SELECTSQL> SELECT emp_name, emp_deptemp_name, emp_dept 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_salary LIKE '%9%';emp_salary LIKE '%9%';
SQL> SELECTSQL> SELECT emp_name, emp_deptemp_name, emp_dept 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_salary LIKE '%9%';emp_salary LIKE '%9%';
2001/03/21 19
IS NULL SQL Operator
Test for null values with the = NULL operator.
Use the = operator.
SQL> SELECTSQL> SELECT emp_name, emp_deptemp_name, emp_dept 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_salary = NULL;emp_salary = NULL;
SQL> SELECTSQL> SELECT emp_name, emp_deptemp_name, emp_dept 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_salary = NULL;emp_salary = NULL;
2001/03/21 20
Multiple Conditions
Use complex criteria. Combine conditions with AND or
OR operators. AND requires both conditions to be
TRUE. OR requires either condition to be OR requires either condition to be
TRUE.TRUE.
2001/03/21 21
Multiple Conditions
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_dept = ‘D01’emp_dept = ‘D01’ 4 AND4 AND emp_salary between 3000 AND 5000;emp_salary between 3000 AND 5000;
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_dept = ‘D01’emp_dept = ‘D01’ 4 AND4 AND emp_salary between 3000 AND 5000;emp_salary between 3000 AND 5000;
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_dept = ‘D01’emp_dept = ‘D01’ 4 OR4 OR emp_salary between 3000 AND 5000;emp_salary between 3000 AND 5000;
SQL> SELECTSQL> SELECT ** 2 FROM 2 FROM empemp 3 WHERE3 WHERE emp_dept = ‘D01’emp_dept = ‘D01’ 4 OR4 OR emp_salary between 3000 AND 5000;emp_salary between 3000 AND 5000;
2001/03/21 22
Displaying Data from Displaying Data from Multiple TablesMultiple Tables
2001/03/21 23
What Is a Join? A join is used to query data from more
than one table. Rows are joined using common values,
typically primary and foreign key values. Join methods
Equijoin Non-equijoin Outer join Self join Set operators
2001/03/21 24
Relations Between TablesS_EMP TableS_EMP TableID LAST_NAME ID LAST_NAME DEPT_IDDEPT_ID-- --------------- --------- --------------- ------- 1 Velasquez1 Velasquez 5050 2 Ngao2 Ngao 4141 3 Nagayama3 Nagayama 3131 4 Quick-To-See4 Quick-To-See 1010 5 Ropeburn5 Ropeburn 5050 6 6 UrguhartUrguhart 4141 7 Menchu7 Menchu 4242 8 Biri8 Biri 4343 9 Catchpole9 Catchpole 444410 Havel10 Havel 454511 Magee11 Magee 313112 Giljum12 Giljum 323213 Sedeghi13 Sedeghi 333314 Nguyen14 Nguyen 343415 Dumas15 Dumas 353516 16 MaduroMaduro 4141
S_EMP TableS_EMP TableID LAST_NAME ID LAST_NAME DEPT_IDDEPT_ID-- --------------- --------- --------------- ------- 1 Velasquez1 Velasquez 5050 2 Ngao2 Ngao 4141 3 Nagayama3 Nagayama 3131 4 Quick-To-See4 Quick-To-See 1010 5 Ropeburn5 Ropeburn 5050 6 6 UrguhartUrguhart 4141 7 Menchu7 Menchu 4242 8 Biri8 Biri 4343 9 Catchpole9 Catchpole 444410 Havel10 Havel 454511 Magee11 Magee 313112 Giljum12 Giljum 323213 Sedeghi13 Sedeghi 333314 Nguyen14 Nguyen 343415 Dumas15 Dumas 353516 16 MaduroMaduro 4141
S_DEPT TableS_DEPT TableID NAME REGION_IDID NAME REGION_ID-- --------------- ----------- --------------- ---------30 Finance 130 Finance 131 Sales 131 Sales 132 Sales 232 Sales 243 43 Operations 3Operations 350 Administration 150 Administration 1
S_DEPT TableS_DEPT TableID NAME REGION_IDID NAME REGION_ID-- --------------- ----------- --------------- ---------30 Finance 130 Finance 131 Sales 131 Sales 132 Sales 232 Sales 243 43 Operations 3Operations 350 Administration 150 Administration 1S_REGION TableS_REGION Table
ID NAMEID NAME-- ----------------------- --------------------- 1 North America1 North America 2 South America2 South America 3 Africa / Middle East3 Africa / Middle East 4 Asia4 Asia 5 Europe5 Europe
S_REGION TableS_REGION TableID NAMEID NAME-- ----------------------- --------------------- 1 North America1 North America 2 South America2 South America 3 Africa / Middle East3 Africa / Middle East 4 Asia4 Asia 5 Europe5 Europe
2001/03/21 25
Simple Join Query: Syntax Write the join condition in the WHERE
clause. Precede each column name with the
table name for clarity. Column names must be prefixed with
the table name when the same column name appears in more than one table.
SELECTSELECT table.column, table.columntable.column, table.column
FROMFROM table1, table2table1, table2
WHEREWHERE table1.column1 = table2.column2table1.column1 = table2.column2;;
SELECTSELECT table.column, table.columntable.column, table.column
FROMFROM table1, table2table1, table2
WHEREWHERE table1.column1 = table2.column2table1.column1 = table2.column2;;
2001/03/21 26
Equijoin: Example
EMPEMP DEPTDEPT
ServerServer
LAST_NAME LAST_NAME DEPT_IDDEPT_ID IDID NAMENAME------------------ -------------- ---- ------------------------------VelasquezVelasquez 5050 5050 AdministrationAdministrationNgaoNgao 4141 4141 OperationsOperationsNagayamaNagayama 3131 3131 SalesSalesRopeburnRopeburn 5050 5050 AdministrationAdministrationUrguhartUrguhart 4141 4141 OperationsOperationsMenchuMenchu 4242 4242 OperationsOperationsBiriBiri 4343 4343 OperationsOperationsHavelHavel 4545 4545 OperationsOperations...... ......
2001/03/21 27
Additional Search Conditions Using the AND Operator: Example
EMPEMP DEPTDEPT
ServerServer
LAST_NAME LAST_NAME DEPT_IDDEPT_ID IDID NAMENAME------------------ -------------- ---- ------------------------------VelasquezVelasquez 5050 5050 AdministrationAdministrationNgaoNgao 4141 4141 OperationsOperationsNagayamaNagayama 3131 3131 SalesSalesRopeburnRopeburn 5050 5050 AdministrationAdministrationUrguhartUrguhart 4141 4141 OperationsOperationsMenchuMenchu 4242 4242 OperationsOperationsBiriBiri 4343 4343 OperationsOperationsHavelHavel 4545 4545 OperationsOperations...... ......
2001/03/21 28
Non-Equijoins: Example
Non-equijoins result when no column in one table corresponds directly to a column in the second table.
The join condition contains an operator other than equal (=).
SQL> SELECT e.ename, e.job, e.sal, s.gradeSQL> SELECT e.ename, e.job, e.sal, s.grade
22 FROM emp e, salgrade sFROM emp e, salgrade s
33 WHERE e.sal BETWEEN s.losal AND s.hisal;WHERE e.sal BETWEEN s.losal AND s.hisal;
SQL> SELECT e.ename, e.job, e.sal, s.gradeSQL> SELECT e.ename, e.job, e.sal, s.grade
22 FROM emp e, salgrade sFROM emp e, salgrade s
33 WHERE e.sal BETWEEN s.losal AND s.hisal;WHERE e.sal BETWEEN s.losal AND s.hisal;
2001/03/21 29
Outer Joins: Syntax
Use an outer join to see rows that do not normally meet the join condition.
Outer join operator is the plus sign (+).
Place the operator on the side of the join where there is no value to join to.
SELECTSELECT table.column, table.columntable.column, table.column
FROMFROM table1, table2table1, table2
WHEREWHERE table1.column(+) = table2.columntable1.column(+) = table2.column;;
SELECTSELECT table.column, table.columntable.column, table.column
FROMFROM table1, table2table1, table2
WHEREWHERE table1.column(+) = table2.columntable1.column(+) = table2.column;;
2001/03/21 30
Outer Joins
EMPEMP CUSTOMERCUSTOMER
SALES_SALES_LAST_NAME LAST_NAME IDID REP_IDREP_ID NAMENAME------------------ ---- ------------ ----------------------------MageeMagee 1111 1111 WomansportWomansportMageeMagee 1111 1111 Beisbol Si!Beisbol Si!MageeMagee 1111 1111 Ojibway RetailOjibway RetailGiljumGiljum 1212 1212 UnisportsUnisportsGiljumGiljum 1212 1212 Futbol SonoraFutbol SonoraSedeghiSedeghi 1313 1313 Hamada SportHamada SportDumasDumas 1515 1515 SportiqueSportique
Sweet Rock SportsSweet Rock Sports
No Sales RepNo Sales RepAssigned toAssigned toSweet RockSweet RockSportsSports
ServerServer
2001/03/21 31
Outer Joins: Example
• Display the sales representative Display the sales representative name and the customer name for all name and the customer name for all customers even if the customer has customers even if the customer has no sales representative.no sales representative.
SQL> SELECTSQL> SELECT e.last_name, e.id, c.namee.last_name, e.id, c.name 2 FROM2 FROM s_emp e, s_customer cs_emp e, s_customer c 3 WHERE3 WHERE e.id (+) = c.sales_rep_ide.id (+) = c.sales_rep_id 4 ORDER BY4 ORDER BY e.id;e.id;
SQL> SELECTSQL> SELECT e.last_name, e.id, c.namee.last_name, e.id, c.name 2 FROM2 FROM s_emp e, s_customer cs_emp e, s_customer c 3 WHERE3 WHERE e.id (+) = c.sales_rep_ide.id (+) = c.sales_rep_id 4 ORDER BY4 ORDER BY e.id;e.id;
2001/03/21 32
Self Joins
S_EMP (WORKER)S_EMP (WORKER) S_EMP (MANAGER)S_EMP (MANAGER)
LAST_NAME LAST_NAME MANAGER_IDMANAGER_ID IDID LAST_NAMELAST_NAME------------------ -------------------- ---- --------------------NgaoNgao 11 11 VelasquezVelasquezNagayamaNagayama 11 11 Velasquez Velasquez RopeburnRopeburn 11 11 Velasquez Velasquez UrguhartUrguhart 22 22 NgaoNgaoMenchuMenchu 22 2 2 Ngao Ngao BiriBiri 22 2 2 Ngao Ngao MageeMagee 33 33 NagaymaNagaymaGiljumGiljum 33 3 3 Nagayma Nagayma ...... ......
ServerServer
2001/03/21 33
Self Joins: Example
Join rows in a table to rows in the same table by using a self join.
Simulate two tables in the FROM clause by creating two aliases for the table.
SQL> SELECTSQL> SELECT worker.last_name||' works for '||worker.last_name||' works for '||
2 2 manager.last_namemanager.last_name
3 FROM3 FROM s_emp worker, s_emp managers_emp worker, s_emp manager
4 WHERE4 WHERE worker.manager_id = manager.id;worker.manager_id = manager.id;
SQL> SELECTSQL> SELECT worker.last_name||' works for '||worker.last_name||' works for '||
2 2 manager.last_namemanager.last_name
3 FROM3 FROM s_emp worker, s_emp managers_emp worker, s_emp manager
4 WHERE4 WHERE worker.manager_id = manager.id;worker.manager_id = manager.id;
2001/03/21 34
What Is a Subquery?
SELECT Syntax
SELECT...SELECT...
FROM...FROM...
WHERE...WHERE...SELECT Syntax
((SELECT...SELECT...
FROM...FROM...
WHERE...);WHERE...);
Main Main QueryQuery
SubquerySubquery
A subsequery is a SELECT statement embedded in a A subsequery is a SELECT statement embedded in a clause of another SQL statement.clause of another SQL statement.
2001/03/21 35
Subqueries: Example
S_EMPS_EMP S_EMPS_EMP
LAST_NAME LAST_NAME TITLETITLE TITLETITLE LAST_NAMELAST_NAME------------------ ---------------------- ---------------------- ------------------MaduroMaduro Stock ClerkStock Clerk Stock Clerk Stock Clerk SmithSmithSmithSmith Stock Clerk Stock Clerk NozakiNozaki Stock Clerk Stock Clerk PatelPatel Stock Clerk Stock Clerk NewmanNewman Stock Clerk Stock Clerk ChangChang Stock Clerk Stock Clerk PatelPatel Stock Clerk Stock Clerk DancsDancs Stock Clerk Stock Clerk SchwartzSchwartz Stock Clerk Stock Clerk
ServerServer
2001/03/21 36
Subqueries: Syntax
The subquery executes once before the main query.
The result of the subquery is used by the main outer query.
SELECTSELECT select_listselect_listFROMFROM tabletableWHEREWHERE expr operatorexpr operator
((SELECTSELECT select_listselect_list FROMFROM tabletable););
SELECTSELECT select_listselect_listFROMFROM tabletableWHEREWHERE expr operatorexpr operator
((SELECTSELECT select_listselect_list FROMFROM tabletable););
2001/03/21 37
How Are Nested Subqueries Processed?
SELECTSELECT last_name, titlelast_name, titleFROMFROM s_emps_empWHEREWHERE dept_id = dept_id =
SELECTSELECT dept_iddept_idFROMFROM s_emps_empWHEREWHERE last_name='Biri';last_name='Biri';
Nested QueryNested Query Main QueryMain Query
4343
1.Nested SELECT statement is executed first.
2.Result is passed into condition of main query.
2001/03/21 38
Single Row Subqueries
SELECT last_name, titleSELECT last_name, titleFROM FROM s_emp s_empWHERE WHERE title = title =
SELECT titleSELECT titleFROM FROM s_emp s_empWHERE WHERE last_name = ’Smith’ last_name = ’Smith’
SQL> SELECT last_name, titleSQL> SELECT last_name, title 2 FROM s_emp 2 FROM s_emp 3 WHERE title = 3 WHERE title = 4 4 (SELECT title (SELECT title 5 5 FROM s_emp FROM s_emp 6 6 WHERE last_name = ’Smith’); WHERE last_name = ’Smith’);
Write the SQLWrite the SQLstatement tostatement todisplay thedisplay thelast name last name and title of and title of an employee.an employee.
Write the SQLWrite the SQLstatement tostatement tofind out find out Smith’s title.Smith’s title.
Put bothPut bothstatementsstatementstogether and together and let SQL let SQL determinedeterminethe title forthe title forSmith.Smith.
2001/03/21 39
Multiple Row Subqueries: Example
A multiple row subquery returns many rows.
You must use a multiple row operator in the WHERE clause, for example the IN operator.SQL> SELECTSQL> SELECT last_name, first_name, titlelast_name, first_name, title 2 FROM2 FROM s_emps_emp 3 WHERE3 WHERE dept_in INdept_in IN 44 (SELECT(SELECT IDID 55 FROMFROM s_depts_dept66 WHERE name = 'Finance'WHERE name = 'Finance'77 OR region_id = 2);OR region_id = 2);
SQL> SELECTSQL> SELECT last_name, first_name, titlelast_name, first_name, title 2 FROM2 FROM s_emps_emp 3 WHERE3 WHERE dept_in INdept_in IN 44 (SELECT(SELECT IDID 55 FROMFROM s_depts_dept66 WHERE name = 'Finance'WHERE name = 'Finance'77 OR region_id = 2);OR region_id = 2);
2001/03/21 40
Creating Tables: Syntax
You must have specific privileges: CREATE TABLE A storage area
CREATE TABLE [CREATE TABLE [schemaschema.].]tabletable
((column datatypecolumn datatype [DEFAULT [DEFAULT exprexpr]]
[[column_constraintcolumn_constraint], ],
......
[[table_constrainttable_constraint]);]);
CREATE TABLE [CREATE TABLE [schemaschema.].]tabletable
((column datatypecolumn datatype [DEFAULT [DEFAULT exprexpr]]
[[column_constraintcolumn_constraint], ],
......
[[table_constrainttable_constraint]);]);
2001/03/21 41
Datatypes
DatatypeDatatype
VARCHAR(size)VARCHAR(size)
CHAR(CHAR(sizesize))
intint
floatfloat
DATEDATE
DATETIMEDATETIME
Decimal(M,D)Decimal(M,D)
DescriptionDescription
Variable length character valuesVariable length character values
Fixed length character valuesFixed length character values
IntegerInteger
單精浮點數單精浮點數
DateDate
Date and time valuesDate and time values
MM為全部數字位數為全部數字位數 ((包括小數位包括小數位 ))
DD為小數位數為小數位數
2001/03/21 42
Create Table: Example
SQL> CREATE TABLE s_deptSQL> CREATE TABLE s_dept
-> (id -> (id NUMBER(7),NUMBER(7),
-> name -> name VARCHAR (25),VARCHAR (25),
-> region_id -> region_id NUMBER(7));NUMBER(7));
SQL> CREATE TABLE s_deptSQL> CREATE TABLE s_dept
-> (id -> (id NUMBER(7),NUMBER(7),
-> name -> name VARCHAR (25),VARCHAR (25),
-> region_id -> region_id NUMBER(7));NUMBER(7));
2001/03/21 43
DescriptionDescription
Adds a new row to the table.Adds a new row to the table.
Modifies existing rows in the table.Modifies existing rows in the table.
Removes existing rows from the table.Removes existing rows from the table.
Makes all pending changes permanent.Makes all pending changes permanent.
Data Manipulation and Transaction Control Commands
CommandCommand
INSERTINSERT
UPDATEUPDATE
DELETEDELETE
COMMITCOMMIT
2001/03/21 44
Inserting New Rows into a Table: Syntax
Add new rows to a table by using the INSERT command.
Only one row is inserted at a time with this syntax.
INSERT INTOINSERT INTO tabletable [( [(columncolumn [, [, columncolumn...])]...])]
VALUESVALUES ((valuevalue [, [, valuevalue...]);...]);
INSERT INTOINSERT INTO tabletable [( [(columncolumn [, [, columncolumn...])]...])]
VALUESVALUES ((valuevalue [, [, valuevalue...]);...]);
2001/03/21 45
Inserting New Rows: Example
Insert a new row containing values for each column.
Optionally list the columns in the INSERT clause.
List values in the default order of the columns in the table.
Enclose character and date values within single quotation marks.
SQL> INSERT INTOSQL> INSERT INTO s_depts_dept
2 VALUES2 VALUES (11, 'Finance', 2);(11, 'Finance', 2);
1 1 row created.row created.
SQL> INSERT INTOSQL> INSERT INTO s_depts_dept
2 VALUES2 VALUES (11, 'Finance', 2);(11, 'Finance', 2);
1 1 row created.row created.
2001/03/21 46
Updating Rows in a Table: Syntax
Modify existing rows with the UPDATE command.
UPDATEUPDATE tabletableSETSET columncolumn = = valuevalue [, [, column column = = valuevalue]][[WHEREWHERE conditioncondition];];
UPDATEUPDATE tabletableSETSET columncolumn = = valuevalue [, [, column column = = valuevalue]][[WHEREWHERE conditioncondition];];
2001/03/21 47
Updating Rows: Examples
Transfer employee number 2 to department 10.Transfer employee number 2 to department 10.
SQL> UPDATESQL> UPDATE s_emps_emp 2 SET2 SET dept_id = 10dept_id = 10 3 WHERE3 WHERE id = 2;id = 2;1 1 row updated.row updated.
SQL> UPDATESQL> UPDATE s_emps_emp 2 SET2 SET dept_id = 10dept_id = 10 3 WHERE3 WHERE id = 2;id = 2;1 1 row updated.row updated.
SQL> UPDATESQL> UPDATE s_emps_emp 2 SET2 SET dept_id = 32, salary = 2550dept_id = 32, salary = 2550 3 WHERE3 WHERE id = 1;id = 1;1 1 row updated.row updated.
SQL> UPDATESQL> UPDATE s_emps_emp 2 SET2 SET dept_id = 32, salary = 2550dept_id = 32, salary = 2550 3 WHERE3 WHERE id = 1;id = 1;1 1 row updated.row updated.
• Transfer employee number 1 to department 32 Transfer employee number 1 to department 32 and change the salary to 2550.and change the salary to 2550.
2001/03/21 48
Updating All Rows in the Table
All rows in the table will be updated if you do not add the WHERE clause.
SQL> UPDATESQL> UPDATE s_emps_emp 2 SET2 SET commission_pct = 10;commission_pct = 10;25 25 rows updated.rows updated.
SQL> UPDATESQL> UPDATE s_emps_emp 2 SET2 SET commission_pct = 10;commission_pct = 10;25 25 rows updated.rows updated.
2001/03/21 49
Deleting Rows from a Table: Syntax
Remove existing rows by using the DELETE Remove existing rows by using the DELETE command.command.
• Remove all information about employees who Remove all information about employees who started after January 1, 1996.started after January 1, 1996.
DELETE [FROM]DELETE [FROM] tabletable[[WHEREWHERE conditioncondition];];
DELETE [FROM]DELETE [FROM] tabletable[[WHEREWHERE conditioncondition];];
SQL> DELETE FROMSQL> DELETE FROM s_emps_emp 2 WHERE 2 WHERE start_date > start_date > 3 TO_DATE('01.01.1996', 'DD.MM.YYYY');3 TO_DATE('01.01.1996', 'DD.MM.YYYY');1 1 row deleted.row deleted.
SQL> DELETE FROMSQL> DELETE FROM s_emps_emp 2 WHERE 2 WHERE start_date > start_date > 3 TO_DATE('01.01.1996', 'DD.MM.YYYY');3 TO_DATE('01.01.1996', 'DD.MM.YYYY');1 1 row deleted.row deleted.
2001/03/21 50
Deleting Rows: Example
Delete all the rows in the table by excluding the WHERE clause.
SQL> DELETE FROMSQL> DELETE FROM test;test;25,000 25,000 rows deleted.rows deleted.
SQL> DELETE FROMSQL> DELETE FROM test;test;25,000 25,000 rows deleted.rows deleted.
• Confirm the deletions.Confirm the deletions.
SQL> SELECTSQL> SELECT ** 2 FROM2 FROM test;test;no rows selectedno rows selected
SQL> SELECTSQL> SELECT ** 2 FROM2 FROM test;test;no rows selectedno rows selected