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);
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)
This is code to create the beginnings of a book library database with Microsoft SQL Server Management Studio.
CREATE DATABASE BOOK_LIBRARY
CREATE TABLE LIBRARY_USER
(
usr_id int not null primary key,
f_name varchar(30) not null,
m_init char(1),
l_name varchar(30) not null,
balance decimal(6,2),
join_date date,
addrss_1 varchar(30) not null,
addrss_2 varchar(30),
city varchar(30) not null,
addrss_state char(2) not null,
zip_code varchar(10) not null,
email varchar(30)
);
CREATE TABLE LIBRARY_TRANSACTIONS
(
transaction_id int not null primary key,
maximum_borrow_duration int not null,
strt_date date not null,
actual_return_date date not null,
borrow_usr_id int not null,
foreign key (borrow_usr_id) references LIBRARY_USER(usr_id)
);
CREATE TABLE BOOKS
(
isbn varchar(17) not null primary key,
title varchar(30) not null,
number_of_copies int not null,
Author varchar(30) not null,
Number_of_pages int not null,
publish_year int not null,
book_type varchar(20)
);
CREATE TABLE DIGITAL_BOOKS
(
digital_id int not null primary key,
format varchar(30) not null,
size_mb int not null,
digital_isbn varchar(17) not null,
foreign key(digital_isbn) references BOOKS(isbn)
);
CREATE TABLE PHYSICAL_BOOKS
(
physical_id int not null primary key,
condition varchar(20) not null,
physical_isbn varchar(17) not null,
foreign key(physical_isbn) references BOOKS(isbn)
);
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id),
foreign key(digi_id) references DIGITAL_BOOKS(digital_id),
foreign key(phys_id) references PHYSICAL_BOOKS(physical_id)
);
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
Despite me being able to look and see that digi_id and phys_id are in fact primary keys of the book_copy table, I keep getting this error:
Msg 1776, Level 16, State 0, Line 66
There are no primary or candidate keys in the referenced table 'BOOK_COPY' that match the referencing column list in the foreign key 'FK__CONTNS__digi_id__37A5467C'.
Msg 1750, Level 16, State 1, Line 66
Could not create constraint or index. See previous errors.
Am I missing something obvious? I'm just starting out with using this program, so any help is greatly appreciated.
Here:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
You are creating two foreign keys to parent table BOOK_COPY. But that table has a compound primary key (ie a multi-column primary key)
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id), --> here
...
)
As a consequence, you need a compound foreign key:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id, phys_id) references BOOK_COPY(digi_id, phys_id)
);
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 )
I am new to SQL and SQL Server. I am trying to implement a small database. However, I keep getting three errors.
Error #1:
String or binary data would be truncated
Error #2:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Formation__Locat__25869641"
Error #3:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DATA__Classifica__2B3F6F97".
I've tried a variety of different changes and nothing seems to work. Can someone explain to me what I am doing wrong? Thanks
CREATE TABLE Location
(
LocationID CHAR(5) NOT NULL,
LocationName VARCHAR(75) NOT NULL,
PRIMARY KEY (LocationID)
);
CREATE TABLE Formation
(
FormationID CHAR(5) NOT NULL,
FormationName VARCHAR(75) NOT NULL,
RockType VARCHAR(75) NOT NULL,
LocationID INT NOT NULL,
PRIMARY KEY (FormationID),
FOREIGN KEY (LocationID) REFERENCES Location(LocationID)
);
CREATE TABLE Classification
(
ClassificationID CHAR(5) NOT NULL,
ClassificationType VARCHAR(75) NOT NULL,
ClassificationDescription VARCHAR(75) NOT NULL,
FormationID VARCHAR(5) NOT NULL,
PRIMARY KEY (ClassificationID),
FOREIGN KEY (FormationID) REFERENCES Formation(FormationID)
);
CREATE TABLE DATA
(
FossilID CHAR(5) NOT NULL,
FossilName VARCHAR(75) NOT NULL,
FossilType VARCHAR(75) NOT NULL,
CatalogueDate DATE NOT NULL,
ClassificationID VARCHAR(5) NOT NULL,
PRIMARY KEY (FossilID),
FOREIGN KEY (ClassificationID) REFERENCES Classification(ClassificationID)
);
INSERT INTO Location
VALUES ('001', 'Montana');
INSERT INTO Formation
VALUES ('R01', 'Hell Creek Formation', 'Cretaceous', 001);
INSERT INTO Classification
VALUES ('001', 'Saurischia', 'Brachiosauridae', 'Hell Creek Formation');
INSERT INTO DATA
VALUES ('F01', 'Brachiosaurus', 'Vertebrae', '01/MAY/2017', '001');
INT <> VARCHAR(5) <> CHAR(5). Column should be the same type:
CREATE TABLE Location (
LocationID CHAR(5) NOT NULL,
LocationName VARCHAR(75) NOT NULL,
PRIMARY KEY (LocationID)
);
CREATE TABLE Formation (
FormationID CHAR(5) NOT NULL,
FormationName VARCHAR(75) NOT NULL,
RockType VARCHAR(75) NOT NULL,
LocationID CHAR(5) NOT NULL, -- here
PRIMARY KEY (FormationID),
FOREIGN KEY (LocationID) REFERENCES Location(LocationID)
);
CREATE TABLE Classification (
ClassificationID CHAR(5) NOT NULL,
ClassificationType VARCHAR(75) NOT NULL,
ClassificationDescription VARCHAR(75) NOT NULL,
FormationID CHAR(5) NOT NULL, -- here
PRIMARY KEY (ClassificationID),
FOREIGN KEY (FormationID) REFERENCES Formation(FormationID)
);
CREATE TABLE DATA (
FossilID CHAR(5) NOT NULL,
FossilName VARCHAR(75) NOT NULL,
FossilType VARCHAR(75) NOT NULL,
CatalogueDate DATE NOT NULL,
ClassificationID CHAR(5) NOT NULL,
PRIMARY KEY (FossilID),
FOREIGN KEY (ClassificationID) REFERENCES Classification(ClassificationID)
);
DBFiddleDemo
And your insert statements:
INSERT INTO Location VALUES ('001', 'Montana');
INSERT INTO Formation VALUES ('R01', 'Hell Creek Formation', 'Cretaceous', '001'); --corrected
INSERT INTO Classification VALUES ('001', 'Saurischia', 'Brachiosauridae', 'R01'); --corrected
INSERT INTO DATA VALUES ('F01', 'Brachiosaurus', 'Vertebrae', '01/MAY/2017', '001');
Truncation error 'Hell Creek Formation' cannot fit CHAR(5). You need to refer PK. Also consider using:
INSERT INTO tab(col_name1, col_name2, ...)
VALUES(...)