sql server 2019 foreign key on multiple columns from different tables - sql

so i have to create three tables :
the first one is :
create table TblProf(profId int IDENTITY(1,1) PRIMARY KEY
,profName nvarchar(50)
,profUsername nvarchar(50));
and the second one is
create table TblStudent(studentId int IDENTITY(1,1) PRIMARY KEY
,studentName nvarchar(50)
,studentUsername nvarchar(50));
and i want to create the third table which should have two foreign keys the first one is profID from the table TblProf and the secaond one is studentId from the table TblStudent.
so it's two foreign keys from two diffrent tables ,is it possible ?
if so how should i create it?

This is called a bridge table
CREATE TABLE bridge(
refprofId int FOREIGN KEY REFERENCES TblProf(profId ),
refstudentId int FOREIGN KEY REFERENCES TblStudent(PersonID)
,PRIMARY KEY(refprofIdm,refstudentId));
you can add more columns, when the bridge has its own propertoes

Related

Sql creating table consisting of keys from other tables

this is probably a simple question but I am quite new to SQL and databases, so I have been following this site: https://www.postgresqltutorial.com/postgresql-foreign-key/ to try and create a table that consist of primary keys from other tables.
Here I have the structure of the database in an excel overview. With colors showing the relations. i am having problems with the One-To-Many tables. As I get the same error every time "ERROR: column "id" referenced in foreign key constraint does not exist
SQL state: 42703".
The SQL query:
DROP TABLE IF EXISTS ingredient_to_unit_relations;
DROP TABLE IF EXISTS ingrediens;
CREATE TABLE ingrediens (
id serial,
name_of_ingredient varchar(255),
price_per_unit int,
PRIMARY KEY (id)
);
CREATE TABLE ingredient_to_unit_relations (
ingredient_relation_id int GENERATED ALWAYS AS IDENTITY,
PRIMARY KEY (ingredient_relation_id),
constraint Fk_ingredient_id
FOREIGN KEY (id)
REFERENCES ingrediens (id)
);
You need to define the column in order to declare it as a foreign key:
CREATE TABLE ingredient_to_unit_relations (
ingredient_relation_id int GENERATED ALWAYS AS IDENTITY,
ingredient_id int,
PRIMARY KEY (ingredient_relation_id),
constraint Fk_ingredient_id FOREIGN KEY (ingredient_id) REFERENCES ingrediens (id)
);
I might recommend some somewhat different naming conventions (I changed the name id in the table above):
CREATE TABLE ingredients (
ingredient_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name varchar(255),
price_per_unit int
);
CREATE TABLE ingredient_to_unit_relations (
ingredient_relation_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
ingredient_id int,
CONSTRAINT Fk_ingredient_id FOREIGN KEY (ingredient_id) REFERENCES ingredients (ingredient_id)
);
Notes:
I am a fan of naming primary keys after the table they are in. That way, foreign keys and primary keys usually have the same name (and you can use using if you choose).
Avoid SERIAL. GENERATED ALWAYS AS IDENTITY is now recommended.
You can inline primary key constraints (as well as other constraints).
There is not generally a need to repeat the table name in a column (other than the primary key). So, name instead of name_of_ingredient.
Using int for a monetary column is suspicious. It doesn't allow smaller units. That might work for some currencies but in general I would expect a numeric/decimal type.

SQL Server : multi CASCADE Operation

I have two tables, and I want HEDE2 columns as FOREIGN KEY REFERENCES by HEDE table. FOR creating second table it will not allow because its having warning:
More than one key specified in column level FOREIGN KEY constraint, table 'HEDE2'.
But when I tried to ALTER TABLE HEDE2 for FOREIGN KEY it allows me to do that. Is anybody knows WHY this happens. Is this a bug?
CREATE TABLE cascde.HEDE
(
HedeID INT,
HedeID2 INT,
HedeID3 INT
CONSTRAINT PK_HEDE
PRIMARY KEY (HedeID, HedeID2, HedeID3)
)
GO
CREATE TABLE HEDE2
(
Hede2ID INT PRIMARY KEY IDENTITY(1,1) ,
HedeID INT,
HedeID2 INT,
HedeID3 INT
CONSTRAINT FK_HedeID
FOREIGN KEY (HedeID, Hede2ID, HedeID3)
REFERENCES cascde.HEDE (HedeID, HedeID2, HedeID3)
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
Altering table HEDE 2 for foreign key. This allows me to do that:
ALTER TABLE cascde.HEDE2
ADD CONSTRAINT FK_HEDE
FOREIGN KEY(HedeID, HedeID2, HedeID3)
REFERENCES cascde.HEDE (HedeID, HedeID2, HedeID3)
ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
You're missing a comma (,) after HedeID3 INT in the CREATE TABLE version.

How to link tables in SQL

I have two tables and i want to create a third that references the other two.
stock_group has groupno as primary key
stock_group2s also has groupno as primary key
I want to create table called stock_group3s, with 5 columns:
key int AUTO INCREMENT PRIMARY KEY
groupno INT
group1 [stock_group.groupno]
group2 [stock_group2s.groupno]
name VARCHAR (30)
The two linked columns then must contain only a value that can be found in their referenced tables.
How do set this up? I am using SQL Server 2008 R2 with SQL Server 2008 Management Studio
perhaps you are looking for
create table stock_group (groupno int primary key)
create table stock_group2s(groupno int primary key)
create table stock_group3s
(
IDkey int PRIMARY KEY IDENTITY(1,1)
,groupno INT
,group1 INT
,group2 INT
,name VARCHAR (30)
)
ALTER TABLE stock_group3s WITH NOCHECK ADD CONSTRAINT FK_stock_group3s_stock_group FOREIGN KEY(group1)
REFERENCES stock_group (groupno)
ALTER TABLE stock_group3s WITH NOCHECK ADD CONSTRAINT FK_stock_group3s_stock_group2s FOREIGN KEY(group2)
REFERENCES stock_group2s (groupno)

Not able to create foreign key "There are no primary or candidate keys in the referenced table"

This is my table 1:
CREATE TABLE PurchasedProducts
(
Purchase_Order_No int,
Purchase_Product_ID int FOREIGN KEY REFERENCES Inventory(In_Product_ID),
Purchase_Quantity int NOT NULL,
Purchase_Status varchar(7) NOT NULL,
PRIMARY KEY(Purchase_Order_No, Purchase_Product_ID)
);
This my table 2:
CREATE TABLE PurchasedDate
(
PD_PO_No int NOT NULL PRIMARY KEY FOREIGN KEY REFERENCES PurchasedProducts(Purchase_Order_No),
PD_Date date NOT NULL
);
I executed the first table successfully, but when I execute the second table It is showing this error message:
There are no primary or candidate keys in the referenced table 'PurchasedProducts' that match the referencing column list in the foreign key 'FK__Purchased__PD_PO__0B5CAFEA'.
I don't what is the problem is. Please help me!
The primary key in your PurchasedProducts table is made up of two columns:
PRIMARY KEY(Purchase_Order_No, Purchase_Product_ID)
So any child table that wants to reference that also must have these exact two columns:
CREATE TABLE PurchasedDate
(
PD_PO_No int NOT NULL PRIMARY KEY,
Purchase_Product_ID INT NOT NULL,
PD_Date date NOT NULL
);
ALTER TABLE dbo.PurchasedDate
ADD CONSTRAINT FK_PurchaseDate_PurchasedProducts
FOREIGN KEY(PD_PO_No, Purchase_Product_ID)
REFERENCES PurchasedProducts(Purchase_Order_No, Purchase_Product_ID)
A foreign key can only reference the whole primary key of a parent table - you cannot reference only one column out of 2 from the parent table's PK.

SQL Management Studio 2012 Foreign Key Is A Mess

I am trying to add a foreign key to a table. I have created two tables.
CREATE TABLE madeupbusiness.staff
(
staffnum int NOT NULL,
forename varchar(30) NOT NULL,
surname varchar(30) NOT NULL,
meeting int NOT NULL,
PRIMARY KEY (staffnum),
)
GO
meeting should use the PK from from the meeting table to create a FK :
CREATE TABLE madeupbusiness.meeting
(
meetingnum int NOT NULL,
room varchar(30) NOT NULL,
PRIMARY KEY (meetingnum),
)
GO
To create the foreign key I run this query
ALTER TABLE madeupbusiness.staff
WITH CHECK
ADD CONSTRAINT FK_staff_meetingnum FOREIGN KEY (meetingnum)
REFERENCES madeupbusiness.meeting(meetingnum)
ON DELETE CASCADE
ON UPDATE CASCADE
;
GO
The query runs but when I create a database diagram there is a square loop the staff table from the staffnum key back onto it. Sorry but I don't really know how to describe it. There is no relationship between the two tables. What am I doing wrong?
I have tried to add the relationship from design view but the foreign key table is greyed out.
If you can rebuild the Table do this:
CREATE TABLE madeupbusiness.meeting
(meetingnum int NOT NULL PRIMARY KEY REFERENCES madeupbusiness.meeting(YourColumnYouWantItShouldBeReferenced),
room varchar(30) NOT NULL);
GO