Presentation is loading. Please wait.

Presentation is loading. Please wait.

In this session, you will learn to: Manage databases Manage tables Objectives.

Similar presentations


Presentation on theme: "In this session, you will learn to: Manage databases Manage tables Objectives."— Presentation transcript:

1 In this session, you will learn to: Manage databases Manage tables Objectives

2 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. Identifying the System Databases in SQL Server 2005

3 Just 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.

4 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 ] [ ]] [ LOG ON { [,...n ] } ] ::= ( [ NAME = logical_file_name, ] FILENAME = 'os_file_name' [, SIZE = size ] [, MAXSIZE = { max_size | UNLIMITED } ] [, FILEGROWTH = growth_increment ] ) [,...n] Let’s see how… Creating a User-Defined Database

5 Just a minute Which statement is used to create a database? Answer: The CREATE DATABASE statement

6 Table: Is created to store data Is created by using the CREATE TABLE statement Syntax: CREATE TABLE [ database_name. [ schema_name ].] table_name ( { | } [ ] [,...n ] ) [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] [ { TEXTIMAGE_ON { filegroup | "default" } ] [ ; ] Creating a Table

7 Example: Create an EmployeeLeave table in the HumanResources schema with the following details: Creating a Table (Contd.) ColumnsData TypeChecks EmployeeIDintNOT NULL LeaveStartDatedateNOT NULL LeaveEndDatedateNOT NULL LeaveReasonvarchar(100)NOT NULL LeaveTypechar(2)NOT NULL Let’s see how…

8 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 Implementing Data Integrity

9 You can maintain data integrity by: Applying constraints Applying rules Using user-defined types Implementing Data Integrity (Contd.)

10 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] ) Implementing Data Integrity (Contd.)

11 Constraints are of the following types: Primary key constraint Unique constraint Foreign key constraint Check constraint Default constraint Implementing Data Integrity (Contd.)

12 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 [, …]]] ) Implementing Data Integrity (Contd.)

13 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 [, …]]] ) Implementing Data Integrity (Contd.)

14 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 Implementing Data Integrity (Contd.)

15 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… Implementing Data Integrity (Contd.)

16 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 Implementing Data Integrity (Contd.)

17 Syntax: CREATE TABLE table_name ( col_name [CONSTRAINT constraint_name] CHECK (expression) (col_name [, col_name [, …]]). ) Let’s see how… Implementing Data Integrity (Contd.)

18 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… Implementing Data Integrity (Contd.)

19 Just 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.

20 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… Implementing Data Integrity (Contd.)

21 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… Implementing Data Integrity (Contd.)

22 Just 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')

23 Flash presentation: Partitioning TablesPartitioning Table 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 Creating a Partitioned Table

24 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 { ADD CONSTRAINT constraint_name constraint_type Let’s see how… Modifying a Table

25 Just 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

26 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… Dropping a Table

27 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. Demo: Managing Tables ColumnsData Type and SizeConstraints RimIDintPrimary key EmployeeIDintForeign Key references the EmployeeID of the Employee Table, NOT NULL AmountmoneyAmount>0 RimTypevarchar(20)RimType should be Medical, Cash, or Local Pending_withvarchar(30)NOT NULL How will you create the table?

28 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. Demo: Managing Tables (Contd.)

29 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. Summary

30 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. Summary (Contd.)

31 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. Summary (Contd.)

32 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. Summary (Contd.)

33 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. Summary (Contd.)


Download ppt "In this session, you will learn to: Manage databases Manage tables Objectives."

Similar presentations


Ads by Google