SQL Server 數據庫項目


ylbtech-.NET Framework: SQL Server 數據庫項目

SQL Server 數據庫項目

類型:SQL Server

用於創建 SQL Server 數據庫的項目

1. 新建“SQL Server 數據庫項目”返回頂部
1-1、新建SQL Server 數據庫項目,名稱為“YlbTech.DataBase”

1-2、創建后

1-2-1、創建后數據庫項目  1-2-2、項目右鍵-->添加-->添加項菜單  

 

 

 

   

1-2、項目右鍵,新建文件夾“Tables”

2. 在Tables文件夾上,新建 Categories 和 Products 表返回頂部
2-0、Categories表 和 Products表 SQL腳本
go
-- =============================================
-- 2,類別
-- =============================================
create table Categories
(
CategoryID int identity(1,1) primary key,   --類別ID  [PK]
CategoryName nvarchar(15) not null, --類別名稱
[Description] ntext,                --說明
Picture image                       --圖片
)
 
go
-- =============================================
--3,產品 
-- =============================================
create table Products
(
ProductID int identity primary key, --產品ID『PK』
ProductName nvarchar(40) not null,  --產品名稱
CategoryID int foreign key references Categories(CategoryID),                   --類別ID
QuantityPerUnit nvarchar(20),   --單位數量
 
UnitPrice money,            --單價
UnitsInStock smallint default(0) check(UnitsInStock>=0),     --庫存量
UnitsOnOrder smallint default(0) check(UnitsOnOrder>=0),     --訂購量
ReorderLevel smallint default(0) check(ReorderLevel>=0),     --再訂購量
Discontinued bit            --中止:0=正常;1=中止
)
View Code
2-1、創建“Categories”表
2-1-2、新建“Categories表”后的界面

2-1-3、創建后的“Categories表”

2-1-3-1、Categories表創建后的生成的腳本

CREATE TABLE [dbo].[Categories]
(
    [CategoryId] INT NOT NULL IDENTITY , 
    [CategoryName] NVARCHAR(50) NULL, 
    [Description] NTEXT NOT NULL, 
    [Picture] IMAGE NOT NULL, 
    CONSTRAINT [PK_Categories] PRIMARY KEY ([CategoryId]) 
)

GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'類別ID',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Categories',
    @level2type = N'COLUMN',
    @level2name = N'CategoryId'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'類別名稱',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Categories',
    @level2type = N'COLUMN',
    @level2name = N'CategoryName'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'秒殺',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Categories',
    @level2type = N'COLUMN',
    @level2name = N'Description'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'圖片',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Categories',
    @level2type = N'COLUMN',
    @level2name = N'Picture'
Categories SQL
2-2、
2-3、
3. 添加/刪除約束返回頂部
3-1、 添加自增長標識
3-2、 添加主鍵
3-2-2、添加主鍵  3-2-3、刪除主鍵  
 
3-3、 添加外鍵
3-3-1、添加外鍵

3-3-2、添加“CategoryID”外鍵關聯Categories表的CategoryId列

3-4、 添加檢查約束
3-4-1、添加檢查約束

3-4-2、為“UnitsInOrder”添加檢查約束,值應大於等於0

3-5、 添加默認值
3-6、 是否允許為空
3-7、
4. 列屬性1返回頂部
4-1、
     
4-2、
5. 比較架構返回頂部
5-1、項目右鍵,選擇“架構比較”
5-1-1、 SQL架構比較窗口

5-1-1-1、單機“選擇目標...”出現窗口如下:

5-1-1-2、單機“選擇連接...”出現窗口如下:

     
 
5-1-1-2-1、高級屬性
     
 
     
   
5-1-3、數據庫連接成功
5-2、 “比較”架構
 
5-3、“更新”架構
圖-1  
 
圖-2  
 
5-4、“更新”架構成功
5-4-1、查看預覽
** 警告
     將 SQL Server 2016 指定為目標平台的項目可能遇到與 SQL Server 2014 有關的兼容性問題。
     項目和目標數據庫的排序規則設置不同。可能會發生部署錯誤。
     將 SQL Server 2016 指定為目標平台的項目可能遇到與 SQL Server 2014 有關的兼容性問題。
     項目和目標數據庫的排序規則設置不同。可能會發生部署錯誤。

** 突出顯示
     將重新生成的表
       無
     將刪除的聚集索引
       無
     將創建的聚集索引
       無
     可能的數據問題
       無

** 用戶操作
     創建
       [dbo].[Categories] (表)
       [dbo].[Categories].[CategoryId].[MS_Description] (擴展屬性)
       [dbo].[Categories].[CategoryName].[MS_Description] (擴展屬性)
       [dbo].[Categories].[Description].[MS_Description] (擴展屬性)
       [dbo].[Categories].[Picture].[MS_Description] (擴展屬性)

** 支持操作
查看預覽文件
5-4-2、查看腳本
/*
NorthwindYlbTech 的部署腳本

此代碼由工具生成。
如果重新生成此代碼,則對此文件的更改可能導致
不正確的行為並將丟失。
*/

GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


GO
:setvar DatabaseName "NorthwindYlbTech"
:setvar DefaultFilePrefix "NorthwindYlbTech"
:setvar DefaultDataPath "C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\"
:setvar DefaultLogPath "C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\"

GO
:on error exit
GO
/*
請檢測 SQLCMD 模式,如果不支持 SQLCMD 模式,請禁用腳本執行。
要在啟用 SQLCMD 模式后重新啟用腳本,請執行:
SET NOEXEC OFF; 
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
    BEGIN
        PRINT N'要成功執行此腳本,必須啟用 SQLCMD 模式。';
        SET NOEXEC ON;
    END


GO
USE [$(DatabaseName)];


GO
PRINT N'已跳過具有鍵 35cbb56d-7deb-4abd-887a-da543c0bbccd, 9e28fbb1-5bde-4095-86c3-3da83fdec94f 的重命名重構操作,不會將元素 [dbo].[Categories].[Id] (SqlSimpleColumn) 重命名為 CategoryId';


GO
PRINT N'正在創建 [dbo].[Categories]...';


GO
CREATE TABLE [dbo].[Categories] (
    [CategoryId]   INT           IDENTITY (1, 1) NOT NULL,
    [CategoryName] NVARCHAR (50) NULL,
    [Description]  NTEXT         NOT NULL,
    [Picture]      IMAGE         NOT NULL,
    CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED ([CategoryId] ASC)
);


GO
PRINT N'正在創建 [dbo].[Categories].[CategoryId].[MS_Description]...';


GO
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'類別ID', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'Categories', @level2type = N'COLUMN', @level2name = N'CategoryId';


GO
PRINT N'正在創建 [dbo].[Categories].[CategoryName].[MS_Description]...';


GO
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'類別名稱', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'Categories', @level2type = N'COLUMN', @level2name = N'CategoryName';


GO
PRINT N'正在創建 [dbo].[Categories].[Description].[MS_Description]...';


GO
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'秒殺', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'Categories', @level2type = N'COLUMN', @level2name = N'Description';


GO
PRINT N'正在創建 [dbo].[Categories].[Picture].[MS_Description]...';


GO
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'圖片', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'Categories', @level2type = N'COLUMN', @level2name = N'Picture';


GO
-- 正在重構步驟以使用已部署的事務日志更新目標服務器

IF OBJECT_ID(N'dbo.__RefactorLog') IS NULL
BEGIN
    CREATE TABLE [dbo].[__RefactorLog] (OperationKey UNIQUEIDENTIFIER NOT NULL PRIMARY KEY)
    EXEC sp_addextendedproperty N'microsoft_database_tools_support', N'refactoring log', N'schema', N'dbo', N'table', N'__RefactorLog'
END
GO
IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '35cbb56d-7deb-4abd-887a-da543c0bbccd')
INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('35cbb56d-7deb-4abd-887a-da543c0bbccd')
IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '9e28fbb1-5bde-4095-86c3-3da83fdec94f')
INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('9e28fbb1-5bde-4095-86c3-3da83fdec94f')

GO

GO
PRINT N'更新完成。';


GO
查看腳本文件
5-4-3、查看結果
已跳過具有鍵 35cbb56d-7deb-4abd-887a-da543c0bbccd, 9e28fbb1-5bde-4095-86c3-3da83fdec94f 的重命名重構操作,不會將元素 [dbo].[Categories].[Id] (SqlSimpleColumn) 重命名為 CategoryId
正在創建 [dbo].[Categories]...
正在創建 [dbo].[Categories].[CategoryId].[MS_Description]...
正在創建 [dbo].[Categories].[CategoryName].[MS_Description]...
正在創建 [dbo].[Categories].[Description].[MS_Description]...
正在創建 [dbo].[Categories].[Picture].[MS_Description]...
更新完成。
查看結果文件

 5-5、

6.  導入-->腳本(*.sql)返回頂部
6-0、導入腳本文件
go 
-- ============================================= 
-- 1,供應商 
-- ============================================= 
create table Suppliers 
( 
SupplierID int identity(1,1) primary key,   --供應商ID [PK] 
CompanyName nvarchar(40) not null,          --公司名稱 
ContactName nvarchar(30),           --聯系人姓名 
ContactTitle nvarchar(30),          --聯系人頭銜 
[Address] nvarchar(60),             --地址 
  
City nvarchar(15),                  --城市 
Region nvarchar(15),                --地區 
PostalCode nvarchar(15),            --郵政編碼 
Country nvarchar(24),               --國家 
Phone nvarchar(24),                 --電話 
  
Fax nvarchar(24),           --傳真 
HomePage ntext              --主頁 
) 
go
-- ============================================= 
-- 8,運貨商 
-- ============================================= 
create table Shippers 
( 
ShipperID int identity primary key,     --運貨商ID【PK】 
CompanyName nvarchar(40) not null,      --公司名稱 
Phone nvarchar(24)                      --電話 
) 
Import Sql
6-1、項目右鍵,導入-->腳本(*.sql)
 

6-1-1、導入 SQL 腳本文件-step1

 6-1-2、step2

6-1-3、step3

6-2、導入后的目錄
6-3、
6-4、
6-5、
7. 從數據庫向架構更新對象(表等)返回頂部
7-1、SQL Server項目數據庫與數據庫比較
7-2、SQL Server數據庫與項目數據庫比較
7-2-1、更新后的項目中,新增Employees.sql 文件
7-3、
8. 在項目中,新建表,然后用SQL腳本創建返回頂部
8-1、step1
8-2、step2
8-3、
9.返回頂部
 
10. 附件資料返回頂部
10-1、Products 表系統創建腳本
CREATE TABLE [dbo].[Products]
(
    [ProductId] INT NOT NULL PRIMARY KEY IDENTITY, 
    [ProductName] NVARCHAR(50) NOT NULL, 
    [CategoryId] INT NULL, 
    [QuantityPerUnit] NVARCHAR(50) NULL, 
    [UnitPrice] MONEY NULL, 
    [UnitsInStock] INT NULL , 
    [UnitsInOrder] INT NULL , 
    [ReorderLevel] INT NULL, 
    [Discontinued] BIT NULL, 
    CONSTRAINT [CK_Products_UnitsInStock] CHECK (UnitsInStock>=0), 
    CONSTRAINT [CK_Products_UnitsInOrder] CHECK (UnitsInOrder>=0), 
    CONSTRAINT [CK_Products_ReorderLevel] CHECK (ReorderLevel>=0), 
    CONSTRAINT [FK_Products_Categories] FOREIGN KEY (CategoryId) REFERENCES Categories(CategoryId)
)

GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'產品ID【 PK,ID】',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = N'COLUMN',
    @level2name = N'ProductId'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'產品名稱',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = N'COLUMN',
    @level2name = N'ProductName'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'類別ID【 FK】',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = N'COLUMN',
    @level2name = N'CategoryId'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'單位數量',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = N'COLUMN',
    @level2name = N'QuantityPerUnit'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'單價',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = N'COLUMN',
    @level2name = N'UnitPrice'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'庫存量',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = N'COLUMN',
    @level2name = N'UnitsInStock'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'訂購量',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = N'COLUMN',
    @level2name = N'UnitsInOrder'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'再購數量',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = N'COLUMN',
    @level2name = N'ReorderLevel'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'中止標識:0=正常;1=中止',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = N'COLUMN',
    @level2name = N'Discontinued'
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
    @value = N'產品表',
    @level0type = N'SCHEMA',
    @level0name = N'dbo',
    @level1type = N'TABLE',
    @level1name = N'Products',
    @level2type = NULL,
    @level2name = NULL
Products SQL

10-2、

11.返回頂部
11-1、MSDN
 
warn 作者:ylbtech
出處:http://ylbtech.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM