Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

06 qmds2005 session08

566 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

06 qmds2005 session08

  1. 1. Querying and Managing Data Using SQL Server 2005Objectives 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. 2. Querying and Managing Data Using SQL Server 2005Storing 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. 3. Querying and Managing Data Using SQL Server 2005Storing 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. 4. Querying and Managing Data Using SQL Server 2005Just 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. 5. Querying and Managing Data Using SQL Server 2005Just 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. 6. Querying and Managing Data Using SQL Server 2005Updating 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. 7. Querying and Managing Data Using SQL Server 2005Just a minute Which statement allows you to modify data in a database? Answer: UPDATE Ver. 1.0 Session 8 Slide 7 of 20
  8. 8. Querying and Managing Data Using SQL Server 2005Deleting 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. 9. Querying and Managing Data Using SQL Server 2005Deleting 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. 10. Querying and Managing Data Using SQL Server 2005Just 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. 11. Querying and Managing Data Using SQL Server 2005Demo: 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. 12. Querying and Managing Data Using SQL Server 2005Demo: 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. 13. Querying and Managing Data Using SQL Server 2005Storing 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. 14. Querying and Managing Data Using SQL Server 2005Storing 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. 15. Querying and Managing Data Using SQL Server 2005Retrieving 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. 16. Querying and Managing Data Using SQL Server 2005Retrieving 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. 17. Querying and Managing Data Using SQL Server 2005Just 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. 18. Querying and Managing Data Using SQL Server 2005Modifying 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. 19. Querying and Managing Data Using SQL Server 2005Summary 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. 20. Querying and Managing Data Using SQL Server 2005Summary (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

×