We already know how to create a primary key on a table. Here is an example: USE Exercise; GO CREATE TABLE Students ( StudentID int PRIMARY KEY, FirstName nvarchar(50) NOT NULL, LastName nvarchar(50)); GO When you create a primary key, the database engine automatically creates an index on the table and chooses the primary key column as its key. You have the option of indicating the type of index you want created. To do this, on the right side of the name of the column, enter CLUSTERED or NONCLUSTERED. If you don't specify the type of index, the CLUSTERED option is applied.
In our introduction, we saw that an index can make it possible to take some action during data entry, such as making sure that a column have unique values for each record or making sure that the combination of values of a group of columns on the same record produces a unique value. Besides this characteristic of indexes, they are actually very valuable when it comes to data analysis. As mentioned for a book, the primary goal of an index is to make it easy to locate the records of a table or view.
An index is made valuable in two ways. On one hand, the records should be sorted. A clustered index itself takes care of this aspect because it automatically and internally sorts its records. What if the records are not unique? For example, in a bad data entry on a list of employees, you may have two or more employees with the same employee's records. If you create an index for such a table, the database engine would create duplicate records on the index. This is usually not good because when it comes time to select records, you may have too many records and take a wrong action. When creating a table, you can create index for it and let the index apply a rule that states that each record would be unique. To take care of this, you can apply a uniqueness rule on the index. If you are visually creating an index, in the Indexes/Keys dialog box, select the index on the left side. On the right list, set the Is Unique field to Yes. On the other hand, if you want to remove this rule, set the Is Unique field to No. To create a uniqueness index in SQL, apply the UNIQUE keyword in the formula: CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX index_name ON Table/View(Column(s)) Start with the CREATE UNIQUE expression, then specify whether it would be clustered or not. The rest follows the descriptions we saw previously. Here is an example: -- ============================================= -- Database: Exercise -- ============================================= USE master GO -- Drop the database if it already exists IF EXISTS ( SELECT name FROM sys.databases WHERE name = N'Exercise' ) DROP DATABASE Exercise GO CREATE DATABASE Exercise GO USE Exercise; GO -- ============================================= -- Database: Exercise -- Table; Employees -- ============================================= CREATE TABLE Employees ( EmployeeNumber int NOT NULL, LastName nvarchar(20) NOT NULL, FirstName nvarchar(20), Username nchar(8) NOT NULL, DateHired date NULL, HourlySalary money ); GO CREATE UNIQUE CLUSTERED INDEX IX_Employees ON Employees(EmployeeNumber); GO
Once you have specified the uniqueness of an index on a table, during data entry, if the user enters a value that exists in the table already, an error would be produced. Here is an example: USE Exercise; GO INSERT INTO Employees(EmployeeNumber, FirstName, LastName, HourlySalary) VALUES(92935, N'Joan', N'Hamilton', 22.50) GO INSERT INTO Employees(EmployeeNumber, FirstName, LastName, HourlySalary) VALUES(22940, N'Peter', N'Malley', 14.25) GO INSERT INTO Employees(EmployeeNumber, FirstName, LastName, HourlySalary) VALUES(27495, N'Christine', N'Fink', 32.05) GO INSERT INTO Employees(EmployeeNumber, FirstName, LastName, HourlySalary) VALUES(22940, N'Gertrude', N'Monay', 15.55) GO INSERT INTO Employees(EmployeeNumber, FirstName, LastName, HourlySalary) VALUES(20285, N'Helene', N'Mukoko', 26.65) GO This would produce: (1 row(s) affected) (1 row(s) affected) (1 row(s) affected) Msg 2601, Level 14, State 1, Line 1 Cannot insert duplicate key row in object 'dbo.Employees' with unique index 'IX_Employees'. The statement has been terminated. (1 row(s) affected)
Data in your database may involve many records, in thousands or millions, so much that at one time, it may become difficult to manage. One way you can deal with this is to store the records of a table in different file groups. This makes it possible to store one section of records in one file group, another section in another file group, possibly another section in another file group, and so on. As a result, when it comes time to look for one or a few records among thousands or millions of records, it would be easier to locate it or to locate them. Of course, the data still belongs to one database and to the same table.
Before partitioning a table, you must create the necessary file groups. This can be done when creating the database since it is at that time that you specify how the database will be stored; that is, what files will hold the information of the database. After creating the database and creating its file groups, before partitioning a table, you must create a partition function and a partition scheme. A partition function is used to define the ranges of records that will be stored in what file group. The SQL formula to create a partition function is: CREATE PARTITION FUNCTION PartitionFunctionName ( ParameterType ) AS RANGE [ LEFT | RIGHT ] FOR VALUES (StartRange1, StartRange2, StartRange_n) To use from a template, open a Query window. In the Templates Explorer, expand the Partition Function node. Drag Create Partition Function and drop it in the Query window. Skeleton code will be generated for you: -- ===================================== -- Create Partition Function template -- ===================================== USE <database_name, sysname, AdventureWorks> GO CREATE PARTITION FUNCTION <partition_function_name, sysname, myRangePF> ( <data_type_name, sysname, int> ) AS RANGE LEFT FOR VALUES (<data_value1,,1>, <data_value2,,100>, <data_value3,,1000>); -- Partition function on a partitioning column col1 would be partitioned as follows: -- Partition 1: col1 less than or equal to <data_value1,,1> -- Partition 2: col1 greater than <data_value1,,1> AND col1 less than or equal to <data_value2,,100> -- Partition 3: col1 greater than <data_value2,,100> AND col1 less than or equal to <data_value3,,1000> -- Partition 4: col1 greater than <data_value3,,1000> The creation of a partition function starts with the CREATE PARTITION FUNCTION expression followed by a name. The name follows the rules for names in Microsoft SQL Server. Because you are creating a function, the name is followed by parentheses. In the parentheses of the function, you must specify the data type of the column that will be used to create a range of records. The values of that column will be used to distinguish ranges of records. This means that the values of this column must allow the database engine to predict a range of records. This is called the partitioning column. For example, you can use a column that has an incremental count of values. This is the case for an identity primary key column. As another example, you can use a column that holds a category of values, such as female customers vs male and child customers. As one more example, you can use a column that holds dates so that you can isolate ranges of records from one date to another. After closing the parenthesis, type AS RANGE, which indicates that you are going to specify the ranges of values. This is followed by either LEFT or RIGHT. When the partition function will have been created and when the table itself will have been created, when the database engine is asked to look for a record or a range of records, it may have to sort the records. If you want it to sort the records from left to right, use the LEFT keyword. If you want the records sorted from right to left, use the RIGHT keyword. The AS RANGE LEFT or AS RANGE RIGHT expression is followed by FOR VALUES that is followed by parentheses. When creating a partition function, you must provide a way for the database engine to get a range of records. For example, you can use records from number 1 to number 1000, then another range from 1001 to 5000, and so on. Or you can specify that a range of records would go from February 11th, 2000 to June 26th, 2005. Then another range would go from June 26th 2005 to December 14th, 2006, and so on. You specify the range in the parentheses that follow the FOR VALUES expression. Type the first value of the first range, followed by a comma, followed by the first value of the second range, and so on.
A partition scheme specifies the names of the file groups, in their order that will store the ranges of records that were created in the partition function. The formula to create a partition scheme is: CREATE PARTITION SCHEME PartitionSchemeName AS PARTITION PartitionFunctionName [ ALL ] TO ( { file_group_name | [ PRIMARY ] } [ ,...n ] ) You start with the CREATION PARTITION SCHEME expression do indication your intention. This is followed by a name. The name follows the rules of objects. After the name of the partition scheme, type AS PARTITION followed by the name of the partition function you should have previously created. If you are planning to use only one file group, after the name of the partition function, enter ALL, followed by parentheses, in which you will type PRIMARY. If you are planning to use different file groups, after the name of the partition function, enter TO, followed by parentheses. We saw that, in the parentheses of the FOR VALUES of the partition function, you entered the starting value of the first range. In the parentheses of the TO keyword, type the name of the file group that will hold the records of the first range of the partition function. We also saw how to specify the second range in the partition function. In the parentheses of the TO clause, after the name of the first file group, type a comma followed by the name of the file group that will hold the records of the second range.
After creating the partition scheme, you can create the table. The formula to specify a partition scheme when creating a table is: CREATE TABLE What We Have Learned So Far ( What We Have Learned So Far ) ON PartitionSchemeName(ColumnName) You start with the CREATE TABLE expression, followed by things we have learned so far: an optional schema and a required name. After the name of the table, you open and close the parentheses, in which you include other things we have seen so far: the columns, the constraints, and their options. Outside the parentheses, type the ON keyword, followed by the name of the partition scheme you will have created, followed by an opening and a closing parentheses. Inside the parentheses of the schema name, enter the name of the table's column that is the partitioning column. After creating the table, you can use it, like any normal table.
|
|
|||||||||||||||||||||||||||||||||||||||||||
|