SQL Server Express

本页使用了标题或全文手工转换,现处于台湾繁体模式
求聞百科,共筆求聞
Microsoft SQL Server Express
在一台Windows Server 2019VM上安裝SQL Server 2017 Express的畫面縮圖。
在一台Windows Server 2019VM上安裝SQL Server 2017 Express的畫面縮圖。
開發者微軟
穩定版本
SQL Server 2017 Express
(2017年11月6日,​6年前​(2017-11-06
程式語言C語言C++
作業系統Microsoft WindowsLinux
系統平台IA-32IA-64
> 512 MB RAM
.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)的訂閱端。

功能與限制

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

  • 資料庫的大小限制:對SQL Server 2008 R2 Express、SQL Server 2012 Express、SQL Server 2014 Express、SQL Server 2016 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:比標準版本更輕便的資料庫。lightweight version of Express Edition with all of its programmability features, but runs in user mode。從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>

編程介面

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;";                
            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]. 
  2. About the SQL Server License Terms - SQL Server | Microsoft Docs. Msdn.microsoft.com. 2017-05-24 [2019-04-03]. 
  3. Disable SQL Server VSS Writer service. [2019-06-06] (英語). 
  4. Features Supported by the Editions of SQL Server 2008 R2. [2010-06-22]. 
  5. Compare Edition Features. [2019-06-06]. 
  6. SQL Server Express with Advanced Services Features. Microsoft. [2012-03-28]. 
  7. How SQL Server 2005 Express Edition determines the CPU count and uses the CPUs during processing. MSDN. 2008-11 [2008-11-25]. 
  8. The SQL Server 2000 Workload Governor. MSDN. 2004-01 [2007-01-15] (英語). 
  9. Download Microsoft® SQL Server® 2012 Service Pack 1 (SP1) Express from Official Microsoft Download Center. Microsoft.com. 2012-11-09 [2019-04-03]. 
  10. Download Microsoft SQL Server 2005 Express Edition from Official Microsoft Download Center. [2012-12-26]. 
  11. 11.0 11.1 Microsoft Support Lifecycle (SQL Server 2005). [2012-12-26]. 
  12. Microsoft SQL Server 2005 Express Edition. Microsoft Download Center. [2017-04-07]. 
  13. Download Microsoft SQL Server 2008 Express from Official Microsoft Download Center. [2012-12-26]. 
  14. 14.0 14.1 Microsoft Support Lifecycle (SQL Server 2008). [2012-12-26]. 
  15. Download Microsoft® SQL Server® 2008 Express from Official Microsoft Download Center. Microsoft.com. 2009-02-08 [2019-04-03]. 
  16. Download Microsoft SQL Server 2008 R2 - Express from Official Microsoft Download Center. [2012-12-26]. 
  17. 17.0 17.1 Microsoft Support Lifecycle (SQL Server 2008 R2). [2012-12-26]. 
  18. Hardware and Software Requirements for Installing SQL Server 2008 R2. msdn.microsoft.com. [2017-10-16] (英語). 
  19. Download Microsoft® SQL Server® 2012 Express from Official Microsoft Download Center. [2012-12-26]. 
  20. 20.0 20.1 Microsoft Support Lifecycle (SQL Server 2012). [2012-12-26]. 
  21. Hardware and Software Requirements for Installing SQL Server 2012. [2017-06-06]. 
  22. Microsoft® SQL Server® 2012 SP2 Express. Microsoft Download Center. [2017-04-07]. 
  23. Download Microsoft SQL Server 2014 Express. [2014-04-11]. 
  24. 24.0 24.1 Microsoft Support Lifecycle (SQL Server 2014). [2014-04-11]. 
  25. Hardware and Software Requirements for Installing SQL Server 2014. [2017-06-30]. 
  26. Microsoft® SQL Server® 2014 Express. Microsoft Download Center. [2017-04-08]. 
  27. Download Microsoft SQL Server 2016 Express. [2016-07-02]. 
  28. 28.0 28.1 Microsoft Support Lifecycle (SQL Server 2016). [2014-10-29]. 
  29. Download Microsoft SQL Server 2017 Express. [2019-06-06]. 
  30. 30.0 30.1 Microsoft Support Lifecycle (SQL Server 2017). [2019-06-06]. 
  31. Navicat Premium for OSX + SQL Server Express. Navicat Community. 2014-01-08 [2019-07-09] (英語). 

參看

外部連結

Template:MS DevTools