簡體   English   中英

SQL Server不了解USE &lt; <DATABASE> &gt;

[英]SQL Server doesn't understand USE <<DATABASE>>

我的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

屏幕截圖1

您需要在CREATE DATABASE語句之后放置一個GO GO命令通知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
...

您在FK_OrderID聲明了FOREIGN KEY ,它不是表上的一列。

您還嘗試引用不存在的名為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

由於無法在同一批中執行所有語句而出現錯誤,您必須將它們分開.Go命令用於分隔批..以下是一些有趣的規則

CREATE DEFAULT,CREATE FUNCTION,CREATE PROCEDURE,CREATE RULE,CREATE TRIGGER和CREATE VIEW語句不能與其他語句批量組合使用。

不能更改表,然后在同一批中引用新列。

因此,如果您執行上述任何操作,則必須將它們用GO分開

參考文獻:
在SQL Server中,什么時候應該使用GO,什么時候應該使用分號; ??
https://technet.microsoft.com/zh-cn/library/aa172435%28SQL.80%29.aspx

請在您的問題中添加“完整代碼”(通過編輯選項)並刪除您的答案,因為這不是答案...

您的代碼充滿錯別字和錯誤:

  • ord_eror_der
  • 沒有FK_OderID列(應為FK_OrderID嗎?)
  • 您創建一個FK到or_der在列FK_KID ,不存在...

???

請清理您的腳本,然后重試...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM