SQL check for unique column combination entry - sql

I have the following table "Pcategory", with the following 4 columns and PCategoryID is my primary key,
Pcategory Table
PCategoryID(PK) |TermID (FK)|SubCategoryID (FK)|CategoryID(FK)
PK -> Primary key
FK -> Foreign key
Please let me know how to write a SQL query to check the table has a unique combination of foriengn keys TermID (FK)|SubCategoryID (FK)|CategoryID (FK).
(i.e) I would be needing a list of duplicate records if any from the table with the combination of these 3 foriegn keys

Hope the below is what you are looking for :
select termid,subcategoryid,categoryid,count(1)
from pcategory
group by termid,subcategoryid,categoryid
having count(1) > 1

Related

why I am getting error trying to add Foreign key to table

I Not able to add foreign key to tbl1 referencing tbl2 (Desc), here are the two tables :
please notice Tbl1 includes all values,tbl2 Has more values that doesn't necessarily exists in tbl1
Error(Msg 1776): There are no primary or candidate keys in the referenced table 'tbl2' that match the referencing column list in the foreign key 'fk_desc'.
tbl1:
alter table tbl1 add constraint pk_desc primary key (desc)
The Error:**alter table tbl1 add constraint fk_desc foreign key (desc)
references tbl2(desc)**
Desc
Astrogator
Geologist
Technician
tbl2:
alter table tbl2 add constraint pk_canid_desc primary
key(id,desc)
ID
Desc
1001
Astrogator
1001
Geologist
1001
Technician
2003
Biochemist
thank you for the help,
you cant make Desc that at table tbl2 as a FK while it contains values not exists at the table tbl1
Adding a foreign key is different depending on the flavor of SQL your using. For sql server you would bring up the parent db in SSMS using table design. Select Foreign key relationships and add a new relationship. From there select other table and column which has foreign key. Keep in mind that there needs to exist a foreign key for each Primary record or you will get an error.

Database join question . How to connect 2 tables correctly

I have a question. I have 2 tables and I want to inner join from left to right.
The left table contains a foreign key from the right table and it has his own primary key.
Should I join with the primary key + foreign key from the left table since I got the foreign key which has a connection to the right table?
Like for example
ON (left table primary key = left table foreign key)
But I can also use the left and the right table primary keys but what is then the different
ON (left table primary key = right table primary key)
In order to locate a row in a SQL table it needs to be uniquely identified by a primary key, this can be a numeric or string value and could use more than one field (a composite primary key).
When this information is used in a related table to reference the original row it is known as a foreign key. You do not need to combine the table's primary key with any foreign key that table holds in order to join the tables.
So you need a join where the left_table.foreign_key = right_table.primary_key
Without some table definitions and names of the actual fields you are using it's hard to give a more specific answer.

SQL Insert into table with 2 non-unique primary keys

I have a table that I need to insert data into.
The table Sales has 4 columns
CustomerValueType_id (int)
Customer_id (int)
Value (NVARCHAR)
Customer_name (NVARCHAR)
The CustomerValueType_id, Customer_id are foreign keys that are not-unique, CustomerValueType_id matches to a value type, while Customer_id matches to the Customer_name.
I need to add additional customer data into the Valuecolumn but how do I ensure that the data matches to the correct CustomerValueType_id and Customer_id and each customer name has to be repeated in the Customer_name
Sales Table
You are trying to do denormalization.
Ideally, in the normalized design, you will just store, parent_ids in
the child table. If you want to see parent_name fields, you have to
get them based on JOIN conditions.
But, here as you are storing both id, name in the child table, I would suggest you to create composite Foreign key in the child table, so that the combination is always present. Also, make this combination as composite primary key in the parent table.
-- Parent Table PRIMARY KEY
ALTER TABLE dbo.CustomerValueType
ADD CONSTRAINT PK_CustomerValueType
PRIMARY KEY CLUSTERED (CustomerValueType_id, Value);
GO
--Child Table FOREIGN KEY
ALTER TABLE dbo.Sales
ADD CONSTRAINT FK_Sales_CustomerValueType
FOREIGN KEY (CustomerValueType_id,Value)
REFERENCES dbo.CustomerValueType(CustomerValueType_id,Value);
GO

Check constraint references to unique column in the same table

I have a table:
ID Name ParentID
== ==== ========
1 A Null
2 B 1
3 C 2
so A is parent of B, and B is parent of C.
I need to create a constraint for column(ParentID) which does not accept a number is not exist in column (ID).
How I would do that?
You need to add Foreign Key.
ALTER TABLE table1
ADD CONSTRAINT fk_parent_id
FOREIGN KEY (parent_id) REFERENCES table1(id);
You can also create a foreign key using SSMS design as shown below. Expand table and right click on Keys - New Foreign key
Now click on the browse button of relationship and specify the colum nane of primary and foreign key table as shown below.
This is one of the way and you might select the preferred way either using query or design for creating the foreign key relationships between tables in Sql Server.

There are no primary or candidate keys in the referenced table '' that match the referencing column

I have a table that has a primary key voucher_no (varchar(10)) and I am trying to create a FK to this table/column from another new table but I am getting an error:
There are no primary or candidate keys in the referenced table 'apinv_hdr' that match the referencing column list in the foreign key 'fk_invoice_cfdi_x_voucher_apinv_hdr'
I have several other FK's tied to this table/column - why would it react this way now?
Primary Key and Foreign Key data types must match. Have you verified the column data types are the same?
Looks like some voucher_no record in another new table doesn't exist in voucher_no in main table.
Below script may help you.
select *
from another_new_table
where voucher_no not in (select voucher_no
from main_table)
If above query returns rows, you have two options:
delete those records from another_new_table or
insert records into main_table