0% found this document useful (0 votes)
10 views

Data Manipulation Language Slides by Hassaan With Importent Notes

Data manipulation language slides by hassaan with importent notes so thaht you can prepare your exams easily

Uploaded by

hassaaniqbaal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Data Manipulation Language Slides by Hassaan With Importent Notes

Data manipulation language slides by hassaan with importent notes so thaht you can prepare your exams easily

Uploaded by

hassaaniqbaal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

DML

INSERT, UPDATE, DELETE


• INSERT
UPDATE and- addDELETE
a row touse
a table
‘WHERE clauses’ to specify which
rows to change or remove
• BE CAREFUL
UPDATE with these
- change row(s)- an
in aincorrect
table WHERE clause can
destroy lots of data
• DELETE - remove row(s) from a table
INSERT
– The number
INSERT INTO of columns and values must be the same
– If you are adding a value to every column, you don’t have to list them
<table>
– SQL doesn’t require that all rows are different (unless a constraint says
(col1,
so) col2, …)
VALUES
(val1, val2, …)
INSERT
Student

INSERT INTO Student ID Name Year


(ID, Name, Year) 1 John 1
VALUES (2, ‘Mary’, 3) 2 Mary 3

Student
Student
INSERT INTO Student ID Name Year
ID Name Year
(Name, ID) 1 John 1
1 John 1 VALUES (‘Mary’, 2) 2 Mary
Student

INSERT INTO Student ID Name Year


VALUES (2, ‘Mary’, 1 John 1
3) 2 Mary 3
UPDATE
– All rows where the condition is true have the columns set to the given
values
UPDATE <table>
– If no condition is given all rows are changed so BE CAREFUL
SET– col1 = constants
Values are val1 or can be computed from columns
[,col2 = val2…]
[WHERE
<condition>]
UPDATE
Student
ID Name Year
UPDATE Student 1 John 1
SET Year = 1, 2 Mark 3
Student Name = 3 Anne 2
‘Jane’ 4 Jane 1
ID Name Year
WHERE ID = 4
1 John 1
2 Mark 3
3 Anne 2 Student
4 Mary 2 ID Name Year
UPDATE Student 1 John 2
SET Year = Year + 1 2 Mark 4
3 Anne 3
4 Mary 3
DELETE
– If no condition
• Removes all rowsis given
which then ALL rows
satisfy the are deleted - BE CAREFUL
condition
– Some versions of SQL also have TRUNCATE TABLE <T> which is like
DELETE FROM <T>
DELETE FROM
<table>
[WHERE
<condition>]
DELETE
Student

DELETE FROM ID Name Year


Student 1 John 1
Student WHERE Year = 2 2 Mark 3
ID Name Year
1 John 1
2 Mark 3
3 Anne 2
4 Mary 2
Student
DELETE FROM Student
or ID Name Year
TRUNCATE TABLE Student
Being Careful
• When using DELETE and UPDATE
– You need to be careful to have theBefore running
right WHERE clause
– You can check it by running a SELECT statement with the same WHERE
clause first DELETE FROM Student
WHERE Year = 3

run
SELECT * FROM Student
WHERE Year = 3
SQL SELECT Overview
SELECT
<column-list>
FROM <table-names>
[WHERE <condition>]
[ORDER BY <column-list>]
[GROUP BY <column-list>]
[HAVING <condition>]
• ([]- optional, | - or)
Simple SELECT
•SELECT
Given a <columns>
table Student with columns
– stuID<table>
FROM
– stuName
– stuAddress
<columns> can be
– stuYear
– A single column
– A comma-separated list of columns
– * for ‘all columns’
Selecting all columns

SELECT *
FROM departments;
Selecting specific columns

SELECT department_id, location_id


FROM departments;
Arithmetic Expressions
• Create expressions with number and date data by using
arithmetic operators.

Operator Description

+ Add

- Subtract

* Multiply

/ Divide
Using arithmetic operators

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.
Operator precedence

SELECT last_name, salary, 12*salary+100


FROM employees;


Using parenthesis
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.last_name, job_id, salary, commission_pct
SELECT
FROM employees;


NULL values in Arithmetic Expressions

• Arithmetic expressions containing a null value


evaluate to null.
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
Using column aliases
SELECT last_name AS name, commission_pct comm
FROM employees;

SELECT last_name "Name", salary*12 "Annual Salary"


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
Using concatenation operator

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.
Using literal character strings

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.
SELECT
SELECT department_id
department_id
FROM
FROM employees;
employees;


Eliminating duplicate rows
• Eliminate duplicate rows by using the
DISTINCT keyword in the SELECT clause.
SELECT DISTINCT department_id
FROM employees;

You might also like