1.0 Querying and Managing Data Using SQL Server 2005 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} [c
Trang 3Slide 1
Slide 1 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
In this session, you will learn to:
Manipulate data by using Data Manipulation Language statements
Manipulate the Extensible Markup Language data
Objectives
Begin the session by sharing the session objectives with the students
Slide 2
Slide 2 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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…
Storing Data in a Table
Trang 4In this topic, 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 For this, you can use the examples given in the Student Guide
Slide 3
Slide 3 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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…
Storing Data in a Table (Contd.)
Tell the students that while inserting rows, it is a good practice to mention the column list Emphasize on the following guidelines to insert data:
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
To demonstrate the example to insert data in the Address table, you need to create the Address table in the database For this, you can use the CreateAddressTable.sql given in the DataFiles_for_Faculty\Chapter 05\01_Instep Demo folder in the TIRM CD
Trang 5Slide 4 of 20 Session 7
Ver 1.0
Which statement allows you to insert data in a table?
Answer:
INSERT INTO
Reiterate the concepts taught earlier by asking the given question
Slide 5
Slide 5 of 20 Session 7
Ver 1.0
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
Reiterate the concepts taught earlier by asking the given question
Trang 6Slide 6
Slide 6 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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…
Updating Data in a Table
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
Trang 7Slide 7 of 20 Session 7
Ver 1.0
Which statement allows you to modify data in a database?
Answer:
UPDATE
Reiterate the concepts taught earlier by asking the given question
Slide 8
Slide 8 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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…
Deleting Data from a Table
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
Trang 8Slide 9
Slide 9 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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…
Deleting Data from a Table (Contd.)
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
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
Trang 9Slide 10 of 20 Session 7
Ver 1.0
Which statement allows you to delete a single row from a table?
Answer:
DELETE table_name
Reiterate the concepts taught earlier by asking the given question
Slide 11
Slide 11 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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.
Demo: Manipulating Data in Tables
At the end of this demo, the students will be able to modify the data present in a table
Trang 10Slide 12
Slide 12 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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.
Demo: Manipulating Data in Tables (Contd.)
Slide 13
Slide 13 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
Flash presentation: Introduction to XML
Store XML data in a table:
By using XML data type
By transforming XML data into a rowset
Storing XML Data in a Table
Trang 11Screen 1
A database server is accessed by heterogeneous clients These clients include desktop
applications, Web applications, and mobile applications The result set in the row and
column format is mot suitable with mobile applications
Screen 2
The SQL Server allows the database developers to store the data in the XML format As a
result, mobile applications are able to retrieve the data from the SQL Server
Screen 3
This screen displays a sample XML data Using this sample data, you need to explain the
different components of XML data
Slide 14
Slide 14 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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…
Storing XML Data in a Table (Contd.)
Trang 12Note
Tell the students that there are two ways to store XML data in tables:
Using OPENXML: This method is used when you need to store the XML data in
relational format In this, data value from each node in the XML data source is stored
in each data cell in the database table
To demonstrate how to use the OPENXML function to store data, you can use the example given in the Student Guide This example explains the four tasks that opens
an XML snippet, converts it into the rowset format, stores it in the database, and finally clears the memory
Using Schema Collection: This method is used when you need to store the XML
data in the XML format only In addition, you also need to validate that the XML data is verified against the given schema The plan for this session does not include how to store data using schema collection Also tell the students that for their additional knowledge the procedure to store data using schema is given in the book, which the students can read at home
Tell the students that for additional knowledge the procedure to extract data using
schema collection is also given in the book The students can read this at home You
need to clearly mention that the knowledge of the students on this topic will not be
assessed in the MT
Slide 15
Slide 15 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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
Retrieving the XML Data from a Table
While teaching the students how to store and retrieve XML data, you need to demonstrate the examples given in the chapter
Trang 13Slide 16
Slide 16 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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…
Retrieving the XML Data from a Table (Contd.)
These directives explain how the rowset data will be converted in the XML format Refer
the Student Guide for detailed explanation of the examples
The chapter also discusses how to extract data using XQuery According to the session
plan, you will not demonstrate this topic in the class However, you can just describe the
concept For your reference, you can execute the example For this, you can use the
CustomDetails.sql file provided in the Datafiles_for_faculty\Chapter 05\01_Instep Demo
folder of the TIRM CD
Trang 14Slide 17
Slide 17 of 20 Session 7
Ver 1.0
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.
Reiterate the concepts taught earlier by asking the given question
Slide 18
Slide 18 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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…
Modifying XML Data
Trang 15Slide 19 of 20 Session 7
Ver 1.0
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.
Summarize the session
Slide 20
Slide 20 of 20 Session 7
Ver 1.0
Querying and Managing Data Using SQL Server 2005
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.
Summary (Contd.)
Trang 161 What is the difference between the DELETE and TRUNCATE statements?
Ans: 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
2 What is the difference between the INSERT INTO and SELECT INTO
statements?
Ans: The INSERT INTO statement is used to insert a single row in a table whereas SELECT INTO is used to retrieve the data from one table and insert it into another
3 What is the need to implement XML inside database?
Ans: There are heterogeneous clients accessing the database who might not be able to interpret the rowset based data, therefore you XML needs to be implemented in the database
4 What is the difference between OPENXML and SCHEMA COLLECTION
methods of storing XML data?
Ans: OPENXML stores the XML data in the rowset format in the database Whereas, SCHEMA COLLECTION is used to store the XML data in the XML format
5 How was the XML data treated in the previous versions of SQL Server?
Ans: In the previous versions of SQL Server, the XML data was stored in textual format Where as, in SQL Server 2005, XML data can be stored in the XML format itself
FAQs