0% found this document useful (0 votes)
39 views27 pages

Week 8

This document recaps SQL and data manipulation language (DML) commands. It discusses using INSERT to add data to tables, UPDATE to modify data, DELETE to remove data, and SELECT to retrieve data. Operators like AND and OR can be used in WHERE clauses. Examples are provided of DML statements to insert, update, delete, and select data from tables. The objectives are to understand and write DML statements to manipulate database objects using SELECT and appropriate operators.

Uploaded by

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

Week 8

This document recaps SQL and data manipulation language (DML) commands. It discusses using INSERT to add data to tables, UPDATE to modify data, DELETE to remove data, and SELECT to retrieve data. Operators like AND and OR can be used in WHERE clauses. Examples are provided of DML statements to insert, update, delete, and select data from tables. The objectives are to understand and write DML statements to manipulate database objects using SELECT and appropriate operators.

Uploaded by

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

Data Manipulation

Language
Week 8
Recap
• In the previous lesson, we discussed:
• SQL
• The categories of SQL commands
• SQL statement to define database structures
Recap – syntax
• Create a database
• CREATE database databasename;
CREATE TABLE
CREATE TABLE TableName
( AttributeName Domain [ Constraint ] ........
AttributeName Domain [ Constraint ] [ OtherConstraints ] );
Recap -syntax
• Alter table
ALTER TABLE table_name ADD column_name column-definition;
ALTER TABLE table_name MODIFY column_name column_type;
ALTER TABLE table_name  
RENAME COLUMN old_name to new_name;  
Recap
• Drop a database
DROP DATABASE database_name;  
• Drop a table
DROP TABLE table_name;  
• TRUNCATE TABLE table_name;  
Recap Activity
• Given the values for the attributes,
what datatypes can you assign for
each of the given attributes
• Create this table, Student
• We will use this table for
illustrations in this lesson.
Objectives
• By the end of this lesson, students should be able to:
• Understand the DML commands
• Write DML statements to manipulate database objects
• Use the SELECT command
• Use appropriate operators to manipulate the database objects
Data Manipulation Language

• Data Manipulation Language (DML) is the part of SQL used to manipulate


data within objects of a relational database.
• it is used to store, modify, retrieve, delete and update data in a database
• The most basic DML commands:
• INSERT - insert data into a table
• UPDATE - updates existing data within a table
• DELETE - Delete all records from a database table
8
Data Manipulation Language
INSERT INTO TABLE_NAME     • The INSERT statement is used to insert data into a
row in a table, use one of these
(col1, col2, col3,.... col N)  
• The list of values must match the table structure
VALUES (value1, value2, value3, .... valueN);  exactly in the number of attributes and the data
INSERT INTO Student type of each attribute.
• Tablename- is the table you want to insert data
VALUES (122929, ‘F’, ”John”);
into
INSERT INTO TABLE_NAME     • Columnname- name of column as defined in the
VALUES (value1, value2, value3, .... valueN);  table
• Values- the value you want to insert into that
column
Data Manipulation Language
• Insert the details of John James with studentnumber 22226789 into the
Students table

Studentno Given_Name Last_Name

• Write all possible statements to achieve this task


Data Manipulation Language
INSERT INTO Student values
(22226789, “John”, “James”);

INSERT INTO Student (Studentno,Given_Name, Last_Name)


Values (22226789,”John”,”James”);
Data Manipulation Language
• UPDATE table_name • This command is used to update or
SET column1 = value1, column2 = v modify the value of a column in the
alue2,column = valuen; table.

• UPDATE table_name • To update all column values, exclude


SET column1 = value1, column2 = v the where clause from the statement
alue2, ... • To WHERE statement indicates the
WHERE condition; condition that needs to be met for the
update to take place
Data Manipulation Language
• We use operators to formulate the WHERE clause
• Can use comparison operators:
• = , <> equals,
• not-equals (!= also usually supported)
• <= less than, less or equal >,
• >= greater than, greater or equal
• Can refer to any attribute in FROM clause
• Can include arithmetic expressions in comparison
Data Manipulation Language
• To update John James student number to 12345678, we can use the
following statement
UPDATE Student
SET Studentno =12345678
WHERE Studentno = 22226789;
Data Manipulation Language
• Can use comparison operators: • We use operators to formulate the
• = , <> equals, WHERE clause

• not-equals (!= also usually • Can refer to any attribute in


supported) FROM clause

• <= less than, less or equal >, • Can include arithmetic


expressions in comparison
• >= greater than, greater or equal
Data Manipulation Language
• Can use AND, OR, NOT in WHERE • AND- both conditions must be
clause
satisfied for update to take place
• UPDATE table_name
SET column1 = value1, column2 = value2, • OR - one of the specified
... conditions must be true for the
WHERE condition1 AND condition2; update to take place
• UPDATE table_name
SET column1 = value1, column2 = value2,
• NOT- all conditions must be false
... for the update to take place
WHERE condition1 OR condition2;
Data Manipulation Language
• The DELETE statement is used to delete data stored in a table.

DELETE FROM table_name WHERE condition;

• Table name represents the table where data is to be deleted


• The where statement indicates the condition that needs to be met for the delete to take place
• Excluding the where command indicates that you want to delete content of every column in
the table
Data Manipulation Language
• Write a statement to delete the details of John James with student number
12345678 from the list of students
Data Manipulation Language
• Given the following tables, • write SQL statements to
• Course • Create the table and insert the details
courseId Description Duration
Pk courseId
varchar(7) CSC100S Introduction 3
Description varchar(30) SAD210S Analysis and 2
Design
Duration int
• Change the duration of SAD210S to
5
Activity
• Write all possible SQL statements
to populate the table Student (you
created as a recap activity when
class started)
SELECT
• To retrieve data from the database we can use the SELECT command
• SELECT will retrieve information from the database that matches the specified
criteria using the SELECT/FROM/WHERE framework.
SELECT
FROM
WHERE
• When you want to retrieve data based on certain criteria, the WHERE clause comes
in handy allowing you to specify the condition.
SELECT
• A query pulls information from one or more relations and creates (temporarily)
a new relation.
• A SQL query is a “select-from-where” expression
• Nested subqueries are “select-from-where” expressions embedded within
another query.
• This allows a query to:
• Create a new relation
• Feed information to another query (as a “sub-query”)
SELECT …WHERE
SELECT col1,col2,…coln • SELECT studentno, Given_name
FROM tablename; FROM Student;
SELECT col1,col2,…coln SELECT studentno, Given_name
FROM tablename FROM Student
WHERE condition; WHERE studentno = 12345678;
SELECT Statement
• To show all of the column values • What do you think will be the
for the rows that match the result of executing this query?
specified criteria, use an asterisk
( * ). SELECT *
SELECT * FROM Student;
FROM tablename;
Next Lecture & resources
• More SQL commands
• Various CLAUSES we can use with SELECT
• How to extract data from more than one relation, joins
• https://www.youtube.com/watch?v=pyG9Nj2HDxI
• https://www.youtube.com/watch?v=kx6m8ABU4ZE
Practicals
• SQL statements to manipulate the database
• Lab 9
• Attend practical sessions
References
• David M. Kroenke And David J. Auer (2013) Database Concepts, 6th
Edition.
• Java Point SQL Tutorial [online] available at
https://www.javatpoint.com/sql-tutorial

You might also like