SQL Server Express

本頁使用了標題或全文手工轉換
維基百科,自由的百科全書
Microsoft SQL Server Express
在一台Windows Server 2019VM上安裝SQL Server 2017 Express的畫面縮圖。
在一台Windows Server 2019VM上安裝SQL Server 2017 Express的畫面縮圖。
開發者微軟
編程語言C語言C++
操作系統Microsoft WindowsLinux
平台IA-32IA-64
> 512 MB RAM
.NET Framework 4.0英語.NET Framework 4.0[1]
語言英文、中文、法文、德文、意大利文、日文、韓文、巴西葡文、俄文及西班牙文
類型關係數據庫管理系統
許可協議專有軟件[2]
網站www.microsoft.com/en-us/sql-server/sql-server-editions-express

Microsoft SQL Server Express微軟公司所開發的關係數據庫產品SQL Server的免費下載版本,可自由下載、分發(需經註冊)及使用。這個版本的設計是專門為嵌入式系統或較小型的程式而設計。本產品可遡源至過往隨同SQL Server 2000軟件分發的Microsoft Database Engine (MSDE),用以讓程式開發者在其測試其程式時建立一個系統需求較低的測試環境。自SQL Server 2005開始,本產品改用現時的名稱。

它繼承了多數的SQL Server功能與特性,像是Transact-SQLSQL CLR等,相當適合使用在小型的網站,或者是小型的桌面型應用程式,它也可以和 SQL Server 整合,作為資料庫複製英語Replication_(computing)#DATABASE(Replication)的訂閱端。

功能與限制[編輯]

SQL Server Express提供了付費的完整版本的多項功能[3],然而其技術限制使之無法適用於大規模的部署。這兩個版本的差異在於:

  • 資料庫的大小限制:對SQL Server 2008 R2 Express、SQL Server 2012 Express、SQL Server 2014 Express、SQL Server 2016 Express、SQL Server 2019 Express單個資料庫的大小限制最大為 10 GB[4];而在較早期的SQL Server 2005 Express 和SQL Server 2008 Express 上,單個資料庫的大小限制最大為4 GB。即使如此,亦已比最初MSDE版本的2 GB要多。而且這個大小的限制只有在資料檔案上,交易記錄檔則不受此限。
  • 沒有 SQL Agent服務[5][6]:若要做排程服務必須自己寫程式。
  • 硬體方面的人為限制:
    • 允許在多核心的電腦上使用,但只會使用一個CPU核心來運算[7]。這在具有多核心CPU的電腦上會造成浪費。MSDE 最高可以支援二顆 CPU核心。
    • 可使用的記憶體量最高只有 1GB。而全功能標準版至少使用4GB內存。

MSDE有一個並發工作負債監控器並限制在少量用戶並發訪問,SQL Server Express沒有這個並發限制[8]

SQL Server Express包括了幾個數據庫管理的圖形用戶界面工具:

MSDE 沒有管理介面

SQL Server Express不包括全功能標準版的幾個工具:

SQL Server Express的各版本是可以遠程連接的,但需要用戶做一定的配置。

過往版本[編輯]

Microsoft SQL Server Express Version History
版本號碼 發行日期 主流支援完結日期 延伸支援完結日期 支援作業系統
SQL Server 2005 Express Edition 2005-11-07[10] 2011-04-12[11] 2016-04-12[11] Windows 2000 Service Pack 4, Windows XP Service Pack 2, Windows Server 2003 Service Pack, Windows 7 Service Pack 1 (only SQL Server 2005 Express Edition SP4)[12]
SQL Server 2008 Express 2009-02-08[13] 2014-07-08[14] 2019-07-09[14] Windows XP Service Pack 2, Windows XP Service Pack 3, Windows Vista, Windows Vista Service Pack 1, Windows Server 2003 Service Pack 2, Windows Server 2008[15]
SQL Server 2008 R2 Express 2010-04-16[16] 2014-07-08[17] 2019-07-09[17] Windows XP, Windows Vista, Windows 7, Windows 8, Windows Server 2003, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2[18]
SQL Server 2012 Express 2012-05-14[19] 2017-07-11[20] 2022-07-12[20] Windows Vista Service Pack 2, Windows 7, Windows 7 Service Pack 1, Windows 8, Windows 8.1, Windows Server 2008,[21] Windows Server 2008 R2, Windows Server 2008 R2 SP1, Windows Server 2012, Windows Server 2012 R2[22]
SQL Server 2014 Express 2014-04-01[23] 2019-07-09[24] 2024-07-09[24] Windows 7 Service Pack 1, Windows 8, Windows 8.1, Windows 10, Windows Server 2008 SP2,[25] Windows Server 2008 R2 SP1, Windows Server 2012, Windows Server 2012 R2[26]
SQL Server 2016 Express 2016-06-01[27] 2021-07-13[28] 2026-07-14[28] Windows 8, Windows 8.1, Windows 10, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016
SQL Server 2017 Express 2017-09-29[29] 2022-10-11[30] 2027-10-12[30] Windows 8, Windows 8.1, Windows 10, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016*,
Red Hat Enterprise Linux 7.3 or 7.4, SUSE Enterprise Linux Server v12 SP2, Ubuntu 16.04LTS,
Docker Engine 1.8+ (on Windows, Mac, or Linux)
  • 並不包括 Windows Server 2016 "Essentials" Edition

安裝版本[編輯]

SQL Server Express 2017 或 2019,SQL Server Express有三款安裝版本:

  • 標準的SQL Server Express,或稱Express Core: SQL Server Engine only
  • 高級版本(with Advanced Services),或稱Express Advanced:還包括有Reporting services及全文檢索(Full Text Service),在舊版本均包括在標準安裝版本內。
  • LocalDB:比標準版本更輕便的數據庫。具有全部的編程特性,但在用戶模式下運行。只能在本地連接數據庫。從SQL Server 2012版開始有了LocalDB。在Visual Studio 2019安裝中,帶有SQL Server Express 2016 LocalDB

資料庫管理工具[編輯]

  • Navicat for SQL Server頁面存檔備份,存於網際網路檔案館) 是一套付費軟體,專為 Microsoft SQL Server 設計的強大資料庫管理及開發工具。它可以用於任何版本的 SQL Server 資料庫(包括SQL Server Express[31]),並支援大部份 SQL Server 的功能,包括觸發器、索引、檢視等。

到服務器的連接[編輯]

SQL Server包括Express版本,有3種到數據庫服務器的連接(connection)方式:

  • 共享內存:僅限於本地訪問
  • 命名管道:
  • TCP/IP通信:

其中後兩種需要在SQL Server Configuration Manager軟件中,SQL Server Network Configuration下的「Protocols for SQLEXPRESS」,把Nameed Pipes與TCP/IP手工置為Enable。對於第三種,還需要在上述的配置時,在TCP/IP Properties的IP Addresses中把IPALL的「TCP Port」設為1433.

工具[編輯]

  • SQL Server Management Studio Express,可用來管理 SQL Server Express 實體。
  • SQL Server Reporting Service Express,簡易版的 SQL Server Reporting Service。
  • SQL Server Configuration Manager,可用來管理 SQL Server Express 的連線組態與服務。
  • Business Intelligence Development Studio Express,可用來開發 Reporting Service Express 的報表。
  • 命令行工具sqlcmd,如下:
$sqlcmd -S thisisaserver.com -U username
Password:
1> SELECT name FROM SYSOBJECTS WHERE xtype = 'U'
2> GO
name
--------
EquitySecMstr
(1 rows affected)
1>

LocalDB[編輯]

LocalDB的全稱是Microsoft SQL Server Express LocalDB。

安裝[編輯]

從Microsoft下載的SqlLocalDB.msi約54MB。也可以使用Microsoft SQL Server Express的安裝程序,在第一頁選擇第3項「Download Media」,隨後在下一頁選擇第3項「localDB(54MB)」即可下載。

使用[編輯]

命令行工具SqlLocalDB,用於創建和管理LocalDB的實例[32]

C:\> SqlLocalDb info	
#输出默认实例名字:MSSQLLocalDB			

#创建一个实例(相当于一个server):
C:\> SqlLocalDb create "MyInstance"	
#输出:LocalDB instance "MyInstance" created with version 15.0.2000.5.

C:\> sqllocaldb info "MyInstance"
#输出:
#Name:               MyInstance
#Version:            15.0.2000.5
#Shared name:
#Owner:              MyDomainName\MyUserName
#Auto-create:        No
#State:              Stopped
#Last start time:    2022/3/9 19:04:41
#Instance pipe name:

C:\> sqllocaldb start "MyInstance"
#输出: LocalDB instance "MyInstance" started.
#类似的,可以有:
#C:\> SqlLocalDb stop "MyInstance"
#C:\> SqlLocalDb delete "MyInstance"

系統數據庫文件存儲於本地AppData路徑下,通常為C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\

LocalDB 是在Windows當前用戶的用戶進程中運行。而SQL Server Express是作為Windows Service運行。所以,LocalDB的數據存儲是Windows用戶私有的。在Visual Studio啟動IIS Express也是當前用戶的用戶進程執行,所以可以訪問LocalDB。[33]

把私有的數據存儲變為共享實例。例如,原存儲為「(localdb)\v11.0」:

sqllocaldb share v11.0 IIS_DB

現在為公共名字IIS_DB。該電腦所有用戶均可以用「(localdb)\.\IIS_DB」訪問該實例。注意,名字中的前綴「\.」表示這是共享實例名字。即其保存的文件的路徑不會放在特定用戶的documents目錄中。連接字符串可為:

Data Source=(localdb)\.\IIS_DB;Initial Catalog=OldFashionedDB;Integrated Security=True

在Visual Studio中通過 SQL Server Object Explorer 連接上述localDB實例,在query窗口執行下述腳本可以授予「」賬號數據實例的管理員權限:

create login [IIS APPPOOL\ASP.NET v4.0] from windows;
exec sp_addsrvrolemember N'IIS APPPOOL\ASP.NET v4.0', sysadmin

SQLCMD使用的連接字符串[編輯]

C:\>sqlcmd -S "(localdb)\MyInstance" 就可以在交互環境下執行查詢:

SELECT @@VERSION;
GO
CREATE DATABASE blat;
GO
USE blat;
GO
CREATE TABLE dbo.splunge(Mort int);
GO
INSERT dbo.Splune(Mort) VALUES(1);
SELECT * FROM dbo.splunge;
GO

Microsoft SQL Server Management Studio連接LocalDB[編輯]

Server Name使用(localdb)\MyInstance

編程接口[編輯]

ADO.Net[編輯]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Connection
{
    class Program
    {
        static void Main(string[] args)
        {
            //构造连接字符串
            SqlConnectionStringBuilder connStr = new SqlConnectionStringBuilder();
            connStr.DataSource = @".\SQLEXPRESS";
            connStr.InitialCatalog = "master";
            connStr.IntegratedSecurity = true;

            SqlConnection conn = new SqlConnection();//创建连接对象
            conn.ConnectionString = connStr.ConnectionString;//设置连接字符串

            conn.Open();//打开连接

            if(conn.State == ConnectionState.Open)
            {
                Console.WriteLine("Database is linked.");
                Console.WriteLine("\nDataSource:{0}",conn.DataSource);
                Console.WriteLine("Database:{0}",conn.Database);
                Console.WriteLine("ConnectionTimeOut:{0}",conn.ConnectionTimeout);
            }

            conn.Close();//关闭连接
            conn.Dispose();//释放资源

            if(conn.State == ConnectionState.Closed)
            {
                Console.WriteLine("\nDatabase is closed.");
            }

            Console.Read();
        }
    }
}

對於較高版本,如SQL Server Express大於版本2010的,可以通過在Visual Studio,依次點擊「視圖」(菜單欄)->「服務器資源管理器(Server Explorer)」,進入「服務器資源管理器」窗口。右鍵點擊「數據連接(Data Connection)」,在彈出的上下文菜單中選擇「添加連接(Add Connection)」,在彈出的窗口中將Data Source選擇為Microsoft SQL Server,「服務器名」設置為(LocalDb)\MSSQLLocalDB,附加服務器文件為具體的mdf文件並點擊確定。此時,通過Microsoft SQL Server(SQL Client)方式訪問指定的mdf文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
            connection.ConnectionString =
                @"Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=D:\apps\Documents\NORTHWND.mdf;Integrated Security = True;Connect Timeout = 30;";
              //@"Data Source=(localdb)\MyInstance;InitialCatalog =blat;Integrated Security = True;Connect Timeout = 30;";
            connection.Open();
            if (connection.State == ConnectionState.Open)
            {
                Console.WriteLine("Database is linked.");
                Console.WriteLine("DataSource:{0}", connection.DataSource);
                Console.WriteLine("Database:{0}", connection.Database);
                Console.WriteLine("ConnectionTimeOut:{0}", connection.ConnectionTimeout);
            }
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Employees", connection);
            DataTable table = new DataTable();
            adapter.Fill(table);
            var result = (from row
                          in table.Rows.Cast<DataRow>()
                          select
                          new { ID = (int)row[0], LastName = (string)row[1], FirstName = (string)row[2] });
            result.ToList().ForEach(x => Console.WriteLine($"{x.ID},{x.LastName},{x.FirstName}"));
        }
    }
}

對於較低版本,如SQL Server Express 2005的mdf文件,可以直接用如下connection string打開訪問(可能需要administrator權限):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
            connection.ConnectionString =
                @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\apps\Documents\NORTHWND.mdf;Integrated Security = True;Connect Timeout = 30;";                
            connection.Open();
            if (connection.State == ConnectionState.Open)
            {
                Console.WriteLine("Database is linked.");
                Console.WriteLine("DataSource:{0}", connection.DataSource);
                Console.WriteLine("Database:{0}", connection.Database);
                Console.WriteLine("ConnectionTimeOut:{0}", connection.ConnectionTimeout);
            }
        }
    }
}

Python[編輯]

需要安裝pymssql包

from sqlalchemy import create_engine
import pandas as pd
 
user = 'username'
pwd = 'password'
url = 'thisisaserver.com'
port = 1433
db = 'database'
engine = create_engine(f'mssql+pymssql://{user}:{pwd}@{url}:{port}/{db}')

sql_stmt = "SELECT name FROM SYSOBJECTS WHERE xtype = 'U'"
pd.read_sql_query(sql_stmt, engine)

參考資料[編輯]

  1. ^ Hardware and Software Requirements for Installing SQL Server 2014 - SQL Server | Microsoft Docs. Msdn.microsoft.com. [2019-04-03]. (原始內容存檔於2017-05-27). 
  2. ^ About the SQL Server License Terms - SQL Server | Microsoft Docs. Msdn.microsoft.com. 2017-05-24 [2019-04-03]. (原始內容存檔於2017-12-11). 
  3. ^ Disable SQL Server VSS Writer service. [2019-06-06]. (原始內容存檔於2017-09-28) (英語). 
  4. ^ Features Supported by the Editions of SQL Server 2008 R2. [2010-06-22]. (原始內容存檔於2010-09-27). 
  5. ^ Compare Edition Features. [2019-06-06]. (原始內容存檔於2011-07-17). 
  6. ^ SQL Server Express with Advanced Services Features. Microsoft. [28 March 2012]. (原始內容存檔於2016-03-17). 
  7. ^ How SQL Server 2005 Express Edition determines the CPU count and uses the CPUs during processing. MSDN. November 2008 [2008-11-25]. (原始內容存檔於2008-12-11). 
  8. ^ The SQL Server 2000 Workload Governor. MSDN. 2004-01 [2007-01-15]. (原始內容存檔於2007-04-23) (英語). 
  9. ^ Download Microsoft® SQL Server® 2012 Service Pack 1 (SP1) Express from Official Microsoft Download Center. Microsoft.com. 2012-11-09 [2019-04-03]. (原始內容存檔於2019-04-03). 
  10. ^ Download Microsoft SQL Server 2005 Express Edition from Official Microsoft Download Center. [26 December 2012]. (原始內容存檔於2013-03-07). 
  11. ^ 11.0 11.1 Microsoft Support Lifecycle (SQL Server 2005). [26 December 2012]. (原始內容存檔於2013-04-04). 
  12. ^ Microsoft SQL Server 2005 Express Edition. Microsoft Download Center. [2017-04-07]. (原始內容存檔於2017-04-08). 
  13. ^ Download Microsoft SQL Server 2008 Express from Official Microsoft Download Center. [26 December 2012]. (原始內容存檔於2013-03-06). 
  14. ^ 14.0 14.1 Microsoft Support Lifecycle (SQL Server 2008). [26 December 2012]. (原始內容存檔於2013-04-03). 
  15. ^ Download Microsoft® SQL Server® 2008 Express from Official Microsoft Download Center. Microsoft.com. 2009-02-08 [2019-04-03]. (原始內容存檔於2019-04-03). 
  16. ^ Download Microsoft SQL Server 2008 R2 - Express from Official Microsoft Download Center. [26 December 2012]. (原始內容存檔於2013-03-05). 
  17. ^ 17.0 17.1 Microsoft Support Lifecycle (SQL Server 2008 R2). [26 December 2012]. (原始內容存檔於2013-04-04). 
  18. ^ Hardware and Software Requirements for Installing SQL Server 2008 R2. msdn.microsoft.com. [2017-10-16]. (原始內容存檔於2017-10-16) (英語). 
  19. ^ Download Microsoft® SQL Server® 2012 Express from Official Microsoft Download Center. [26 December 2012]. (原始內容存檔於2013-03-07). 
  20. ^ 20.0 20.1 Microsoft Support Lifecycle (SQL Server 2012). [26 December 2012]. (原始內容存檔於2013-04-04). 
  21. ^ Hardware and Software Requirements for Installing SQL Server 2012. [6 June 2017]. (原始內容存檔於2017-11-11). 
  22. ^ Microsoft® SQL Server® 2012 SP2 Express. Microsoft Download Center. [2017-04-07]. (原始內容存檔於2017-04-08). 
  23. ^ Download Microsoft SQL Server 2014 Express. [11 April 2014]. (原始內容存檔於2014-04-13). 
  24. ^ 24.0 24.1 Microsoft Support Lifecycle (SQL Server 2014). [11 April 2014]. (原始內容存檔於2014-07-23). 
  25. ^ Hardware and Software Requirements for Installing SQL Server 2014. [30 June 2017]. (原始內容存檔於2017-05-27). 
  26. ^ Microsoft® SQL Server® 2014 Express. Microsoft Download Center. [2017-04-08]. (原始內容存檔於2017-05-04). 
  27. ^ Download Microsoft SQL Server 2016 Express. [2016-07-02]. (原始內容存檔於2016-08-09). 
  28. ^ 28.0 28.1 Microsoft Support Lifecycle (SQL Server 2016). [2014-10-29]. (原始內容存檔於2014-07-23). 
  29. ^ Download Microsoft SQL Server 2017 Express. [2019-06-06]. (原始內容存檔於2019-06-17). 
  30. ^ 30.0 30.1 Microsoft Support Lifecycle (SQL Server 2017). [2019-06-06]. (原始內容存檔於2019-01-19). 
  31. ^ Navicat Premium for OSX + SQL Server Express. Navicat Community. 2014-01-08 [2019-07-09]. (原始內容存檔於2019-07-09) (英語). 
  32. ^ SqlLocalDB Utility in Microsoft Docs. [2022-03-10]. (原始內容存檔於2021-12-14). 
  33. ^ Using LocalDB with Full IIS, Part 2: Instance Ownership, source from MSDN. [2022-07-29]. (原始內容存檔於2022-07-29). 

參看[編輯]

外部連結[編輯]