SlideShare ist ein Scribd-Unternehmen logo
1 von 29
More SQL Data Definition
INSERT, UPDATE, DELETE
• INSERT - add a row to a
table
• UPDATE - change row(s) in
a table
• DELETE - remove row(s)
from a table
• UPDATE and DELETE use
‘WHERE clauses’ to specify
which rows to change or
remove
• BE CAREFUL with these - an
incorrect WHERE clause can
destroy lots of data
More SQL Data Definition
INSERT
INSERT INTO
<table>
(col1, col2, …)
VALUES
(val1, val2, …)
– The number of columns and
values must be the same
– If you are adding a value to
every column, you don’t have
to list them
– SQL doesn’t require that all
rows are different (unless a
constraint says so)
More SQL Data Definition
INSERT
Student
ID
1
Name
John
Year
1
Student
ID
1
2
Name
John
Mary
Year
1
Student
ID
1
2
Name
John
Mary
Year
1
3
Student
ID
1
2
Name
John
Mary
Year
1
3
INSERT INTO Student
(ID, Name, Year)
VALUES (2, ‘Mary’, 3)
INSERT INTO Student
(Name, ID)
VALUES (‘Mary’, 2)
INSERT INTO Student
VALUES (2, ‘Mary’, 3)
More SQL Data Definition
UPDATE
UPDATE <table>
SET col1 = val1
[,col2 = val2…]
[WHERE
<condition>]
– All rows where the condition
is true have the columns set
to the given values
– If no condition is given all
rows are changed so BE
CAREFUL
– Values are constants or can
be computed from columns
More SQL Data Definition
UPDATE
Student
ID
1
2
3
4
Name
John
Mark
Anne
Mary
Year
1
3
2
2
Student
ID
1
2
3
4
Name
John
Mark
Anne
Mary
Year
2
4
3
3
UPDATE Student
SET Year = Year + 1
UPDATE Student
SET Year = 1,
Name = ‘Jane’
WHERE ID = 4
Student
ID
1
2
3
4
Name
John
Mark
Anne
Jane
Year
1
3
2
1
More SQL Data Definition
DELETE
• Removes all rows which
satisfy the condition
DELETE FROM
<table>
[WHERE
<condition>]
– If no condition is given then
ALL rows are deleted - BE
CAREFUL
– Some versions of SQL also
have TRUNCATE TABLE <T>
which is like DELETE FROM
<T>
More SQL Data Definition
DELETE
Student
ID
1
2
3
4
Name
John
Mark
Anne
Mary
Year
1
3
2
2
Student
ID Name Year
DELETE FROM Student
or
TRUNCATE TABLE Student
DELETE FROM
Student
WHERE Year = 2
Student
ID
1
2
Name
John
Mark
Year
1
3
More SQL Data Definition
Being Careful
• When using DELETE and
UPDATE
– You need to be careful to
have the right WHERE clause
– You can check it by running a
SELECT statement with the
same WHERE clause first
Before running
DELETE FROM Student
WHERE Year = 3
run
SELECT * FROM Student
WHERE Year = 3
More SQL Data Definition
SQL SELECT Overview
SELECT
<column-list>
FROM <table-names>
[WHERE <condition>]
[GROUP BY <column-list>]
[HAVING <condition>]
[ORDER BY <column-list>]
• ([]- optional, | - or)
More SQL Data Definition
Simple SELECT
SELECT <columns>
FROM <table>
<columns> can be
– A single column
– A comma-separated list of
columns
– * for ‘all columns’
• Given a table Student with
columns
– stuID
– stuName
– stuAddress
– stuYear
Selecting all columns
11
SELECT *
FROM departments;
Selecting specific columns
12
SELECT department_id, location_id
FROM departments;
Arithmetic Expressions
• Create expressions with number and date data by using
arithmetic operators.
13
Operator
+
-
*
/
Description
Add
Subtract
Multiply
Divide
Using arithmetic operators
14
SELECT last_name, salary, salary + 300
FROM employees;
…
Operators precedence
• Multiplication and division take priority over
addition and subtraction.
• Operators of the same priority are evaluated
from left to right.
• Parentheses are used to force prioritized
evaluation and to clarify statements.
15
* / +
_
Operator precedence
16
SELECT last_name, salary, 12*salary+100
FROM employees;
…
Using parenthesis
17
…
SELECT last_name, salary, 12*(salary+100)
FROM employees;
Defining a NULL value
• A null is a value that is unavailable,
unassigned, unknown, or inapplicable.
• A null is not the same as zero or a blank
space.
18
SELECT last_name, job_id, salary, commission_pct
FROM employees;
…
…
NULL values in Arithmetic Expressions
• Arithmetic expressions containing a null value
evaluate to null.
19
SELECT last_name, 12*salary*commission_pct
FROM employees;
…
…
Defining a column alias
• A column alias:
– Renames a column heading
– Is useful with calculations
– Immediately follows the column name - there can
also be the optional AS keyword between the
column name and alias
– Requires double quotation marks if it contains
spaces or special characters or is case sensitive
20
Using column aliases
21
SELECT last_name "Name", salary*12 "Annual Salary"
FROM employees;
SELECT last_name AS name, commission_pct comm
FROM employees;
…
…
Concatenation operator
• A concatenation operator:
– Concatenates columns or character strings to
other columns
– Is represented by two vertical bars (||)
– Creates a resultant column that is a character
expression
22
Using concatenation operator
23
SELECT last_name||job_id AS "Employees"
FROM employees;
…
Literal character strings
• A literal is a character, a number, or a date
included in the SELECT list.
• Date and character literal values must be
enclosed within single quotation marks.
• Each character string is output once for each
row returned.
24
Using literal character strings
25
…
SELECT last_name ||' is a '||job_id
AS "Employee Details"
FROM employees;
Duplicate rows
• The default display of queries is all rows,
including duplicate rows.
26
SELECT department_id
FROM employees;
…
Eliminating duplicate rows
• Eliminate duplicate rows by using the
DISTINCT keyword in the SELECT clause.
27
SELECT DISTINCT department_id
FROM employees;
The CASE Expression
Facilitates conditional inquiries by doing the work of
an IF-THEN-ELSE statement:
CASE expr WHEN comparison_expr1 THEN return_expr1
[WHEN comparison_expr2 THEN return_expr2
WHEN comparison_exprn THEN return_exprn
ELSE else_expr]
END
SELECT last_name, job_id, salary,
CASE job_id WHEN 'IT_PROG' THEN 1.10*salary
WHEN 'ST_CLERK' THEN 1.15*salary
WHEN 'SA_REP' THEN 1.20*salary
ELSE salary END "REVISED_SALARY"
FROM employees;
Using the CASE Expression
Facilitates conditional inquiries by doing the work of
an IF-THEN-ELSE statement:
…
…

Weitere ähnliche Inhalte

Was ist angesagt?

DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptgourav kottawar
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)Sabana Maharjan
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
 
Adbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queriesAdbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queriesVaibhav Khanna
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL CommandsShrija Madhu
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsAshwin Dinoriya
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands1keydata
 
Revision sql te it new syllabus
Revision sql te it new syllabusRevision sql te it new syllabus
Revision sql te it new syllabussaurabhshertukde
 
Relational database management system
Relational database management systemRelational database management system
Relational database management systemPraveen Soni
 

Was ist angesagt? (19)

Dbms record
Dbms recordDbms record
Dbms record
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
Lab
LabLab
Lab
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Adbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queriesAdbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queries
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
Sql commands
Sql commandsSql commands
Sql commands
 
Create table
Create tableCreate table
Create table
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Ddl commands
Ddl commandsDdl commands
Ddl commands
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Revision sql te it new syllabus
Revision sql te it new syllabusRevision sql te it new syllabus
Revision sql te it new syllabus
 
Relational database management system
Relational database management systemRelational database management system
Relational database management system
 

Ähnlich wie DML using oracle

Ähnlich wie DML using oracle (20)

0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
 
Introduction sql
Introduction sqlIntroduction sql
Introduction sql
 
sql language
sql languagesql language
sql language
 
DML Commands
DML CommandsDML Commands
DML Commands
 
SQL Query
SQL QuerySQL Query
SQL Query
 
Query
QueryQuery
Query
 
Best sql plsql_material for B.TECH
Best sql plsql_material for B.TECH Best sql plsql_material for B.TECH
Best sql plsql_material for B.TECH
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Les01
Les01Les01
Les01
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
 
UNIT2.ppt
UNIT2.pptUNIT2.ppt
UNIT2.ppt
 
01 basic orders
01   basic orders01   basic orders
01 basic orders
 
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
 

Kürzlich hochgeladen

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Kürzlich hochgeladen (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 

DML using oracle

  • 1. More SQL Data Definition INSERT, UPDATE, DELETE • INSERT - add a row to a table • UPDATE - change row(s) in a table • DELETE - remove row(s) from a table • UPDATE and DELETE use ‘WHERE clauses’ to specify which rows to change or remove • BE CAREFUL with these - an incorrect WHERE clause can destroy lots of data
  • 2. More SQL Data Definition INSERT INSERT INTO <table> (col1, col2, …) VALUES (val1, val2, …) – The number of columns and values must be the same – If you are adding a value to every column, you don’t have to list them – SQL doesn’t require that all rows are different (unless a constraint says so)
  • 3. More SQL Data Definition INSERT Student ID 1 Name John Year 1 Student ID 1 2 Name John Mary Year 1 Student ID 1 2 Name John Mary Year 1 3 Student ID 1 2 Name John Mary Year 1 3 INSERT INTO Student (ID, Name, Year) VALUES (2, ‘Mary’, 3) INSERT INTO Student (Name, ID) VALUES (‘Mary’, 2) INSERT INTO Student VALUES (2, ‘Mary’, 3)
  • 4. More SQL Data Definition UPDATE UPDATE <table> SET col1 = val1 [,col2 = val2…] [WHERE <condition>] – All rows where the condition is true have the columns set to the given values – If no condition is given all rows are changed so BE CAREFUL – Values are constants or can be computed from columns
  • 5. More SQL Data Definition UPDATE Student ID 1 2 3 4 Name John Mark Anne Mary Year 1 3 2 2 Student ID 1 2 3 4 Name John Mark Anne Mary Year 2 4 3 3 UPDATE Student SET Year = Year + 1 UPDATE Student SET Year = 1, Name = ‘Jane’ WHERE ID = 4 Student ID 1 2 3 4 Name John Mark Anne Jane Year 1 3 2 1
  • 6. More SQL Data Definition DELETE • Removes all rows which satisfy the condition DELETE FROM <table> [WHERE <condition>] – If no condition is given then ALL rows are deleted - BE CAREFUL – Some versions of SQL also have TRUNCATE TABLE <T> which is like DELETE FROM <T>
  • 7. More SQL Data Definition DELETE Student ID 1 2 3 4 Name John Mark Anne Mary Year 1 3 2 2 Student ID Name Year DELETE FROM Student or TRUNCATE TABLE Student DELETE FROM Student WHERE Year = 2 Student ID 1 2 Name John Mark Year 1 3
  • 8. More SQL Data Definition Being Careful • When using DELETE and UPDATE – You need to be careful to have the right WHERE clause – You can check it by running a SELECT statement with the same WHERE clause first Before running DELETE FROM Student WHERE Year = 3 run SELECT * FROM Student WHERE Year = 3
  • 9. More SQL Data Definition SQL SELECT Overview SELECT <column-list> FROM <table-names> [WHERE <condition>] [GROUP BY <column-list>] [HAVING <condition>] [ORDER BY <column-list>] • ([]- optional, | - or)
  • 10. More SQL Data Definition Simple SELECT SELECT <columns> FROM <table> <columns> can be – A single column – A comma-separated list of columns – * for ‘all columns’ • Given a table Student with columns – stuID – stuName – stuAddress – stuYear
  • 11. Selecting all columns 11 SELECT * FROM departments;
  • 12. Selecting specific columns 12 SELECT department_id, location_id FROM departments;
  • 13. Arithmetic Expressions • Create expressions with number and date data by using arithmetic operators. 13 Operator + - * / Description Add Subtract Multiply Divide
  • 14. Using arithmetic operators 14 SELECT last_name, salary, salary + 300 FROM employees; …
  • 15. Operators precedence • Multiplication and division take priority over addition and subtraction. • Operators of the same priority are evaluated from left to right. • Parentheses are used to force prioritized evaluation and to clarify statements. 15 * / + _
  • 16. Operator precedence 16 SELECT last_name, salary, 12*salary+100 FROM employees; …
  • 17. Using parenthesis 17 … SELECT last_name, salary, 12*(salary+100) FROM employees;
  • 18. Defining a NULL value • A null is a value that is unavailable, unassigned, unknown, or inapplicable. • A null is not the same as zero or a blank space. 18 SELECT last_name, job_id, salary, commission_pct FROM employees; … …
  • 19. NULL values in Arithmetic Expressions • Arithmetic expressions containing a null value evaluate to null. 19 SELECT last_name, 12*salary*commission_pct FROM employees; … …
  • 20. Defining a column alias • A column alias: – Renames a column heading – Is useful with calculations – Immediately follows the column name - there can also be the optional AS keyword between the column name and alias – Requires double quotation marks if it contains spaces or special characters or is case sensitive 20
  • 21. Using column aliases 21 SELECT last_name "Name", salary*12 "Annual Salary" FROM employees; SELECT last_name AS name, commission_pct comm FROM employees; … …
  • 22. Concatenation operator • A concatenation operator: – Concatenates columns or character strings to other columns – Is represented by two vertical bars (||) – Creates a resultant column that is a character expression 22
  • 23. Using concatenation operator 23 SELECT last_name||job_id AS "Employees" FROM employees; …
  • 24. Literal character strings • A literal is a character, a number, or a date included in the SELECT list. • Date and character literal values must be enclosed within single quotation marks. • Each character string is output once for each row returned. 24
  • 25. Using literal character strings 25 … SELECT last_name ||' is a '||job_id AS "Employee Details" FROM employees;
  • 26. Duplicate rows • The default display of queries is all rows, including duplicate rows. 26 SELECT department_id FROM employees; …
  • 27. Eliminating duplicate rows • Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause. 27 SELECT DISTINCT department_id FROM employees;
  • 28. The CASE Expression Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement: CASE expr WHEN comparison_expr1 THEN return_expr1 [WHEN comparison_expr2 THEN return_expr2 WHEN comparison_exprn THEN return_exprn ELSE else_expr] END
  • 29. SELECT last_name, job_id, salary, CASE job_id WHEN 'IT_PROG' THEN 1.10*salary WHEN 'ST_CLERK' THEN 1.15*salary WHEN 'SA_REP' THEN 1.20*salary ELSE salary END "REVISED_SALARY" FROM employees; Using the CASE Expression Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement: … …

Hinweis der Redaktion

  1. The CASE Expression CASE expressions let you use IF-THEN-ELSE logic in SQL statements without having to invoke procedures. In a simple CASE expression, Oracle searches for the first WHEN ... THEN pair for which expr is equal to comparison_expr and returns return_expr. If none of the WHEN ... THEN pairs meet this condition, and an ELSE clause exists, then Oracle returns else_expr. Otherwise, Oracle returns null. You cannot specify the literal NULL for all the return_exprs and the else_expr. All of the expressions ( expr, comparison_expr, and return_expr) must be of the same data type, which can be CHAR, VARCHAR2, NCHAR, or NVARCHAR2. Instructor Note There is also a searched CASE expression. Oracle searches from left to right until it finds an occurrence of a condition that is true, and then returns return_expr. If no condition is found to be true, and an ELSE clause exists, Oracle returns else_expr. Otherwise Oracle returns null. For more information, see Oracle9i SQL Reference, “Expressions.”
  2. Using the CASE Expression In the preceding SQL statement, the value of JOB_ID is decoded. If JOB_ID is IT_PROG, the salary increase is 10%; if JOB_ID is ST_CLERK, the salary increase is 15%; if JOB_ID is SA_REP, the salary increase is 20%. For all other job roles, there is no increase in salary. The same statement can be written with the DECODE function.