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.

05 qmds2005 session07

414 views

Published on

Published in: Technology
  • Be the first to comment

05 qmds2005 session07

  1. 1. Querying and Managing Data Using SQL Server 2005Objectives In this session, you will learn to: Manage databases Manage tables Ver. 1.0 Session 7 Slide 1 of 33
  2. 2. Querying and Managing Data Using SQL Server 2005Identifying the System Databases in SQL Server 2005 SQL Server 2005 contains the following system databases: – master: The master database that records all the server-specific configuration information. – tempdb: The temporary database that holds all temporary tables and stored procedures. – model: The model database that acts as a template or prototype for the new databases. – msdb: The database that handles the task of scheduling, exception handling, etc. – Resource: Read-only database that contains all the system objects included in the SQL Server 2005. Ver. 1.0 Session 7 Slide 2 of 33
  3. 3. Querying and Managing Data Using SQL Server 2005Just a minute What is the utility of the model database? Answer: The model database acts as a template or a prototype for the new databases. Ver. 1.0 Session 7 Slide 3 of 33
  4. 4. Querying and Managing Data Using SQL Server 2005Creating a User-Defined Database User-defined database: Is stored as a set of files. These files include: • Primary data file • Secondary data file • Transaction log file Syntax: CREATE DATABASE database_name [ ON [ PRIMARY ] [ < filespec >]] [ LOG ON { < filespec > [ ,...n ] } ] < filespec > ::= ( [ NAME = logical_file_name , ] FILENAME = os_file_name [ , SIZE = size ] [ , MAXSIZE = { max_size | UNLIMITED } ] [ , FILEGROWTH = growth_increment ] ) [,...n] Let’s see how… Ver. 1.0 Session 7 Slide 4 of 33
  5. 5. Querying and Managing Data Using SQL Server 2005Just a minute Which statement is used to create a database? Answer: The CREATE DATABASE statement Ver. 1.0 Session 7 Slide 5 of 33
  6. 6. Querying and Managing Data Using SQL Server 2005Creating a Table Table: Is created to store data Is created by using the CREATE TABLE statement Syntax: CREATE TABLE [ database_name . [ schema_name ] .] table_name ( { <column_definition> | <computed_column_definition> } [ <table_constraint> ] [ ,...n ] ) [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] [ { TEXTIMAGE_ON { filegroup | "default" } ] [ ; ] Ver. 1.0 Session 7 Slide 6 of 33
  7. 7. Querying and Managing Data Using SQL Server 2005Creating a Table (Contd.) Example: Create an EmployeeLeave table in the HumanResources schema with the following details: Columns Data Type Checks EmployeeID int NOT NULL LeaveStartDate date NOT NULL LeaveEndDate date NOT NULL LeaveReason varchar(100) NOT NULL LeaveType char(2) NOT NULL Let’s see how… Ver. 1.0 Session 7 Slide 7 of 33
  8. 8. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity Avoids data redundancy Ensures that the data in a database is accurate, consistent, and reliable Is broadly classified into the following categories: Entity integrity: Ensures that each row can be uniquely identified by an attribute called the primary key Domain integrity: Ensures that only a valid range of values is allowed to be stored in a column Referential integrity: Ensures that the values of the foreign key match the value of the corresponding primary key User-defined integrity: Refers to a set of rules specified by a user, which do not belong to the entity, domain, and referential integrity categories Ver. 1.0 Session 7 Slide 8 of 33
  9. 9. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) You can maintain data integrity by: Applying constraints Applying rules Using user-defined types Ver. 1.0 Session 7 Slide 9 of 33
  10. 10. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Constraints can be applied: By using CREATE TABLE statement By using ALTER TABLE statement Syntax: CREATE TABLE table_name ( column_name CONSTRAINT constraint_name constraint_type [,CONSTRAINT constraint_name constraint_type] ) Ver. 1.0 Session 7 Slide 10 of 33
  11. 11. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Constraints are of the following types: Primary key constraint Unique constraint Foreign key constraint Check constraint Default constraint Ver. 1.0 Session 7 Slide 11 of 33
  12. 12. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Primary key constraint: Is defined on a column or a set of columns whose values uniquely identify all the rows in a table Does not allow NULL values in the column Ensures entity integrity Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name PRIMARY KEY [CLUSTERED|NONCLUSTERED] col_name [, col_name [, col_name [, …]]] ) Ver. 1.0 Session 7 Slide 12 of 33
  13. 13. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Unique constraint: Is defined to enforce uniqueness in non-primary key columns Allows one NULL value in the column Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name UNIQUE [CLUSTERED | NONCLUSTERED] (col_name [, col_name [, col_name [, …]]]) col_name [, col_name [, col_name [, …]]] ) Ver. 1.0 Session 7 Slide 13 of 33
  14. 14. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Foreign key constraint: Is defined to remove the inconsistency in two tables when the data in one table depends on the data in another table Associates one or more columns (the foreign key) of a table with an identical set of columns (a primary key column) in another table Ver. 1.0 Session 7 Slide 14 of 33
  15. 15. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name FOREIGN KEY (col_name [, col_name [, …]]) REFERENCES table_name (column_name [, column_name [, …]])] (col_name [, col_name [, col_name [, …]]]) col_name [, col_name [, col_name [, …]]] ) Let’s see how… Ver. 1.0 Session 7 Slide 15 of 33
  16. 16. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Check constraint: Is defined to enforce domain integrity by restricting the values to be inserted in a column Can be defined on multiple columns Is specified by using the following keywords: IN LIKE BETWEEN Ver. 1.0 Session 7 Slide 16 of 33
  17. 17. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name] CHECK (expression) (col_name [, col_name [, …]]) . ) Let’s see how… Ver. 1.0 Session 7 Slide 17 of 33
  18. 18. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Default constraint: – Is defined to assign a constant value to a column – Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name] DEFAULT (constant_expression | NULL) (col_name [, col_name [, …]]) . ) Let’s see how… Ver. 1.0 Session 7 Slide 18 of 33
  19. 19. Querying and Managing Data Using SQL Server 2005Just a minute Which keyword is used to specify a check constraint? Answer: A check constraint can be specified by using the LIKE, IN, and BETWEEN keywords. Ver. 1.0 Session 7 Slide 19 of 33
  20. 20. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) Rules: Help in enforcing domain integrity for columns or user-defined data types Can be specified to the column or the user-defined data type before an INSERT or UPDATE statement is issued Are used to implement business-related restrictions or limitations Can be created by using the CREATE RULE statement Syntax: CREATE RULE rule_name AS conditional_expression Can be bound by using the sp_bindrule stored procedure Let’s see how… Ver. 1.0 Session 7 Slide 20 of 33
  21. 21. Querying and Managing Data Using SQL Server 2005Implementing Data Integrity (Contd.) User-defined data types: Are custom data types defined by the users with a custom name Can be created by using the CREATE TYPE statement Syntax: CREATE TYPE [ schema_name. ] type_name { FROM base_type [ ( precision [ , scale ] ) ] [ NULL | NOT NULL ] } [ ; ] Let’s see how… Ver. 1.0 Session 7 Slide 21 of 33
  22. 22. Querying and Managing Data Using SQL Server 2005Just a minute You want to create a rule, rule1, which allows the user to enter any of the four values: Tea, Coffee, Soup, or Miranda in a column. Which command should you execute? Answer: CREATE RULE rule1 AS @TypeRule IN (Tea, Coffee, Soup, Miranda) Ver. 1.0 Session 7 Slide 22 of 33
  23. 23. Querying and Managing Data Using SQL Server 2005Creating a Partitioned Table Flash presentation: Partitioning Tables Partitioned table: Is created when the table contains voluminous data Is created to separate data into multiple physical locations based on a range of values for a specific column Helps in improving query performance Can be created by: Creating a partition function Creating a partition scheme Creating a table by using the partition scheme Ver. 1.0 Session 7 Slide 23 of 33
  24. 24. Querying and Managing Data Using SQL Server 2005Modifying a Table Modify a table: When there is a requirement to add or remove columns and constraints Using the ALTER TABLE statement Syntax: ALTER TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name { ALTER COLUMN column_name { [ NULL | NOT NULL ] } | [ WITH { CHECK | NOCHECK } ] ADD COLUMN <column_definition> { ADD CONSTRAINT constraint_name constraint_type Let’s see how… Ver. 1.0 Session 7 Slide 24 of 33
  25. 25. Querying and Managing Data Using SQL Server 2005Just a minute You are managing a large table. You want to improve the performance of the table and want the table to be more manageable. Which strategy can you use? Answer: Partition the table Ver. 1.0 Session 7 Slide 25 of 33
  26. 26. Querying and Managing Data Using SQL Server 2005Dropping a Table Drop a table: When a table is not required Using the DROP TABLE statement Syntax: DROP TABLE [ database_name . [ schema_name ] . ] table_name Let’s see how… Ver. 1.0 Session 7 Slide 26 of 33
  27. 27. Querying and Managing Data Using SQL Server 2005Demo: Managing Tables Problem Statement: The management of AdventureWorks, Inc. has decided to provide travel and medical reimbursements to the employees. They want to store the details of these reimbursements in the database. For this, you need to create a database table, EmployeeReimbursements. The details of the tables are shown in the following table. Columns Data Type and Size Constraints RimID int Primary key EmployeeID int Foreign Key references the EmployeeID of the Employee Table, NOT NULL Amount money Amount>0 RimType varchar(20) RimType should be Medical, Cash, or Local Pending_with varchar(30) NOT NULL How will you create the table? Ver. 1.0 Session 7 Slide 27 of 33
  28. 28. Querying and Managing Data Using SQL Server 2005Demo: Managing Tables (Contd.) Solution: To solve the preceding problem, you need to perform the following tasks: 1. Write the query to create a table. 2. Execute the statement to verify the result. Ver. 1.0 Session 7 Slide 28 of 33
  29. 29. Querying and Managing Data Using SQL Server 2005Summary In this session, you learned that: A database is a repository of information that contains data in an organized way. The master database records all the server-specific configuration information, including authorized users, databases, system configuration settings, and remote servers. The tempdb database is a temporary database that holds all temporary tables and stored procedures. The model database acts as a template or a prototype for the new databases. The msdb database supports the SQL Server Agent. The SQL Server Agent includes features that schedule periodic activities of the SQL Server. The Resource database is a read-only database that contains all the system objects that are included with SQL Server 2005. Ver. 1.0 Session 7 Slide 29 of 33
  30. 30. Querying and Managing Data Using SQL Server 2005Summary (Contd.) The user-defined databases are created by the users to store data for client-server applications. A database consists of the following types of files: • Primary data file • Secondary data file • Transaction log file – A database must consist of a primary data file and one transaction log file. – The CREATE DATABASE statement is used to create a database, which also includes determining the name of the database, the size of the database, and the files used to store data in the database. – Tables are used to store data. Ver. 1.0 Session 7 Slide 30 of 33
  31. 31. Querying and Managing Data Using SQL Server 2005Summary (Contd.) Tables are used to store data. The CREATE TABLE statement is used to create a table. Data integrity is enforced to keep the data in a database accurate, consistent, and reliable. It is broadly classified into the following categories: • Entity integrity: Ensures that each row can be uniquely identified by an attribute called the primary key. • Domain integrity: Ensures that only a valid range of values is allowed to be stored in a column. • Referential integrity: Ensures that the values of the foreign key match the value of the corresponding primary key. • User-defined integrity: Refers to a set of rules specified by a user, which do not belong to the entity, domain, and referential integrity categories. Ver. 1.0 Session 7 Slide 31 of 33
  32. 32. Querying and Managing Data Using SQL Server 2005Summary (Contd.) Constraints define rules that must be followed to maintain consistency and correctness of data. A primary key constraint is defined on a column or a set of columns whose values uniquely identify rows in a table. The unique constraint is used to enforce uniqueness on non- primary key columns. A foreign key constraint associates one or more columns of a table (the foreign key) with an identical set of columns on which a primary key constraint has been defined (a primary key column in another table). A check constraint enforces domain integrity by restricting the values to be inserted in a column. The IN, LIKE, and BETWEEN keywords are used to define the check constraint. Ver. 1.0 Session 7 Slide 32 of 33
  33. 33. Querying and Managing Data Using SQL Server 2005Summary (Contd.) A default constraint can be used to assign a constant value to a column, and the user need not insert values for such a column. A rule provides a mechanism for enforcing domain integrity for columns or user-defined data types. User-defined data types are custom data types defined by the users with a custom name. A partitioned table is created to manage the data and improve the query performance. The ALTER TABLE statement is used to modify a table. The DROP TABLE statement is used to delete a table. Ver. 1.0 Session 7 Slide 33 of 33

×