Scenarios: Range-clustered tables
Range-clustered tables can have single-column or multiple-column keys, and can allow or disallow rows with key values that are outside of the defined range of values. This section contains scenarios that illustrate how such tables can be created.
Scenario 1: Creating a range-clustered table (overflow allowed)
- School ID
- Program ID
- Student number
- Student ID
- Student first name
- Student last name
- Student grade point average (GPA)
CREATE TABLE students (
school_id INT NOT NULL,
program_id INT NOT NULL,
student_num INT NOT NULL,
student_id INT NOT NULL,
first_name CHAR(30),
last_name CHAR(30),
gpa FLOAT
)
ORGANIZE BY KEY SEQUENCE
(student_id STARTING FROM 1 ENDING AT 1000000)
ALLOW OVERFLOW
;
In this example, the STUDENT_ID column, which serves
as the table key, is used to add, update, or delete student records.The size of each record is based on the sum of the column lengths. In this example, each record is 97 bytes long (10-byte header + 4 + 4 + 4 + 4 + 30 + 30 + 8 + 3 bytes for nullable columns). With a 4-KB (or 4096-byte) page size, after accounting for overhead, there are 4038 bytes (enough for 41 records) available per page. A total of 24391 such pages is needed to accommodate 1 million student records. Assuming four pages for table overhead and three pages for extent mapping, 24384 4-KB pages would be pre-allocated when this table is created. (The extent mapping assumes a single three-page container for the table.)
Scenario 2: Creating a range-clustered table (overflow not allowed)
CREATE TABLE students (
school_id INT NOT NULL,
class_id INT NOT NULL,
student_num INT NOT NULL,
student_id INT NOT NULL,
first_name CHAR(30),
last_name CHAR(30),
gpa FLOAT
)
ORGANIZE BY KEY SEQUENCE
(school_id STARTING FROM 1 ENDING AT 200,
class_id STARTING FROM 1 ENDING AT 20,
student_num STARTING FROM 1 ENDING AT 35)
DISALLOW OVERFLOW
;
In this example, the SCHOOL_ID, CLASS_ID, and STUDENT_NUM
columns together serve as the table key, which is used to add, update,
or delete student records.Overflow is not allowed, because school board policy restricts the number of students in each classroom, and there is a fixed number of schools and classrooms being administered by this school board. Some smaller schools (schools with fewer classrooms than the largest school) will have pre-allocated space in the table that will likely never be used.