Can we refer to a column from sysconstraints using name in sybase? - sybase

since the option of accessing via 'name' attribute is possible via
sysobjects
, is the same possible by
sysconstraints/syscolumns??
because i have not given id attribute to table or column or any constraints.

sysobjects does not contains any column name. I don't understand your first statement.
But if you have a table name and want to display sysconstraints table from table name and column name you can have a join like this :
SELECT constrid
FROM sysconstraints sc
JOIN sysobjects o ON o.id = sc.tableid
JOIN syscolumns c ON c.colid = sc.colid AND c.id = o.id
WHERE o.name = 'tablename'
AND c.name = 'columnname'
The sysconstraints has colid and tableid columns that match syscolumns.colid and sysobjects.id.

Related

What are the tables whose names start with `TT_`?

Using this SQL statement to find in my database all tables the have columns whose names contain ItemId:
SELECT o.name,
c.name
FROM sys.columns c
INNER JOIN sys.objects o ON c.object_id = o.object_id
WHERE c.name LIKE '%ItemId%'
ORDER BY o.name, c.column_id
I received a result containing two tables:
ResourceData
TT_Data_117F9D94
I know about the ResourceData table and I can find it in the list of my tables.
But I have not been able to find in my database any table name TT_Data_117F9D94.
Any idea where/how did this table show up in the result of my query?
First of all
You should have used
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%itemid%';
Probably TT_Data_117F9D94 is a data table in your database.
Please check the database design of that table. Or simply do a select query from that table.
Check about table types -
CREATE TYPE dbo.TT_Type AS TABLE (a INT)
SELECT o.name, c.name
FROM sys.columns c
JOIN sys.objects o ON c.[object_id] = o.[object_id]
WHERE c.name = 'a'
Correct query -
SELECT SCHEMA_NAME(o.[schema_id]), o.name, c.name
FROM sys.columns c
JOIN sys.objects o ON c.[object_id] = o.[object_id]
WHERE o.[type] = 'U' -- only user tables

Find tables for which specific column not exists sql server

How to find tables in which specific column not exists.
E.g column ID not present in table Contact, then Contact table will be in result set.
I tried:
SELECT DISTINCT t.name
FROM sys.tables t
INNER join sys.columns C ON c.object_id = t.object_id
WHERE c.name <> 'ID'
But above query return all table for which column ID is present in it.
Can u try this query,
SELECT * FROM sys.tables WHERE type = 'U' AND object_id NOT IN (
SELECT DISTINCT c.object_id FROM sys.columns c WHERE c.name = 'ID')
SELECT name
FROM sys.Tables
WHERE Object_ID NOT IN (SELECT Object_ID FROM sys.Columns WHERE name = 'ID')
You don't need to perform a join, you can just add a subquery that identifies tables that have an ID column and exclude the object_id's using NOT IN(..the subquery..):
SELECT DISTINCT t.name
FROM sys.tables t
WHERE t.object_id NOT IN (SELECT object_id FROM sys.columns WHERE name = 'id')
The reason your query doesn't work is because you are simply getting all columns that aren't = ID and each table has many columns that match that criteria so they will be returned even if there is an ID column.
One method is with NOT EXISTS:
SELECT OBJECT_SCHEMA_NAME(t.object_id) AS SchemaName
, t.name AS TableName
FROM sys.tables t
WHERE NOT EXISTS ( SELECT *
FROM sys.columns c
WHERE c.object_id = t.object_id
AND c.name = 'ID' );
Try the following, lists all table names NOT having ID.
SELECT tableName
FROM (
SELECT DISTINCT
t.name AS tableName
,c.name AS columnName
FROM sys.tables t
INNER JOIN sys.columns C
ON c.object_id = t.object_id
) AS t
WHERE columnName NOT IN( 'ID')
GROUP BY tableName
If you down't want to see system tables, then..
SELECT DISTINCT OBJECT_NAME(OBJECT_ID) TableName FROM SYS.COLUMNS WHERE NAME <> 'id'
and OBJECTPROPERTY (OBJECT_ID, 'IsUserTable') = 1
EXCEPT
SELECT DISTINCT OBJECT_NAME(OBJECT_ID) TableName FROM SYS.COLUMNS WHERE NAME = 'id'

How to find what table foreign key is from?

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..
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
Or simply use
sp_help Tablename or [Alt]+F1
This should help. Just run it in the DB you wish to query:
SELECT f.NAME AS ForeignKey
,SCHEMA_NAME(f.SCHEMA_ID) SchemaName
,OBJECT_NAME(f.parent_object_id) AS TableName
,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName
,SCHEMA_NAME(o.SCHEMA_ID) ReferenceSchemaName
,OBJECT_NAME(f.referenced_object_id) AS ReferenceTableName
,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
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
GO
Source: http://blog.sqlauthority.com/2009/02/26/sql-server-2008-find-relationship-of-foreign-key-and-primary-key-using-t-sql-find-tables-with-foreign-key-constraint-in-database/
Basically, the first column is the FK, followed by the FK schema and object. Following those are the PK column name, its schema and object.
Either of the answers by NoDisplayName or Kris G. should work, but if you want something easier to remember while you're in SSMS, just right click the Foreign Key and choose Script As>Create To>New Window.
You will then get a script that can be used to (re-)create the FK, and you will be able to see what column it references in what table by reading the script.

how to identify in a query whether the table has a Primary key or not

Is it possible to write a query that determines if the table has a primary key or not?
SELECT * FROM sysobjects WHERE id = OBJECT_ID('TRS') and xtype = PK
If it doesn't have primary then it will return nothing.
In this link the author said about using Xtype but it seems it's an old term.
This will give you the key and it's column name if there is a primary key, else nothing.
SELECT sysobjects.name, syscolumns.name
FROM
sysobjects INNER JOIN
syscolumns ON syscolumns.id = sysobjects.id INNER JOIN
syskeys ON syskeys.id = sysobjects.id
WHERE
sysobjects.type = 'U' AND syskeys.type = 1

Find SQL Server Tables that have two specified column names

I would like to search tables in sql server for a table that has two specific column names ex (columnA and columnB). I have a query that searches on one column name
SELECT name FROM sysobjects WHERE id IN
( SELECT id FROM syscolumns WHERE name = 'columnA' )
I don't know how to modify this to search for two columns.
SELECT name FROM sysobjects WHERE id IN
( SELECT id FROM syscolumns WHERE name = 'columnA' )
and id in
( SELECT id FROM syscolumns WHERE name = 'columnB' )
should do the trick.
This is the right way to do it:
select so.name
from sysobjects so
where so.type = 'U' -- it's a user's table
and exists (select * from syscolumns sc where sc.id = so.id and sc.name='columnA')
and exists (select * from syscolumns sc where sc.id = so.id and sc.name='columnB')
It's important to check that it's a user table. On the contrary you could find views, table valued functions and so on.
Try something like:
select syscolumns.id, sysobjects.name from syscolumns
join sysobjects so on sysobjects.id = syscolumns.id
where exists (select 1 from syscolumns sc where sc.id = syscolumns.id and name = 'columnA')
and exists (select 1 from syscolumns sc2 where sc2.id = syscolumns.id and name = 'columnB')

Resources