SQL Server database and table management
1. Data storage structure of SQL Server
SQL Server is a database management system that needs to store high-capacity data in an efficient manner.
1. File type
The database is stored on the disk in units of files and consists of database files and transaction log files . A database should contain at least one data file and one transaction log file
The SQL Server database has the following four types of files:
- Main data file : contains the startup information of the database, database and objects, such as tables and indexes; the suffix of the main data file is .mdf
- Secondary (auxiliary) data file : All other data files except the main data file are secondary data files; the suffix of the secondary data file is .ndf
- Transaction log file : records all transaction SQL statements, used to restore the database; the suffix of the transaction log file is .ldf
- Filestream data files : can enable SQL Server-based applications to store unstructured data in the file system, such as documents, pictures, audio, video, etc. The file stream mainly integrates the SQL Server database engine and the New Technology File System (NTFS).
2. Data file
From a more microscopic point of view, the data file is composed of several 64KB extents, each of which is composed of 8 8KB continuous pages.
The smallest unit of storage recognized by SQL Server is called a page. The size of a page is 8K, which is the actual data storage unit of SQL Server. Disk I/O operations are performed at the page level. An area consists of 8 physically consecutive pages (ie 64 KB), which are used to effectively manage pages. All pages are stored in the area. When the table is created in SQL Server, the object is allocated to the zone. Smaller tables can be in the same area as other database objects.
2. Transaction log
A transaction is a collection of one or more T-SQL (Transact-SQL) statements, which is equivalent to an "atomic" task, which either succeeds or fails . Every SQL Server database has a transaction log, which is used to record all transactional SQL statements. When a data disaster occurs, the database can be recovered through T-SQL statements recorded in the transaction log. If the system fails, SQL Server will use the transaction log to roll forward (redo) all confirmed transactions and roll back (undo) all outstanding transactions.
2. Database related management
You can use SSMS tools to design and build a database and store corporate data to meet corporate needs.
-
If the SQL Server database is not installed, please see: https://blog.csdn.net/weixin_46902396/article/details/110436865 to install.
-
Log in to the database:
[Start]|[All Programs] You can see the program group of Microsoft SQL Server 2008 R2
1. Create a database
2. Expand the database
- Role: Allocate more space for data files and log files.
3. Shrink the database
- Role: release unused space in the database
1) Manually shrink the database
Shrink the database:
Shrink the file:
2) Automatically shrink the database
4. Detach and attach the database
- Role: Change the database to a different SQL instance
1) Separate the database
Separating the database refers to removing the database from the SQL Server instance, but to ensure that the data files and log files in the database are intact. Later, these files can be reattached to other database instances.
2) Additional database
When appending data, all data files (primary data file and secondary data file) must be available. If the path of any data file is different from the path when the database was first created or the database was last attached, the current path of the file must be specified.
5. Delete the database
Only the user database can be deleted, but the system database cannot be deleted; after deleting the database, the files and their data are deleted from the disk on the server. Once the database is deleted, it is permanently deleted.
Three, the basic concept of the table
A table is a database object that contains all the data in the database. The organization of data in tables is similar to that in spreadsheets, which are organized in row and column format. Each row represents a unique record, and each column represents a field in the record . Tables in SQL Server include the following main components:
- Columns : Each column represents a certain attribute of the object modeled by the table. For example, the employee table has number column, name column, and position column.
- Rows : Each row represents a separate instance of the object modeled by the table. For example, each employee of the company has a row in the table.
1. Data integrity
Data integrity refers to the accuracy and reliability of data. It is proposed to prevent the existence of data that does not meet the semantic requirements in the database and to prevent invalid operations or error information caused by the input/output of error information. It is divided into the following four categories:
1) Entity integrity
Entity integrity defines a row as the only entity of a particular table. Entity integrity through the UNIQUE (unique) index, UNIQUE constraint or PRIMARY BY (primary key) constraint, to enforce the integrity of the identity column or primary key of the table.
2) Domain integrity
Domain integrity refers to the validity of items in a particular column. You can force domain integrity to restrict the type (by using data types), restrict the format (by using CHECK constraints and rules), or restrict the range of possible values (by using FOREIGN KEY constraints, CHECK constraints, DEFAULT definitions, NOT NULL definitions and rules).
3) Referential integrity
When entering or deleting rows, referential integrity preserves the relationships defined between the tables. In SQL Server, referential integrity is based on FOREIGN KEY (foreign key) and CHECK (check) constraints, based on the relationship between the foreign key and the primary key or between the foreign key and the unique key. Referential integrity ensures that key values are consistent in all tables.
4) User-defined integrity
User-defined integrity can define specific business rules that do not belong to any other integrity category. All integrity categories support user-defined integrity, including all column-level constraints and table-level constraints in CREATE TABLE, stored procedures and triggers.
2. Primary key
The primary key uniquely identifies the row data in the table, and a primary key value corresponds to a row of data. The primary key is composed of one or more fields, and its value is unique, and it is not allowed to take the null value (NULL). A table can only have one primary key.
3. Use SSMS to manipulate data sheets
1) Data type
The data type is an attribute of data, which is used to specify the type of data that the object can store. Data types in SQL Server can be summarized into the following categories: precise numbers, approximate numbers, date and time, character strings, Unicode character strings, binary character strings, and other data types.
Accurate numbers can be divided into nine data types:
type of data | description | storage |
---|---|---|
int | Store an integer between -2 31 ~ +2 31 -1 | 4 bytes |
bigint | Store an integer between -2 63 ~ +2 63 -1 | 8 bytes |
smallint | Store an integer between -2 15 ~ +2 15 -1 | 2 bytes |
tinyint | Store an integer between 0 and 255 | 1 byte |
bit | It belongs to plastic data, and its value can only be 0, 1 or NULL (empty value) | 1 byte |
decimal | Store the value between -10 38 ~ +10 38 -1 | Up to 17 bytes |
numeric | Equivalent to decimal | Up to 17 bytes |
money | Store the value between -922337203685477.5808 ~+922337203685477.5807 |
8 bytes |
smallmoney | Store the data between -214748.3648 ~+214748.3647 |
8 bytes |
Approximate numbers can be divided into two data types:
type of data | description | storage |
---|---|---|
float[(n)] | The value is any number between -1.79+10 308 ~ +1.79+10 308 | N<24-4 bytes, N>24-8 bytes |
real | The value is a floating point number between -3.40+10 38 ~ +3.40+10 38 | 4 bytes |
Date and time can be divided into six data types:
type of data | description | storage |
---|---|---|
date | Store from January 1, AD to December 31, 9999 AD | 3 bytes |
time | Used to indicate a time of day | 5 bytes |
datetime | Stored from January 1, 1753 to December 31, 9999 | 8 bytes |
datetime2 | The extension of the existing datetime type, the data range is larger, and the default decimal precision is higher | 6~8 bytes |
datetimeoffset | Used to indicate date and time. Compared with datetime2, increased time zone | 8~10 bytes |
smalldatetime | Store from January 1, 1900 to June 6, 2079, accurate to 1 minute | 4 bytes |
Strings can be divided into three data types:
type of data | description | storage |
---|---|---|
char(n) | N is between 1 and 8000 characters | n bytes |
varchar(n) | N is between 1 and 8000 characters | 1 byte per character + 2 bytes extra overhead |
text | Up to 2 31 -1 | 1 byte per character |
Unicode strings can be divided into three data types:
type of data | description | storage |
---|---|---|
nchar(n) | N is 1~4000 | (2n bytes) + 2 bytes additional overhead |
nvarchar(max) | Up to 2 30 -1 | 2 × number of characters + 2 bytes of additional overhead |
ntest | Up to 2 30 -1 | 2 bytes per character |
Binary strings can be divided into three data types:
type of data | description | storage |
---|---|---|
binary(n) | Store fixed-length binary data up to 8000 bytes long. The content used for the input table is close to the same length | n bytes |
varbinary(n) | Store up to 8000 bytes of binary data. When the content size of the input table is variable | 1 byte per character + 2 node additional overhead |
image | Store variable-length binary data, up to 2 31 -1 or about 2 billion bytes | 1 byte per character |
2) Default value
If no value is specified for a column when inserting a row, the column uses the default value. The default value can be any value whose calculation result is a constant, such as a constant, a built-in function, or a mathematical expression. For each column in the table, you can specify the default value that will be entered in the column when the user leaves the column blank. If no default value is assigned and the column is left blank, then:
- If the option to allow null values is set, NULL will be inserted into the column.
- If the option to allow null values is not set, the column will remain blank, but they will not be able to save the row until the user provides a value for the column.
3) Identity column
For each table, you can create an identification column that contains the serial number value generated by the system, and the serial number value uniquely identifies each row in the table.
Identifiers have the following three characteristics:
- The data type of the column is a numeric type without decimals.
- During the insert operation, the value of this column is generated by the system according to certain rules, and null values are not allowed.
- The column values are not repeated and have the function of identifying each row in the table. Each table can only have one identifying column.
To create an identity column, you usually specify the following three contents:
- Type : In SQL Server 2008, the identification column type must be a numeric type, such as decimal, int, numeric, smallint, bigint, tinyint. It should be noted that when decimal and numeric are selected, the number of decimal places must be zero. Also pay attention to the value range represented by each data type.
- Seed : The value assigned to the first row in the table, the default is 1.
- Increment : The increment between two adjacent identification values, the default is 1.
4) Check constraints
By restricting the values that a column can accept, CHECK constraints can enforce domain integrity. CHE constraints can be created by any logical (Boolean) expression based on logical operators that return True or False.
Four, T-SQL statement management table
The operation of SQL Server can also be done using T-SQL statements, which is also the most commonly used way for database administrators to manage databases in actual production environments. T-SQL statements are generally executed in the query window. Select the database where the table is located, and click the "New Query" button in the SSMS toolbar to create a new query window.
1. Create a table
The basic syntax for creating a table is as follows:
create table 表名
(
列名1 数据类型(大小),
列名2 数据类型(大小),
列名3 数据类型(大小),
...
)
Use T-SQL statements to create score tables
create table 成绩表
(
编号 int identity (1,1) not null,
姓名 nvarchar(50) not null,
身份证号 varchar(18) primary key,
成绩 tinyint not null check(成绩 >=0 and 成绩<=100),
)
annotation:
- identity (1,1): indicates that the column is an identity column, and the seed and increment values are both 1.
- not null: Identifies that the column does not allow null values.
- primary key: indicates that this column is the primary key column.
- check (): Indicates to create a constraint for the column, and the constraint conditions are in parentheses.
2. Inquiry Form
select *from [成绩表]
3. Modify the table structure
Use the alter table statement to add, modify, or delete columns from an existing table.
1) Add column
Basic syntax:
alter table 表名
add 列名 数据类型(大小)
Use T-SQL statements to add a "remarks" column to the score table.
alter table [成绩表]
add 备注 nvarchar(2000)
Use T-SQL to query the score table
select *from [成绩表]
2) Modify the data type of the column
Basic syntax:
alter table 表名
alter column 列名 数据类型(大小)
Use T-SQL statements to modify the length of the "remarks" column in the score table to 1000.
alter table [成绩表]
alter column 备注 nvarchar(1000)
3) Delete column
Basic syntax:
alter table 表名
drop column 列名
Use T-SQL statements to delete the "remarks" column in the score table.
alter table [成绩表]
drop column 备注
Use T-SQL statement to query the score table
select *from [成绩表]
4. Delete the table
Basic syntax:
drop table 表名
Use T-SQL statement to delete the score table.
drop table [成绩表]
Use T-SQL statement to query the score table
select *from [成绩表]