SQL Server无法理解USE&lt; <database>&gt;

时间:2016-06-02 20:03:15

标签: sql sql-server

我的SQL代码有问题。当我尝试使用数据库DIGITECH时,SQL Server说数据库不存在。

这是我的代码

create database DIGITECH;
Go

use DIGITECH;

/*Erstellen der Tabellen*/
create table produkt(
    PID int identity(1,1) NOT NULL,
    Produktname nvarchar(50) NULL,
    Spezifikationen nvarchar(100) NULL,
    Beschreibung nvarchar(200) NULL,
    Preis int NOT NULL,
    FK_HeID int NULL,
    FK_KatID int NULL,
    FK_StID int NULL
);
go

create table hersteller(
    HeID int identity(1,1) NOT NULL,
    Hersteller nvarchar(50) NOT NULL
);
go

create table kategorie(
    KatID int identity(1,1) NOT NULL,
    KategorieName nvarchar(20) NULL
);
go

create table stat_us(
    StID int identity(1,1) NOT NULL,
    Status nvarchar(20) NOT NULL
);
go

create table verbindungstabelle_produkt_order(
    FK_OderID int NOT NULL,
    FK_PID int NOT NULL
);
go

create table or_der(
    OrderID int identity(1,1) NOT NULL,
    Status char NOT NULL,
    FK_Kunde int NULL
);

create table kunde(
    KID int identity NOT NULL,
    Name nvarchar(50) NULL,
    Vorname nvarchar(50) NULL,
    Email nvarchar(70) NULL,
    Geschlecht char NULL,
    FK_AdID int NULL
);
go

create table adresse(
    AdID int identity(1,1) NOT NULL,
    Strasse nvarchar(50) NULL,
    Hausnummer int NULL,
    FK_PLZ int NULL
);
go

create table plz(
    PLZ int identity(1,1) NOT NULL,
    Ort nvarchar(60) NOT NULL
);
go

/* Primärschlussel*/

alter table produkt
    add constraint PK_produkt
        primary key (PID);
go

alter table kategorie
    add constraint PK_kategorie
        primary key (KatID);
go

alter table stat_us
    add constraint PK_status
        primary key (StID);
go

alter table hersteller
    add constraint PK_hersteller
        primary key (HeID);
go

alter table or_der
    add constraint PK_order
        primary key (OrderID);
go

alter table kunde
    add constraint PK_kunde
        primary key (KID);
go

alter table adresse
    add constraint PK_adresse
        primary key (KID);
go

alter table plz
    add constraint PK_plz
        primary key (PLZ);
go

/* Fremdkeys hinzufügen */

alter table produkt
    add constraint FK_kategorie
        foreign key  (FK_KatID)
        references kategorie (KatID);
go

alter table produkt
    add constraint FK_status
        foreign key (FK_StID)
        references stat_us (StID);
go

alter table produkt
    add constraint FK_hersteller
        foreign key (FK_HeID)
        references hersteller (HeID);
go

alter table verbindungstabelle_produkt_order
    add constraint FK_PID
        foreign key (FK_PID)
        references produkt (PID);
go

alter table verbindungstabelle_produkt_order
    add constraint FK_OrderID
        foreign key (FK_OrderID)
        references ord_er (OrderID);
go

alter table ord_er
    add constraint FK_JID
        foreign key (FK_KID)
        references kunde (KID);
go

alter table kunde
    add constraint FK_AdID
        foreign key (FK_AdID)
        references adresse (AdID);
go

alter table adresse
    add constraint FK_PLZ
        foreign key (FK_PLZ)
        references plz (PLZ);
go

创建数据库和使用在一个文档中非常重要。

这是完整的代码

create database DIGITECH;
Go

use DIGITECH;

/*Erstellen der Tabellen*/
create table produkt(
    PID int identity(1,1) NOT NULL,
    Produktname nvarchar(50) NULL,
    Spezifikationen nvarchar(100) NULL,
    Beschreibung nvarchar(200) NULL,
    Preis int NOT NULL,
    FK_HeID int NULL,
    FK_KatID int NULL,
    FK_StID int NULL
);
go

create table hersteller(
    HeID int identity(1,1) NOT NULL,
    Hersteller nvarchar(50) NOT NULL
);
go

create table kategorie(
    KatID int identity(1,1) NOT NULL,
    KategorieName nvarchar(20) NULL
);
go

create table stat_us(
    StID int identity(1,1) NOT NULL,
    Status nvarchar(20) NOT NULL
);
go

create table verbindungstabelle_produkt_order(
    FK_OderID int NOT NULL,
    FK_PID int NOT NULL
);
go

create table or_der(
    OrderID int identity(1,1) NOT NULL,
    Status char NOT NULL,
    FK_Kunde int NULL
);

create table kunde(
    KID int identity NOT NULL,
    Name nvarchar(50) NULL,
    Vorname nvarchar(50) NULL,
    Email nvarchar(70) NULL,
    Geschlecht char NULL,
    FK_AdID int NULL
);
go

create table adresse(
    AdID int identity(1,1) NOT NULL,
    Strasse nvarchar(50) NULL,
    Hausnummer int NULL,
    FK_PLZ int NULL
);
go

create table plz(
    PLZ int identity(1,1) NOT NULL,
    Ort nvarchar(60) NOT NULL
);
go

/* Primärschlussel*/

alter table produkt
    add constraint PK_produkt
        primary key (PID);
go

alter table kategorie
    add constraint PK_kategorie
        primary key (KatID);
go

alter table stat_us
    add constraint PK_status
        primary key (StID);
go

alter table hersteller
    add constraint PK_hersteller
        primary key (HeID);
go

alter table or_der
    add constraint PK_order
        primary key (OrderID);
go

alter table kunde
    add constraint PK_kunde
        primary key (KID);
go

alter table adresse
    add constraint PK_adresse
        primary key (KID);
go

alter table plz
    add constraint PK_plz
        primary key (PLZ);
go

/* Fremdkeys hinzufügen */

alter table produkt
    add constraint FK_kategorie
        foreign key  (FK_KatID)
        references kategorie (KatID);
go

alter table produkt
    add constraint FK_status
        foreign key (FK_StID)
        references stat_us (StID);
go

alter table produkt
    add constraint FK_hersteller
        foreign key (FK_HeID)
        references hersteller (HeID);
go

alter table verbindungstabelle_produkt_order
    add constraint FK_PID
        foreign key (FK_PID)
        references produkt (PID);
go

alter table verbindungstabelle_produkt_order
    add constraint FK_OrderID
        foreign key (FK_OrderID)
        references ord_er (OrderID);
go

alter table ord_er
    add constraint FK_JID
        foreign key (FK_KID)
        references kunde (KID);
go

alter table kunde
    add constraint FK_AdID
        foreign key (FK_AdID)
        references adresse (AdID);
go

alter table adresse
    add constraint FK_PLZ
        foreign key (FK_PLZ)
        references plz (PLZ);
go

Screenshot 1

3 个答案:

答案 0 :(得分:2)

您需要在GO声明后加CREATE DATABASEGO命令通知SSMS将脚本分成不同的批次,在每个GO上拆分。由于您尝试选择数据库与创建数据库的批次相同,因此它会抛出一个错误,因为它在执行时尚不存在。

create database DIGITECH;
Go

use DIGITECH;

/*Erstellen der Tabellen*/
create table produkt(
    PID int identity(1,1) NOT NULL,
    Produktname nvarchar(50) NULL,
    Spezifikationen nvarchar(100) NULL,
    Beschreibung nvarchar(200) NULL,
    Preis int NOT NULL,
    FK_HeID int NULL,
    FK_KatID int NULL,
    FK_StID int NULL
);
go

编辑(问题中的新信息)

您的脚本有三个问题:

...
alter table adresse
    add constraint PK_adresse
        primary key (AdID);     -- This was changed from KID
go
...

您将PRIMARY KEY设置为KID,这不是表格中的列。

...
alter table verbindungstabelle_produkt_order
    add constraint FK_OrderID
        foreign key (FK_OderID) -- Changed from FK_OrderID
        references or_der (OrderID); -- Changed from ord_er
go
...

您在FOREIGN KEY上声明了FK_OrderID,这不是表格中的列。

您还尝试引用一个名为ord_er的表,该表不存在。

将这些更改为应该是什么让我们再犯一个错误:

alter table or_der 
    add constraint FK_JID
        foreign key (FK_Kunde) -- Changed from FK_KID
        references kunde (KID);
go

只需运行脚本并阅读错误消息即可轻松搞清楚......

完全正确的脚本

create database DIGITECH;
Go

use DIGITECH;

/*Erstellen der Tabellen*/
create table produkt(
    PID int identity(1,1) NOT NULL,
    Produktname nvarchar(50) NULL,
    Spezifikationen nvarchar(100) NULL,
    Beschreibung nvarchar(200) NULL,
    Preis int NOT NULL,
    FK_HeID int NULL,
    FK_KatID int NULL,
    FK_StID int NULL
);
go

create table hersteller(
    HeID int identity(1,1) NOT NULL,
    Hersteller nvarchar(50) NOT NULL
);
go

create table kategorie(
    KatID int identity(1,1) NOT NULL,
    KategorieName nvarchar(20) NULL
);
go

create table stat_us(
    StID int identity(1,1) NOT NULL,
    Status nvarchar(20) NOT NULL
);
go

create table verbindungstabelle_produkt_order(
    FK_OderID int NOT NULL,
    FK_PID int NOT NULL
);
go

create table or_der(
    OrderID int identity(1,1) NOT NULL,
    Status char NOT NULL,
    FK_Kunde int NULL
);

create table kunde(
    KID int identity NOT NULL,
    Name nvarchar(50) NULL,
    Vorname nvarchar(50) NULL,
    Email nvarchar(70) NULL,
    Geschlecht char NULL,
    FK_AdID int NULL
);
go

create table adresse(
    AdID int identity(1,1) NOT NULL,
    Strasse nvarchar(50) NULL,
    Hausnummer int NULL,
    FK_PLZ int NULL
);
go

create table plz(
    PLZ int identity(1,1) NOT NULL,
    Ort nvarchar(60) NOT NULL
);
go

/* Primärschlussel*/

alter table produkt
    add constraint PK_produkt
        primary key (PID);
go

alter table kategorie
    add constraint PK_kategorie
        primary key (KatID);
go

alter table stat_us
    add constraint PK_status
        primary key (StID);
go

alter table hersteller
    add constraint PK_hersteller
        primary key (HeID);
go

alter table or_der
    add constraint PK_order
        primary key (OrderID);
go

alter table kunde
    add constraint PK_kunde
        primary key (KID);
go

alter table adresse
    add constraint PK_adresse
        primary key (AdID);     -- This was changed from KID
go

alter table plz
    add constraint PK_plz
        primary key (PLZ);
go

/* Fremdkeys hinzufügen */

alter table produkt
    add constraint FK_kategorie
        foreign key  (FK_KatID)
        references kategorie (KatID);
go

alter table produkt
    add constraint FK_status
        foreign key (FK_StID)
        references stat_us (StID);
go

alter table produkt
    add constraint FK_hersteller
        foreign key (FK_HeID)
        references hersteller (HeID);
go

alter table verbindungstabelle_produkt_order
    add constraint FK_PID
        foreign key (FK_PID)
        references produkt (PID);
go

alter table verbindungstabelle_produkt_order
    add constraint FK_OrderID
        foreign key (FK_OderID) -- Changed from FK_OrderID
        references or_der (OrderID);
go

alter table or_der 
add constraint FK_JID
    foreign key (FK_Kunde) -- Changed from FK_KID
    references kunde (KID);
go

alter table kunde
    add constraint FK_AdID
        foreign key (FK_AdID)
        references adresse (AdID);
go

alter table adresse
    add constraint FK_PLZ
        foreign key (FK_PLZ)
        references plz (PLZ);
go

答案 1 :(得分:0)

你得到了错误,因为你无法在同一批次中执行所有语句。你必须将它们分开.Go命令用于隔离批次。这是一些有趣的规则

  

创建默认值,创建函数,创建过程,创建规则,创建触发器和创建VIEW语句不能与批处理中的其他语句组合。

     

无法更改表格,然后在同一批次中引用新列。

因此,如果您执行上述任何操作,则必须将它们分隔为 GO

参考文献:
In SQL Server, when should you use GO and when should you use semi-colon ;?
https://technet.microsoft.com/en-us/library/aa172435%28SQL.80%29.aspx

答案 2 :(得分:0)

请将您的“完整代码”添加到您的问题中(通过编辑选项)并删除您的答案,因为这不是答案......

您的代码中存在拼写错误和错误:

  • ord_eror_der
  • 没有FK_OderID列(应该是FK_OrderID?)
  • 您在列or_der上创建了FK_KID的FK,该列不存在...

???

请清理您的脚本,然后重试......