Manage Data in TablesObjectivesIn this lesson, you will learn to: Create rules Create defaults Maintain data in a table by...
Manage Data in Tables6.D.1 Creating a Rule The zip code in the Newspaper table should be of the  character type and should...
Manage Data in TablesTask List Identify how to implement the constraint without changing  the table structure Draft the st...
Manage Data in TablesIdentify how to implement the constraint withoutchanging the table structure A rule provides a mechan...
Manage Data in TablesDraft the statement to create a rule The CREATE RULE Statement: Is used to create a rule    Syntax   ...
Manage Data in TablesDraft the statement to create a rule (Contd.)     The rule can be created as follows:        CREATE R...
Manage Data in TablesCreate the rule Action:     In the Query Analyzer window, type the query     Press F5 to execute the ...
Manage Data in TablesBind the rule to the column Binding Rules     A rule can be bound using the sp_bindrule system      s...
Manage Data in TablesBind the rule to the column (Contd.) Action:     In the Query Analyzer, type:        sp_bindrule rulZ...
Manage Data in TablesVerify the constraint by inserting data in the table                                Action        Tes...
Manage Data in TablesJust a Minute… Which system stored procedure is used to bind and unbind a  rule?©NIIT                ...
Manage Data in Tables6.D.2 Creating a Default The data entry operator complains that for most rows, he  has to repeatedly ...
Manage Data in TablesTask List Identify how the data entry task can be simplified Draft the statement to create a default ...
Manage Data in TablesIdentify how the data entry task can be simplified Default     It is a constant value assigned to a c...
Manage Data in TablesDraft the statement to create a default The CREATE DEFAULT Statement     Syntax        CREATE DEFAULT...
Manage Data in TablesDraft the statement to create a default (Contd.)     The code for creating the default can be written...
Manage Data in TablesCreate the default Action:     In the Query Analyzer window, type the query     Press F5 to execute t...
Manage Data in TablesBind the default to a column Binding Defaults - After a DEFAULT is created, it needs to  be bound to ...
Manage Data in TablesBind the default to a column (Contd.) Action:     In the Query Analyzer, type:        sp_bindefault  ...
Manage Data in TablesVerify the default by adding a row with the DEFAULTvalue Action:     In the Query Analyzer, type:    ...
Manage Data in TablesMaintaining Databases Data Manipulation Language - Data manipulation involves  inserting, modifying, ...
Manage Data in Tables6.D.3 Storing Details in a Table Recruitment of candidates is done through recruitment  agencies. One...
Manage Data in TablesTask List Decide in which table the information is to be added Identify the values to be inserted Ins...
Manage Data in TablesDecide in which table the information is to be added Result:     The information is to be added in th...
Manage Data in TablesIdentify the values to be inserted Result:     In the RecruitmentAgencies table, the values to be ins...
Manage Data in TablesInsert rows into the tableThe INSERT Statement     You must add data to the database to maintain the ...
Manage Data in TablesInsert rows into the table (Contd.) Action:     In the Query Analyzer Window, type:        INSERT INT...
Manage Data in TablesQuery the table to verify that data has beeninserted Action:     In the Query Analyzer window, type: ...
Manage Data in Tables6.D.4 Storing Data From an Existing Table Into a newTable Data for the external candidates with a rat...
Manage Data in TablesTask List Identify rows to be inserted Create the new table with the selected values Add more rows to...
Manage Data in TablesIdentify rows to be inserted Result:     The rows to be inserted are of those candidates with a      ...
Manage Data in TablesCreate the new table with the selected values The SELECT INTO Statement is used to copy contents of o...
Manage Data in TablesAdd more rows to the table that has been created The INSERT INTO Statement is used to add data from o...
Manage Data in TablesQuery the table to check if the rows have been added Action:     In the Query Analyzer window, type: ...
Manage Data in TablesJust a Minute… Which statements allow you to copy contents of one table  into another table?©NIIT    ...
Manage Data in Tables6.D.5 Updating a Table The test score of Jane Schaffer who is an external candidate  (cCandidateCode ...
Manage Data in TablesTask List Identify attributes that have to be updated Identify values to be updated Update rows Query...
Manage Data in TablesIdentify attributes that have to be updated Result:     In the ExternalCandidate table, the siTestSco...
Manage Data in TablesIdentify values to be updated Result:     The test score of Jane Schaffer who is an external      can...
Manage Data in TablesUpdate rows The UPDATE Statement is used to modify data in a database    Syntax     UPDATE table_name...
Manage Data in TablesQuery Tables Action:     In the Query Analyzer window, type:        SELECT * from ExternalCandidate  ...
Manage Data in Tables6.D.6 Deleting Data The ExternalCandidate table contains data on all external  candidates who were re...
Manage Data in TablesTask List Identify rows that need to be deleted Delete the row(s) Query the table ©NIIT              ...
Manage Data in TablesIdentify rows that need to be deleted Result:     The rows containing data about the candidates who t...
Manage Data in TablesDelete the row(s) The DELETE Statement is used to delete a row from a table    Syntax     DELETE [FRO...
Manage Data in TablesQuery the table Action:     In the Query Analyzer window, type:        SELECT * FROM ExternalCandidat...
Manage Data in TablesJust a Minute… Which statement allows the modification of data in a  database?©NIIT                  ...
Manage Data in TablesTruncating a Table The TRUNCATE TABLE statement     Is used to remove rows from a table     Is identi...
Manage Data in TablesSummaryIn this lesson, you learned that: Rules and defaults are objects that are bound to columns or ...
Manage Data in TablesSummary (Contd.) A default is unbound using the sp_unbindefault procedure Data is modified in the dat...
Upcoming SlideShare
Loading in …5
×

Sql xp 06

383
-1

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
383
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql xp 06

  1. 1. Manage Data in TablesObjectivesIn this lesson, you will learn to: Create rules Create defaults Maintain data in a table by using INSERT statement UPDATE statement DELETE statement Truncate a table©NIIT SQL/Lesson 6/Slide 1 of 50
  2. 2. Manage Data in Tables6.D.1 Creating a Rule The zip code in the Newspaper table should be of the character type and should have the following pattern: [0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9] An example of such a pattern is: 42482-4353 Without changing the table structure, how can you ensure that you can meet this requirement?©NIIT SQL/Lesson 6/Slide 2 of 50
  3. 3. Manage Data in TablesTask List Identify how to implement the constraint without changing the table structure Draft the statement to create a rule Create the rule Bind the rule to the column Verify the constraint by inserting data in the table©NIIT SQL/Lesson 6/Slide 3 of 50
  4. 4. Manage Data in TablesIdentify how to implement the constraint withoutchanging the table structure A rule provides a mechanism for enforcing domain integrity for columns or user-defined datatypes. The rule is applied to the column or the user-defined datatype before an INSERT or UPDATE statement is issued. Result: The constraint can be implemented by using rules.©NIIT SQL/Lesson 6/Slide 4 of 50
  5. 5. Manage Data in TablesDraft the statement to create a rule The CREATE RULE Statement: Is used to create a rule Syntax CREATE RULE rule_name AS conditional_expression Action: The rule would be applied on the cNewspaperCode attribute The condition to be applied is: @ZipCode LIKE [0-9][0-9][0-9][0-9][0-9]- [0-9][0-9][0-9][0-9] The name of the rule would be rulZipCode©NIIT SQL/Lesson 6/Slide 5 of 50
  6. 6. Manage Data in TablesDraft the statement to create a rule (Contd.) The rule can be created as follows: CREATE RULE rulZipCode AS @ZipCode LIKE [0-9][0-9][0-9][0-9][0-9]- [0-9][0-9][0-9][0-9]©NIIT SQL/Lesson 6/Slide 6 of 50
  7. 7. Manage Data in TablesCreate the rule Action: In the Query Analyzer window, type the query Press F5 to execute the query©NIIT SQL/Lesson 6/Slide 7 of 50
  8. 8. Manage Data in TablesBind the rule to the column Binding Rules A rule can be bound using the sp_bindrule system store procedure Syntax sp_bindrule rule_name, object_name [,FUTUREONLY] Unbinding Rules A rule can be unbound from a column or user-defined datatype using the sp_unbindrule system stored procedure Syntax sp_unbindrule object_name [, FUTUREONLY]©NIIT SQL/Lesson 6/Slide 8 of 50
  9. 9. Manage Data in TablesBind the rule to the column (Contd.) Action: In the Query Analyzer, type: sp_bindrule rulZipCode,Newspaper.cZip Press F5 to execute the code©NIIT SQL/Lesson 6/Slide 9 of 50
  10. 10. Manage Data in TablesVerify the constraint by inserting data in the table Action Test case cZip to be Result inserted 1 3452345 The row would not be inserted, as the zip code is not in the valid format 2 34563-5678 The row would be inserted, as the zip code is in the valid format©NIIT SQL/Lesson 6/Slide 10 of 50
  11. 11. Manage Data in TablesJust a Minute… Which system stored procedure is used to bind and unbind a rule?©NIIT SQL/Lesson 6/Slide 11 of 50
  12. 12. Manage Data in Tables6.D.2 Creating a Default The data entry operator complains that for most rows, he has to repeatedly enter the code 001 for the cCountryCode attribute of the Newspaper table. You need to simplify the data entry task without modifying the table structure.©NIIT SQL/Lesson 6/Slide 12 of 50
  13. 13. Manage Data in TablesTask List Identify how the data entry task can be simplified Draft the statement to create a default Create the default Bind the default to the column Verify the default by adding a row with the DEFAULT value©NIIT SQL/Lesson 6/Slide 13 of 50
  14. 14. Manage Data in TablesIdentify how the data entry task can be simplified Default It is a constant value assigned to a column, into which the user need not insert values Result: The data entry task can be simplified by using defaults©NIIT SQL/Lesson 6/Slide 14 of 50
  15. 15. Manage Data in TablesDraft the statement to create a default The CREATE DEFAULT Statement Syntax CREATE DEFAULT default_name AS constant_expression Action: The default would be applied on the Newspaper table The column on which the default would be applied is cCountryCode The default value is 001’©NIIT SQL/Lesson 6/Slide 15 of 50
  16. 16. Manage Data in TablesDraft the statement to create a default (Contd.) The code for creating the default can be written as CREATE DEFAULT defCountry AS 001©NIIT SQL/Lesson 6/Slide 16 of 50
  17. 17. Manage Data in TablesCreate the default Action: In the Query Analyzer window, type the query Press F5 to execute the code©NIIT SQL/Lesson 6/Slide 17 of 50
  18. 18. Manage Data in TablesBind the default to a column Binding Defaults - After a DEFAULT is created, it needs to be bound to a column or a user-defined datatype Syntax sp_bindefault default_name, object_name [, FUTUREONLY] Unbinding Defaults - Defaults can be unbound from a column or user-defined datatype using the sp_unbindefault system stored procedure Syntax sp_unbindefault object_name [, FUTUREONLY]©NIIT SQL/Lesson 6/Slide 18 of 50
  19. 19. Manage Data in TablesBind the default to a column (Contd.) Action: In the Query Analyzer, type: sp_bindefault defCountry,Newspaper.cCountryCode Press F5 to execute the code©NIIT SQL/Lesson 6/Slide 19 of 50
  20. 20. Manage Data in TablesVerify the default by adding a row with the DEFAULTvalue Action: In the Query Analyzer, type: INSERT INTO Newspaper VALUES(0008,Kansas Today,Kansas,Genral,Robin Paul,1925 Shawnee Dr ,Kansas City,Kansas,66106-3025,DEFAULT,(913) 362-9529,(913)362-9515) Press F5 to execute©NIIT SQL/Lesson 6/Slide 20 of 50
  21. 21. Manage Data in TablesMaintaining Databases Data Manipulation Language - Data manipulation involves inserting, modifying, and deleting data. The Data Manipulation Language (DML) of Transact–SQL is used to manipulate data The three operations that you will typically perform to maintain a database are: Insert Rows Update Rows Delete Rows©NIIT SQL/Lesson 6/Slide 21 of 50
  22. 22. Manage Data in Tables6.D.3 Storing Details in a Table Recruitment of candidates is done through recruitment agencies. One of the new recruitment agencies is called ‘Head Hunters’. The details of the Head Hunters are: Attributes Data Agency Code 0010 Name Head Hunters Address 223 Hill Street City Cleveland State Ohio Zip 44167-5943 Phone (440)345-8872 Fax (440)345-8943 Charge 7 Total Paid 1000 The above details are required to be stored in the RecruitmentAgencies table©NIIT SQL/Lesson 6/Slide 22 of 50
  23. 23. Manage Data in TablesTask List Decide in which table the information is to be added Identify the values to be inserted Insert rows into the table Query the table to verify that data has been inserted©NIIT SQL/Lesson 6/Slide 23 of 50
  24. 24. Manage Data in TablesDecide in which table the information is to be added Result: The information is to be added in the RecruitmentAgencies table©NIIT SQL/Lesson 6/Slide 24 of 50
  25. 25. Manage Data in TablesIdentify the values to be inserted Result: In the RecruitmentAgencies table, the values to be inserted are: cAgencyCode = 0010 cName = ‘Head Hunters , vAddress = ‘223 Hill Street , cCity = Cleveland, cState = ‘Ohio’ cZip = 44167-5943, cPhone = (440)345-8872, cFax = (440)345-8943, siPercentageCharge = 7, mTotalPaid = 1000 ©NIIT SQL/Lesson 6/Slide 25 of 50
  26. 26. Manage Data in TablesInsert rows into the tableThe INSERT Statement You must add data to the database to maintain the latest information about the organization and the transactions performed by it. This is done using the INSERT statement Syntax INSERT [INTO]{table_name}[(column_list)] VALUES {DEFAULT values_list|select_statement}©NIIT SQL/Lesson 6/Slide 26 of 50
  27. 27. Manage Data in TablesInsert rows into the table (Contd.) Action: In the Query Analyzer Window, type: INSERT INTO RecruitmentAgencies VALUES(0010,Head Hunters,223 Hill Street,Cleveland, Ohio,44167- 5943,(440)345-8872,(440)345- 8943,7,1000) Press F5 to execute the query©NIIT SQL/Lesson 6/Slide 27 of 50
  28. 28. Manage Data in TablesQuery the table to verify that data has beeninserted Action: In the Query Analyzer window, type: SELECT * FROM RecruitmentAgencies Press F5 to execute the query©NIIT SQL/Lesson 6/Slide 28 of 50
  29. 29. Manage Data in Tables6.D.4 Storing Data From an Existing Table Into a newTable Data for the external candidates with a rating of eight or above from the ExternalCandidate table is to be copied into a new table called PreferredCandidate. (The PreferredCandidate table does not exist). After the PreferredCandidate table has been created with the required values, it needs to be updated by adding rows of external candidates with a rating of seven.©NIIT SQL/Lesson 6/Slide 29 of 50
  30. 30. Manage Data in TablesTask List Identify rows to be inserted Create the new table with the selected values Add more rows to the table that has been created Query the table to check if the rows have been added©NIIT SQL/Lesson 6/Slide 30 of 50
  31. 31. Manage Data in TablesIdentify rows to be inserted Result: The rows to be inserted are of those candidates with a rating of eight or above from the ExternalCandidate table©NIIT SQL/Lesson 6/Slide 31 of 50
  32. 32. Manage Data in TablesCreate the new table with the selected values The SELECT INTO Statement is used to copy contents of one table into another table Syntax SELECT columns_list INTO new_table_name FROM table_names WHERE conditions Action: In the Query Analyzer window, type: SELECT * INTO PreferredCandidate FROM ExternalCandidate WHERE cRating = 8 Press F5 to execute the query ©NIIT SQL/Lesson 6/Slide 32 of 50
  33. 33. Manage Data in TablesAdd more rows to the table that has been created The INSERT INTO Statement is used to add data from one table to another Syntax INSERT [INTO] table_name1 SELECT column_name(s) FROM table_name2 [WHERE condition] Action: In the Query Analyzer window, type: INSERT INTO PreferredCandidate SELECT * FROM ExternalCandidate WHERE cRating = 7 Press F5 to execute the query©NIIT SQL/Lesson 6/Slide 33 of 50
  34. 34. Manage Data in TablesQuery the table to check if the rows have been added Action: In the Query Analyzer window, type: SELECT * FROM PreferredCandidate Press F5 to execute the query©NIIT SQL/Lesson 6/Slide 34 of 50
  35. 35. Manage Data in TablesJust a Minute… Which statements allow you to copy contents of one table into another table?©NIIT SQL/Lesson 6/Slide 35 of 50
  36. 36. Manage Data in Tables6.D.5 Updating a Table The test score of Jane Schaffer who is an external candidate (cCandidateCode 000049) is to be increased by 2 marks, due to an error detected in the test correction process.©NIIT SQL/Lesson 6/Slide 36 of 50
  37. 37. Manage Data in TablesTask List Identify attributes that have to be updated Identify values to be updated Update rows Query tables©NIIT SQL/Lesson 6/Slide 37 of 50
  38. 38. Manage Data in TablesIdentify attributes that have to be updated Result: In the ExternalCandidate table, the siTestScore attribute has to be updated©NIIT SQL/Lesson 6/Slide 38 of 50
  39. 39. Manage Data in TablesIdentify values to be updated Result: The test score of Jane Schaffer who is an external candidate is to be increased by 2 marks. The cCandidateCode of Jane is 000049©NIIT SQL/Lesson 6/Slide 39 of 50
  40. 40. Manage Data in TablesUpdate rows The UPDATE Statement is used to modify data in a database Syntax UPDATE table_name SET column_name = value[,column_name = value] [FROM table_name] [WHERE condition] Action: In the Query Analyzer window, type: UPDATE ExternalCandidate SET siTestScore=siTestScore+2 WHERE cCandidateCode=000049 Press F5 to execute the query©NIIT SQL/Lesson 6/Slide 40 of 50
  41. 41. Manage Data in TablesQuery Tables Action: In the Query Analyzer window, type: SELECT * from ExternalCandidate WHERE cCandidateCode = 000049 Press F5 to execute the query©NIIT SQL/Lesson 6/Slide 41 of 50
  42. 42. Manage Data in Tables6.D.6 Deleting Data The ExternalCandidate table contains data on all external candidates who were rejected after the entrance test. Some of the data in this table present now is more than two years old. It is occupying hard disk space that can be used for some other purpose. This data is not required anymore. You are required to ensure that this old data is removed from the ExternalCandidate table.©NIIT SQL/Lesson 6/Slide 42 of 50
  43. 43. Manage Data in TablesTask List Identify rows that need to be deleted Delete the row(s) Query the table ©NIIT SQL/Lesson 6/Slide 43 of 50
  44. 44. Manage Data in TablesIdentify rows that need to be deleted Result: The rows containing data about the candidates who took the entrance test more than two years ago©NIIT SQL/Lesson 6/Slide 44 of 50
  45. 45. Manage Data in TablesDelete the row(s) The DELETE Statement is used to delete a row from a table Syntax DELETE [FROM] table_name [ FROM table(s)] [WHERE condition] Action: In the Query Analyzer window, type: DELETE FROM ExternalCandidate WHERE dTestDate dateadd(yy,-2,getdate()) Press F5 to execute the query©NIIT SQL/Lesson 6/Slide 45 of 50
  46. 46. Manage Data in TablesQuery the table Action: In the Query Analyzer window, type: SELECT * FROM ExternalCandidate WHERE dTestDate dateadd(yy,-2,getdate()) Press F5 to execute©NIIT SQL/Lesson 6/Slide 46 of 50
  47. 47. Manage Data in TablesJust a Minute… Which statement allows the modification of data in a database?©NIIT SQL/Lesson 6/Slide 47 of 50
  48. 48. Manage Data in TablesTruncating a Table The TRUNCATE TABLE statement Is used to remove rows from a table Is identical (functionally) to the DELETE statement Is faster than DELETE statement Does not fire a trigger ® Syntax TRUNCATE TABLE table_name©NIIT SQL/Lesson 6/Slide 48 of 50
  49. 49. Manage Data in TablesSummaryIn this lesson, you learned that: Rules and defaults are objects that are bound to columns or user-defined datatypes for specifying the restricted values and default values respectively A rule is created using the CREATE RULE statement, and bound to the column and user-defined datatypes using the sp_bindrule procedure A rule is unbound using the sp_unbindrule procedure A default is created using the CREATE DEFAULT statement, and bound to the column and user-defined datatypes using the sp_bindefault procedure©NIIT SQL/Lesson 6/Slide 49 of 50
  50. 50. Manage Data in TablesSummary (Contd.) A default is unbound using the sp_unbindefault procedure Data is modified in the database to keep the data up-to-date The INSERT statement is used to insert rows into tables You can copy contents of one table into another table by using the SELECT INTO command SQL Server provides a row update statement called UPDATE to modify values within tables 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©NIIT SQL/Lesson 6/Slide 50 of 50

×