Upload
diego-duran
View
228
Download
2
Embed Size (px)
DESCRIPTION
Module_4 Using DML Statements
Citation preview
Brian Alderman | MCT, CEO / Founder of MicroTechPointPete Harris | Microsoft Senior Content Publisher
Database Fundamentals
Meet Brian Alderman | @brianalderman
Chief Executive Office, Founder MicroTechPointIndustry-recognized consultant Noted author and conference speakerBrian’s expertise and designs range across Microsoft operating systems
More than 25 years of industry experienceBrian has been focused on helping IT Pros and Database Administrators (DBAs) better understand core Microsoft technologies for over 25 years. A frequent presenter at SharePoint Conferences around the world, he has authored or contributed to several SharePoint, SQL Server, and other technical books, and is a MCSE, MCT, and MCITP: SharePoint and SQL Server Administrator. Brian has a BS and MS in Computer Information Systems where he graduated summa cum laude from Regis University of Colorado Springs and lives in Scottsdale, AZ where he enjoys playing golf year round and traveling around the world.
LinkedIn /brianalderman
Blog http://brianalderman.wordpress.
com
Meet Pete Harris | @SQLPete
Content Development Manager in Microsoft’s Learning Experiences team
Focuses on SQL Server and Web training
With Microsoft since 1995 Part of the first team of developer training folks in the post-Microsoft University eraHas built a variety of content and spoken to customers all over the world
Course Modules
Database Fundamentals
01 | Introducing core database concepts (50 minutes) Define databases, example of relational database tables, and introduce common database
terminology
02 | Relational Concepts (50 minutes)Normalization, referential integrity, and constraints
03 | Creating databases and database objects (50 minutes)Data types, database objects, DDL statements, and creating scripts
04 | Using DML statements (50 minutes)DML statements, using the SELECT statement; using INSERT, UPDATE, and DELETE to manage data; indexes and triggers
05 | SQL Server Administration Fundamentals (50 minutes)SQL Server security; securing database and objects; performing database backups and database restores
Click to edit Master subtitle style
04 | Using DML Statements
Brian Alderman | MCT, CEO / Founder of MicroTechPointPete Harris | Microsoft Senior Content Publisher
Introducing DML statementsUsing the SELECT statementModifying data using DML statementsIndexes and triggers
Module Overview
SELECT – retrieve data INSERT – add data UPDATE – modify dataDELETE – remove dataBULK INSERT – Import a data file
Common DML statements
Using the basic SELECT statement
The SELECT statement is used to retrieve rows and columns from a table
SELECT * FROM tablename
The SELECT statement requires the name of the table and either the * (retrieves all columns) or specific column names
To limit the number of rows returned you can include the WHERE clause in the SELECT statement
Sample SELECT statement SELECT BusinessEntityID, JobTitle, Gender
FROM HumanResources.Employee WHERE BusinessEntityID <= 50
Returns the following results:BusinessEntityID Title Gender------------------ -------------- ---------1 Chief Executive Officer
M2 Vice President of Engineering
F3 Engineering Manager
M4 Senior Tool Designer
M
Multiple WHERE clauses
You can combine several WHERE clauses in one query statement to create more specific queries.
SELECT BusinessEntityID, Jobtitle, VacationHoursFROM HumanResources.EmployeeWHERE JobTitle = ‘Design Engineer’ AND gender = ‘F’ AND HireDate >= ‘2000-JAN-01’
SELECT BusinessEntityID, Jobtitle, VacationHoursFROM HumanResources.EmployeeWHERE VacationHours > 80 OR BusinessEntityID <= 50
Using the BETWEEN clause
Retrieving rows within a date range using the BETWEEN clause
SELECT BusinessEntityID, Jobtitle, VacationHoursFROM HumanResources.EmployeeWHERE VacationHours BETWEEN 75 AND 100
Sorting the result set using ORDER By
Sorting the result set by using the ORDER BY to specify what field to sort by.
SELECT BusinessEntityID, Jobtitle, VacationHoursFROM HumanResources.EmployeeWHERE VacationHours BETWEEN 75 AND 100ORDER BY VacationHours
You can sort in descending order by using the DESC clause.
SELECT BusinessEntityID, Jobtitle, VacationHoursFROM HumanResources.EmployeeWHERE VacationHours BETWEEN 75 AND 100ORDER BY VacationHours DESC
Using the NOT clause
Write a query to return data that specifies what you don’t want returned
SELECT BusinessEntityID, Jobtitle, GenderFROM HumanResources.EmployeeWHERE NOT Gender = ‘M’
UNION clause
The UNION clause allows you to combine the rows returned from multiple SELECT statements into a single result set
SELECT BusinessEntityID, Jobtitle, HireDateFROM HumanResources.EmployeeWHERE JobTitle = 'Design Engineer' UNIONSELECT BusinessEntityID, Jobtitle, HireDateFROM HumanResources.EmployeeWHERE HireDate BETWEEN '2005-01-01' AND
'2005-12-31'
EXCEPT and INTERSECT clauses
The EXCEPT clause returns distinct values from the left query that are not found on the right query
SELECT ProductID FROM Production.Product EXCEPT SELECT ProductID FROM Production.WorkOrder ;
The INTERSECT clause returns any distinct values returned by both the query on the left and right sides of intersect operand
SELECT ProductID FROM Production.ProductINTERSECT SELECT ProductID FROM Production.WorkOrder ;
JOIN clause
The JOIN clause allows you to combine related data from multiple tables into one result set
INNER JOINS uses a comparison operator to match rows from two tables based on values in a common column that exists in both tables
OUTER JOINS (left, right, or full) includes rows from one or both tables even if they don’t have matching values CROSS JOINS return all rows from the left table with all rows from the right table. WHERE conditions should always be included.
Aggregate sample
SQL Server provides aggregate functions to assist with the summarization of large volumes of data
SELECT COUNT (DISTINCT SalesOrderID) AS UniqueOrders, AVG(UnitPrice) AS Avg_UnitPrice, MIN(OrderQty)AS Min_OrderQty, MAX(LineTotal) AS Max_LineTotalFROM Sales.SalesOrderDetail;
Demo query generator
Inserting data
You can add a new row to a table using the INSERT statement
INSERT INTO Production.UnitMeasureVALUES (N'FT', N'Feet', '20080414')
You can add multiple rows to a table using the following INSERT statement
INSERT INTO Production.UnitMeasure VALUES (N'FT2', N'Square Feet ', '20080923'), (N'Y', N'Yards', '20080923'), (N'Y3', N'Cubic Yards', '20080923'
BULK INSERT can be used to import a data file into a table with a user-specified format.
Update statement
The UPDATE statement is used to modify the data that is already stored in a table
UPDATE Sales.SalesPerson SET Bonus = 6000, CommissionPct = .10, SalesQuota = NULL WHERE sales.SalesPerson.BusinessEntityID = 289
DELETE statement
The DELETE statement is used to delete rows from a table
DELETE FROM Production.UnitMeasureWHERE Production.UnitMeasure.Name =
‘Feet’
A DELETE statement without a WHERE clause will cause all rows to be deleted
DELETE FROM Sales.SalesPersonQuotaHistory;
SQL Server indexes
Indexes allow you to speed up the retrieval of data stored within a table or view
The SQL Server query optimizer evaluates each method for retrieving the data and selects the most efficient method which may be a table scan or using one or more indexes if they exist.
The most commonly used indexes includeclusterednonclusteredunique
Creating a DML trigger
Triggers are used to enforce business rules when data is modifiedThis DML trigger displays a message to the user when they try to add or change data in the customer table
CREATE TRIGGER Reminder1 ON Sales.Customer AFTER INSERT, UPDATE AS RAISERROR ('Notify Customer Relations', 16, 10); GO
Summary
The SELECT statement is use to retrieve data from one or more tables stored in a database
The SELECT command must indicate what columns and what table you want to retrieve data from when executing the query.
Optionally the SELECT statement can include the WHERE clause to define the conditions used to determine what rows will be returned
Summary
Other arguments that can be used to control what data is returned include;
BETWEENNOTUNIONEXCEPTINTERSECTJOIN (INNER, OUTER, CROSS)
Summary
DML commands that can be used to manage table and view data include
INSERTBULK INSERTUPDATEDELETE
Indexes are used to speed up the retrieval of data from tables and viewsTriggers are used to enforce business rules when data is modified
©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.