You are on page 1of 16

• Click to edit Master text styles

– Second level
• Third level
– Fourth level ISYS6465
» Fifth levelDatabase System

Week 4 – Session 4
SQL: Data Manipulation (1)
Learning Outcomes

• LO 2 : Apply database language and SQL


• Click to edit Master text styles
Programming Language
– Second level
• Third level
– Fourth level
» Fifth level
Outlines

• INSERT
• Click to edit Master text styles
• UPDATE
– Second level
• DELETE• Third level
– Fourth level
• SELECT »
Fifth level
PREPARATIONS

• Make sure your SQL Server


• Click to edit Master text stylesdatabase is running
• Open– Microsoft
Second level SQL Server Management Studio
• Login to• your database (localhost\sqlexpress)
Third level
– Fourth level
• Open query editor by clicking New Query
» Fifth level
(Ctrl+N)
• Click on Execute or press F5 to execute any
command
INSERT

INSERT INTO TableName [ (columnList) ]


• Click
VALUES to edit Master text
(dataValueList) styles
– Second
• columnList level if omitted, SQL assumes a list of all
is optional;
columns in their level
• Third original CREATE TABLE order.
• Any columns– Fourth
omittedlevelmust have been declared as NULL
when table was »created,
Fifth levelunless DEFAULT was specified when
creating column.
• dataValueList must match columnList as follows:
– number of items in each list must be same;
– must be direct correspondence in position of items in two lists;
– data type of each item in dataValueList must be compatible with
data type of corresponding column.
INSERT

• INSERT INTO siswa VALUES(1001,'Adi','IT01','12345');


• Click to edit Master text styles
• INSERT INTO siswa VALUES(1002,'Budi','IT01','23456');
• INSERT– INTO
Second level
siswa VALUES(1003,'Cecep','IT02','34567’);
• Third level
– Fourth level
» Fifth level
UPDATE

UPDATE TableName
• columnName1
SET Click to edit Master text
= dataValue1 styles
–[, columnName2
Second level= dataValue2...]
[WHERE searchCondition]
• Third level
• TableName can–beFourth
namelevel
of a base table or an updatable view.
» Fifth
• SET clause specifies names levelof one or more columns that are to be
updated.
• WHERE clause is optional:
– if omitted, named columns are updated for all rows in table;
– if specified, only those rows that satisfy searchCondition are updated.
• New dataValue(s) must be compatible with data type for
corresponding column.
UPDATE

• UPDATE siswa SET phone='54321' WHERE nim=1001;


• Click to edit Master text styles
• UPDATE siswa SET phone='65432' WHERE nama='Budi';
– Second
• UPDATE siswa SETlevel
nama='Mike Tyson' WHERE nim=1003;
• Third level
– Fourth level
» Fifth level
DELETE

DELETE FROM TableName


• Click to edit Master text styles
[WHERE searchCondition]
– Second level
• TableName• can be name
Third level of a base table or an updatable view.
• searchCondition– is optional;
Fourth level if omitted, all rows are deleted from table.
This does not delete table.
» Fifth If search_condition is specified, only those
level
rows that satisfy condition are deleted.
DELETE

• DELETE FROM siswa WHERE nim=1001;


• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
• DELETE FROM siswa WHERE nama='Budi';
SELECT

SELECT [DISTINCT | ALL]


•{*Click to edit Master text styles
| [columnExpression [AS newName]] [,...] }
FROM – Second level [alias] [, ...]
TableName
• Third level
[WHEREcondition]
– Fourth level
[GROUP BY columnList] [HAVING condition]
» Fifth level
[ORDER BY columnList]

• Order of the clauses cannot be changed.

• Only SELECT and FROM are mandatory.


SELECT

SELECT Specifies which columns are to


• Click to editinMaster
appear output. text styles
FROM – SecondSpecifies
level table(s) to be used.
WHERE • Third level
Filters rows.
– Fourth level
GROUP BY Forms groups of rows with same column value.
» Fifth level
HAVING Filters groups subject to some
condition.
ORDER BY Specifies the order of the output.
SELECT

• Select Between
• Click to edit Master text styles
SELECT { * | field_name [, …] } SELECT { * | field_name [, …] }
FROM table_name [, …] FROM table_name [, …]
– Second level WHERE field_name BETWEEN
• Distinct value1AND value2
• Third level
SELECT DISTINCT { * | field_name [,
– Fourth level Like
…] }
FROM table_name [,»…]Fifth level SELECT { * | field_name [, …] }
FROM table_name [, …]
WHERE field_name LIKE {PATTERN}
• Where
SELECT { * | field_name [, …] }
FROM table_name [, …]
WHERE {condition}
SELECT

• SELECT * FROM siswa;


• Click to edit Master text styles
– Second level
• Third level
– Fourth level
• SELECT * FROM siswa WHERE nim=1001;
» Fifth level

• SELECT nim, nama FROM siswa WHERE nama LIKE 'Mike%';


Exercise

• Using your previous created table, write some DML


• Click to edit Master text styles
statements
– (course_id,
Courses Second levelcourse_name, sks)
• Third
1. Input 3 data into courses
level table
– Fourth level
» Fifth level

2. Show the course name that has sks more than 2


3. Change the sks value of algorithm become 3
4. Erase the data where the course name contains ‘D’ letter
References

• Database Systems: A Practical Approach To


• Click to edit Master text styles
Design, Implementation, and Management.
– Second level
Chapter• 7Third level
• https://docs.microsoft.com/en-us/sql/t-
– Fourth level
» Fifth level
sql/statements/statements?view=sql-server-
2017

You might also like