Scaling API-first – The story of a global engineering organization
06 qmds2005 session08
1. Querying and Managing Data Using SQL Server 2005
Objectives
In this session, you will learn to:
Manipulate data by using Data Manipulation Language
statements
Manipulate the Extensible Markup Language data
Ver. 1.0 Session 8 Slide 1 of 20
2. Querying and Managing Data Using SQL Server 2005
Storing Data in a Table
Data can be stored in a table:
By adding data in the form of rows
By using the INSERT statement
Syntax:
INSERT [INTO] {table_name} [(column_list)]
VALUES {DEFAULT | values_list |
select_statement}
Let’s see how…
Ver. 1.0 Session 8 Slide 2 of 20
3. Querying and Managing Data Using SQL Server 2005
Storing Data in a Table (Contd.)
Data can be inserted in a table in the following ways:
By inserting partial data
By inserting data in related tables
By copying data from an existing table into a new table
Let’s see how…
Ver. 1.0 Session 8 Slide 3 of 20
4. Querying and Managing Data Using SQL Server 2005
Just a minute
Which statement allows you to insert data in a table?
Answer:
INSERT INTO
Ver. 1.0 Session 8 Slide 4 of 20
5. Querying and Managing Data Using SQL Server 2005
Just a minute
Which statement allows you to copy contents of one table
into another table?
Answer:
SELECT INTO
Ver. 1.0 Session 8 Slide 5 of 20
6. Querying and Managing Data Using SQL Server 2005
Updating Data in a Table
Data in a table can be updated:
When there is a change in the data
By using the UPDATE statement
Syntax:
UPDATE table_name
SET column_name = value [, column_name =
value]
[FROM table_name]
[WHERE condition]
Let’s see how…
Ver. 1.0 Session 8 Slide 6 of 20
7. Querying and Managing Data Using SQL Server 2005
Just a minute
Which statement allows you to modify data in a database?
Answer:
UPDATE
Ver. 1.0 Session 8 Slide 7 of 20
8. Querying and Managing Data Using SQL Server 2005
Deleting Data from a Table
Data in a table can be deleted :
When the data is no longer required
By using the DELETE statement
Syntax:
DELETE [FROM] table_name
FROM table(s)]
WHERE condition]
Let’s see how…
Ver. 1.0 Session 8 Slide 8 of 20
9. Querying and Managing Data Using SQL Server 2005
Deleting Data from a Table (Contd.)
While deleting data from related tables:
Ensure that first the records are deleted from the table that
contains foreign key and then from the table that contains the
primary key
Delete all records from a table:
Using the DELETE statement or the TRUNCATE TABLE
statement
Syntax:
TRUNCATE TABLE table_name
Let’s see how…
Ver. 1.0 Session 8 Slide 9 of 20
10. Querying and Managing Data Using SQL Server 2005
Just a minute
Which statement allows you to delete a single row from a
table?
Answer:
DELETE table_name
Ver. 1.0 Session 8 Slide 10 of 20
11. Querying and Managing Data Using SQL Server 2005
Demo: Manipulating Data in Tables
Problem Statement:
You are the database developer in AdventureWorks, Inc. As a
part of the regular database operations, you need to
implement the following changes in the AdventureWorks
database:
1. The management has decided to create a new department named
Inventory Control under the Inventory Management group. You
need to add the details of this new department into the
Department table. The details should adhere to the structure of the
Department table.
2. Change the department names for the following employees to the
Inventory Control department:
Vamsi N. kuppa
Susan W. Eaton
The Department ID of the Inventory Control department is 17.
Ver. 1.0 Session 8 Slide 11 of 20
12. Querying and Managing Data Using SQL Server 2005
Demo: Manipulating Data in Tables (Contd.)
Solution:
To solve the preceding problem, you need to perform the
following tasks:
1. Insert a new record in the Department table.
2. Update the employee details to change the department.
3. Verify that data is inserted and modified.
Ver. 1.0 Session 8 Slide 12 of 20
13. Querying and Managing Data Using SQL Server 2005
Storing XML Data in a Table
Flash presentation: Introduction to XML
Store XML data in a table:
By using XML data type
By transforming XML data into a rowset
Ver. 1.0 Session 8 Slide 13 of 20
14. Querying and Managing Data Using SQL Server 2005
Storing XML Data in a Table (Contd.)
Storing XML data in a rowset involves the following tasks:
1. Parsing the XML document.
2. Retrieving a rowset from the tree.
3. Storing the data from the rowset.
4. Clearing the memory.
Let’s see how…
Ver. 1.0 Session 8 Slide 14 of 20
15. Querying and Managing Data Using SQL Server 2005
Retrieving the XML Data from a Table
Involves extracting data from a table in the form of well-
formed XML fragments
Is performed by using:
FOR XML clause in the SELECT statement
XQuery language
Ver. 1.0 Session 8 Slide 15 of 20
16. Querying and Managing Data Using SQL Server 2005
Retrieving the XML Data from a Table (Contd.)
FOR XML:
Retrieves the XML data using the following directives:
• RAW: Is used to return an xml file with each row representing an
XML element
• AUTO: Is used to return query results as nested XML elements
• PATH: Is used to return specific values by specifying the column
names for which you need to return the data
• EXPLICIT: Is used to return an XML file that gets the format as
specified in the SELECT statement
Let’s see how…
Ver. 1.0 Session 8 Slide 16 of 20
17. Querying and Managing Data Using SQL Server 2005
Just a minute
Which clause is used to extract data from a table in the XML
format?
Answer:
FOR XML clause is used to extract data from a table in
the XML format.
Ver. 1.0 Session 8 Slide 17 of 20
18. Querying and Managing Data Using SQL Server 2005
Modifying XML Data
Is performed by using the modify function, which allows to
perform the following operations:
Insert: Used to add nodes to XML in an XML column or
variable
Replace: Used to update the XML data
Delete: Used to remove a node from the XML data
Let’s see how…
Ver. 1.0 Session 8 Slide 18 of 20
19. Querying and Managing Data Using SQL Server 2005
Summary
In this session, you learned that:
The INSERT statement is used to insert data into the table.
While inserting data into a table, the data type of the
information must match the data types of the columns of the
table.
It is not essential to insert data into a column that allows NULL
or has a default constraint assigned to it.
You can copy contents from one table into another table by
using the SELECT INTO command.
The SQL Server provides a row update statement called
UPDATE to modify values within tables.
Ver. 1.0 Session 8 Slide 19 of 20
20. Querying and Managing Data Using SQL Server 2005
Summary (Contd.)
You can delete a row from a table by using the DELETE
statement.
You use the TRUNCATE TABLE statement to remove all the
rows from a table.
SQL Server 2005 uses XML as a data type to save the XML
data in its original state.
The SQL Server allows you to shred the XML data by using the
OPENXML statement.
You can use the FOR XML clause of the SELECT statement to
retrieve the XML data in different ways by using the RAW,
AUTO, PATH, EXPLICIT modes.
You can modify the XML data by using the modify function
provided by the XML data type.
Using the modify function, you can insert, update, or remove
a node from the XML data.
Ver. 1.0 Session 8 Slide 20 of 20
Editor's Notes
Begin the session by sharing the session objectives with the students.
In this slide, you will teach the students how to store data in a table by using the INSERT statement. You need to explain the various ways in which data can be inserted with the example given in the Student Guide: You can demonstrate the examples, by executing the following statements: To insert a row into the Address table with all the column values, you can use the following statements: INSERT Address VALUES (104, '24, Herbon Apts', 'Arthor Lane', 56, '607009') Or INSERT Address (AddressID, AddressLine1, AddressLine2, StateProvinceID, PostalCode) VALUES (104, '24, Herbon Apts',’ Arthor Lane’,56, ‘607009’) Or INSERT Address (AddressID, AddressLine1, AddressLine2, PostalCode, StateProvinceID) VALUES (104, '24, Herbon Apts',’ Arthor Lane’, ‘607009’, 56) NOTE: Before executing the preceding statements, you need to create the Address table. For this, execute the database script named CreateAddressTable.sql present in the Chapter 5\\Instep folder of the TIRM CD. Guidelines to Inserting Data While inserting rows into a table, explain to the students that values for columns containing the char and varchar data types should be enclosed in single quotes. While inserting values into a column that has a default constraint defined on it, if you want the default value to be inserted for that column, you can use the DEFAULT keyword. While entering data, the column-list needs to be specified when the data being entered is not in the order of the columns in the table or when the data is not being entered for all the columns. The SQL Server will automatically generate values for an Identity column. It is not a good practice to insert values for the column. While inserting partial data, the columns for which data is not being inserted should support NULL or default values. You cannot insert rows into two tables with a single INSERT statement. Example:
Additional Inputs While inserting rows, it is a good practice to mention the column list.
Reiterate the concepts taught earlier by asking the given question.
Reiterate the concepts taught earlier by asking eh given question.
Updating Rows Explain to the students that whenever data is updated, the modified value should adhere to the business rules that were effective when the row was entered. In addition, inform the students that you cannot update columns from two different tables with a single UPDATE statement.
Reiterate the concepts taught earlier by asking the given question.
Explain the students that whenever Delete statement should be used only in the case when the data is highly redundant. You cannot delete rows from two different tables using a single DELETE statement.
Explain to the students that the WHERE clause cannot be used in the TRUNCATE TABLE statement as it is used to remove all the data from the table. Additional Input The TRUNCATE TABLE command deletes data from a table page wise and this operation is not logged in the transaction log. This is the reason a delete operation in a transaction can be rolled back, but a TRUNCATE TABLE command cannot be rolled back. Transactions are dealt with in detail later in the course. FAQs Question : What is the difference between the DELETE and TRUNCATE statements? Answer : The DELETE statement is used to delete specific rows from a table, whereas the TRUNCATE statement is used to delete all the rows from a table.
Reiterate the concepts taught earlier by asking the given question.
You need to ensure that at the end of this demo, the students are able to modify the data present in a table.
Input: Before showing this presentation, ensure that all the Flash presentations have been copied to the C:\\SQL Flash Presentations folder. Input for the Flash Presentation: Screen 1: Screen 2: Explain to the students that a database server is accessed by various clients. These clients include desktop applications, Web applications, and mobile applications. The relational data might not be suitable for all types of applications, as a mobile application is unable to access data directly from the SQL Server. Therefore, a mechanism is required that ensures the data can be accessed by any client using any hardware or software platform. Screen 3: Explain to the students that the SQL Server allows the database developers to store the data in the XML format, which enables the data to be accessed by any client. Now even mobile applications are able to retrieve the data from the SQL Server. Screen 4: Explain the various components of the XML document to the students.
In this topic, you need to teach the students only about XML. You need not give details of schema and XQuery. You can inform the students that they will not be marked based on XQuery and schema in their MT. Additional Input Unlike the previous versions of SQL Server, now you can store XML data in the SQL Server itself. You can use the XML data type to store the XML data in a table. You can use the XQUERY expressions to query the XML snippet stored in the table.
CG Input: While teaching the students how to store and retrieve XML data, you need to demonstrate the examples given in the chapter. For the demonstration, you can use the data files present in the TIRM CD to create the required database objects or files. Before showing the example, execute the CreateTable.SQL database script , present in the Chapter 5\\Instep folder of the TIRM CD. Use CustomerDetails.xml for the XML snippet .
Refer SG for detail explanation of the examples
Reiterate the concepts taught earlier by asking the given question.
In the chapter, an activity has been given to save data in an XML column. You do not need to demonstrate this activity in the class. You can handle the class by saying that they will not be judged on the concept in their MT.
You can summarize the session with the help of the Summary given in the SG. In addition, you can ask the students to summarize what they have learned in this session.
You can summarize the session with the help of the Summary given in the SG. In addition, you can ask the students to summarize what they have learned in this session.