query to know a table is referenced to how many other table - sql-server

I have recently started using Microsoft SQL Server and there is a below query which fetch the data from the table named tblPole , request you to please provide the query by which i can know that this table is referenced into how many other tables in database below is my query
select * from [PDB_MASTER_ERT].[ter].[tblPole]
For all the tables relationship in databsae below is the query i am using
SELECT obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas sch
ON tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id

Use this query:
SELECT obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas sch
ON tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id
where tab1.name = 'tblPole'

Related

Query a list of tables with last user update + number of rows

Can anyone help me combining these 2 queries to have a single output with 3 columns: TableName, last_user_update and NumberOfRows? If you could add TotalSpaceMB as well, even better.
SELECT object_name(Object_id) as TableName,
last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'databasename')
and
SELECT T.name TableName,i.Rows NumberOfRows
FROM sys.tables T
JOIN sys.sysindexes I ON T.OBJECT_ID = I.ID
WHERE indid IN (0,1)
Thank you.
SELECT
T.name TableName,
i.Rows NumberOfRows,
last_user_update,
(SUM(a.data_pages) * 8) / 1024 AS DataSpaceMB
FROM sys.tables T
JOIN sys.sysindexes I ON T.OBJECT_ID = I.ID
JOIN sys.dm_db_index_usage_stats us ON us.object_id = T.object_id
INNER JOIN
sys.indexes IND ON t.OBJECT_ID = IND.object_id
INNER JOIN
sys.partitions p ON IND.object_id = p.OBJECT_ID AND IND.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE indid IN (0,1)
AND database_id = DB_ID( 'databasename')
GROUP BY T.name, I.rows, us.last_user_update
I think this works well.
Test this and take about the result.
SELECT T.name TableName,i.Rows NumberOfRows, st.last_user_update
FROM sys.tables T
JOIN sys.sysindexes I ON T.OBJECT_ID = I.ID
JOIN sys.dm_db_index_usage_stats st on st.object_id = T.object_id
WHERE I.indid IN (0,1)
and st.database_id = DB_ID(DB_NAME())
Thank you.

Determine implicit conversion before comparing in SQL Server

We have all inherited bad data models. I have a data model where the FK constraint is going to a data type that isn't implicitly castable. For example, I have a VARCHAR reference column (constraintColumn) which has an FK relationship with an INT parent column (constrainedColumn).
SELECT
OBJECT_NAME(fk.constraint_object_id) KeyName
, t.name constrainedTable
, c.name constrainedColumn
, t2.name constraintTable
, c2.name constraintColumn
FROM
sys.foreign_key_columns AS fk
INNER JOIN
sys.tables AS t ON
fk.parent_object_id = t.object_id
INNER JOIN
sys.columns AS c ON
fk.parent_object_id = c.object_id
AND
fk.parent_column_id = c.column_id
INNER JOIN
sys.tables AS t2 ON
fk.referenced_object_id = t2.object_id
INNER JOIN
sys.columns AS c2 ON
fk.referenced_object_id = c2.object_id
AND
fk.constraint_column_id = c2.column_id
ORDER BY
t.name
, c.name;
How can I use sys.types or something else to determine which data types are implicitly castable?

How to get tableName , columnName ,constraintsType and cascade type in sql server 2008

i need list of tableName , columnName ,constraintsType and cascade type in sql server 2008 so i tried below query but cascade type is null
SELECT TB.name AS "TableName",CL.name AS
"ColumnName",KC.type,FK.delete_referential_action,FK.update_referential_action
FROM SYS.key_constraints KC JOIN SYS.tables TB ON
KC.parent_object_id=TB.object_id JOIN SYS.columns CL ON
KC.parent_object_id=CL.object_id LEFT JOIN SYS.foreign_keys FK ON FK.name = KC.name
I believe this is what you are looking for
SELECT
o1.name AS FK_table,
c1.name AS FK_column,
fk.name AS FK_name,
fk.type as FK_TYPE,
pk.name AS PK_name,
pk.type as PK_TYPE,
fk.delete_referential_action_desc AS Delete_Action,
fk.update_referential_action_desc AS Update_Action
FROM sys.objects o1
INNER JOIN sys.foreign_keys fk
ON o1.object_id = fk.parent_object_id
INNER JOIN sys.foreign_key_columns fkc
ON fk.object_id = fkc.constraint_object_id
INNER JOIN sys.columns c1
ON fkc.parent_object_id = c1.object_id
AND fkc.parent_column_id = c1.column_id
INNER JOIN sys.key_constraints pk
ON fk.referenced_object_id = pk.parent_object_id
AND fk.key_index_id = pk.unique_index_id
ORDER BY o1.name, fkc.constraint_column_id
I didn't try out your query as I don't a sql server in front of me but I believe you are missing a DMV and that is why your left join is returning nulls.

how to get table's schema name

I'm using SQL Server 2008 and have the following query:
SELECT SO1.name AS Tab,
SC1.name AS Col,
SO2.name AS RefTab,
SC2.name AS RefCol,
FO.name AS FKName
FROM dbo.sysforeignkeys FK
INNER JOIN dbo.syscolumns SC1 ON FK.fkeyid = SC1.id AND FK.fkey = SC1.colid
INNER JOIN dbo.syscolumns SC2 ON FK.rkeyid = SC2.id AND FK.rkey = SC2.colid
INNER JOIN dbo.sysobjects SO1 ON FK.fkeyid = SO1.id
INNER JOIN dbo.sysobjects SO2 ON FK.rkeyid = SO2.id
INNER JOIN dbo.sysobjects FO ON FK.constid = FO.id
How do you retrieve the table's schema name?
Thanks for your help
Use OBJECT_SCHEMA_NAME
SELECT
OBJECT_SCHEMA_NAME(f.parent_object_id) AS TableNameSchema, -- this
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
OBJECT_SCHEMA_NAME(f.referenced_object_id) AS ReferenceTableNameSchema,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName,
f.name AS ForeignKey
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
As per Sql Server 2008:
SELECT Object_name(f.parent_object_id)
AS
TableName,
Col_name(fc.parent_object_id, fc.parent_column_id)
AS ColumnName,
Object_name (f.referenced_object_id)
AS ReferenceTableName,
Col_name(fc.referenced_object_id, fc.referenced_column_id)
AS
ReferenceColumnName,
f.name
AS ForeignKey,
Quotename(Schema_name(f.schema_id)) + '.' + Quotename(
Object_name(f.parent_object_id)) AS
schemaname,
Quotename(Schema_name(o.schema_id)) + '.' + Quotename(
Object_name(f.referenced_object_id))
AS ReferenceSchemaName
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
If to want to know schema name on basis of object_id then use OBJECT_SCHEMA_NAME(), if you want to get schema name on basis of schema_id then use SCHEMA_NAME().

List which columns have a full-text index in SQL Server 2005

How do I list all tables / columns in my database that have a full-text index?
select distinct
object_name(fic.[object_id]) table_name,
[name] column_name
from
sys.fulltext_index_columns fic
inner join sys.columns c
on c.[object_id] = fic.[object_id]
and c.[column_id] = fic.[column_id]
This one gives you more information
SELECT
t.name AS TableName,
c.name AS FTCatalogName ,
i.name AS UniqueIdxName,
cl.name AS ColumnName,
cdt.name AS DataTypeColumnName
FROM
sys.tables t
INNER JOIN
sys.fulltext_indexes fi
ON
t.[object_id] = fi.[object_id]
INNER JOIN
sys.fulltext_index_columns ic
ON
ic.[object_id] = t.[object_id]
INNER JOIN
sys.columns cl
ON
ic.column_id = cl.column_id
AND ic.[object_id] = cl.[object_id]
INNER JOIN
sys.fulltext_catalogs c
ON
fi.fulltext_catalog_id = c.fulltext_catalog_id
INNER JOIN
sys.indexes i
ON
fi.unique_index_id = i.index_id
AND fi.[object_id] = i.[object_id]
LEFT JOIN
sys.columns cdt
ON
ic.type_column_id = cdt.column_id
AND fi.object_id = cdt.object_id;

Resources