logo
down
shadow

How to find what table foreign key is from?


How to find what table foreign key is from?

By : user2953937
Date : November 21 2020, 07:31 AM
Does that help I have a table with a foreign key. How can I tell what table the FK is a primary key in? There's about 200 tables and I don't know how to find where that info is coming from/connected to. , Use this..
code :
SELECT fk.name,
       Object_name(fk.parent_object_id) [Parent table],
       c1.name                          [Parent column]
FROM   sys.foreign_keys fk
       INNER JOIN sys.foreign_key_columns fkc
               ON fkc.constraint_object_id = fk.object_id
       INNER JOIN sys.columns c1
               ON fkc.parent_column_id = c1.column_id
                  AND fkc.parent_object_id = c1.object_id
       INNER JOIN sys.columns c2
               ON fkc.referenced_column_id = c2.column_id
                  AND fkc.referenced_object_id = c2.object_id
WHERE  Object_name(fk.referenced_object_id) = 'Tablename' -- Replace with your tablename
       AND c2.name = 'Columname' -- Replace with your columname
sp_help Tablename or [Alt]+F1


Share : facebook icon twitter icon
Easy way to find out if there is no foreign key link from table A to table B?

Easy way to find out if there is no foreign key link from table A to table B?


By : Brij
Date : March 29 2020, 07:55 AM
Access - Loop through table and find foreign keys in a second table and Concatenate multiple fields into original table

Access - Loop through table and find foreign keys in a second table and Concatenate multiple fields into original table


By : tyler bagot
Date : March 29 2020, 07:55 AM
Hope that helps If you are okay with adding a new field to your primary table it would be pretty simple using VBA, you could do something like this:
code :
Function updateReasonForChange()
Dim db AS DAO.Database
Dim Req AS DAO.Recordset
Dim Chg AS DAO.Recordset

Set db = currentDb()
Set Req = db.OpenRecordset("tblRequests")
Set Chg = db.OpenRecordset(SQL here to select only changes that have not previously
been added to tblRequests, use a date range would be my preference but you could also
create a yes/no field in the date changes table to mark it as processed)

Req.MoveFirst
Chg.MoveFirst

Do While Not Chg.EOF 'run though all date change records selected
    Req.Seek "=" Chg![RegProjID] 'This assumes you have RegProjID indexed

          If Req.NoMatch
               'do nothing if no match is found, you could add some error handling here
               'but if you have a parent-child relationship you should have no issue
          Else
              Req.Edit
              Req![DESCRIPTION_OF_REQUEST] = Req![DESCRIPTION_OF_REQUEST] & " " & Chg![ReasonForChange]
              Req.Update
          End if
Loop

Req.Close
Chg.Close
db.Close

Set Req = Nothing
Set Chg = Nothing
Set db = Nothing

End Function
SQL Find all rows with foreign key 1 when any of foreign key 2 appear in second table

SQL Find all rows with foreign key 1 when any of foreign key 2 appear in second table


By : SreejithPDas
Date : March 29 2020, 07:55 AM
Hope that helps First you need get the composite keys matching your items, but include DISTINCT to avoid duplicates
code :
SELECT  DISTINCT extid,  extdt
FROM ents
JOIN items
  ON ents.itemid = items.itemid 
SELECT *
FROM ents
JOIN ( SELECT  DISTINCT extid,  extdt
       FROM ents
       JOIN items
         ON ents.itemid = items.itemid 
     ) comp_key
 ON ents.extid = comp_key.extid
AND ents.extdt = comp_key.extdt
SQL I have two table which join with foreign key . I want to find out missing id from second table

SQL I have two table which join with foreign key . I want to find out missing id from second table


By : Sebastian Stone
Date : March 29 2020, 07:55 AM
Any of those help The normative approach is an anti-join pattern.
To find rows in table1 which have an value in the id column which does not have a matching row in table2 (i.e. there are no rows in table2 with fk_id value that match)
code :
 SELECT t1.id
   FROM table1 t1
   LEFT
   JOIN table2 t2
     ON t2.fk_id = t1.id
  WHERE t2.fk_id IS NULL
How to find all tables that have foreign keys that reference particular table.column and have values for those foreign k

How to find all tables that have foreign keys that reference particular table.column and have values for those foreign k


By : Shashi Bharti
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have a table whose primary key is referenced in several other tables as a foreign key. For example: , Here you go:
Related Posts Related Posts :
  • FreeTDS working, but ODBC cannot connect
  • How to change datetime format of varchar datatype column
  • Trigger on extended properties? SQL SERVER
  • Database Schema guidance for an app store
  • sql trigger to update based on specific column is updated and if value is equal to 'x'
  • How to descope a local variable in t-sql
  • SQL Server Compact stored procedure with multiple values in 1 input
  • How to validate the ICCID number of a SIM card in TSQL?
  • how to calculate all previous datas sum of a sql column on a date range report
  • How to create view listing all friendship for "friend1,friend2" table in T-SQL and prevent inserting duplicate
  • Sum of multiple column in SQL Server
  • Check month and day lies between two dates
  • How would I configure Effort Testing Tool to mock Entity Framework's DbContext withOut the actual SQL Server Database up
  • While looping through a function, Error: Maximum stored procedure, function, trigger, or view nesting level exceeded (li
  • Collapsing records with adjacent start and end dates
  • SQL Server Management Studio 2008 - Timeout Expired error
  • Insert date from VB to SQL Server
  • Sql Server Nested Inserts possible
  • Deterministic way of getting WeekEnding (Sun) for given date column (for use in computed column)
  • SQL Server, why does one order by query work with a string, but not with an integer?
  • How do I install a SQL Server on the hosted build agent in Visual Studio Online?
  • EXECUTE AS/REVERT within a transaction in SQL Server
  • Distinct 2 field AS one column SQL SERVER
  • how to create a new database after a trigger fired from a table
  • How to sort string alphabetically
  • Checking data in one column in a temp table to a column in another table
  • What's wrong with my IF/ELSE? "ELSE: Incorrect syntax near 'ELSE'."
  • CASE CLAUSE IN WHERE CONDITION
  • TSQL - Creating a running total of a column
  • Why am I getting not null here?
  • Why does sp_refreshview raise an exception on views that are not schema bound?
  • EXEC parameters from a SELECT
  • SQL Server Compact connection
  • Star Schema Structure - To many Dimensions
  • sort utf string in sql server with join clause
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org