There are no primary or candidate keys in the referenced table 'PayRoll' that match the referencing column list in the foreign key 'fk_EmployeeNumber' - sql

I'm having some problem with the table called HoursandEarnings and I kept getting an error saying,
"There are no primary or candidate keys in the referenced table
'PayRoll' that match the referencing column list in the foreign key
'fk_EmployeeNumber'"
create table Address
(
PostalCode nvarchar(6) not null,
Address nvarchar(50) not null,
City nvarchar(30) not null,
Province nvarchar(30) not null,
constraint pk_postalcode primary key(PostalCode)
)
create table Payperiod
(
StartDate DateTime not null,
EndDate DateTime not null,
constraint pk_StartDate primary key(StartDate)
)
create table PayRoll
(
EmployeeNumber nvarchar(30) not null,
StartDate DateTime not null
constraint fk_StartDate references Payperiod(StartDate),
PostalCode nvarchar(6) not null
constraint fk_PostalCode references Address(PostalCode),
Department nvarchar(50) not null,
TotalEarningsCurrent decimal(5,2) not null,
TotalEarningsYearToDate decimal(5,2) not null,
Netpay decimal(5,2) not null,
EmployeeName nvarchar(30) not null,
constraint pk_PayRoll primary key(EmployeeNumber,StartDate)
)
create table HoursandEarnings
(
EmployeeNumber nvarchar(30) not null
constraint fk_EmployeeNumber references PayRoll(EmployeeNumber),
StartDate DateTime not null
constraint fk_StartDate references Payperiod(StartDate),
HoursAndEarningsDescription nvarchar(50) not null,
HoursandEarningsCurrent decimal(5,2) not null,
HoursandEarningsYearToDate decimal(5,2) not null,
constraint pk_HoursandEarnings primary
key clustered(EmployeeNumber,StartDate,HoursAndEarningsDescription)
)
create table EmployerPaidBenefits
(
EmployeeNumber nvarchar(30) not null
constraint fk_EmployerPaidBenefits_EmployeeNumber references PayRoll(EmployeeNumber),
StartDate DateTime not null
constraint fk_EmployerPaidBenefits_StartDate references Payperiod(StartDate),
EmployerpaidBenefitsDescription nvarchar(50) not null,
EmployerPaidBenefitsCurrent decimal(5,2) not null,
EmployerPaidBenefitsYearToDate decimal(5,2) not null
constraint pk_EmployerPaidBenefits
primary key(EmployeeNumber,StartDate,EmployerpaidBenefitsDescription)
)
create table Taxesanddeductions
(
EmployeeNumber nvarchar(30) not null,
constraint fk_Taxesanddeductions_EmployeeNumber references PayRoll(EmployeeNumber),
StartDate DateTime not null
constraint fk_Taxesanddeductions_StartDate references Payperiod(StartDate),
TaxesandDeductionsDescription nvarchar(50) not null,
TaxesandDeductionsCurrent decimal(5,2) not null,
EmployerPaidBenefitsYeartoDate decimal(5,2) not null,
constraint pk_Taxesanddeductions
primary key(EmployeeNumber,StartDate,TaxesandDeductionsDescription)
)

Because you have a composite key (EmployeeNumber,StartDate) on table PayRoll, you will need to reference both keys in the foreign key in the other tables which reference it.
If EmployeeNumber is unique in table Payroll, then you can make just EmployeeNumber the primary key on Payroll, and then your foreign keys on the other 3 tables to Payroll will be valid as-is.(Not the case)
Not related, but I believe you may run into problems making PostalCode a primary key on Address - this will mean that only one address with the same PostalCode may be added to the table. I would suggest a surrogate key instead.
Edit
Example of both columns in the foreign key:
constraint fk_HE_EmployeeNumber
foreign key(EmployeeNumber, StartDate)
references PayRoll(EmployeeNumber, StartDate)
Note also that primary and foreign key constraint names must be unique within the database (not just in the table) - you'll need to rename the duplicate keys like fk_StartDate.
SqlFiddle here
You may also find at some point that the encumberance and additional storage requirements of composite / compound keys may make implementation of simple surrogate keys (like int identity() or Guids) a simpler approach.

Related

Error in create table T-SQL "Incorrect syntax near ','."

When create table have error "Incorrect syntax near ','."
I cant see this error in code. Please point out this error.
CREATE TABLE books(
id INT NOT NULL IDENTITY PRIMARY KEY,
author VARCHAR(150) NOT null,
date DATETIME NOT null,
city VARCHAR(50) NOT null,
publishing VARCHAR(50) NOT null,
udc INT NOT null,
quantity INT NOT null,
inventory_numbers INT NOT NULL PRIMARY KEY)
CREATE TABLE systematic_catalog(
id INT NOT NULL IDENTITY PRIMARY KEY,
udc_id INT FOREIGN KEY REFERENCES books(udc),
knowledge_area VARCHAR)
CREATE TABLE issued_books(
date_issued DATETIME,
inventory_numbers_id INT FOREIGN KEY REFERENCES books(inventory_numbers))
CREATE TABLE readers(
id INT NOT NULL IDENTITY PRIMARY KEY,
last_name VARCHAR CONSTRAINT,
first_name VARCHAR CONSTRAINT,
middle_name VARCHAR,
phone_number INT(11),
address VARCHAR,
ticket_number INT CONSTRAINT,
date_registration DATETIME,
date_reregistratiom DATETIME,
issued_books_id FOREIGN KEY REFERENCES issued_books(inventory_numbers_id))
You cannot add multiple primary keys to the table, either composite ey (combination of two fields) or Unique constraint can be added instead.
CREATE TABLE books(
id INT NOT NULL IDENTITY PRIMARY KEY,
author VARCHAR(150) NOT null,
date DATETIME NOT null,
city VARCHAR(50) NOT null,
publishing VARCHAR(50) NOT null,
udc INT NOT null,
quantity INT NOT null,
inventory_numbers INT NOT NULL PRIMARY KEY)
this is an error, you cannot ass multiple primary keys. Instead you can add Unique constraint
Correction would be,
CREATE TABLE books(
id INT NOT NULL IDENTITY PRIMARY KEY,
author VARCHAR(150) NOT null,
date DATETIME NOT null,
city VARCHAR(50) NOT null,
publishing VARCHAR(50) NOT null,
udc INT NOT null Unique, --this is used to create the second table
quantity INT NOT null,
inventory_numbers INT NOT NULL Unique )
CREATE TABLE systematic_catalog(
id INT NOT NULL IDENTITY PRIMARY KEY,
udc_id INT FOREIGN KEY REFERENCES books(udc), --Referenced column needs to be unique
knowledge_area VARCHAR)
CREATE TABLE issued_books(
date_issued DATETIME,
inventory_numbers_id INT FOREIGN KEY REFERENCES books(inventory_numbers))
CREATE TABLE readers(
id INT NOT NULL IDENTITY PRIMARY KEY,
last_name VARCHAR (255),
first_name VARCHAR (255),
middle_name VARCHAR(255),
phone_number INT,
[address] VARCHAR(255),
ticket_number INT ,
date_registration DATETIME,
date_reregistratiom DATETIME)
You cannot use a foreign key of another table as a foreign key for some other table. You need to read more on table, Primary key constraints and foreign key referencing.
This cannot be done. you are trying to refer to the foreign key of issued_books table as a foreign key in readers table, which is wrong.
issued_books_id INT FOREIGN KEY REFERENCES issued_books(inventory_numbers_id))
I hope this explanation will give you a better understanding of errors. There were synatx errors as well as wrong use of the SQL table creation steps. I have added the corrected code for you. Feel free to contact any time.

How to reference to primary key?

create database [PostaShpejte]
use PostaShpejte
create table Posta
(
ID_Posta int not null Primary Key,
Emri varchar(50)not null,
Qyteti varchar(15) not null,
)
create table Dergesa
(
ID_Dergesa int IDENTITY(1,1) not null Primary Key,
Emri_Dergeses varchar(30) not null,
Pershkrimi varchar(100),
Qmimi int not null,
Statusi varchar(30) not null,
CONSTRAINT CHK_Statusi CHECK (Statusi='E regjistruar' or Statusi='E nisur' or Statusi='Ne depo' or Statusi='E refuzuar' or Statusi='E derguar'),
)
create table Menaxhon
(
ID_Dergesa int not null references Dergesa (ID_Dergesa),
ID_Posta int not null references Posta(ID_Posta),
Primary Key(ID_Dergesa,ID_Posta),
)
--drop table TelBleresi
create table TelBleresi
(
ID_Tel_Bleresi int not null,
--ID_Bleresi int not null,
NumriTel int not null Unique,
Primary Key(ID_Tel_Bleresi),
)
--drop table Bleresi
create table Bleresi
(
ID_Bleresi int not null,
ID_Tel_Bleresi int not null,
Emri varchar(20) not null,
Mbiemri varchar(20) not null,
Shteti varchar(20) not null,
Qyteti varchar(20) not null,
Rruga varchar(50) not null,
ZIPKodi int not null,
FOREIGN KEY(ID_Tel_Bleresi) references TelBleresi(ID_Tel_Bleresi),
Primary Key (ID_Bleresi , ID_Tel_Bleresi),
)
create table Dergohet
(
ID_Dergesa int not null,
ID_Bleresi int not null,
Data_e_regj date not null,
Data_e_mbrritjes date not null,
----------------PROBLEM HERE---------------------------
FOREIGN KEY (ID_Dergesa) references Dergesa(ID_Dergesa),
FOREIGN KEY (ID_Bleresi) references **Bleresi**(ID_Bleresi),
*Error: There are no primary or candidate key to table Bleresi ....*
---------------------------------------------------------
PRIMARY KEY (ID_Dergesa,ID_Bleresi),
)
Bleresi has a compound primary key (ID_Bleresi, ID_Tel_Bleresi), so you need to reference all columns. That means adding ID_Tel_Bleresi to Dergohet.
create table Dergohet(
ID_Dergesa int not null,
ID_Bleresi int not null,
ID_Tel_Bleresi int not null, -- add this column
Data_e_regj date not null,
Data_e_mbrritjes date not null,
FOREIGN KEY (ID_Dergesa) references Dergesa(ID_Dergesa),
-- Reference the full compound key
FOREIGN KEY (ID_Bleresi, ID_Tel_Bleresi) references Bleresi(ID_Bleresi, ID_Tel_Bleresi),
PRIMARY KEY (ID_Dergesa,ID_Bleresi),
)
While they have some uses, compound primary keys are annoying as they create a proliferation of foreign key columns and complicate indexing. Some of yours seem unnecessary: Bleresi already has a ID_Bleresi, is that not unique?
In general, I'd recommend using simple big integer (2 billion creeps up on you surprisingly fast) auto incrementing primary keys. If you need to guarantee other uniquenesses, make a unique index.
The error say that ID_Bleresi is not the primary key on Bleresi table and for that can't be a foreign key. The primary key is:
Primary Key (ID_Bleresi , ID_Tel_Bleresi)
If ID_Bleresi is not a unique column, I recommend that in the table, you create a new unique column that is the primary key. In case it is, it would be best to set ID_Bleresi as the unique primary key

SQL Syntax error with foreign keys

now i have table place CREATE TABLE [dbo].[Place] (
[Place_Id] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR (50) NOT NULL,
[Building_Date] DATE NULL,
[Longitude] VARCHAR (50) NULL,
[Latitude] VARCHAR (50) NULL,
[Location] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Place_Id] ASC)
); , and table Citeria CREATE TABLE [dbo].[Criteria] (
[Criteria] VARCHAR (50) NOT NULL,
[Place_Id] INT NOT NULL, PRIMARY KEY CLUSTERED ([Criteria], [Place_Id]), CONSTRAINT [FK_Criteria_ToTable] FOREIGN KEY (Place_Id) REFERENCES Place(Place_Id)
);and The referenced table '[dbo].[Criteria]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted.
.
The error is correct. Subqueries are not allowed in check constraints.
But, you already have a foreign key reference between user_name and likes(user_name), so this condition is already in place. The only thing is would really be checking is that user_name is not NULL, but that is already true by the definition of the column.
Now, there are other issues. Your foreign keys should be to primary keys or unique keys in other tables. I think this is your intention:
CREATE TABLE [dbo].Normal_Upload
(
[User_Name] VARCHAR(50) NOT NULL ,
[Place_Id] INT NOT NULL,
[Image] IMAGE NOT NULL,
CONSTRAINT [FK_Normal_Upload] FOREIGN KEY (User_Name) REFERENCES Member(User_Name),
CONSTRAINT [FK_Normal_Upload_1] FOREIGN KEY (Place_Id) REFERENCES Place(Place_Id),
CONSTRAINT [FK_Normal_Upload_2] FOREIGN KEY (User_Name, Place_Id) REFERENCES Likes(User_Name, Place_Id)
);
As a note on naming. I think the primary keys of tables should include the table name. So, consider Member_Name rather than User_Name for the Member table.
It wrong db design, never use varchar or character type column in reference key or primary-key(try to avoid as much as possible).
For you solution, create a column "useid" with int datatype and give pk to it. and update the following table
CREATE TABLE [dbo].[Likes] (
[User_id] VARCHAR (50) Identity (1,1),
[User_Name] VARCHAR (50) NOT NULL,
[Place_Id] INT NOT NULL,
CONSTRAINT [PK_Likes] PRIMARY KEY CLUSTERED ([User_id] ASC, [Place_Id] ASC),
CONSTRAINT [FK_Likes_ToTable] FOREIGN KEY ([User_Name]) REFERENCES Normal ([User_Name]),
CONSTRAINT [FK_Likes_ToTable_1] FOREIGN KEY ([Place_Id]) REFERENCES [dbo].[Place] ([Place_Id]),
);

How to insert multiple rows into an Order table?

So I set up a simple database and I am at the point where I am trying to test that it is working correctly. I am trying to figure out how I get multiple entries into an Order table. I need to have an order# and be able to order multiple items and use multiple shippers and such. I just can't seem to figure out a way to get the data into my DB properly.
Below is how I have my DB set up. Could someone please explain to me how to get the my test data into the DB.
Here are the reference tables:
CREATE TABLE Product_Table (
ProductID INT NOT NULL,
Product_Name char(50) NOT NULL,
Product_Cost number(9,2) NOT NULL,
Product_In_Stock INT NOT NULL,
CONSTRAINT Products_PK PRIMARY KEY (ProductID)
);
CREATE TABLE Payment_Terms_Table (
PayTermNum INT NOT NULL,
Payment_Time_Frame CHAR(20) NOT NULL,
CONSTRAINT Payment_Terms_PK PRIMARY KEY (PayTermNum)
);
CREATE TABLE Shipper_Table (
ShipperNum INT NOT NULL,
Shipper_Name CHAR(50) NOT NULL,
Shipper_Phone CHAR(22) NULL,
CONSTRAINT ShipperNum_PK PRIMARY KEY (ShipperNum)
);
CREATE TABLE Supplier_Table (
SupplierID INT NOT NULL,
Supplier_Name CHAR(50) NOT NULL,
Sup_Address CHAR(50) NOT NULL,
Sup_City CHAR(20) NOT NULL,
Sup_State CHAR(20) NOT NULL,
Sup_Zip CHAR(9) NOT NULL,
Sup_Phone CHAR(22) NULL,
ShipperNum INT NOT NULL,
PayTermNum INT NOT NULL,
CONSTRAINT Supplier_PK PRIMARY KEY (SupplierID),
CONSTRAINT ShipperNum_Relationship FOREIGN KEY (ShipperNum)
REFERENCES Shipper_Table (ShipperNum),
CONSTRAINT PayTermNum_Relationship FOREIGN KEY (PayTermNum)
REFERENCES Payment_Terms_Table (PayTermNum)
);
Here is my Order table:
CREATE TABLE Order_Table (
OrderID INT NOT NULL,
ProductID INT NOT NULL,
SupplierID INT NOT NULL,
Wholesale_Price NUMBER (9,2) NOT NULL,
Units_Ordered INT NOT NULL,
Order_Date DATE DEFAULT SYSDATE NOT NULL,
Order_Received DATE NULL,
CONSTRAINT Order_PK PRIMARY KEY (OrderID),
CONSTRAINT ProductID_Relationship FOREIGN KEY (ProductID)
REFERENCES Product_Table (ProductID),
CONSTRAINT SupplierID_Relationship FOREIGN KEY (SupplierID)
REFERENCES Supplier_Table (SupplierID)
);
Your problem is that you have a defined one table to hold Orders, so you can have only one Item per Order. Normally we handle this scenario by having two tables: a Header with the information for the whole Order and a Line table for each ordered Item.
CREATE TABLE Order_Header (
OrderID INT NOT NULL,
Order_Date DATE DEFAULT SYSDATE NOT NULL,
Order_Received DATE NULL,
CONSTRAINT Order_PK PRIMARY KEY (OrderID)
)
/
CREATE TABLE Order_Line (
OrderID INT NOT NULL,
LineNo INT NOT NULL,
ProductID INT NOT NULL,
SupplierID INT NOT NULL,
Wholesale_Price NUMBER (9,2) NOT NULL,
Units_Ordered INT NOT NULL,
CONSTRAINT Order_Line_PK PRIMARY KEY (OrderID, LineNo),
CONSTRAINT Order_Line_Header_FK FOREIGN KEY (OrderID)
REFERENCES Order_Header (OrderID)
CONSTRAINT ProductID_Relationship FOREIGN KEY (ProductID)
REFERENCES Product_Table (ProductID),
CONSTRAINT SupplierID_Relationship FOREIGN KEY (SupplierID)
REFERENCES Supplier_Table (SupplierID)
)
/
I have declared a composite primary key because it is easier to understand what's happening.
Now that you have two tables you can easily create orders with more than one line.
On the subject of normal practice, an Order should have a CUSTOMER. That would be an attribute of ORDER_HEADER. Also you have a SHIPPER table but don't use it. Again that would probably be an attribute of ORDER_HEADER.
Also your naming convention is ugly. There's no need to include _TABLE: just name the objects for teh things they represent. Likewise your foreign key needs are opaque; it doesn't matter so much in this toy example but in a real database you will find it helpful to specify the child and parent tables in the FK names.

Created Primary Key on SQL Server not Identified

CREATE TABLE sampProduct
(
Product_ID VARCHAR(15) NOT NULL,
Supplier_ID INT NOT NULL,
Category_ID INT NOT NULL,
Unit_Price DECIMAL(10, 2)
PRIMARY KEY(Product_ID)
)
CREATE TABLE sampMachine
(
M_Product_ID VARCHAR(15) NOT NULL,
Serial_No VARCHAR(15) NOT NULL,
Machine_Model VARCHAR(20),
PRIMARY KEY(M_Product_ID, Serial_No),
FOREIGN KEY(M_Product_ID) REFERENCES sampProduct(Product_ID)
)
CREATE TABLE sampService
(
Service_ID VARCHAR(15) NOT NULL,
Serial_No VARCHAR(15) NOT NULL,
Complaint VARCHAR(40) NOT NULL,
PRIMARY KEY(Service_ID),
FOREIGN KEY(Serial_No) REFERENCES sampMachine(Serial_No)
)
Machine is a subtype of Product. M_ProductID is referencing the primary key of the parent table Product by this key. And Serial_No is used to uniquely identify a Machine. The problem is, I want to create a foreign key from Serial_No into the Service table, since I want to track the serial numbers of the machine (this is because, for example, we can have many machine for each model or type, but each of them has their unique serial numbers), but there is an error for the creation. Can you help me out?
One of the requirements for creating a foreign key is that the table you reference must have a unique key on the columns you want referenced. So in your example in order for it to work you would need a unique key on sampMachine (Serial_No) so that you could create foreign keys to it using just Serial_No. The way you have it now you would need to use M_Product_Id in the table sampService and add it to the foreign key definition.
create table sampService(
M_Product_ID varchar(15) not null,
Service_ID varchar(15) not null,
Serial_No varchar(15) not null,
Complaint varchar(40) not null,
primary key (Service_ID),
foreign key (Serial_No) references sampMachine(m_Product_Id, Serial_No)
)