I have a problem with my SQL code. When I try to use database DIGITECH
, SQL Server says the database does not exist.
This is my code
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
It is important that the create database AND the use are in one document.
This is the full Code
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