2.2 The structure, creation, viewing, modification, and deletion of the database

2.2.1 The structure of the database

Database storage mode: files and file groups

file type:

  • Main database file
  • Secondary database file
  • Transaction log file

Default storage path: "X"\program Files\Microsoft SQL Serve\MSSQL.1\MSSQL\Data"

The main database file (the Pri m ary D the Database F. Ile):
         key database file, each database has one and only one primary data file.

  • effect:

    • Store the startup information of the database;
    • Store some or all of the data;
    • Contains pointers to other files in the database.
  • Recommended file extension: mdf

Secondary database file (Seco n- Dary D the Database F. Ile, also called a secondary file) : can not, there may be a plurality of secondary data file.

  • Function: Store the remaining data and database objects that are not stored in the main data file.
  • Recommended file extension: .ndf

The transaction log file ( L OG D ATA F Ile): Each database must have one or more log files.

  • Role: Store the transaction log information needed to restore the database.
  • Recommended file extension: .ldf

File group

  • Definition: a named collection of files.
  • Function: It is convenient for users to manage, distribute/place data.
  • Type :
    • ※The main file group (there is only one) contains the main data files and the secondary data files that are not allocated to other file groups.
    • User-defined file group (there can be none, or there can be more than one): It can only contain secondary data files.
    • Usage rules:
      (1) A file and file group can only be used by one database;
      (2) A file can only belong to one file group;
      (3) Log files cannot belong to a file group.
      Insert picture description here

Benefits of using file group management to distribute data:

  • Make the database structure clear
  • Convenient user management
  • Improve efficiency

Database object

  • Types of:
    • Table
    • View
    • Stored Procedures
    • Triggers
    • User-defined Data Types
    • User-defined Functions
    • Indexes
    • constraint
  • Representation method:
    server name. database name. owner name. object name
    serve.database.owner.object
    Insert picture description here

2.2.2 System database

The system databases of SQL Server 2005 are:

  • master database

  • model database

  • msdb database

  • tempdb database

  • master database

    • Function: Record all system-level information of SQL Server . If the master database is unavailable, SQL Server cannot be started.
  • model database

    • Role: Template for all databases . You can add other database objects to the model database, and these objects can be inherited by the database created later.
  • msdb database

    • Role: SQL Server Agent (SQL Server Agent) to plan alarms and jobs.
  • tempdb database

    • Role: is the connection to SQL Server instances available to all users of global resources , which holds all temporary tables and temporary stored procedures.

2.2.3 Create a database

method:

  • Use SQL Server Management Studio
  • Use SQL statements

The sequence of operations for creating a "complete" database:
1. Give the database a name and set the owner of the database.
2. Define the file group.
3. Assign data files to the file group
. 4. Create a table and set related options and constraint rules for
the table. 5. Put the table into the file group.
6. Create other database objects.

1. Use SQL Server Management Studio
1. Step
(1) Name the database
(2) Define the file group

  • "File Group" tab → "Add"
  • "General" tab → "File group" → "New file group"

(3) Assign data files to the file group

  • The configuration of the database file:
    • File name (logical name)
    • file type
    • File group
    • Initial size
    • Automatic growth
    • path

2. "Options" tab settings

  • Recovery mode:
    • simple
    • complete
    • Bulk log
  • Compatibility level: SQL Server7.0, 2000, 2005
  • Automatic shutdown: Specify whether the database is completely closed and resources are released after the previous user exits. After selection, the database file can be treated like a normal file (such as copying).
  • Restrict access: Specify those users who can access the database. The possible values ​​are:
    • Multiple: The normal state of the database, allowing multiple users to access the database at the same time.
    • Single: Used for maintenance operations. Only one user is allowed to access the database at a time.
    • Restricted: Only members of the db_owner, dbcreate, or sys admin roles can use the database.

3. View database information
Insert picture description here
2. Use SQL statements
1. Introduction to SQL : SQL was proposed by Boyce and Chamberlin in 1974 and is the standard language in relational databases.

  • SQL features:

    • The function is comprehensive , can finish all the work in the database.
    • The user only needs to propose "what to do" without specifying how to do it. SQL language submits the user's requirements to the system, and automatically completes all the work.
    • The SQL language is very concise and simple , very close to the natural language of English.
    • The SQL language can be directly used interactively in a command mode , or it can be embedded in a programming language and used programmatically .
    • Although SQL is used differently in different environments, the syntax of SQL language is basically the same .
  • SQL main functions:Insert picture description here

2. Create a database

  • grammar:
create database database_name
[on
[[primary]<filespec>[,...n]
[,<filegroup>[,...n]]]
[log on {
   
   <filespec>[,...n]}]
[collate<collation_name>]
[for attach]

<filespec>::=
(name='逻辑文件名',
filename = '存放数据库的物理路径和文件名'
[,size = 数据文件的初始大小]
[,maxsize = 指定文件的最大大小]
[,filegrowth = 指出文件每次的增量])
<filegroup>::=
{
filegroup filegroup_name[default]
<filespec>[,...n]
}

Insert picture description here
Insert picture description here
Insert picture description here
The corresponding structure corresponds to the code:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

2.2.4 View database information

1
1
1
1
    

1.sp_helpdb[database name]

  • Features:
    • If no database parameter is specified, the information of all databases in the server will be displayed.
    • Specify specific database parameters, and the information of the specified database will be displayed.

2.sp_databases

  • Features:
    • Display the information of all available databases in the server.

3.sp_helpfile[logical file name]

  • Function: View the information of files in the current database
    • If the file name parameter is not specified, the information of all files in the current database will be displayed .
    • Specify the specific file name parameter, and the information of the specified file in the database will be displayed.

4.sp_helpfilegroup[file group name]

  • Features:
    • Without specifying the file group name parameter, the information of all file groups in the database will be displayed.
    • Specify the specific file group name parameter, and the information of the specified file group in the database will be displayed.

2.2.5 Modify the database

method:

  • Use SQL Server Management Studio
  • Use SQL statements

Modified content:

  • Database composition
  • Database options

※Use SQL statement

  • (1) Modify the database composition
    Syntax:
alter database database_name
{
   
   add file <filespec>[,...n]
	[to filefoup filegroup_name]  /*在文件组中增加数据文件*/
| add log file <filespec>[,...n]  /* 增加事务日志文件*/
| remove file logical_file_name   /*删除数据文件*/
| add filegroup filegroup_name    /*增加文件组*/
| remove filegroup filegroup_name /*删除文件组*/
| modify file <filespec>[,...n]   /*修改文件属性*/
| modify name = new_dbname        /* 更新数据库名称*/

Insert picture description here
Insert picture description here

  • (2) Modify database options
    Method one:
alter database database_name
set database_options[value]

'value': Option value, which can be true, false, on, or off.
Insert picture description here
Insert picture description here

2.2.6 Delete database

method:

  • Use SQL Server Management Studio
  • Use SQL statements

grammar:drop database database_name[,...n]

Guess you like

Origin blog.csdn.net/diviner_s/article/details/107232684