SQL Server 2008 R2 learning summary

Posted by newburcj on Mon, 18 Oct 2021 05:06:17 +0200

Beginning SQL Server 2008 R2 summary

The reference room for learning is the Chinese version of SQL Server 2008. From entry to mastery, the whole learning idea is also to learn according to the idea of the text. But not C#, and JAVA. The latter two examples did not do it.

1, SQL Server quick start
1. Basic concepts
(1) Watch
A table is a database object that contains all the data in a database. It is composed of rows and columns and is used to organize and store data.
(2) Field
Each column in the table is called a field, which has its own attributes, such as field type, field size, etc. Among them, the segment type is the most important attribute of the field, which determines which data the field can store.
The SQL specification supports five basic field types: character type, text type, numeric type, logical type and date / time type.
(3) Index
An index is a separate, physical database structure. It depends on the table. Indexing in the database enables the database program to find the required data without scanning the whole table.
(4) View
View is a table exported from one or more tables (also known as virtual table). It is a way for users to view the data in the data table. The table includes several defined data columns and rows, and its structure and data are based on the query of the table.
(5) Stored procedure
Stored Procedure is a set of SQL statements (including operations such as query, insert, delete and update) to complete specific functions. After compilation, it is stored in the database on the SQL Server side in the form of name, which is executed by the user by specifying the name of the Stored Procedure. When the Stored Procedure is called for execution, these operations will also be executed at the same time

2. Database files and basic databases
In a SQL Server 2008 database, the following three types of files can be used to store information.
(1) Master data file
The master data file contains the startup information of the database and points to other files in the database. User data and objects can be stored in this file or in a secondary data file. Each database can only have one master data file, and the default file extension is mdf
(2) Auxiliary data file
Auxiliary data files are optional, user-defined and store user data. By placing each file on a different disk drive, auxiliary files can be used to spread data across multiple disks. In addition, if the database exceeds the maximum limit of a single Windows file, you can use auxiliary data files so that the database can continue to grow. The default file extension for auxiliary data files is ndf
(3) Transaction log file
The transaction log file holds the log information used to recover the database. Each database must have at least one log file, and its default file extension is. ldf

Basic database

2, Database creation
1. Create a database.
Open SQL Server Management Studio (database manager) - right click database - new database - enter database name - OK - creation succeeded.

2. Table creation
Open the database - open the database you want to create (usually the database you created yourself, do not touch the four basic databases) - right click the table - create a new table - enter the column name and data type you want - right click the header after entering - enter the table name and click OK - create successfully.


3. Delete libraries and tables
Right click the library or table to delete - click delete - click OK.

You can also delete with a statement, but there will be no confirmation message for this statement deletion. That is, the legendary database deletion and running (no, most companies backup data regularly. Unless the whole database is deleted, I advise you to run faster.)

4. Other database operations. (not very useful, except for backing up and restoring the database)




5. Data type

3, Transact SQL statement
1. SELECT get simple data
1.1 SELECT syntax

USE Library management system   \\Library name
SELECT series,title	\\Search column names
FROM Book inventory information	\\Table name of the search column
WHERE series='Economics'	\\Conditional search statement


Original table information

WHERE statement

1.2 optional statements

USE The educational administration management system management system
SELECT * FROM Transcript ORDER BY achievement DESC


2. Keywords
2.1DISTINCT


2.2 TOP keyword
Returns the number of rows of the result, from top to bottom.

USE Library management system
SELECT TOP 10 series,title
FROM Book inventory information


3. Insert data.

USE Library management system
INSERT INTO Book inventory information VALUES('A','Sociology','Convince others with eloquence',10,5)

Edit the inserted data information according to the column name order of the table you want to insert.




USE The educational administration management system management system
UPDATE Teacher information form SET working years=working years+1




4, Advanced operations




After declaring the cursor, you can operate on the cursor. It mainly opens the cursor, retrieves the cursor specific row, closes the cursor and releases the cursor.
	DECLARE cursor_name CURSOR   //Set up a tour label
	FOR SELECT * FROM Table name	//Table name
	OPEN Tour label		//Open tag name
	
	FETCH NEXT FROM Tea_cursor		//Retrieve cursor specific rows
	WHILE @@FETCH_STATUS = 0
	BEGIN
	FETCH NEXT FROM Tea_cursor
	END
	CLOSE Tea_cursor		//Close cursor
	DEALLOCATE Tea_cursor	//Release cursor

```![Insert picture description here](https://img-blog.csdnimg.cn/3d52703b89dc4fbc8760a0ae4dc419f5.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_13,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/47c2b49e3d5c46fbb357178c3944c67d.png)
![Insert picture description here](https://img-blog.csdnimg.cn/0564436a974949e6a8efb802e6017e97.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/e74eb42c29f04a468498ef2922c3d38b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/a7b049df82df455b81148d2f3ce04eb5.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/ac06aa78a3c941589531bb0ea5bc735c.png)
1,Create stored procedure
![Insert picture description here](https://img-blog.csdnimg.cn/60b81ae40ec247aa9c9b8bdb612ea81b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_15,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/aa84483d27bf4cadb0b1f3ea538bfc8c.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
2,Using stored procedure parameters
![Insert picture description here](https://img-blog.csdnimg.cn/1fd7336d96a24064ac2251b4730c885d.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/c39eaef24e5849d29371eab7dcb0efdf.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/f5ee960d336d4c9f85e53b4313b92cb3.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/3aaef696651d48a79f4c68fb99068eae.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/965f73f41c9f40f7b10cc88d8f594eb2.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/bc8d15d5c6884c9b8acafe9894cd1d12.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/6727e60fa68b47d49d9ebf5d1c4801d0.png)
![Insert picture description here](https://img-blog.csdnimg.cn/462cf9a31d904673acf2a7ee8e1cdc00.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/8658f2ab8a194aa9897891c7dbf1f59a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/34d2ce10a6b7400999c52fc40472a032.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/abdac1e99b70461385966ba8dd3a5692.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/d3fcddbcffe947a9a2ae9e3486545698.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/5432a15b1e304593bb8e3eee3eb9f894.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/8a2668ace55a44ecbd8ee191bd25d09b.png)
![Insert picture description here](https://img-blog.csdnimg.cn/8ae3f4c972414b10ab8dd21c98f99748.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/ac9c1b1e14b84cab92917f3779e36891.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/ac713cef743e4705a798c7264015d205.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/19fc136b98b647d38b7f768bb27b164e.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)

![Insert picture description here](https://img-blog.csdnimg.cn/672b8897f0a24aacab468e497aca4c96.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/951f84c089a648f0a3fd0a8309489b79.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/85b7bde5a69d40d4a2d10bd07ff4d774.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/9041d9fac4b64bbb93cb714878afbe89.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_19,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/8054da9a93b943e89ff10609a8c50592.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/1e13cf5bae1c49df94402e45a974ed67.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/bb38a7be46ad4e8485c60177f2c3d84c.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/f50767b79d3446ff98712a8d812c9a98.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/9cdda998994f4f239703705354e99877.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/b8ee9c631f524cf7a787ed63bc2651fd.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/d4e7155cfd4341e2898b30aff7efc670.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/fc07ccfaf9824be5a52bc484842d8076.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/72f9005e1c894d0880cab046e046ab0e.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/5f41728375cf42e4bde930de0943a8ce.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/337d027cc51746d18bbca4af6efe56f4.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
There is a problem here. When I use the manager to create a backup device, the creation is normal. When I open the backup device of the server object, it prompts me that I can't open the system file. But I use it T-SQL It is normal to create a statement and then open it. I see that others are normal, only mine SQL There's a problem. I don't know if it's a problem with the old version of the database.
![Insert picture description here](https://img-blog.csdnimg.cn/c9063764c05748b993847ee973a0daf6.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/b827b3e7b48c48a4bbd09b39c1554618.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/89028538e5ca42d0b9098518b77b25b8.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/7247c6f3febc43e9a94e593e2825bdc9.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/de59211e3dbe4e8fb0f606c868003732.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
![Insert picture description here](https://img-blog.csdnimg.cn/8d2ebb5039a141f0bf0677567cb16a9b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA54Gs6YCG5pe26ZKI54Gs6K-66KiA,size_18,color_FFFFFF,t_70,g_se,x_16)
Here's the last one SQL Server 2008 From introduction to proficient Chinese version PDF Information.
Link: https://pan.baidu.com/s/1Lu76FsWLevQipuucimeDSA 
Extraction code: u6pw

Topics: C# Database SQL