I have 3 different sql environments which are similar but not same. I have a column "CLMID" used in different tables. Most of them are linked with the foreign key mapping. But some tables are corrupted so do not have the foreign key mapping done right. I now need to update a data into this column in all tables. So I need a query which will find for me the list of tables that are having proper mapping, so that if I update the parent they update the child by them self. I also need to find the constraint the other way where, only if I update the child I can update the parent table.
Please note that I need to get this list without doing any update or insert operations as Its a critical database. Please help me with the query. Thank you.
Every SQL product has some way to query the database for schema data, including keys. Learning to use the Information_Schema is best IMHO because it is a standard across most platforms (as I recall Oracle does not implement it).
SQL Server probably has some easier queries with its own catalogs, but the following somewhat hairy query gives you exactly what you want and is the most cross-platform you can get.
SELECT
FK.TABLE_NAME as child_table,
CU.COLUMN_NAME as child_column,
PK.TABLE_NAME as parent_table,
PT.COLUMN_NAME as parent_column,
C.CONSTRAINT_NAME
FROM
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK
ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN
(
SELECT
i1.TABLE_NAME, i2.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2
ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT
ON PT.TABLE_NAME = PK.TABLE_NAME
ORDER BY
1,2,3,4
try this
show create table table-name
Upon seeing this, you can see the foreign keys the columns have. This doesnt change any of the table data
Check out sys.foreign_keys for some SQL Server-specific action. Specifically, it seems that you're interested in the delete_referential_action_desc and update_referential_action_desc columns. As far as I know, there's no way to update a child table and have the update automatically propagate to the parent. I'm willing to be corrected, though.
Related
Provided there are tables Application, Person, Status, etc...
Status and Person have a link to Application.
Is there a simple sql query that may be written that will give a list of all the tables linked to the Application table (in contrast of using the database diagram).
Application expected results:
Status
Person
Please advise on a feasible solution (if any).
Using SP_depends. There are other ways as well like DMV's.
sp_depends 'TableName'
Assuming you're using MSSQL Server then you can run
EXEC sp_fkeys 'YourTableName'
Otherwise you can use
SELECT *
FROM information_schema.referential_constraints
WHERE table_name = <tablename>
to establish the foreign key relationships
Another way to check each table name that references to other tables would be this:
SELECT DISTINCT OBJECT_NAME(f.parent_object_id) AS TableName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o
ON o.OBJECT_ID = fc.referenced_object_id
You can, of course, add more conditions to check for a single table, for example:
WHERE OBJECT_NAME(f.referenced_object_id) = 'Application'
This should give you a result like:
TableName ReferenceTableName
---------- ------------------
Status Application
Person Application
I am using following code to get all tables that have a foreign key of the table in where clause which in this example is SolutionUser :
select
(select name from sys.tables
where object_id = fk.parent_object_id) Name
from
sys.foreign_keys fk
left outer join
sys.tables st on fk.referenced_object_id = st.object_id
where
st.name='SolutionUser'
What I can't achieve is to get the cardinality of these relationships (one-to-one, one-to-many)
Is it possible to get these cardinalities using T-SQL ?
This is a conceptual subject. You can't find out using schema or even data.
In PostgreSQL, running \d command will list the tables along with their table type. Currently I'm trying to list out all foreign tables that I've created using foreign data wrapper. What is the query to list out those tables?
Query for listing foreign tables only:
select * from information_schema.foreign_tables
According to the manual \dE[S+] should do it.
http://www.postgresql.org/docs/current/static/app-psql.html
To see the query behind this, start psql with the -e ("echo queries") option.
Or use the information_schema.tables view: http://www.postgresql.org/docs/current/static/infoschema-tables.html
The table_type column will contain FOREIGN TABLE for those tables.
You can also use this command:
\detr
SELECT tc.table_name , ctu.table_name
FROM information_schema.table_constraints tc
inner join information_schema.constraint_table_usage ctu on ctu.constraint_name = tc.constraint_name
where constraint_type = 'FOREIGN KEY'
Is there a query I can do to find which tables have foreign keys on a given table? Our DBA does not believe in (or understand?) "ON DELETE CASCADE", so I when I delete something from a table, I want to make sure I delete all the dependent stuff first.
(Note, I don't need to find the tables programmatically, I can do that in SQL*Plus.)
SELECT dc.constraint_name, dc.constraint_type, dc.owner, dc.table_name
FROM dba_cons_columns dcc
JOIN dba_constraints dc ON (dcc.constraint_name = dc.r_constraint_name and dc.owner = dcc.owner)
WHERE dcc.owner = 'OWNER_NAME' and dcc.table_name = 'TABLE_NAME';
Check all_constraints and all_cons_columns dictionaries.
I would like to generate a Data Dictionary for a SQL Server 2008 database that has one row for each field, and the following columns:
table_name
field_name
data_type
link_table (for when the field in question is a foreign key)
link_field (for when the field in question is a foreign key)
I can get the first 3 columns with something like the SQL script below...but I don't know how to get the last two columns of foreign key information. INFORMATION_SCHEMA.TABLE_CONSTRAINTS gets close, but doesn't have the data I'm looking for. Can someone help with this point?
SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
Secondarily if anyone has any suggestions on additional fields which would be helpful please post.
The following query will duplicate a column if it is involved in more than one relationship.
Select C.TABLE_SCHEMA, C.TABLE_NAME, C.COLUMN_NAME, C.DATA_TYPE
, PKCol.TABLE_SCHEMA, PKCol.TABLE_NAME, PKCol.COLUMN_NAME
From INFORMATION_SCHEMA.COLUMNS As C
Left Join (INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As FKCol
Join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS As FK
On FK.CONSTRAINT_NAME = FKCol.CONSTRAINT_NAME
Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As PKCol
On PKCol.CONSTRAINT_NAME = FK.UNIQUE_CONSTRAINT_NAME)
On FKCol.TABLE_SCHEMA = C.TABLE_SCHEMA
And FKCol.TABLE_NAME = C.TABLE_NAME
ANd FKCol.COLUMN_NAME = C.COLUMN_NAME