0% found this document useful (0 votes)
110 views15 pages

Data Manipulation Language: Database Management Systems

The document discusses the basic data manipulation commands in SQL - INSERT, UPDATE, DELETE, and SELECT. INSERT adds rows to a table. UPDATE changes data in existing rows. DELETE removes rows from a table. SELECT queries tables and returns results. Care must be taken with UPDATE and DELETE to specify the correct rows with a WHERE clause.

Uploaded by

Anuj Singh Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views15 pages

Data Manipulation Language: Database Management Systems

The document discusses the basic data manipulation commands in SQL - INSERT, UPDATE, DELETE, and SELECT. INSERT adds rows to a table. UPDATE changes data in existing rows. DELETE removes rows from a table. SELECT queries tables and returns results. Care must be taken with UPDATE and DELETE to specify the correct rows with a WHERE clause.

Uploaded by

Anuj Singh Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Manipulation

Language
Database Management Systems
INSERT, UPDATE, DELETE
• INSERT - add a row to a • UPDATE and DELETE use
table ‘WHERE clauses’ to specify
which rows to change or
remove
• UPDATE - change row(s) in
a table • BE CAREFUL with these - an
incorrect WHERE clause can
destroy lots of data
• DELETE - remove row(s)
from a table

More SQL Data Definition


INSERT
INSERT INTO • The number of columns and
values must be the same
<table> • If you are adding a value to
(col1, col2, …) every column, you don’t
have to list them
VALUES • SQL doesn’t require that all
(val1, val2, …) rows are different (unless a
constraint says so)

More SQL Data Definition


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’, 3) 1 John 1
2 Mary 3

More SQL Data Definition


UPDATE
• All rows where the condition
is true have the columns set
UPDATE <table> to the given values
SET col1 = val1 • If no condition is given all
rows are changed so BE
[,col2 = val2…] CAREFUL
[WHERE • Values are constants or can
be computed from columns
<condition>]

More SQL Data Definition


UPDATE
Student
ID Name Year
UPDATE Student 1 John 1
SET Year = 1, 2 Mark 3
Student Name = ‘Jane’ 3 Anne 2
WHERE ID = 4 4 Jane 1
ID Name Year
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

More SQL Data Definition


DELETE
• Removes all rows which • If no condition is given then
satisfy the condition ALL rows are deleted - BE
CAREFUL
• Some versions of SQL also
DELETE FROM have TRUNCATE TABLE <T>
which is like DELETE FROM
<table> <T> but it is quicker as it
doesn’t record its actions
[WHERE
<condition>]

More SQL Data Definition


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

More SQL Data Definition


SELECT
• The SQL command you will • SQL’s SELECT is different
use most often from the relational
• Queries a set of tables and algebra’s selection 
returns results as a table • SELECT in SQL does all of the
• Lots of options, we will look relational algebra
at many of them • But it is a bit different
• Usually more than one way because SQL differs from the
to do any given query relational model

More SQL Data Definition


SQL SELECT Overview
SELECT
[DISTINCT | ALL] <column-list>
FROM <table-names>
[WHERE <condition>]
[ORDER BY <column-list>]
[GROUP BY <column-list>]
[HAVING <condition>]
• ([]- optional, | - or)

More SQL Data Definition


Simple SELECT
SELECT <columns> • Given a table Student with
FROM <table> columns
• stuID
• stuName
<columns> can be • stuAddress
• A single column • stuYear
• A comma-separated list of
columns
• * for ‘all columns’

More SQL Data Definition


Sample SELECTs
SELECT * FROM Student
stuID stuName stuAddress stuYear
1 Anderson 15 High St 1
2 Brooks 27 Queen’s Rd 3
3 Chen Lenton Hall 1
4 D’Angelo Derby Hall 1
5 Evans Lenton Hall 2
6 Franklin 13 Elm St 3
7 Gandhi Lenton Hall 1
8 Harrison Derby Hall 1

More SQL Data Definition


Sample SELECTs
SELECT stuName FROM Student
stuName
Anderson
Brooks
Chen
D’Angelo
Evans
Franklin
Gandhi
Harrison

More SQL Data Definition


Sample SELECTs
SELECT stuName, stuAddress
FROM Student
stuName stuAddress
Anderson 15 High St
Brooks 27 Queen’s Rd
Chen Lenton Hall
D’Angelo Derby Hall
Evans Lenton Hall
Franklin 13 Elm St
Gandhi Lenton Hall
Harrison Derby Hall

More SQL Data Definition


Being Careful
• When using DELETE and Before running
UPDATE
• You need to be careful to
have the right WHERE clause DELETE FROM Student
• You can check it by running a WHERE Year = 3
SELECT statement with the
same WHERE clause first
run

SELECT * FROM Student


WHERE Year = 3

More SQL Data Definition

You might also like