debian/ubuntu安装mssql
<p>添加源:</p><p>debian源:deb https://packages.microsoft.com/debian/10/prod buster main</p>
<p>ubuntu源:deb https://packages.microsoft.com/ubuntu/19.04/prod disco main</p>
<p>apt install multiarch-support</p>
<p>wget http://ftp.cn.debian.org/debian/pool/main/j/jemalloc/libjemalloc1_3.6.0-3_amd64.deb</p>
<p>wget http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.0.0_1.0.1t-1+deb8u11_amd64.deb</p>
<p>dpkg -i libjemalloc1_3.6.0-3_amd64.deb libssl1.0.0_1.0.1t-1+deb8u11_amd64.deb</p>
<div style="color: rgba(0, 0, 0, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: "Segoe UI", SegoeUI, "Segoe WP", "Helvetica Neue", Helvetica, Tahoma, Arial, sans-serif; font-size: 16px; font-style: normal; font-weight: 400; word-spacing: 0; white-space: normal; box-sizing: inherit; orphans: 2; widows: 2; background-color: rgba(255, 255, 255, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial" data-moniker="sql-server-linux-2017 sql-server-2017"> </div>
<h2 id="install"><span data-ttu-id="de01d-118">安装 SQL Server<span class="sxs-lookup"><span data-stu-id="de01d-118">Install SQL Server</span></span></span></h2>
<p><span data-ttu-id="de01d-119">要在 Ubuntu 上配置 SQL Server,请在终端中运行以下命令以安装 mssql-server 包 。<span class="sxs-lookup"><span data-stu-id="de01d-119">To configure SQL Server on Ubuntu, run the following commands in a terminal to install the <strong>mssql-server</strong> package.</span></span></span></p>
<ol>
<li>
<p><span data-ttu-id="de01d-120">导入公共存储库 GPG 密钥:<span class="sxs-lookup"><span data-stu-id="de01d-120">Import the public repository GPG keys:</span></span></span></p>
<div id="code-try-0" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
">wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
</code></pre>
</li>
<li>
<p><span data-ttu-id="de01d-121">注册 Microsoft SQL Server Ubuntu 存储库:<span class="sxs-lookup"><span data-stu-id="de01d-121">Register the Microsoft SQL Server Ubuntu repository:</span></span></span></p>
<div id="code-try-1" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list)"
">sudo add-apt-repository <span class="hljs-string">"<span class="hljs-variable">$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list)"
</span></span></code></pre>
<div class="alert is-success">
<p class="alert-title"><span class="docon docon-lightbulb"> 提示</span></p>
<p><span data-ttu-id="de01d-122">如果想试用 SQL Server 2019,需改为注册预览版 (2019) 存储库 。<span class="sxs-lookup"><span data-stu-id="de01d-122">If you want to try SQL Server 2019 , you must instead register the <strong>Preview (2019)</strong> repository. <span data-ttu-id="de01d-123">使用以下命令安装 SQL Server 2019:<span class="sxs-lookup"><span data-stu-id="de01d-123">Use the following command for SQL Server 2019 installations:</span></span></span></span></span></span></p>
<div id="code-try-2" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-preview.list)"
">sudo add-apt-repository <span class="hljs-string">"<span class="hljs-variable">$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-preview.list)"
</span></span></code></pre>
</div>
</li>
<li>
<p><span data-ttu-id="de01d-124">运行以下命令以安装 SQL Server:<span class="sxs-lookup"><span data-stu-id="de01d-124">Run the following commands to install SQL Server:</span></span></span></p>
<div id="code-try-3" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sudo apt-get update
sudo apt-get install -y mssql-server
">sudo apt-get update
sudo apt-get install -y mssql-server
</code></pre>
</li>
<li>
<p><span data-ttu-id="de01d-125">包安装完成后,运行 <strong>mssql-conf setup</strong>,按照提示设置 SA 密码并选择版本。<span class="sxs-lookup"><span data-stu-id="de01d-125">After the package installation finishes, run <strong>mssql-conf setup</strong> and follow the prompts to set the SA password and choose your edition.</span></span></span></p>
<div id="code-try-4" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sudo /opt/mssql/bin/mssql-conf setup
">sudo /opt/mssql/bin/mssql-conf setup
</code></pre>
<div class="alert is-success">
<p class="alert-title"><span class="docon docon-lightbulb"> 提示</span></p>
<p><span data-ttu-id="de01d-126">以下 SQL Server 2017 版本是免费提供许可的:Evaluation、Developer 和 Express 版。<span class="sxs-lookup"><span data-stu-id="de01d-126">The following SQL Server 2017 editions are freely licensed: Evaluation, Developer, and Express.</span></span></span></p>
</div>
<div class="alert is-info">
<p class="alert-title"><span class="docon docon-status-error-outline"> 备注</span></p>
<p><span data-ttu-id="de01d-127">请确保为 SA 帐户指定强密码(最少 8 个字符,包括大写和小写字母、十进制数字和/或非字母数字符号)。<span class="sxs-lookup"><span data-stu-id="de01d-127">Make sure to specify a strong password for the SA account (Minimum length 8 characters, including uppercase and lowercase letters, base 10 digits and/or non-alphanumeric symbols).</span></span></span></p>
</div>
</li>
<li>
<p><span data-ttu-id="de01d-128">完成配置后,验证服务是否正在运行:<span class="sxs-lookup"><span data-stu-id="de01d-128">Once the configuration is done, verify that the service is running:</span></span></span></p>
<div id="code-try-5" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="systemctl status mssql-server --no-pager
">systemctl status mssql-server --no-pager
</code></pre>
</li>
<li>
<p><span data-ttu-id="de01d-129">如果计划远程连接,可能还需要在防火墙上打开 SQL Server TCP 端口(默认值为 1433)。<span class="sxs-lookup"><span data-stu-id="de01d-129">If you plan to connect remotely, you might also need to open the SQL Server TCP port (default 1433) on your firewall.</span></span></span></p>
</li>
</ol>
<p><span data-ttu-id="de01d-130">此时,SQL Server 已在 Ubuntu 计算机上运行,随时可以使用!<span class="sxs-lookup"><span data-stu-id="de01d-130">At this point, SQL Server is running on your Ubuntu machine and is ready to use!</span></span></span></p>
<p></p>
<div data-moniker="sql-server-linux-ver15 sql-server-ver15 sqlallproducts-allversions">
<h2 id="---sql-server" data-id="install"><span data-ttu-id="de01d-131">安装 SQL Server<span class="sxs-lookup"><span data-stu-id="de01d-131">Install SQL Server</span></span></span></h2>
<p><span data-ttu-id="de01d-132">要在 Ubuntu 上配置 SQL Server,请在终端中运行以下命令以安装 mssql-server 包 。<span class="sxs-lookup"><span data-stu-id="de01d-132">To configure SQL Server on Ubuntu, run the following commands in a terminal to install the <strong>mssql-server</strong> package.</span></span></span></p>
<ol>
<li>
<p><span data-ttu-id="de01d-133">导入公共存储库 GPG 密钥:<span class="sxs-lookup"><span data-stu-id="de01d-133">Import the public repository GPG keys:</span></span></span></p>
<div id="code-try-6" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
">wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
</code></pre>
</li>
<li>
<p><span data-ttu-id="de01d-134">为 SQL Server 2019(预览版)注册 Microsoft SQL Server Ubuntu 存储库:<span class="sxs-lookup"><span data-stu-id="de01d-134">Register the Microsoft SQL Server Ubuntu repository for SQL Server 2019 preview:</span></span></span></p>
<div id="code-try-7" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-preview.list)"
">sudo add-apt-repository <span class="hljs-string">"<span class="hljs-variable">$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-preview.list)"
</span></span></code></pre>
</li>
<li>
<p><span data-ttu-id="de01d-135">运行以下命令,安装 SQL Server:<span class="sxs-lookup"><span data-stu-id="de01d-135">Run the following commands to install SQL Server:</span></span></span></p>
<div id="code-try-8" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sudo apt-get update
sudo apt-get install -y mssql-server
">sudo apt-get update
sudo apt-get install -y mssql-server
</code></pre>
</li>
<li>
<p><span data-ttu-id="de01d-136">包安装完成后,运行 <strong>mssql-conf setup</strong>,按照提示设置 SA 密码并选择版本。<span class="sxs-lookup"><span data-stu-id="de01d-136">After the package installation finishes, run <strong>mssql-conf setup</strong> and follow the prompts to set the SA password and choose your edition.</span></span></span></p>
<div id="code-try-9" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sudo /opt/mssql/bin/mssql-conf setup
">sudo /opt/mssql/bin/mssql-conf setup
</code></pre>
<div class="alert is-info">
<p class="alert-title"><span class="docon docon-status-error-outline"> 备注</span></p>
<p><span data-ttu-id="de01d-137">请确保为 SA 帐户指定强密码(最少 8 个字符,包括大写和小写字母、十进制数字和/或非字母数字符号)。<span class="sxs-lookup"><span data-stu-id="de01d-137">Make sure to specify a strong password for the SA account (Minimum length 8 characters, including uppercase and lowercase letters, base 10 digits and/or non-alphanumeric symbols).</span></span></span></p>
</div>
</li>
<li>
<p><span data-ttu-id="de01d-138">完成配置后,验证服务是否正在运行:<span class="sxs-lookup"><span data-stu-id="de01d-138">Once the configuration is done, verify that the service is running:</span></span></span></p>
<div id="code-try-10" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="systemctl status mssql-server --no-pager
">systemctl status mssql-server --no-pager
</code></pre>
</li>
<li>
<p><span data-ttu-id="de01d-139">如果计划远程连接,可能还需要在防火墙上打开 SQL Server TCP 端口(默认值为 1433)。<span class="sxs-lookup"><span data-stu-id="de01d-139">If you plan to connect remotely, you might also need to open the SQL Server TCP port (default 1433) on your firewall.</span></span></span></p>
</li>
</ol>
<p><span data-ttu-id="de01d-140">此时,SQL Server 2019(预览版)已在 Ubuntu 计算机上运行,随时可以使用!<span class="sxs-lookup"><span data-stu-id="de01d-140">At this point, SQL Server 2019 preview is running on your Ubuntu machine and is ready to use!</span></span></span></p>
</div>
<h2 id="tools"><span data-ttu-id="de01d-141">安装 SQL Server 命令行工具<span class="sxs-lookup"><span data-stu-id="de01d-141">Install the SQL Server command-line tools</span></span></span></h2>
<p><span data-ttu-id="de01d-142">若要创建数据库,则需要使用可在 SQL Server 上运行 Transact-SQL 语句的工具进行连接。<span class="sxs-lookup"><span data-stu-id="de01d-142">To create a database, you need to connect with a tool that can run Transact-SQL statements on the SQL Server. <span data-ttu-id="de01d-143">以下步骤安装 SQL Server 命令行工具:sqlcmd 和 bcp。<span class="sxs-lookup"><span data-stu-id="de01d-143">The following steps install the SQL Server command-line tools: sqlcmd and bcp.</span></span></span></span></span></span></p>
<p><span data-ttu-id="de01d-144">通过下列步骤在 Ubuntu 上安装 mssql-tools 。<span class="sxs-lookup"><span data-stu-id="de01d-144">Use the following steps to install the <strong>mssql-tools</strong> on Ubuntu.</span></span></span></p>
<ol>
<li>
<p><span data-ttu-id="de01d-145">导入公共存储库 GPG 密钥。<span class="sxs-lookup"><span data-stu-id="de01d-145">Import the public repository GPG keys.</span></span></span></p>
<div id="code-try-11" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
">curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
</code></pre>
</li>
<li>
<p><span data-ttu-id="de01d-146">注册 Microsoft Ubuntu 存储库。<span class="sxs-lookup"><span data-stu-id="de01d-146">Register the Microsoft Ubuntu repository.</span></span></span></p>
<div id="code-try-12" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
">curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
</code></pre>
</li>
<li>
<p><span data-ttu-id="de01d-147">更新源列表,并使用 unixODBC 开发人员包运行安装命令。<span class="sxs-lookup"><span data-stu-id="de01d-147">Update the sources list and run the installation command with the unixODBC developer package.</span></span></span></p>
<div id="code-try-13" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sudo apt-get update
sudo apt-get install mssql-tools unixodbc-dev
">sudo apt-get update
sudo apt-get install mssql-tools unixodbc-dev
</code></pre>
<div class="alert is-info">
<p class="alert-title"><span class="docon docon-status-error-outline"> 备注</span></p>
<p><span data-ttu-id="de01d-148">若要将 mssql-tools 更新至最新版本,请运行以下命令 :<span class="sxs-lookup"><span data-stu-id="de01d-148">To update to the latest version of <strong>mssql-tools</strong> run the following commands:</span></span></span></p>
<div id="code-try-14" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sudo apt-get update
sudo apt-get install mssql-tools
">sudo apt-get update
sudo apt-get install mssql-tools
</code></pre>
</div>
</li>
<li>
<p><span data-ttu-id="de01d-149"><strong>可选</strong>:向 bash shell 中的 PATH 环境变量添加 <code>/opt/mssql-tools/bin/</code> 。<span class="sxs-lookup"><span data-stu-id="de01d-149"><strong>Optional</strong>: Add <code>/opt/mssql-tools/bin/</code> to your <strong>PATH</strong> environment variable in a bash shell.</span></span></span></p>
<p><span data-ttu-id="de01d-150">要使 sqlcmd/bcp 能从登陆会话的 bash shell 进行访问,请使用下列命令修改 ~/.bash_profile 文件中的 PATH :<span class="sxs-lookup"><span data-stu-id="de01d-150">To make <strong>sqlcmd/bcp</strong> accessible from the bash shell for login sessions, modify your <strong>PATH</strong> in the <strong>~/.bash_profile</strong> file with the following command:</span></span></span></p>
<div id="code-try-15" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
"><span class="hljs-built_in">echo <span class="hljs-string">'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
</span></span></code></pre>
<p><span data-ttu-id="de01d-151">要使 sqlcmd/bcp 能从交互式/非登录会话的 bash shell 进行访问,请使用下列命令修改 ~/.bashrc 文件中的 PATH :<span class="sxs-lookup"><span data-stu-id="de01d-151">To make <strong>sqlcmd/bcp</strong> accessible from the bash shell for interactive/non-login sessions, modify the <strong>PATH</strong> in the <strong>~/.bashrc</strong> file with the following command:</span></span></span></p>
<div id="code-try-16" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
"><span class="hljs-built_in">echo <span class="hljs-string">'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
<span class="hljs-built_in">source ~/.bashrc
</span></span></span></code></pre>
</li>
</ol>
<h2 id="connect-locally"><span data-ttu-id="76492-101">本地连接<span class="sxs-lookup"><span data-stu-id="76492-101">Connect locally</span></span></span></h2>
<p><span data-ttu-id="76492-102">以下步骤使用 sqlcmd 本地连接到新的 SQL Server 实例。<span class="sxs-lookup"><span data-stu-id="76492-102">The following steps use <strong>sqlcmd</strong> to locally connect to your new SQL Server instance.</span></span></span></p>
<ol>
<li>
<p><span data-ttu-id="76492-103">使用 SQL Server 名称 (-S),用户名 (-U) 和密码 (-P) 的参数运行 sqlcmd 。<span class="sxs-lookup"><span data-stu-id="76492-103">Run <strong>sqlcmd</strong> with parameters for your SQL Server name (-S), the user name (-U), and the password (-P). <span data-ttu-id="76492-104">在本教程中,用户进行本地连接,因此服务器名称为 <code>localhost</code>。<span class="sxs-lookup"><span data-stu-id="76492-104">In this tutorial, you are connecting locally, so the server name is <code>localhost</code>. <span data-ttu-id="76492-105">用户名为 <code>SA</code>,密码是在安装过程中为 SA 帐户提供的密码。<span class="sxs-lookup"><span data-stu-id="76492-105">The user name is <code>SA</code> and the password is the one you provided for the SA account during setup.</span></span></span></span></span></span></span></span></span></p>
<div id="code-try-17" class="codeHeader" data-bi-name="code-header"><span class="language">bash <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-bash" data-author-content="sqlcmd -S localhost -U SA -P '<YourPassword>'
">sqlcmd -S localhost -U SA -P <span class="hljs-string">'<YourPassword>'
</span></code></pre>
<div class="alert is-success">
<p class="alert-title"><span class="docon docon-lightbulb"> 提示</span></p>
<p><span data-ttu-id="76492-106">可以在命令行上省略密码,以收到密码输入提示。<span class="sxs-lookup"><span data-stu-id="76492-106">You can omit the password on the command line to be prompted to enter it.</span></span></span></p>
</div>
<div class="alert is-success">
<p class="alert-title"><span class="docon docon-lightbulb"> 提示</span></p>
<p><span data-ttu-id="76492-107">如果以后决定进行远程连接,请指定 -S 参数的计算机名称或 IP 地址,并确保防火墙上的端口 1433 已打开。<span class="sxs-lookup"><span data-stu-id="76492-107">If you later decide to connect remotely, specify the machine name or IP address for the <strong>-S</strong> parameter, and make sure port 1433 is open on your firewall.</span></span></span></p>
</div>
</li>
<li>
<p><span data-ttu-id="76492-108">如果成功,应会显示 sqlcmd 命令提示符:<code>1></code>。<span class="sxs-lookup"><span data-stu-id="76492-108">If successful, you should get to a <strong>sqlcmd</strong> command prompt: <code>1></code>.</span></span></span></p>
</li>
<li>
<p><span data-ttu-id="76492-109">如果连接失败,先尝试诊断错误消息中所述的问题。<span class="sxs-lookup"><span data-stu-id="76492-109">If you get a connection failure, first attempt to diagnose the problem from the error message. <span data-ttu-id="76492-110">然后查看连接故障排除建议。<span class="sxs-lookup"><span data-stu-id="76492-110">Then review the connection troubleshooting recommendations.</span></span></span></span></span></span></p>
</li>
</ol>
<h2 id="create-and-query-data"><span data-ttu-id="76492-111">创建和查询数据<span class="sxs-lookup"><span data-stu-id="76492-111">Create and query data</span></span></span></h2>
<p><span data-ttu-id="76492-112">下面各部分将逐步介绍如何使用 sqlcmd 新建数据库、添加数据并运行简单查询。<span class="sxs-lookup"><span data-stu-id="76492-112">The following sections walk you through using <strong>sqlcmd</strong> to create a new database, add data, and run a simple query.</span></span></span></p>
<h3 id="create-a-new-database"><span data-ttu-id="76492-113">新建数据库<span class="sxs-lookup"><span data-stu-id="76492-113">Create a new database</span></span></span></h3>
<p><span data-ttu-id="76492-114">以下步骤创建一个名为 <code>TestDB</code> 的新数据库。<span class="sxs-lookup"><span data-stu-id="76492-114">The following steps create a new database named <code>TestDB</code>.</span></span></span></p>
<ol>
<li>
<p><span data-ttu-id="76492-115">在 sqlcmd 命令提示符中,粘贴以下 Transact-SQL 命令以创建测试数据库:<span class="sxs-lookup"><span data-stu-id="76492-115">From the <strong>sqlcmd</strong> command prompt, paste the following Transact-SQL command to create a test database:</span></span></span></p>
<div id="code-try-18" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="CREATE DATABASE TestDB
"><span class="hljs-keyword">CREATE <span class="hljs-keyword">DATABASE TestDB
</span></span></code></pre>
</li>
<li>
<p><span data-ttu-id="76492-116">在下一行中,编写一个查询以返回服务器上所有数据库的名称:<span class="sxs-lookup"><span data-stu-id="76492-116">On the next line, write a query to return the name of all of the databases on your server:</span></span></span></p>
<div id="code-try-19" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="SELECT Name from sys.Databases
"><span class="hljs-keyword">SELECT <span class="hljs-keyword">Name <span class="hljs-keyword">from sys.Databases
</span></span></span></code></pre>
</li>
<li>
<p><span data-ttu-id="76492-117">前两个命令没有立即执行。<span class="sxs-lookup"><span data-stu-id="76492-117">The previous two commands were not executed immediately. <span data-ttu-id="76492-118">必须在新行中键入 <code>GO</code> 才能执行以前的命令:<span class="sxs-lookup"><span data-stu-id="76492-118">You must type <code>GO</code> on a new line to execute the previous commands:</span></span></span></span></span></span></p>
<div id="code-try-20" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="GO
">GO
</code></pre>
</li>
</ol>
<div class="alert is-success">
<p class="alert-title"><span class="docon docon-lightbulb"> 提示</span></p>
<p><span data-ttu-id="76492-119">若要详细了解如何编写 Transact-SQL 语句和查询,请参阅教程:编写 Transact-SQL 语句。<span class="sxs-lookup"><span data-stu-id="76492-119">To learn more about writing Transact-SQL statements and queries, see Tutorial: Writing Transact-SQL Statements.</span></span></span></p>
</div>
<h3 id="insert-data"><span data-ttu-id="76492-120">插入数据<span class="sxs-lookup"><span data-stu-id="76492-120">Insert data</span></span></span></h3>
<p><span data-ttu-id="76492-121">接下来创建一个新表 <code>Inventory</code>,然后插入两个新行。<span class="sxs-lookup"><span data-stu-id="76492-121">Next create a new table, <code>Inventory</code>, and insert two new rows.</span></span></span></p>
<ol>
<li>
<p><span data-ttu-id="76492-122">在 sqlcmd 命令提示符中,将上下文切换到新的 <code>TestDB</code> 数据库:<span class="sxs-lookup"><span data-stu-id="76492-122">From the <strong>sqlcmd</strong> command prompt, switch context to the new <code>TestDB</code> database:</span></span></span></p>
<div id="code-try-21" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="USE TestDB
"><span class="hljs-keyword">USE TestDB
</span></code></pre>
</li>
<li>
<p><span data-ttu-id="76492-123">创建名为 <code>Inventory</code> 的新表:<span class="sxs-lookup"><span data-stu-id="76492-123">Create new table named <code>Inventory</code>:</span></span></span></p>
<div id="code-try-22" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
"><span class="hljs-keyword">CREATE <span class="hljs-keyword">TABLE Inventory (<span class="hljs-keyword">id <span class="hljs-built_in">INT, <span class="hljs-keyword">name <span class="hljs-keyword">NVARCHAR(<span class="hljs-number">50), quantity <span class="hljs-built_in">INT)
</span></span></span></span></span></span></span></span></code></pre>
</li>
<li>
<p><span data-ttu-id="76492-124">将数据插入新表:<span class="sxs-lookup"><span data-stu-id="76492-124">Insert data into the new table:</span></span></span></p>
<div id="code-try-23" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
"><span class="hljs-keyword">INSERT <span class="hljs-keyword">INTO Inventory <span class="hljs-keyword">VALUES (<span class="hljs-number">1, <span class="hljs-string">'banana', <span class="hljs-number">150); <span class="hljs-keyword">INSERT <span class="hljs-keyword">INTO Inventory <span class="hljs-keyword">VALUES (<span class="hljs-number">2, <span class="hljs-string">'orange', <span class="hljs-number">154);
</span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
</li>
<li>
<p><span data-ttu-id="76492-125">要执行上述命令的类型 <code>GO</code>:<span class="sxs-lookup"><span data-stu-id="76492-125">Type <code>GO</code> to execute the previous commands:</span></span></span></p>
<div id="code-try-24" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="GO
">GO
</code></pre>
</li>
</ol>
<h3 id="select-data"><span data-ttu-id="76492-126">选择数据<span class="sxs-lookup"><span data-stu-id="76492-126">Select data</span></span></span></h3>
<p><span data-ttu-id="76492-127">现在,运行查询以从 <code>Inventory</code> 表返回数据。<span class="sxs-lookup"><span data-stu-id="76492-127">Now, run a query to return data from the <code>Inventory</code> table.</span></span></span></p>
<ol>
<li>
<p><span data-ttu-id="76492-128">通过 sqlcmd 命令提示符输入查询,以返回 <code>Inventory</code> 表中数量大于 152 的行:<span class="sxs-lookup"><span data-stu-id="76492-128">From the <strong>sqlcmd</strong> command prompt, enter a query that returns rows from the <code>Inventory</code> table where the quantity is greater than 152:</span></span></span></p>
<div id="code-try-25" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="SELECT * FROM Inventory WHERE quantity > 152;
"><span class="hljs-keyword">SELECT * <span class="hljs-keyword">FROM Inventory <span class="hljs-keyword">WHERE quantity > <span class="hljs-number">152;
</span></span></span></span></code></pre>
</li>
<li>
<p><span data-ttu-id="76492-129">执行命令:<span class="sxs-lookup"><span data-stu-id="76492-129">Execute the command:</span></span></span></p>
<div id="code-try-26" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="GO
">GO
</code></pre>
</li>
</ol>
<h3 id="exit-the-sqlcmd-command-prompt"><span data-ttu-id="76492-130">退出 sqlcmd 命令提示符<span class="sxs-lookup"><span data-stu-id="76492-130">Exit the sqlcmd command prompt</span></span></span></h3>
<p><span data-ttu-id="76492-131">要结束 sqlcmd 会话,请键入 <code>QUIT</code>:<span class="sxs-lookup"><span data-stu-id="76492-131">To end your <strong>sqlcmd</strong> session, type <code>QUIT</code>:</span></span></span></p>
<div id="code-try-27" class="codeHeader" data-bi-name="code-header"><span class="language">SQL <button class="action" data-bi-name="copy"><span class="docon docon-edit-copy"> 复制 </span></button></span></div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="QUIT
">QUIT<br><br></code></pre>
<h2 id="performance-best-practices"><span data-ttu-id="76492-132">性能最佳做法<span class="sxs-lookup"><span data-stu-id="76492-132">Performance best practices</span></span></span></h2>
<p><span data-ttu-id="76492-133">在 Linux 上安装 SQL Server 后,请查看配置 Linux 和 SQL Server 以提高生产性能的最佳做法。<span class="sxs-lookup"><span data-stu-id="76492-133">After installing SQL Server on Linux, review the best practices for configuring Linux and SQL Server to improve performance for production scenarios. <span data-ttu-id="76492-134">有关详细信息,请参阅 Linux 上的 SQL Server 的性能最佳做法和配置指南。<span class="sxs-lookup"><span data-stu-id="76492-134">For more information, see Performance best practices and configuration guidelines for SQL Server on Linux.</span></span></span></span></span></span></p>
<h2 id="cross-platform-data-tools"><span data-ttu-id="76492-135">跨平台数据工具<span class="sxs-lookup"><span data-stu-id="76492-135">Cross-platform data tools</span></span></span></h2>
<p><span data-ttu-id="76492-136">除“sqlcmd”以外,还可以使用以下跨平台工具来管理 SQL Server :<span class="sxs-lookup"><span data-stu-id="76492-136">In addition to <strong>sqlcmd</strong>, you can use the following cross-platform tools to manage SQL Server:</span></span></span></p>
<div class="table-scroll-wrapper">
<table>
<thead>
<tr><th> </th><th> </th></tr>
</thead>
<tbody>
<tr>
<td><span data-ttu-id="76492-137">Azure Data Studio<span class="sxs-lookup"><span data-stu-id="76492-137">Azure Data Studio</span></span></span></td>
<td><span data-ttu-id="76492-138">跨平台 GUI 数据库管理实用程序。<span class="sxs-lookup"><span data-stu-id="76492-138">A cross-platform GUI database management utility.</span></span></span></td>
</tr>
<tr>
<td><span data-ttu-id="76492-139">Visual Studio Code<span class="sxs-lookup"><span data-stu-id="76492-139">Visual Studio Code</span></span></span></td>
<td><span data-ttu-id="76492-140">一种跨平台 GUI 代码编辑器,它使用 mssql 扩展运行 Transact-SQL 语句。<span class="sxs-lookup"><span data-stu-id="76492-140">A cross-platform GUI code editor that run Transact-SQL statements with the mssql extension.</span></span></span></td>
</tr>
<tr>
<td><span data-ttu-id="76492-141">PowerShell Core<span class="sxs-lookup"><span data-stu-id="76492-141">PowerShell Core</span></span></span></td>
<td><span data-ttu-id="76492-142">基于 cmdlet 的跨平台自动化和配置工具。<span class="sxs-lookup"><span data-stu-id="76492-142">A cross-platform automation and configuration tool based on cmdlets.</span></span></span></td>
</tr>
<tr>
<td><span data-ttu-id="76492-143">mssql-cli<span class="sxs-lookup"><span data-stu-id="76492-143">mssql-cli</span></span></span></td>
<td><span data-ttu-id="76492-144">用于运行 Transact-SQL 命令的跨平台命令行接口。<span class="sxs-lookup"><span data-stu-id="76492-144">A cross-platform command-line interface for running Transact-SQL commands.</span></span></span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="connecting-from-windows"><span data-ttu-id="76492-145">从 Windows 连接<span class="sxs-lookup"><span data-stu-id="76492-145">Connecting from Windows</span></span></span></h2>
<p><span data-ttu-id="76492-146">Windows 上的 SQL Server 工具连接到 Linux 上的 SQL Server 实例,操作方式与连接到任何远程 SQL Server 实例一样。<span class="sxs-lookup"><span data-stu-id="76492-146">SQL Server tools on Windows connect to SQL Server instances on Linux in the same way they would connect to any remote SQL Server instance.</span></span></span></p>
<p><span data-ttu-id="76492-147">如果有一台可以连接到 Linux 计算机的 Windows 计算机,请从运行 sqlcmd 的 Windows 命令提示符尝试执行本主题中的相同步骤。<span class="sxs-lookup"><span data-stu-id="76492-147">If you have a Windows machine that can connect to your Linux machine, try the same steps in this topic from a Windows command-prompt running <strong>sqlcmd</strong>. <span data-ttu-id="76492-148">仅验证所使用的是目标 Linux 计算机名称或 IP 地址,而非 localhost,并确保 TCP 端口 1433 已打开。<span class="sxs-lookup"><span data-stu-id="76492-148">Just verify that you use the target Linux machine name or IP address rather than localhost, and make sure that TCP port 1433 is open. <span data-ttu-id="76492-149">如果从 Windows 进行连接存在任何问题,请参阅连接故障排除建议。<span class="sxs-lookup"><span data-stu-id="76492-149">If you have any problems connecting from Windows, see connection troubleshooting recommendations.</span></span></span></span></span></span></span></span></span></p>
<p><span data-ttu-id="76492-150">有关在 Windows 上运行,但连接到 Linux 上的 SQL Server 的其他工具,请参阅:<span class="sxs-lookup"><span data-stu-id="76492-150">For other tools that run on Windows but connect to SQL Server on Linux, see:</span></span></span></p>
<ul>
<li><span data-ttu-id="76492-151">SQL Server Management Studio (SSMS)<span class="sxs-lookup"><span data-stu-id="76492-151">SQL Server Management Studio (SSMS)</span></span></span></li>
<li><span data-ttu-id="76492-152">Windows PowerShell<span class="sxs-lookup"><span data-stu-id="76492-152">Windows PowerShell</span></span></span></li>
<li><span data-ttu-id="76492-153">SQL Server Data Tools (SSDT)<span class="sxs-lookup"><span data-stu-id="76492-153">SQL Server Data Tools (SSDT)</span></span></span></li>
</ul>
<h2 id="other-deployment-scenarios"><span data-ttu-id="76492-154">其他部署方案<span class="sxs-lookup"><span data-stu-id="76492-154">Other deployment scenarios</span></span></span></h2>
<p><span data-ttu-id="76492-155">有关其他安装方案,请参阅以下资源:<span class="sxs-lookup"><span data-stu-id="76492-155">For other installation scenarios, see the following resources:</span></span></span></p>
<div class="table-scroll-wrapper">
<table>
<thead>
<tr><th> </th><th> </th></tr>
</thead>
<tbody>
<tr>
<td><span data-ttu-id="76492-156">升级<span class="sxs-lookup"><span data-stu-id="76492-156">Upgrade</span></span></span></td>
<td><span data-ttu-id="76492-157">了解如何升级 Linux 版 SQL Server 的现有安装<span class="sxs-lookup"><span data-stu-id="76492-157">Learn how to upgrade an existing installation of SQL Server on Linux</span></span></span></td>
</tr>
<tr>
<td><span data-ttu-id="76492-158">卸载<span class="sxs-lookup"><span data-stu-id="76492-158">Uninstall</span></span></span></td>
<td><span data-ttu-id="76492-159">在 Linux 上卸载 SQL Server<span class="sxs-lookup"><span data-stu-id="76492-159">Uninstall SQL Server on Linux</span></span></span></td>
</tr>
<tr>
<td><span data-ttu-id="76492-160">无人参与安装<span class="sxs-lookup"><span data-stu-id="76492-160">Unattended install</span></span></span></td>
<td><span data-ttu-id="76492-161">了解如何编写无提示安装脚本<span class="sxs-lookup"><span data-stu-id="76492-161">Learn how to script the installation without prompts</span></span></span></td>
</tr>
<tr>
<td><span data-ttu-id="76492-162">脱机安装<span class="sxs-lookup"><span data-stu-id="76492-162">Offline install</span></span></span></td>
<td><span data-ttu-id="76492-163">了解如何手动下载脱机安装程序包<span class="sxs-lookup"><span data-stu-id="76492-163">Learn how to manually download the packages for offline installation</span></span></span></td>
</tr>
</tbody>
</table>
</div>
<div class="alert is-success">
<p class="alert-title"><span class="docon docon-lightbulb"> 提示</span></p>
<p><span data-ttu-id="76492-164">有关常见问题的解答,请参阅 Linux 上的 SQL Server 常见问题解答。<span class="sxs-lookup"><span data-stu-id="76492-164">For answers to frequently asked questions, see the SQL Server on Linux FAQ.</span></span></span></p>
</div>
<h2 id="next-steps"><span data-ttu-id="76492-165">后续步骤<span class="sxs-lookup"><span data-stu-id="76492-165">Next steps</span></span></span></h2>
<div class="nextstepaction">
<p><span data-ttu-id="76492-166">浏览有关 Linux 上的 SQL Server 的教程<span class="sxs-lookup"><span data-stu-id="76492-166">Explore the tutorials for SQL Server on Linux</span></span></span></p>
</div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="QUIT
"> </code></pre>
<div class="is-hidden-desktop has-border-top has-margin-top-large has-padding-top-small">
<div class="feedback-verbatim has-border-bottom has-padding-bottom-small has-margin-bottom-small" data-bi-name="rating">
<div class="binary-rating">
<div class="binary-rating-buttons">
<h3 class="has-text-weight-semibold has-margin-top-none has-margin-bottom-small">此页面有帮助吗?</h3>
<div><button class="thumb-rating like has-inner-focus has-padding-left-extra-small has-padding-right-extra-small" title="是" data-bi-name="rating-yes" data-bi-sat="1"><span class="icon docon docon-like">是 </span></button> <button class="thumb-rating dislike has-inner-focus has-padding-none has-padding-right-extra-small" title="否" data-bi-name="rating-no" data-bi-sat="0"><span class="icon docon docon-dislike">否 </span></button></div>
</div>
<form class="feedback-verbatim-form is-hidden">
<div class="verbatim-textarea box is-relative has-box-shadow-none has-border has-margin-top-small has-padding-extra-small is-size-8"><label class="visually-hidden" for="rating-feedback-mobile">还有其他反馈吗?</label> <textarea id="rating-feedback-mobile" class="textarea has-border-none has-box-shadow-none has-inner-focus" rows="4">还有其他反馈吗?</textarea></div>
<div class="buttons is-right has-margin-top-medium has-margin-right-extra-small"><button class="skip-rating button is-text is-link is-small" type="button">跳过</button> <button class="submit-rating button is-primary is-small" type="submit" disabled="disabled" data-bi-name="rating-verbatim" data-m="{"cN":"rating-verbatim","value":"","vtbm":""}">提交</button></div>
</form></div>
<div class="thankyou-rating is-hidden">
<p>谢谢。</p>
</div>
</div>
</div>
<pre class="has-inner-focus"><code class="lang-sql" data-author-content="QUIT
"> </code></pre>
<h2 id="feedback">反馈</h2>
<div class="alert choose-feedback-type">
<p id="send-feedback-about">发送有关以下对象的反馈</p>
<div class="choose-feedback-buttons">此产品
<div class="field has-addons is-inline-flex has-margin-bottom-small">
<div class="control has-margin-top-none has-margin-bottom-none"><button class="button feedback-sign-in-button has-text-wrap" data-bi-name="feedback-sign-in"><span class="icon docon docon-brand-github"> 此页面 </span></button></div>
<div class="control dropdown has-caret has-margin-top-none has-margin-bottom-none"><button class="button dropdown-trigger is-full-height" data-bi-name="documentation-feedback-menu"></button>
<div id="fbm0" class="dropdown-menu documentation-feedback-menu">也可以直接在 GitHub 上提供反馈。</div>
</div>
</div>
<div class="field has-addons is-inline-flex has-margin-bottom-small">
<div class="control has-margin-top-none has-margin-bottom-none"><button class="button feedback-form-toggle" data-bi-name="documentation-feedback">此页面 </button></div>
<div class="control dropdown has-caret has-margin-top-none has-margin-bottom-none"><button class="button dropdown-trigger" data-bi-name="documentation-feedback-menu"></button>
<div id="fbm1" class="dropdown-menu documentation-feedback-menu">也可以直接在 GitHub 上提供反馈。</div>
</div>
</div>
</div>
</div>
<form id="feedback-form" class="feedback-form" accept-charset="UTF-8">
<h3 class="feedback-form-heading current-user"><img class="avatar lazyload" alt="头像" width="36" height="36" data-src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIwIiBoZWlnaHQ9IjEyMCIgdmlld0JveD0iMCAwIDEyMCAxMjAiIGZpbGw9InRyYW5zcGFyZW50IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg=="> <span class="username"> <span class="visually-hidden">Liquid error: Can't find the localized string giveDocumentationFeedback for template Conceptual. </span></span></h3>
<label><span class="visually-hidden">问题标题 <input class="input is-radiusless has-margin-bottom-small" type="text" name="title"> </span></label><label><span class="visually-hidden">发表评论 <textarea class="textarea is-radiusless" name="body">发表评论</textarea></span></label>
<div class="action-container"><button class="button is-primary" name="submit" type="submit">提交反馈</button></div>
</form>
<div class="feedback-status alert">本文档当前无任何反馈。 页面反馈将显示在此处。</div><br><br>
来源:https://www.cnblogs.com/surplus/p/11484116.html
頁:
[1]