SQL Server doesn't understand USE <<DATABASE>> - sql

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
Screenshot 1

You need to put a GO after your CREATE DATABASE statement. The GO command informs SSMS to separate the script into different batches, split on each GO. Since your attempt to select the database was in the same batch as creating it, it threw an error as it didn't yet exist at the time of execution.
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
EDIT (New Information in Question)
You have three problems with your script:
...
alter table adresse
add constraint PK_adresse
primary key (AdID); -- This was changed from KID
go
...
You had the PRIMARY KEY set as KID, which isn't a column on the table.
...
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
...
You had the FOREIGN KEY declared on FK_OrderID, which isn't a column on the table.
You are also trying to reference a table named ord_er, which doesn't exist.
Changing those to what they should be leaves us with one more error:
alter table or_der
add constraint FK_JID
foreign key (FK_Kunde) -- Changed from FK_KID
references kunde (KID);
go
All of this was easy to figure out by just running your script and reading the error messages...
FULL CORRECTED SCRIPT
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

You got error because you cant execute all statements in same batch.You have to seperate them.Go Command is used to segregate batches..Here are some interesting rules
CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE TRIGGER, and CREATE VIEW statements cannot be combined with other statements in a batch.
A table cannot be altered and then the new columns referenced in the same batch.
So if you do any of the above operations,then they must be seperated by GO
References:
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

Please add your "full code" into your question (via edit option) and delete your answer, as this is no answer...
Your code is full of typos and errors:
ord_er or or_der?
No FK_OderID column (should be FK_OrderID?)
You create an FK to or_der on a column FK_KID, which does not exist...
???
Please clean your script and try again...

Related

Im not sure if my code works and i don't know how to test it

Ignore the names of everything, they're in Croatian. Im not sure if the logic of my alter tables and constraints are correct and if the syntax is properly written. If someone could check I would be very grateful.
This is the code
CREATE DATABASE Radionice1;
use Radionice1;
create table Ucenik
(
Sifra_ucenika int not null,
Ime varchar(20) not null,
Prezime varchar(20) not null,
Mjesto varchar(50) not null,
constraint Ucenik_PK primary key(Sifra_ucenika),
constraint Sifra_ucenika_chk CHECK (Sifra_ucenika between 1 and 1000)
);
create table Aktivnost
(
Sifra_aktivnosti int not null,
Naziv varchar(50) not null,
constraint Aktivnost_PK primary key(Sifra_aktivnosti),
);
create table Evidencija
(
Sifra_polazika int not null,
Sifra_aktivnosti int not null,
constraint Evidencija_FK foreign key(Sifra_polazika) REFERENCES Ucenik(Sifra_ucenika),
constraint Evidencija_FK foreign key(Sifra_aktivnosti) REFERENCES Aktivnst(Sifra_aktivnosti),
);
ALTER TABLE Ucenik ADD Datum_rodenja DATE,
ALTER TABLE Ucenik DROP CONSTRAINT Ucenik_chk,
ALTER TABLE Aktivnost ADD Trajanje TIME,
ALTER TABLE Aktivnost ADD CONSTRAINT Trajanje_chk CHECK (Trajanje between 2 and 10),
ALTER TABLE Aktivnost ADD Uvjet INT,
ALTER TABLE Aktivnost ADD CONSTRAINT Uvjet_chk CHECK (Uvjet>10),
ALTER TABLE Ucenik ALTER COLUMN Prezime nvarchar(30),
ALTER TABLE Ucenik DROP Mjesto,

Referenced table not found in the data dictionary mariadb

so I have written this Mariadb code as the solution for an assignment and am running it on HeidiSQL. It should work in theory however I am getting the error message SQL Error (1005): Can't create table bestellung.arbeitetin (errno: 150 "Foreign key constraint is incorrectly formed"). Upon using show warning, the program elaborates that "referenced table bestellung.arbeitetin not found in the data dictionary". I am wondering, is there something wrong with the code?
Datum date not NULL,
Abholtermin DATE,
Kostenstelle int not NULL,
Abteilung char(5) not NULL,
Mitarbeiter int not NULL,
Telefon int not NULL,
primary key (Bestellnummer)
);
create table enthaelt (
FK_Bestellnummer int not NULL,
FK_Artikelnummer char(10) not NULL,
Menge int not NULL,
unique key (FK_Bestellnummer, FK_Artikelnummer)
);
alter table enthaelt ADD
constraint FK_ArtikelNr
foreign key (FK_Artikelnummer)
references Artikel (Artikelnummer);
alter table enthaelt ADD
constraint FK_BestellNr
foreign key (FK_Bestellnummer)
references Bestellung (Bestellnummer);
create table arbeitetin
(
FK_PersNr int not NULL,
FK_ProjektNr int not NULL,
unique key(FK_PersNr, FK_ProjektNr)
);
alter table arbeitetin ADD
constraint FK_ARBEITETIN_MITARBEITER1
FOREIGN KEY (FK_PersNr)
REFERENCES Mitarbeiter (PersNr);
alter table arbeitetin ADD
constraint FK_ARBEITETIN_PROJEKT
foreign key (FK_ProjektNr)
references Projekt (ProjektNr);
alter table Mitarbeiter ADD
constraint FK_MITARBEITER_ABTEILUNG
foreign key (FK_Abkuerzung)
references Abteilung (Abkuerzung); ```

Setting up indexes for multiple foreign keys

An error message regarding indexes displays when foreign keys are added using the following scripts. I have added primary keys and indexes different ways but not sure if the diagram is providing information about how to setup the indexes/primary keys that I'm not seeing.
CREATE TABLE aocommercial_building(
parcel CHAR(25) NOT NULL,
suffix INT NOT NULL,
owner VARCHAR(255) NULL,
owner2 VARCHAR(255) NULL,
);
CREATE TABLE aocommercial_heatcool(
parcel CHAR(25) NOT NULL,
suffix INT NOT NULL,
section INT NULL,
heat_cool_cd VARCHAR(255) NULL
);
CREATE TABLE aocommercial_unit(
parcel CHAR(25) NOT NULL,
suffix INT NOT NULL,
section INT NOT NULL,
occ_desc VARCHAR(255) NULL
);
CREATE TABLE aocom_misc_bldg(
parcel CHAR(25) NOT NULL,
suffix INT NOT NULL,
section INT NOT NULL,
misc_bldg_type VARCHAR(255) NULL
);
ALTER TABLE aocommercial_unit ADD FOREIGN KEY (parcel) REFERENCES aocommercial_building(parcel);
ALTER TABLE aocommercial_unit ADD FOREIGN KEY (suffix) REFERENCES aocommercial_building(suffix);
ALTER TABLE aocommercial_heatcool ADD FOREIGN KEY (parcel) REFERENCES aocommercial_unit(parcel);
ALTER TABLE aocommercial_heatcool ADD FOREIGN KEY (suffix) REFERENCES aocommercial_unit(suffix);
ALTER TABLE aocommercial_heatcool ADD FOREIGN KEY (section) REFERENCES aocommercial_unit(section);
ALTER TABLE aocom_misc_bldg ADD FOREIGN KEY (parcel) REFERENCES aocommercial_unit(parcel);
ALTER TABLE aocom_misc_bldg ADD FOREIGN KEY (suffix) REFERENCES aocommercial_unit(suffix);
ALTER TABLE aocom_misc_bldg ADD FOREIGN KEY (section) REFERENCES aocommercial_unit(section);
enter image description here
For each table, what column (or combination of columns) uniquely identifies each row? Then, include in each CREATE TABLE another line with PRIMARY KEY(...).
For each table, if there is no particular set of columns to uniquely define each row, then add
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
Look around; there are lots and lots of examples.

I have no idea why I get an error when putting the foreing keys in MariaDB

I have no idea why I get an error when putting the foreing keys in MariaDB
I drop the full database if it exists
drop database if exists proveedoresCarrito;
create database proveedoresCarrito;
use proveedoresCarrito;
And now I'm going to create the tables
CREATE TABLE INVENTARIO (
NUMPIEZA CHAR(16) NOT NULL,
NUMBIN SMALLINT NOT NULL,
CANTDISPONIBLE SMALLINT,
FECHARECUENTO DATE,
PERIODORECUEN SMALLINT,
CANTAJUSTE SMALLINT,
CARTREORD SMALLINT,
PUNTOREORD SMALLINT);
CREATE TABLE LINPED (
NUMPEDIDO SMALLINT NOT NULL,
NUMLINEA SMALLINT NOT NULL,
NUMPIEZA CHAR(16),
PRECIOCOMPRA INTEGER,
CANTPEDIDA SMALLINT,
FECHARECEP DATE,
CANTRECIBIDA SMALLINT);
CREATE TABLE PEDIDO (
NUMPEDIDO SMALLINT NOT NULL,
NUMVEND SMALLINT,
FECHA DATE);
CREATE TABLE PIEZA (
NUMPIEZA CHAR(16) NOT NULL,
NOMPIEZA CHAR(30),
PRECIOVENT INTEGER);
CREATE TABLE PRECIOSUM (
NUMPIEZA CHAR(16) NOT NULL,
NUMVEND SMALLINT NOT NULL,
PRECIOUNIT INTEGER,
DIASSUM SMALLINT,
DESCUENTO SMALLINT);
CREATE TABLE VENDEDOR (
NUMVEND SMALLINT NOT NULL,
NOMVEND CHAR(30),
NOMBRECOMER CHAR(30),
TELEFONO CHAR(15),
CALLE CHAR(30),
CIUDAD CHAR(20),
PROVINCIA CHAR(20),
COD_POSTAL CHAR(5));
create table usuarios(
nombre SMALLINT NOT NULL,
pass CHAR(15)
);
I Set up the primary keys
ALTER TABLE INVENTARIO ADD CONSTRAINT CP_INVENTARIO PRIMARY KEY (NUMBIN);
ALTER TABLE LINPED ADD CONSTRAINT CP_LINPED PRIMARY KEY (NUMPEDIDO, NUMLINEA);
ALTER TABLE PEDIDO ADD CONSTRAINT CP_PEDIDO PRIMARY KEY (NUMPEDIDO);
ALTER TABLE PIEZA ADD CONSTRAINT CP_PIEZA PRIMARY KEY (NUMPIEZA);
ALTER TABLE PRECIOSUM ADD CONSTRAINT CP_PRECIOSUM PRIMARY KEY (NUMPIEZA, NUMVEND);
ALTER TABLE VENDEDOR ADD CONSTRAINT CP_VENDEDOR PRIMARY KEY (NUMVEND);
AND FINALLY i set up the foreign keys
ALTER TABLE INVENTARIO ADD CONSTRAINT CP_INVENTARIO PRIMARY KEY (NUMBIN);
ALTER TABLE LINPED ADD CONSTRAINT CP_LINPED PRIMARY KEY (NUMPEDIDO, NUMLINEA);
ALTER TABLE PEDIDO ADD CONSTRAINT CP_PEDIDO PRIMARY KEY (NUMPEDIDO);
ALTER TABLE PIEZA ADD CONSTRAINT CP_PIEZA PRIMARY KEY (NUMPIEZA);
ALTER TABLE PRECIOSUM ADD CONSTRAINT CP_PRECIOSUM PRIMARY KEY (NUMPIEZA, NUMVEND);
ALTER TABLE VENDEDOR ADD CONSTRAINT CP_VENDEDOR PRIMARY KEY (NUMVEND);
ALTER TABLE usuarios ADD CONSTRAINT CA_VENDEDOR_USUARIOS_NOMBRE FOREIGN KEY (nombre) REFERENCES VENDEDOR(NUMVEND);
ALTER TABLE usuarios ADD CONSTRAINT CA_VENDEDOR_USUARIOS_PASS FOREIGN KEY (pass) REFERENCES VENDEDOR(TELEFONO);
However, all the foreign keys work correctly except for the last two
when i run the sql script i get this error:
Error de SQL (1822): Failed to add the foreign key constaint. Missing index for constraint 'CA_VENDEDOR_USUARIOS_PASS' in the referenced table 'vendedor'
if I drop the last two foreign keys the error disappears
Anyone can give me a solution?
Look closely at your "set up the foreign keys" code. It creates primary keys again (except the two last lines).
Table usuarios seems to have no primary key.
ALTER TABLE usuarios ADD CONSTRAINT CP_usuarios PRIMARY KEY (nombre);
Delete the first 6 lines and keep only these
ALTER TABLE usuarios ADD CONSTRAINT CA_VENDEDOR_USUARIOS_NOMBRE
FOREIGN KEY (nombre)
REFERENCES VENDEDOR(NUMVEND);
ALTER TABLE usuarios ADD CONSTRAINT CA_VENDEDOR_USUARIOS_PASS
FOREIGN KEY (pass)
REFERENCES VENDEDOR(TELEFONO);
Also (and this is what generates your error), you are trying to add a foreign key to a column VENDEDOR(TELEFONO) which is not a primary key nor has a UNIQUE constraint.
According to Microsoft:
A foreign key constraint does not have to be linked only to a primary key constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.
See: Create Foreign Key Relationships
Add this before creating the foreign keys:
ALTER TABLE VENDEDOR ADD CONSTRAINT UX_telefono UNIQUE (TELEFONO);
A foreign key has to reference always a primary key.
For you to reference the table VENDEDOR you should use the columns that compose its PK. In this example, NUMVEND.
If you need any suggestions on what to do, i'd have to know what are your intentions on this DB structure.

SQL ALTER TABLE foreign key

I have a problem with adding foreign keys with alter table command. I don't know how to make it so it works.
I need to add ISIK_ID and STAADION_ID to ISIK_STAADIONIL table as foreign key.
Here is my code:
CREATE TABLE ISIK(
ISIK_ID INT NOT NULL,
EESNIMI VARCHAR(25) NOT NULL,
PEREKONNANIMI VARCHAR(25) NOT NULL,
ISIKUKOOD VARCHAR(20),
KODAKONDSUS VARCHAR(30),
SUGU CHAR(1) NOT NULL,
HARIDUSTASE CHAR(1) NOT NULL,
TELEFONI_NR VARCHAR(20),
SYNNIPAEV DATE,
CONSTRAINT ISIK_ID_PK PRIMARY KEY (ISIK_ID)
);
CREATE TABLE ISIK_STAADIONIL(
ISIK_STAADIONIL_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_STAADIONIL_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP
);
CREATE TABLE STAADION(
STAADION_ID INT NOT NULL,
NIMETUS VARCHAR(20),
KIRJELDUS VARCHAR(100),
ASUKOHT VARCHAR(50),
SUURUS VARCHAR(20),
MAHUTAVUS INT,
EHITATUD VARCHAR(20),
EHITAJA VARCHAR(20),
CONSTRAINT STAADION_ID_PK PRIMARY KEY (STAADION_ID)
);
ALTER TABLE ISIK_STAADIONIL
ADD CONSTRAINT ISIK_ID_FK
FOREIGN KEY(ISIK_ID)
REFERENCES ISIK(ID);
You need to add the columns first (to the table) before you can create FK constraints using them.
CREATE TABLE ISIK_STAADIONIL(
ISIK_STAADIONIL_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_STAADIONIL_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP,
ISIK_ID INT,
STAADION_ID INT
);
And then your ALTER statement should work fine.
UPDATE
Changing the table-design slightly would make more sense, as per following. The logic is that ISIK_ID and STAADION_ID would together be sufficient to determine uniqueness in the ISIK_STAADIONIL table, so a separate ID is redundant:
CREATE TABLE ISIK_STAADIONIL(
ISIK_ID INT NOT NULL,
STAADION_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_ID, STAADION_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP
);