Firstly I want to point, that I'm newbe in that.
I have some problem with insert into table.
When i try to run whole code at once, it gives me error:
Msg 547, Level 16, State 0, Line 134 The INSERT statement conflicted
with the FOREIGN KEY constraint "FK__Zwierze__Klatka___440B1D61". The
conflict occurred in database "zoo2", table "dbo.Klatka", column
'ID_Klatka'.
What I tried:
First create all tables, after that insert.
To change the order of lines in query, but it doesn't help
To search duplicates, but with no effect.
I'm trying for about 3 hours and I don't have any idea what to do.
I know, that error is really simple to understand, but even that I don't know what to do.
You can find whole code below.
Please help me with that.
Many thanks in advance.
USE zoo2
Create Table Domena (
ID_Domena INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nazwa_Domena varchar(80) NOT NULL,
)
Create Table Krolestwo (
ID_Krolestwo INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nazwa_Krolestwo varchar(80) NOT NULL,
ID_Domena INT NOT NULL REFERENCES Domena(ID_Domena)
)
Create Table Typ (
ID_Typ INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nazwa_Typ varchar(80) NOT NULL,
ID_Krolestwo INT NOT NULL REFERENCES Krolestwo(ID_Krolestwo)
)
Create Table Gromada (
ID_Gromada INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nazwa_Rodzina varchar(80) NOT NULL,
ID_Typ INT NOT NULL REFERENCES Typ(ID_Typ)
)
Create Table Rzad (
ID_Rzad INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nazwa_Rodzina varchar(80) NOT NULL,
ID_Gromada INT NOT NULL REFERENCES Gromada(ID_Gromada)
)
Create Table Rodzina (
ID_Rodzina INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nazwa_Rodzina varchar(80) NOT NULL,
ID_Rzad INT NOT NULL REFERENCES Rzad(ID_Rzad)
)
Create Table Rodzaj (
ID_Rodzaj INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nazwa_Rodzaj varchar(80) NOT NULL,
ID_Rodzina INT NOT NULL REFERENCES Rodzina(ID_Rodzina)
)
Create Table Gatunek (
ID_Gatunek INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nazwa_Gatunek varchar(80) NOT NULL,
ID_Rodzaj INT NOT NULL REFERENCES Rodzaj(ID_Rodzaj),
Opis_Gatunek varchar(500)
)
Create Table Strefa(
ID_Strefa INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Strefa char(1) NOT NULL
)
Create Table Klatka_Rozmiar(
ID_Klatka_Rozmiar INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Wysokosc INT NOT NULL,
Szerokosc INT NOT NULL,
Dlugosc INT NOT NULL
)
Create Table Pomieszczenie(
ID_Pomieszczenie INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nr_Pomieszczenie INT NOT NULL,
ID_Strefa INT NOT NULL REFERENCES Strefa(ID_Strefa)
)
Create Table Klatka (
ID_Klatka INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
ID_Klatka_Rozmiar INT NOT NULL REFERENCES Klatka_Rozmiar(ID_Klatka_Rozmiar),
ID_Pomieszczenie INT NOT NULL REFERENCES Pomieszczenie(ID_Pomieszczenie),
)
Create Table Zwierze (
ID_Zwierze INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
Nazwa_Zwierze varchar(255) NOT NULL,
Klatka_ID INT NOT NULL REFERENCES Klatka(ID_Klatka),
Zwierze_ID_Domena INT NOT NULL REFERENCES Domena(ID_Domena),
Zwierze_ID_Krolestwo INT NOT NULL REFERENCES Krolestwo(ID_Krolestwo),
Zwierze_ID_Typ INT NOT NULL REFERENCES Typ(ID_Typ),
Zwierze_ID_Gromada INT NOT NULL REFERENCES Gromada(ID_Gromada),
Zwierze_ID_Rzad INT NOT NULL REFERENCES Rzad(ID_Rzad),
Zwierze_ID_Rodzina INT NOT NULL REFERENCES Rodzina(ID_Rodzina),
Zwierze_ID_Rodzaj INT NOT NULL REFERENCES Rodzaj(ID_Rodzaj),
Zwierze_ID_Gatunek INT NOT NULL REFERENCES Gatunek(ID_Gatunek)
)
--Koniec Tworzenie Tabel--
--Dodawanie wartości--
--Dodawanie domeny--
INSERT INTO Domena
VALUES ('Eukarionty');
--Dodawanie królestwa--
INSERT INTO Krolestwo
VALUES ('Zwierzeta', 1);
--Dodawanie typu--
INSERT INTO Typ
VALUES ('Miesny', 1),('Strunowce', 1);
--Dodawanie Gromady--
INSERT INTO Gromada
VALUES ('Ssaki', 1),('Kręgowce', 2);
--Dodawanie Rzędu--
INSERT INTO Rzad
VALUES ('Parzystokopytne', 1),('Sowy', 2);
--Dodawanie Rodziny--
INSERT INTO Rodzina
VALUES ('Wołowate', 1),('Ptaki', 2);
--Dodawanie Rodzaju--
INSERT INTO Rodzaj
VALUES ('Bydło', 1),('Ptactwo', 2);
--Dodawanie Gatunku--
INSERT INTO Gatunek
VALUES ('Tur europejski', 1, '*Taki niby byk*'),('Drapieżny', 2, '*Ptak drapieżny*');
--Dodawanie Strefy--
INSERT INTO Strefa
VALUES ('A'),('B'),('C')
--Dodawanie Pomieszczenia--
INSERT INTO Pomieszczenie
VALUES (1,1),(2,1),(3,2),(4,3),(5,3)
--Dodawanie Klatek_rozmiar--
INSERT INTO Klatka_Rozmiar
VALUES (100,100,100),(500,500,500),(7000,7000,7000)
--Dodawanie Zwierząt--
INSERT INTO Zwierze
VALUES ('Byk', 1, 1, 1, 1, 1, 1, 1, 1 ,1 )
Related
I'm creating two tables and I would like to use UserID in User_info_table as a foreign key in the Trail_info_table, but it keeps showing as NULL.
CREATE TABLE CW2.User_info_table
(
UserID int IDENTITY PRIMARY KEY,
User_name varchar(255),
Email_Address varchar(255) NOT NULL,
User_password varchar(255) NOT NULL,
User_access_level int NOT NULL,
);
INSERT INTO CW2.User_info_table (User_name, Email_Address, User_password, User_access_level)
VALUES ('Grace Hopper', 'grace#plymouth.ac.uk', 'ISAD123!', 1)
INSERT INTO CW2.User_info_table (User_name, Email_Address, User_password, User_access_level)
VALUES ('Tim Berners-Lee', 'tim#plymouth.ac.uk', 'COMP2001!', 1)
INSERT INTO CW2.User_info_table (User_name, Email_Address, User_password, User_access_level)
VALUES ('Ada Lovelace', 'ada#plymouth.ac.uk', 'insecurePassword', 1)
CREATE TABLE CW2.Trail_info_table
(
TrailID int IDENTITY,
Trail_name char(255) NOT NULL,
Trail_owner varchar(255),
Trail_owner_ID int
FOREIGN KEY REFERENCES CW2.User_info_table(UserID),
Trail_difficulty int NOT NULL,
Trail_length int NOT NULL,
CONSTRAINT Trail_primary PRIMARY KEY (TrailID),
);
INSERT INTO CW2.Trail_info_table (Trail_name, Trail_owner, Trail_difficulty, Trail_length)
VALUES ('Spring Sprint', 'Grace Hopper', 1, 10)
INSERT INTO CW2.Trail_info_table (Trail_name, Trail_owner, Trail_difficulty, Trail_length)
VALUES ('Summer Stroll', 'Tim Berners-Lee', 2, 15)
INSERT INTO CW2.Trail_info_table (Trail_name, Trail_owner, Trail_difficulty, Trail_length)
VALUES ('Winter Waltz', 'Ada Lovelace', 3, 20)
I tried to get the UserID (1,2,3) in the Trail_owner_ID column as a foreign key but it keeps showing up as NULL
I am getting an invalid foreign key value and I cannot figure out what it is, here is the error.
Executed as multiple statements.
STATEMENT 1:
DATABASE completed. 0 rows processed. Elapsed time = 00:00:00.047
STATEMENT 2:
Insert Statement failed. Failed [2700 : 23000] Referential constraint violation: invalid foreign key value.
This happens when I try to insert:
insert into SALE values (1,30.45,'2020-12-31','VMBNH',1,1);
into the SALE Table
Here is the table, and I am also including the inserts that I already put on the table.
CREATE TABLE EQUIPMENT_DETAIL
(
EquipDetailID INT NOT NULL,
Make VARCHAR(20) NOT NULL,
Equipment_Type VARCHAR(20) NOT NULL,
Model VARCHAR(20) NOT NULL,
PRIMARY KEY (EquipDetailID)
);
CREATE TABLE CUSTOMER
(
CustID INT NOT NULL,
CustName VARCHAR(40) NOT NULL,
CustCategory VARCHAR(20) NOT NULL,
PRIMARY KEY (CustID)
);
CREATE TABLE EQUIPMENT
(
SerialNo VARCHAR(20) NOT NULL,
LastInspectedDate DATE NOT NULL,
DateMade DATE NOT NULL,
EquipDetailID INT NOT NULL,
PRIMARY KEY (SerialNo),
FOREIGN KEY (EquipDetailID) REFERENCES EQUIPMENT_DETAIL(EquipDetailID)
);
CREATE TABLE SALE
(
SaleTransID INT NOT NULL,
Price DECIMAL(4,2) NOT NULL,
Sale_Date DATE NOT NULL,
SerialNo VARCHAR(20) NOT NULL,
CustID INT NOT NULL,
SRepID INT NOT NULL,
PRIMARY KEY(SaleTransID),
FOREIGN KEY (SerialNo) REFERENCES EQUIPMENT(SerialNo),
FOREIGN KEY (CustID) REFERENCES CUSTOMER(CustID),
FOREIGN KEY (SRepID) REFERENCES SALES_REP(SRepID)
);
CREATE TABLE RENTAL
(
Price DECIMAL(4,2) NOT NULL,
Rental_Date DATE NOT NULL,
RentTransID INT NOT NULL,
SerialNo VARCHAR(20) NOT NULL,
CustID INT NOT NULL,
SRepID INT NOT NULL,
PRIMARY KEY (RentTransID),
FOREIGN KEY (SerialNo) REFERENCES EQUIPMENT(SerialNo),
FOREIGN KEY (CustID) REFERENCES CUSTOMER(CustID),
FOREIGN KEY (SRepID) REFERENCES SALES_REP(SRepID)
);
CREATE TABLE SALES_REP
(
SRepID INT NOT NULL,
SRepLName VARCHAR(40) NOT NULL,
SRepFName VARCHAR(20) NOT NULL,
Rank_ID VARCHAR(20) NOT NULL,
Mentors_SRepID INT,
PRIMARY KEY (SRepID),
FOREIGN KEY (Mentors_SRepID) REFERENCES SALES_REP(SRepID)
);
insert into EQUIPMENT_DETAIL values (1,'CFT','Drill','B123');
insert into EQUIPMENT_DETAIL values (2,'CFT','Hammer','B124');
insert into EQUIPMENT_DETAIL values (3,'MKT','Saw','M123');
insert into EQUIPMENT_DETAIL values (4'MKT','Wrench','M124');
insert into EQUIPMENT_DETAIL values (5'SNP','Flat Head','S123');
insert into CUSTOMER values (1,'Jim Halpert','A');
insert into CUSTOMER values (2,'Michael Scott','B');
insert into EQUIPMENT values ('KDJHS','2021-01-12','2010-11-11',1);
insert into EQUIPMENT values ('VMBNH','2021-03-05','2007-12-12',2);
insert into EQUIPMENT values ('QIEIR','2021-03-05','2007-12-12',3);
insert into EQUIPMENT values ('PTPYO','2021-03-05','2007-12-12',4);
insert into EQUIPMENT values ('AGSGD','2021-03-05','2007-12-12',5);
I had to drop the foreign key constraint from SALE (SRepID)
create database shop1
use shop1
create table mathang
(
MatHangID INT primary key not null,
TenMatHang varchar(50),
SoLuong int not null,
Price int not null,
)
create table nhacungcap
(
MatHangID INT foreign key references mathang,
TenNhaCungCap varchar(50) ,
DiaChi varchar(100),
SoDienThoai int ,
CONSTRAINT pk_M_CC primary key (MatHangID)
)
create table khachhang
(
KhachHangID int not null primary key,
TenKhachHang varchar(50) not null,
[DiaChi] varchar(100) not null,
[SoDienThoai] varchar(50) not null,
)
create table donhang
(
DonHangID int references to khachhang(KhachHangID),
TenDonHang varchar(50),
SoLuong int,
CONSTRAINT pk_DHID primary key (DonHangID)
)
I can not find errors with this, it shows the foreign key error to add this relation ship. Any one please help.
Remove the to in
...
DonHangID int references to khachhang(KhachHangID)
...
to get
...
DonHangID int references khachhang(KhachHangID)
...
. The to doesn't belong there syntactically.
Here is my code it generates Foreign key conflict error
CREATE TABLE tblProducts
(
ProductID int NOT NULL IDENTITY PRIMARY KEY,
ProductName nvarchar(30) NOT NULL,
BatchID int NOT NULL FOREIGN KEY REFERENCES tblBatches (BatchID)
)
CREATE TABLE tblBatches
(
BatchID INT NOT NULL IDENTITY PRIMARY KEY,
BatchCode nvarchar(20) NOT NULL,
Quantity int NOT NULL,
BatchMnf Date NOT NULL,
BatchExp Date NOT NULL,
PurchaseRate int NOT NULL,
SalesRate int NOT NULL,
ProductID int NOT NULL FOREIGN KEY REFERENCES tblProducts (ProductID)
)
You cannot do that. This is a circular reference.
This is a bad design but if you want to do that, you need to make foreign key columns Nullable.
CREATE TABLE tblProducts
(
ProductID int NOT NULL IDENTITY PRIMARY KEY,
ProductName nvarchar(30) NOT NULL,
BatchID int NULL FOREIGN KEY REFERENCES tblBatches (BatchID)
)
CREATE TABLE tblBatches
(
BatchID INT NOT NULL IDENTITY PRIMARY KEY,
BatchCode nvarchar(20) NOT NULL,
Quantity int NOT NULL,
BatchMnf Date NOT NULL,
BatchExp Date NOT NULL,
PurchaseRate int NOT NULL,
SalesRate int NOT NULL,
ProductID int NULL FOREIGN KEY REFERENCES tblProducts (ProductID)
)
Then you need to update reference fields after inserting records in tblBatches and tblProducts.
The good design says you need to create a bridge table like this:
CREATE TABLE tblProductsBatch
(
ID int NOT NULL IDENTITY PRIMARY KEY,
ProductID int NOT NULL,
BatchID int NOT NULL
)
And after inserting product and batch, you need to insert a record in this table to link rows to each other.
DDL:
CREATE TABLE OSOBA
(
id INT PRIMARY KEY not null IDENTITY,
imie VARCHAR(30) not null,
nazwisko VARCHAR(40) not null,
zawod VARCHAR(30) not null,
dataUrodzenia DATE,
plec BIT not null,
narodowosc VARCHAR(30) not null,
);
CREATE TABLE OSOBISTOSCI
(
nazwaMiasta VARCHAR(30) not null,
id INT not null,
adres VARCHAR(50),
czy BIT,
PRIMARY KEY (id),
FOREIGN KEY (id) REFERENCES OSOBA,
FOREIGN KEY (nazwaMiasta) REFERENCES MIASTA,
);
CREATE TABLE ARCHITEKCI
(
id INT not null,
uprawnienia VARCHAR(30) UNIQUE,
styl VARCHAR(30) not null,
liczba INT not null,
PRIMARY KEY (id),
FOREIGN KEY (id) REFERENCES OSOBA,
);
And I have these insert statements:
-- these work fine
INSERT INTO OSOBA (imie,nazwisko,zawod,dataUrodzenia,plec,narodowosc) VALUES ('Lech', 'Wałęsa', 'Elektryk', '19430129', 1, 'Polak');
INSERT INTO OSOBISTOSCI (nazwaMiasta, id, adres, czy) VALUES ('Gdańsk', IDENT_CURRENT('OSOBA'), 'ul. Polanki', 0);
-- these throw the below error message
INSERT INTO OSOBA (imie,nazwisko,zawod,dataUrodzenia,plec,narodowosc) VALUES ('Marcia','Frank','Architekt','19570720','0','Niemiec');
INSERT INTO ARCHITEKCI (id,styl,liczba) VALUES (IDENT_CURRENT('OSOBA'),'Nowoczesny',8);
Error:
Msg 2627, Level 14, State 1, Line 124
Violation of UNIQUE KEY constraint 'UQ_ARCHITEK_982D973E2BA18BE7'. Cannot insert duplicate key in object 'dbo.ARCHITEKCI'. The duplicate key value is ().
As Aaron mentioned the IDENT_CURRENT is not very reliable. It would be better to use an OUTPUT clause. See the following example:
DECLARE #LatestID TABLE (ID INT);
INSERT INTO #OSOBA (imie,nazwisko,zawod,dataUrodzenia,plec,narodowosc)
OUTPUT INSERTED.ID INTO #LatestID
VALUES ('Marcia','Frank','Architekt','19570720','0','Niemiec');
INSERT INTO #ARCHITEKCI (id,styl,liczba) VALUES ((SELECT ID FROM #LatestID),'Nowoczesny',8);