National Computer Rank Examination Level 3 Database Technology (7)

test analysis

◆Generally, in the examination, it will appear in multiple choice questions, fill in the blank questions, and big questions.
◆Common test knowledge points include:
1. Master the types, layout and composition of SQL Server databases
2. Master the grammar of modifying databases, separating and attaching databases
3. Understand the meaning of architecture and grammatical formats
4. Master the concept of partition tables and create partitions Solution
5. Master the creation of indexes and indexed views

7.1 Create and maintain database

1. An overview of the SQL Server database
1. The database in SQL Server 2008 consists of a collection of tables containing data and other objects (such as views, indexes, stored procedures, etc.), the purpose of which is to provide support for the execution of data-related activities. From the perspective of database application and management, SQL Server divides databases into two categories, the specific content is as follows:
(1) System database: It is automatically created and maintained by the SQL Server database management system. These databases are used to hold information to maintain the proper functioning of the system.
(2) User database: It stores data related to the user's business. Usually, the establishment of a database refers to the creation of a user database, and the maintenance of the database refers to the maintenance of the user database.

General users only have the right to query the system database.

2. Five system databases installed automatically by the system and their purpose
(1) tempdb
01. Temporary database, used to save temporary objects or intermediate result sets, and provide a temporary workspace for data sorting and other operations.
02. The tempdb database will be recreated every time SQL Server is started.
(2) Resource
01. is a read-only database that contains all system objects in SQL Server.
02. SQL Server system objects are physically stored in the Resource database, but logically displayed in the sys architecture of each database.
03. In the Object Explorer of SSMS, this database cannot be seen under "System Database".
(3) master
01. is the most important database in SQL Server2008.
02. Log all system-level information about the SQL Server instance, including instance-wide metadata (such as login accounts), endpoints, connection servers, and system configuration settings.
03. The existence of all other databases, the location of the database files, and the initialization information of SQL Server are recorded.
(4) msdb
01. It is used by the SQL Server agent service to schedule alarms and jobs and record operators. Save information about scheduling alarms, jobs, operators, etc.
02. A job is a collection of automatically executed series of operations defined in SQL Server, and the execution of a job does not require any manual intervention.
(5) model
01. Used as a template for all databases created on the SQL Server instance.
02. Modifications made to the mode| database (such as database size collation, recovery mode, and other database options) will be applied to all user databases created in the future.
03. When the user creates a database, the system automatically copies all the contents of the model database to the newly created database.

Second, the composition of the SQL Server database
1.SQL Server maps the database to a set of operating system files, which are divided into two categories: data files and log files. Data files contain data and objects, and log files contain
the information needed to restore all transactions in the database.
2. Data files
Data files are used to store database data. Data files are divided into main data files and secondary data files. The specific features are as follows:
(1) The main data file
01. The extension is . System information, which can store user data.
02. Each database has and can only have one main data file.
03. The main data file is the first data file created for the database.
04. SQL Server 2008 requires that the size of the main data file should not be less than 3MB.
(2) The secondary data file
01. The extension is .ndf
02. A database may not contain secondary data files, or may contain multiple secondary data files ; These secondary data files can be created on one disk or on different disks.

3. Database space allocation rules
(1) When creating a user database, the model database is automatically copied to the new user database, and is copied to the main data file.
(2) In SQL Server 2008, the data storage allocation unit is the data page. A page is an 8KB block of contiguous disk space. A page is the smallest unit for storing data, and the size of a page determines the maximum size of a row of data in a database table.
(3) SQL Server does not allow a row of data in a table to be stored on different pages. That is, rows cannot be stored across pages. Therefore, the size of a row of data in the table cannot exceed 8060B. This means that the size of each row of data should be considered when designing relational tables to improve space utilization.

4. Properties of the database file
1. File name and its location
filename and its location

2. Properties of database files
(1) Initial size
You can specify the initial size of each data file and log file. When specifying the initial size of the main data file, its size cannot be smaller than the size of the main data file of the model database. Because the system copies the content of the main data file of the model database to the main data file of the user database.
(2) Growth method
If necessary, you can specify whether the file will grow automatically. The default configuration of this option is automatic growth. That is, when the space of the data file is insufficient, the system automatically expands the file space. This can prevent the error that new data cannot be inserted or data operation cannot be performed due to the exhaustion of space.
(3) Maximum size
The maximum size of a file refers to the maximum space limit for file growth. The default is unlimited. It is recommended that users set the maximum size of the space that allows the file to grow.
Because, if the user does not set the maximum space size, but sets the file automatic growth method, the file will grow without limit until the disk space is used up.
5. Create a database with T-SQL statement
1
2
3
6. Modify the database
1. Expand the database space
If the automatic growth mode is not set when creating the database, the database may not have enough space after a period of use. These spaces include data space and log space,
1
6

2. Shrink the database space
(1) Shrink the database is to release unused space in the database and return the released space to the operating system. The space of data files and log files can be shrunk, and database files can be shrunk manually in groups or individually, or can be automatically shrunk at specified time intervals by setting database options. File shrinkage starts from the end.
(2) The two cases of manually shrinking the database space are as follows.
01. Shrink the size of a file in the database.
02. Shrink the size of the entire database proportionally.

Note: When shrinking the size of the entire database space, the size of each file after shrinking cannot be smaller than the initial size specified when creating these files, or the size set during the last shrinking file operation. If you shrink the size of a file, there is no such limit.

(3) shrink the size of the entire database
Shrink the size of the entire database

(4) Shrink the size of the specified file
Shrink the size of the specified file

3. Add and delete database files
You can expand the database space by adding files in the database, or reduce the database space by deleting files.
(1) Add file
01. When adding a data file, the system will immediately use the newly added file.
02. The use of log files is different from that of data files. Log files are independent of each other and there is no file group.
03. When writing information to the log file, a fill-to-full strategy is used instead of a proportional fill strategy.
04. Use the ALTER DATABASE statement of T-SQL to add files to the database, including specifying the initial size, storage location, growth mode and other attributes of the file, which is the same as the method of specifying file attributes when creating a database. You can also specify the filegroup to which the newly added data files belong.
(2) Delete a file
Only when the file is completely empty, the file can be deleted from the database.
The T-SQL statement to delete a database file is ALTER DATABASE, and its grammatical format is:

ALTER DATABASE database_name
REMOVE FILE logical_file_name

The meaning of each parameter is as follows:
database_ name: the database name of the file to be deleted.
logical_file_name : The logical file name of the deleted file.
Note: A log file can only be deleted from the database if it does not contain any active or inactive transactions.

(3) Example
example

7. Separating and attaching the database
The operation of separating and attaching the database can realize the purpose of moving the database from one database server to another without recreating the database.
1. Detaching the database
(1) Detaching the database refers to deleting the database from the SQL Server instance, but not deleting the data files and log files of the database. This is different from deleting the database. Deleting the database will delete all the files of the database together, while detaching the database will keep the data files and log files of the database intact and consistent.
(2) Separating the database actually means that the files of the database are not managed by the database management system, so that the user can copy the data files and log files of the database to another computer or other places on the same computer.
(3) Grammatical format and parameter description of the separated database
insert image description here

2. Attaching the database
(1) Attaching the database is to reattach the separated database to the database management system, which can be attached to another SQL Server instance of the local machine or to another database server.
insert image description here

(2) Grammatical format and parameter description of the additional database
insert image description here

(3) Example
example

7.2 Architecture

1. Overview of architecture
Schema (also known as mode) is a logical namespace under the database, which can store database objects such as tables and views. It is a container of database objects.
01. A database can contain one or more schemas, which are owned by specific authorized users. Schema names must be unique within the same database.
02. Objects belonging to a schema are called schema objects. That is, they depend on the schema. The types of schema objects include basic tables, views, triggers, etc.
03. A schema can consist of zero or more schema objects. The schema name can be explicit or a default name provided by the DBMS.
04. The reference to the object in the database can be qualified by the prefix of the schema name, and the CREATE statement without any schema restriction refers to creating the object in the current schema.
2. Define the architecture
Define Schema 1

Define Schema 2

3. Delete the structure
delete schema

7.3 Partition table

1. Basic concept of partition table
1. Description: A partition table divides the data in the table into different subsets horizontally, and these data subsets are stored in one or more file groups of the database.
(1) Advantage
01. Using partitions can quickly and efficiently manage and access data subsets, making large tables or indexes easier to manage.
02. Reasonable use of partitions will greatly improve the performance of the database.
(2) Creation conditions
Whether to create a partition table mainly depends on the current data volume and future data volume of the table, and also depends on the operation characteristics of the data in the table.
(3) Condition 01 satisfied by a large table partition
. The table contains (or will contain) a large amount of data that is used in many different ways.
02. The data is segmented, for example, the data is separated by year.

2. Description: A large amount of data is not the only condition for creating a partition table.
01. If a large amount of data in the table is frequently used data. And their operation methods are basically the same, it is better not to use partition table
02. If the amount of data is large, and the data is segmented. And different operations are used for different segments of data, so it is suitable to use a partition table.
03. When the operation on the data involves only a part of the data rather than all the data, you can consider creating a partition table.
Note: The partition table physically divides a large table into several small tables, but logically it is still a large table. For the user, as long as the user inserts the record into the large table (logical table), the database management system will automatically place the data into the corresponding physical small table.

2. Create a partition table
1. In SQL Server 2008, create a partition table through the following steps:
01. Create a partition function
The purpose of creating a partition function is to tell the database management system how to partition the table.
02. Create a partition scheme
The function of the partition scheme is to map the partition generated by the partition function to the file group.
The role of the partition function is to tell SQL Server how to partition the data, and the role of the partition scheme is to tell SQL Server which filegroup to put the partitioned data in.
03. Create a table using a partition scheme
Before creating a partition table, in order to facilitate management, you can first create several file groups and put different small tables in different file groups A. On the one hand, it is easy to understand, on the other hand, it can improve the running speed.
2. Create a partition function
1
2
3
4
3. Create a partition scheme
1
2
3
4
5

7.4 Index

1. Create an index
create index
The meaning of each parameter 1
The meaning of each parameter 2
example

2. Delete the index
delete index
example

7.5 Indexed Views

1. Overview of basic concepts
1. Standard view is also called virtual table, because the format of the result set returned by this view is the same as that of the basic table, which is composed of columns and rows, and the way to refer to the view in the SQL statement is also the same as the reference The method of the basic table is the same.
2. The result set of the standard view is not permanently stored in the database. Every time the data is queried through the standard view, the database management system will internally replace the definition of the view with the query statement for the basic table. Then, execute the query against the base table.
3. The overhead of dynamically generating a result set for each query that references a view is significant, especially for views that involve complex processing of a large number of data rows (such as aggregating large amounts of data or joining many rows). If such views are frequently referenced in queries, query performance can be improved by creating a unique clustered index on the views.
4. After creating a unique clustered index on the view, the result set of the view will be stored in the database, just like a table with a clustered index. A view with a unique clustered index is called an indexed view, also known as a materialized view.
2. Occasions suitable for establishing indexed views
1. Occasions suitable for establishing indexed views
01. If the basic data is rarely updated, the effect of indexed views is the best.
02. If the underlying data is frequently updated, the cost of maintaining indexed views may exceed the performance benefits of using indexed views.
03. If the basic data is regularly updated in the form of batch processing, but it is mainly processed as read-only data between updates, consider deleting all indexed views before updating, and then rebuilding indexed views, which can improve update performance.
2. The types of queries that indexed views can and cannot improve performance are as follows:
Types of queries that indexed views can and cannot improve performance

3. Define the indexed view
Before creating a clustered index on the view, the view must meet the following requirements:
1. The first index created on the view must be a unique clustered index, and then create other non-clustered indexes.
2. When defining an indexed view, the view cannot reference any other views, only basic tables.
3. All functions referenced by expressions in the view must be deterministic.
4. All basic tables referenced by the view must be in the same database as the view. And the owner is also the same as the view.
5. The view must be created with the SCHEMABINDING option.

Guess you like

Origin blog.csdn.net/weixin_47288291/article/details/123519476