Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

3_Basic-DML-Statements-and-Operator

Uploaded by

Binh Quoc Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

3_Basic-DML-Statements-and-Operator

Uploaded by

Binh Quoc Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

BASIC DML STATEMENTS

Fsoft Academy

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use


Lesson Objectives

Describe each dataThis is a sample text that you can edit. You can
manipulation language (DML) statement
01 change font(size, color, name), or apply any
desired formatting.

02 Insert rows into a table

03 Update rows in a table


This is a sample text that you can edit. You can
03 change font(size, color, name), or apply any
04 Delete rows from adesired
table formatting.

05 Able to use basic SQL Operators

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 2


Agenda

1. DML Statements

2. SQL Operators

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 3


Section 1

DML Statements

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 4


DML Statement
✓ Insert Statement
✓ Update Statement
✓ Delete Statement
✓ Select Statement

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 5


INSERT Statements
▪ The INSERT INTO statement is used to adds one or more rows to a table
or a view

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 6


INSERT Statements
▪ Syntax:
(1) Inserting data to all columns
INSERT INTO table_name
VALUES (value1,value2,value3,...);
✓ Ex1: USE Fsoft_Training -- Database name

INSERT INTO dbo.Persons


VALUES ( 1,'Tom', 'B. Erichsen','Skagen 21','Stavanger')

(2) Inserting data to selected columns


INSERT INTO table_name(column1,column2,column3,...)
VALUES (value1,value2,value3,...);
✓ Ex2:
USE Fsoft_Training
INSERT INTO dbo.Customer (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 7
INSERT Statement (3/3)
▪ Demo
✓Inserting data to selected columns
✓Inserting data to all columns with identity column
✓Insert many rows at one time

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 8


UPDATE Statement
▪ The UPDATE statement is used to changes existing data in a table or view

▪ Best Practice
✓ Use the @@ROWCOUNT function to return the number of inserted rows to the client
application.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 9
UPDATE Statement
▪ Syntax:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

Notice the WHERE clause in the SQL UPDATE statement!

The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause,
all records will be updated!

▪ Code:
USE Fsoft_Training
UPDATE dbo.Customer
SET PostalCode = '4006' WHERE Country = 'Norway‘

SELECT @@ROWCOUNT AS ROW_COUNT

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 10


DELETE Statement
▪ Removes one or more rows from a table or view:
CustomerI CustomerName ContactName
d
1 Alfreds Futterkiste Maria Anders
2 Around the Horn Thomas Hardy
3 Berglunds snabbköp Christina
Berglund
4 Antonio Moreno Antonio Moreno
5 Ana Trujillo Ana Trujillo
▪ Best Practice:
To delete all the rows in a table, use TRUNCATE TABLE. TRUNCATE TABLE is faster than
DELETE and uses fewer system and transaction log resources.
TRUNCATE TABLE has restrictions, for example, the table cannot participate in replication

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 11


DELETE Statement
▪ Syntax:
DELETE FROM table_name
WHERE some_column=some_value;

▪ Notice the WHERE clause in the SQL DELETE statement!


The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE
clause, all records will be deleted!

Please note that the DELETE FROM command cannot delete any rows of data that would violate
FOREIGN KEY or other constraints.

▪ Ex: USE Fsoft_Training


DELETE dbo.Customer WHERE Country = 'Germany‘

SELECT @@ROWCOUNT AS ROW_COUNT

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 12


SELECT Statement
▪ Retrieves rows from the database and enables the selection of one or
many rows or columns from one or many tables

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 13


SELECT Statement
▪ Syntax:
SELECT [ALL/DISTINCT/TOP [ WITH TIES ] ] <Column name1>, <Column name2>,..
FROM <Table name>
[WHERE <Search condition>]
[GROUP BY grouping columns]
[HAVING search condition]
[ORDER BY sort specification]

✓ Ex1: USE AdventureWorks


GO
SELECT ProductID, Name FROM Production.Product
ORDER BY Name ASC;
(504 rows)
✓ Ex2:
SELECT DISTINCT E.Title FROM HumanResources.Employee E
ORDER BY E.Title;
(67 rows)

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 14


SELECT Statement
▪ The SELECT INTO statement selects data from one table and inserts
it into a different table.
▪ Syntax:
SELECT *
INTO new_table_name
FROM old_tablename

Tips: The SELECT INTO statement can also be used to create a new, empty table using the
schema of another. Just add a WHERE clause that causes the query to return no data:

SELECT *
INTO newtable
FROM table1 WHERE 1=0;

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 15


SELECT Statement
▪ SQL Alias syntax:
✓ For table
SELECT column_name(s)
FROM table_name AS alias_name
✓ For Column(s)
SELECT column_name AS alias_name
FROM table_name
▪ Ex:
USE AdventureWorks
GO
SELECT c.CustomerID, s.Name
FROM Sales.Customer AS c JOIN Sales.Store AS s
ON c.CustomerID = s.SalesPersonID

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 16


Section 2

SQL Operators

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 17


What is an Operator in SQL?
▪ An operator is a symbol specifying an action that is performed on one or
more expressions.
▪ Operators are used to specify conditions in an SQL statement and to serve as
conjunctions for multiple conditions in a statement.
▪ In this section, we learn some types of following most operators:

1 Arithmetic operators 4 Compound Operators (Transact-SQL)

2 Comparison operators 5 String operators (Transact-SQL)

3 Logical operators.

https://learn.microsoft.com/en-us/sql/t-sql/language-elements/operators-transact-sql?view=sql-server-ver16

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 18


SQL Arithmetic Operators
▪ Here is a list of the Arithmetic operators available in SQL

Operator Description Example


+ Addition a + b → 30
- Subtraction a - b → -10
* Multiplication a * b → 200
/ Division b/a →2
% Modulus b%a→0

( Assume variable a holds 10 and variable b holds 20)

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 19


SQL Comparison Operators
▪ Here is a list of all the Comparison operators available in SQL

Operator Description Operator Description


= equal to >= greater than or equal to
!=, <> not equal to <= less than or equal to
< less than !< not less than
> greater than !> not greater than
❑ Example
SQL: SELECT * FROM CUSTOMERS WHERE SALARY > 5000;

CUSTOMERS TABLE

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 20


SQL Logical Operators
Operator Description
ALL ✓ Used to compare a value to all values in another value set.
AND ✓ Used when both conditions are included
✓ Used to compare a value to any applicable value in the list according to
ANY
the condition
BETWEEN ✓ Used to limit the values in a range e.g.
✓ Used to search for the presence of a row in a specified table that meets
EXISTS
certain criteria
IN ✓ Included in the list e.g.
LIKE ✓ Equal to some character (use quotes)
NOT ✓ Opposite of the logical value
OR ✓ Used when either of the condition is true
IS NULL ✓ This checks if the field has a null
UNIQUE ✓ Searches every row of a specified table for uniqueness

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 21


Compound Operators (Transact-SQL)
▪ Compound operators execute some operation and set an original value to
the result of the operation.
▪ Transact-SQL provides the following compound operators:
Operator Action
+= Adds some amount to the original value and sets the original value to the result.
-= Subtracts some amount from the original value and sets the original value to the result.
*= Multiplies by an amount and sets the original value to the result.
/= Divides by an amount and sets the original value to the result.
%= Divides by an amount and sets the original value to the modulo.
&= Performs a bitwise AND and sets the original value to the result.
^= Performs a bitwise exclusive OR and sets the original value to the result.
|= Performs a bitwise OR and sets the original value to the result.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 22


String Operators (Transact-SQL)
▪ Wildcard string operators can match one or more characters in a string
comparison operation. LIKE and PATINDEX are examples of two of these
operations.
▪ SQL Server provides the following string operators.
▪ = (String comparison or assignment)
▪ + (String concatenation)
▪ += (String concatenation assignment)
▪ % (Wildcard - character(s) to match)
▪ [ ] (Wildcard - character(s) to match)
▪ [^] (Wildcard - character(s) not to match)
▪ _ (Wildcard - match one character)

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 23


String Operators (Transact-SQL)
▪ Examples:
• Returns the first and last names of people in the Person.Person table of AdventureWorks2022,
where the first name starts with Dan.

SELECT FirstName, LastName


FROM Person.Person WHERE FirstName LIKE 'Dan%';

• Find the top 5 people in the Contact table who have a first name that starts with Al and has a
third letter that is not the letter a.

SELECT TOP 5 FirstName, LastName


FROM Person.Person WHERE FirstName LIKE 'Al[^a]%';

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 24


Summary
▪ DML Statements: INSERT, UPDATE, DELETE, SELECT
▪ SQL Operators:
✓Arithmetic operators
✓Comparison operators
✓Compound operators
✓String Concatenation operator
✓Logical operators

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 25


THANK YOU!

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use

You might also like