如何使用此脚本创建数据库

时间:2018-03-24 15:10:39

标签: sql asp.net

我想从这篇文章中创建数据库

因为我丢失了CustomersDB.mdf

我在visual studio中从model.edmx获得了这个脚本。

以这种方式

1.在模型上单击鼠标右键,然后选择“从模型生成数据库”。

2.Copy生成的SQL脚本。

3.在MSSQL中点击新查询并粘贴代码并运行 ctrl + f5

但不起作用。

请帮我在MSSQL中创建数据库。

谢谢!

SET QUOTED_IDENTIFIER OFF;
GO
USE [CustomersDB];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO



IF OBJECT_ID(N'[dbo].[FK_tbl_User_tbl_Role]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[tbl_User] DROP CONSTRAINT [FK_tbl_User_tbl_Role];
GO
IF OBJECT_ID(N'[dbo].[FK_tbl_Customer_tbl_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[tbl_Customer] DROP CONSTRAINT [FK_tbl_Customer_tbl_User];
GO
IF OBJECT_ID(N'[dbo].[FK_tbl_Customer_tbl_CatGift]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[tbl_Customer] DROP CONSTRAINT [FK_tbl_Customer_tbl_CatGift];
GO
IF OBJECT_ID(N'[dbo].[FK_tbl_darsad_tbl_CatGift]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[tbl_darsad] DROP CONSTRAINT [FK_tbl_darsad_tbl_CatGift];
GO
IF OBJECT_ID(N'[dbo].[FK_tbl_darsad_tbl_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[tbl_darsad] DROP CONSTRAINT [FK_tbl_darsad_tbl_User];
GO



IF OBJECT_ID(N'[dbo].[sysdiagrams]', 'U') IS NOT NULL
    DROP TABLE [dbo].[sysdiagrams];
GO
IF OBJECT_ID(N'[dbo].[tbl_Role]', 'U') IS NOT NULL
    DROP TABLE [dbo].[tbl_Role];
GO
IF OBJECT_ID(N'[dbo].[tbl_User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[tbl_User];
GO
IF OBJECT_ID(N'[dbo].[tbl_Customer]', 'U') IS NOT NULL
    DROP TABLE [dbo].[tbl_Customer];
GO
IF OBJECT_ID(N'[dbo].[tbl_CatGift]', 'U') IS NOT NULL
    DROP TABLE [dbo].[tbl_CatGift];
GO
IF OBJECT_ID(N'[dbo].[tbl_darsad]', 'U') IS NOT NULL
    DROP TABLE [dbo].[tbl_darsad];
GO



-- Creating table 'sysdiagrams'
CREATE TABLE [dbo].[sysdiagrams] (
    [name] nvarchar(128)  NOT NULL,
    [principal_id] int  NOT NULL,
    [diagram_id] int IDENTITY(1,1) NOT NULL,
    [version] int  NULL,
    [definition] varbinary(max)  NULL
);
GO

-- Creating table 'tbl_Role'
CREATE TABLE [dbo].[tbl_Role] (
    [RoleID] int  NOT NULL,
    [RoleTitle] nvarchar(50)  NULL,
    [RoleName] nvarchar(50)  NULL
);
GO

-- Creating table 'tbl_User'
CREATE TABLE [dbo].[tbl_User] (
    [UserID] int  NOT NULL,
    [RoleID_FK] int  NULL,
    [Username] nvarchar(150)  NULL,
    [Pass] nvarchar(150)  NULL,
    [Name] nvarchar(150)  NULL
);
GO

-- Creating table 'tbl_Customer'
CREATE TABLE [dbo].[tbl_Customer] (
    [CustomerID] int IDENTITY(1,1) NOT NULL,
    [UserID_FK] int  NULL,
    [Name] nvarchar(50)  NULL,
    [Family] nvarchar(50)  NULL,
    [Code] nvarchar(50)  NULL,
    [Mobile] nvarchar(50)  NULL,
    [Price] decimal(18,0)  NULL,
    [Address] nvarchar(250)  NULL,
    [BirthdayYear] nchar(4)  NULL,
    [BirthdayMonth] nchar(2)  NULL,
    [BirthdayDay] nchar(2)  NULL,
    [Tell] nvarchar(50)  NULL,
    [CatGiftID_FK] int  NULL
);
GO

-- Creating table 'tbl_CatGift'
CREATE TABLE [dbo].[tbl_CatGift] (
    [CatGiftID] int  NOT NULL,
    [title] nvarchar(50)  NULL,
    [name] nvarchar(50)  NULL
);
GO

-- Creating table 'tbl_darsad'
CREATE TABLE [dbo].[tbl_darsad] (
    [DarsadID] int IDENTITY(1,1) NOT NULL,
    [UserID_FK] int  NULL,
    [CatGiftID_FK] int  NULL,
    [DarsadAval] int  NULL,
    [DarsadDovom] int  NULL,
    [Darsadsevom] int  NULL
);
GO




ALTER TABLE [dbo].[sysdiagrams]
ADD CONSTRAINT [PK_sysdiagrams]
    PRIMARY KEY CLUSTERED ([diagram_id] ASC);
GO

ALTER TABLE [dbo].[tbl_Role]
ADD CONSTRAINT [PK_tbl_Role]
    PRIMARY KEY CLUSTERED ([RoleID] ASC);
GO

ALTER TABLE [dbo].[tbl_User]
ADD CONSTRAINT [PK_tbl_User]
    PRIMARY KEY CLUSTERED ([UserID] ASC);
GO

ALTER TABLE [dbo].[tbl_Customer]
ADD CONSTRAINT [PK_tbl_Customer]
    PRIMARY KEY CLUSTERED ([CustomerID] ASC);
GO

ALTER TABLE [dbo].[tbl_CatGift]
ADD CONSTRAINT [PK_tbl_CatGift]
    PRIMARY KEY CLUSTERED ([CatGiftID] ASC);
GO

ALTER TABLE [dbo].[tbl_darsad]
ADD CONSTRAINT [PK_tbl_darsad]
    PRIMARY KEY CLUSTERED ([DarsadID] ASC);
GO


ALTER TABLE [dbo].[tbl_User]
ADD CONSTRAINT [FK_tbl_User_tbl_Role]
    FOREIGN KEY ([RoleID_FK])
    REFERENCES [dbo].[tbl_Role]
        ([RoleID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

CREATE INDEX [IX_FK_tbl_User_tbl_Role]
ON [dbo].[tbl_User]
    ([RoleID_FK]);
GO

ALTER TABLE [dbo].[tbl_Customer]
ADD CONSTRAINT [FK_tbl_Customer_tbl_User]
    FOREIGN KEY ([UserID_FK])
    REFERENCES [dbo].[tbl_User]
        ([UserID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

CREATE INDEX [IX_FK_tbl_Customer_tbl_User]
ON [dbo].[tbl_Customer]
    ([UserID_FK]);
GO

ALTER TABLE [dbo].[tbl_Customer]
ADD CONSTRAINT [FK_tbl_Customer_tbl_CatGift]
    FOREIGN KEY ([CatGiftID_FK])
    REFERENCES [dbo].[tbl_CatGift]
        ([CatGiftID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

CREATE INDEX [IX_FK_tbl_Customer_tbl_CatGift]
ON [dbo].[tbl_Customer]
    ([CatGiftID_FK]);
GO

ALTER TABLE [dbo].[tbl_darsad]
ADD CONSTRAINT [FK_tbl_darsad_tbl_CatGift]
    FOREIGN KEY ([CatGiftID_FK])
    REFERENCES [dbo].[tbl_CatGift]
        ([CatGiftID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

CREATE INDEX [IX_FK_tbl_darsad_tbl_CatGift]
ON [dbo].[tbl_darsad]
    ([CatGiftID_FK]);
GO

ALTER TABLE [dbo].[tbl_darsad]
ADD CONSTRAINT [FK_tbl_darsad_tbl_User]
    FOREIGN KEY ([UserID_FK])
    REFERENCES [dbo].[tbl_User]
        ([UserID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

CREATE INDEX [IX_FK_tbl_darsad_tbl_User]
ON [dbo].[tbl_darsad]
    ([UserID_FK]);
GO

1 个答案:

答案 0 :(得分:0)

尚未创建数据库对象本身。这就是您收到错误的原因:

  

Msg 911,Level 16,State 1,Line 1 Database' CustomersDB'才不是   存在。确保正确输入名称。

如果这仅用于测试目的,此脚本将帮助您入门。我在这个脚本中使用了SSMS 17.4。

我建议您自己创建数据库,选择所有属性/选项。

如果使用SQL Server Management Studio,您可以右键单击Databases文件夹并选择" New Database ..."。

enter image description here

然后找到教程和/或浏览您的选项,并为您自己的特定用例选择所需的选项。

CREATE DATABASE [CustomersDB]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'CustomersDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\CustomersDB.mdf' , SIZE = 8192KB , FILEGROWTH = 65536KB )
 LOG ON 
( NAME = N'CustomersDB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\CustomersDB_log.ldf' , SIZE = 8192KB , FILEGROWTH = 65536KB )
GO
ALTER DATABASE [CustomersDB] SET COMPATIBILITY_LEVEL = 140
GO
ALTER DATABASE [CustomersDB] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [CustomersDB] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [CustomersDB] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [CustomersDB] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [CustomersDB] SET ARITHABORT OFF 
GO
ALTER DATABASE [CustomersDB] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [CustomersDB] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [CustomersDB] SET AUTO_CREATE_STATISTICS ON(INCREMENTAL = OFF)
GO
ALTER DATABASE [CustomersDB] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [CustomersDB] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [CustomersDB] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [CustomersDB] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [CustomersDB] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [CustomersDB] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [CustomersDB] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [CustomersDB] SET  DISABLE_BROKER 
GO
ALTER DATABASE [CustomersDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [CustomersDB] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [CustomersDB] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [CustomersDB] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [CustomersDB] SET  READ_WRITE 
GO
ALTER DATABASE [CustomersDB] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [CustomersDB] SET  MULTI_USER 
GO
ALTER DATABASE [CustomersDB] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [CustomersDB] SET TARGET_RECOVERY_TIME = 60 SECONDS 
GO
ALTER DATABASE [CustomersDB] SET DELAYED_DURABILITY = DISABLED 
GO
USE [CustomersDB]
GO
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = Off;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = Primary;
GO
ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = On;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = Primary;
GO
ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = Off;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = Primary;
GO
USE [CustomersDB]
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY') ALTER DATABASE [CustomersDB] MODIFY FILEGROUP [PRIMARY] DEFAULT
GO