SQL query to get the precision value of a column - sql

I need a sql query that gets the precision value for certain columns.. I am mainly concerned with a decimal type column and I need the precision value for the same.
I realise that it is possible to do so in certain versions and vendors of database servers. It would be nice if you could list down for a few of them.

for sql server:
select precision from sys.columns where name='<column_name>'
for oracle:
select data_precision from all_tab_columns where column_name = '<columnName>'

Oracle:
SELECT DATA_LENGTH, DATA_PRECISION
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = :TableName
AND COLUMN_NAME = :ColumnName
Firebird (+ Interbase) :
SELECT
rdb$field_scale,
rdb$field_precision
FROM rdb$relations r
JOIN rdb$relation_fields rf
ON rf.rdb$relation_name = r.rdb$relation_name
JOIN rdb$fields fld
ON rf.rdb$field_source = fld.rdb$field_name
WHERE r.rdb$relation_name = :TableName
AND rf.rdb$field_name = :ColumnName
MySql (Not Tested !) :
SELECT
NUMERIC_PRECISION, NUMERIC_SCALE
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = :TableName AND
COLUMN_NAME = :ColumnName

For SQL Server you can also use NUMERIC_PRECISION in INFORMATION_SCHEMA.COLUMNS.
I assume this may well be portable.

For SQL Server, it's:
select object_name(object_id), name, precision from sys.columns

For FireBird and InterBase
SELECT
RDB$FIELD_PRECISION, RDB$FIELD_SCALE
FROM RDB$FIELDS
WHERE RDB$FIELD_NAME = columnsDomainName

MySQL:
SELECT numeric_precision,
numeric_scale
FROM information_schema.columns
WHERE TABLE_NAME = 'table_name'
AND COLUMN_NAME = 'column_name';

Related

SQL Server - Search for a column across all database tables

I would like to execute a SQL query to find all tables within a database that does not have a particular column?
E.g. I would like a list of tables that do not have a column called 'BranchID'.
Thank you for your attention.
Information Schema Views contain metadata about the database. For more information, go here
Using these views you can do something like this...
SELECT
t.TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLES AS t
WHERE NOT EXISTS
(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS AS c WHERE c.TABLE_NAME = t.TABLE_NAME AND c.COLUMN_NAME = 'BranchID')
select table_name
from INFORMATION_SCHEMA.COLUMNS
except
select table_name
from INFORMATION_SCHEMA.COLUMNS
where column_name = 'BranchID'
(from memory, I might have the column names wrong but should get you most of the way there :) )
This should work.
SELECT table_name
FROM INFORMATION_SCHEMA.columns c
WHERE NOT EXISTS (
SELECT table_name
FROM INFORMATION_SCHEMA.columns cc
WHERE cc.table_name = c.table_name
AND cc.column_name = 'Branch_id'
)
First of all, we select the table_name from Information_Schema.Columns where the table_name is NOT in a query that selects tables WITH the Branch_ID Column
You can use the INFORMATION_SCHEMA.COLUMNS table and INFORMATION_SCHEMA.TABLES table to get the information you want using normal SQL Queries.
In your case you would use a subquery:
select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_NAME NOT IN (select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME='BranchID')
I have created a re-usable procedure that could help you.
CREATE PROC SP_UTIL_findcolumns
#SearchString As varchar(250) = NULL
AS
BEGIN
SELECT
t.TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE NOT EXISTS
(SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS AS c
WHERE c.TABLE_NAME = t.TABLE_NAME
AND c.COLUMN_NAME = #SearchString )
END

How to fetch Column Names from Table?

I am trying to fetch column names form Table in Oracle. But I am not getting Column Names.
I used Many query's, And Find may query's in Stack overflow But I didn't get answer.
I used below query's:
1. SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='TABLE_NAME';
2. SELECT COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='TABLE_NAME';
But Out Put is
no row selected
What is the problem here. Thank you very much
both of the queries are correct, just the thing which can cause this problem is that ,maybe you did n't write your table name with capital letters you must do something like this:
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS
where TABLE_NAME = UPPER('TABLE_NAME');
OR
SELECT COLUMN_NAME FROM USER_TAB_COLUMNS
where TABLE_NAME = UPPER('TABLE_NAME');
Try this:
SELECT column_name
FROM all_tab_cols
WHERE upper(table_name) = 'TABLE_NAME'
AND owner = ' || +_db+ || '
AND column_name NOT IN ( 'password', 'version', 'id' )
or
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS where TABLE_NAME = UPPER('TABLE_NAME');
I hope This query would help yu out
SELECT SCHEMA_NAME (schema_id) + '.' + t.name AS 'Table Name'
FROM sys.tables t
INNER JOIN sys.columns c
ON c.object_id = t.object_id
WHERE c.name like '%hmy%
ORDER BY 'Table Name'

Position of a column in a table

How to know the Column position in a table in MS-SQL.
Eg: if a table consists of 3 columns namely column1, column2 and column3.
I should write a query so that i can get the position of column3 as 3
You will get all these from information_schema.
select ordinal_position from information_schema.columns
where schema_name = 'databasename'
and table_name = 'tablename'
and column_name = 'column name'
There're two ways to do this:
select colid
from sys.syscolumns
where id = object_id('schemaname.tablename') and name = 'column3'
and
select ordinal_position
from information_schema.columns
where
schema_name = 'schemaname' and
table_name = 'tablename' and
column_name = 'column3'
Here's an article about why you have to avoid information_schema views - The case against INFORMATION_SCHEMA views, I don't have to write this types of query often, so I don't really care about it, but sys.syscolumns tends to be a bit faster because it doesn't have many redundant joins which you may not need.
OTOH, information_schema views are ISO standard - here's dicussion about this - SQL Server: should I use information_schema tables over sys tables?
try :
SELECT ORDINAL_POSITION
FROM information_schema.columns
WHERE table_name = 'YourTableName' AND COLUMN_NAME = 'YourColumnName'
Try the query and check for the result.
select column_name,ordinal_position
from information_schema.columns
where table_catalog = 'yourdatabasename'
and table_schema = 'yourschemaname'
and table_name = 'yourtablename'

How do I list all the columns in a table?

For the various popular database systems, how do you list all the columns in a table?
For MySQL, use:
DESCRIBE name_of_table;
This also works for Oracle as long as you are using SQL*Plus, or Oracle's SQL Developer.
For Oracle (PL/SQL)
SELECT column_name
FROM user_tab_cols
WHERE table_name = 'myTableName'
For MySQL
SHOW COLUMNS FROM table_name
For MS SQL Server:
select COLUMN_NAME from information_schema.columns where table_name = 'tableName'
(5 years laters, for the Honor of PostgreSQL, the most advanced DDBB of the Kingdom)
In PostgreSQL:
\d table_name
Or, using SQL:
select column_name, data_type, character_maximum_length
from INFORMATION_SCHEMA.COLUMNS
where table_name = 'table_name';
I know it's late but I use this command for Oracle:
select column_name,data_type,data_length from all_tab_columns where TABLE_NAME = 'xxxx' AND OWNER ='xxxxxxxxxx'
SQL Server
SELECT
c.name
FROM
sys.objects o
INNER JOIN
sys.columns c
ON
c.object_id = o.object_id
AND o.name = 'Table_Name'
or
SELECT
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'Table_Name'
The second way is an ANSI standard and therefore should work on all ANSI compliant databases.
Call below code in MS SQL Server:
sp_columns [tablename]
Microsoft SQL Server Management Studio 2008 R2:
In a query editor, if you highlight the text of table name (ex dbo.MyTable) and hit ALT+F1, you'll get a list of column names, type, length, etc.
ALT+F1 while you've highlighted dbo.MyTable is the equivalent of running EXEC sp_help 'dbo.MyTable' according to this site
I can't get the variations on querying INFORMATION_SCHEMA.COLUMNS to work, so I use this instead.
For SQL Server
sp_help tablename
Just a slight correction on the others in SQL Server (schema prefix is becoming more important!):
SELECT name
FROM sys.columns
WHERE [object_id] = OBJECT_ID(N'dbo.tablename');
SQL Server
To list all the user defined tables of a database:
use [databasename]
select name from sysobjects where type = 'u'
To list all the columns of a table:
use [databasename]
select name from syscolumns where id=object_id('tablename')
Example:
select Table_name as [Table] , column_name as [Column] , Table_catalog as [Database], table_schema as [Schema] from information_schema.columns
where table_schema = 'dbo'
order by Table_name,COLUMN_NAME
Just my code

How can I get column names from a table in Oracle?

I need to query the database to get the column names, not to be confused with data in the table. For example, if I have a table named EVENT_LOG that contains eventID, eventType, eventDesc, and eventTime, then I would want to retrieve those field names from the query and nothing else.
I found how to do this in:
Microsoft SQL Server
MySQL
PostgreSQL
But I need to know: how can this be done in Oracle?
You can query the USER_TAB_COLUMNS table for table column metadata.
SELECT table_name, column_name, data_type, data_length
FROM USER_TAB_COLUMNS
WHERE table_name = 'MYTABLE'
In SQL Server...
SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = (SELECT id FROM sysobjects WHERE type = 'V' AND [Name] = 'Your table name')
Type = 'V' for views
Type = 'U' for tables
You can do this:
describe EVENT_LOG
or
desc EVENT_LOG
Note: only applicable if you know the table name and specifically for Oracle.
For SQL Server 2008, we can use information_schema.columns for getting column information
SELECT *
FROM information_schema.columns
WHERE table_name = 'Table_Name'
ORDER BY ordinal_position
For SQLite I believe you can use something like the following:
PRAGMA table_info(table-name);
Explanation from sqlite.org:
This pragma returns one row for each column in the named table. Columns in the result set include the column name, data type, whether or not the column can be NULL, and the default value for the column. The "pk" column in the result set is zero for columns that are not part of the primary key, and is the index of the column in the primary key for columns that are part of the primary key.
See also: Sqlite.org Pragma Table Info
That information is stored in the ALL_TAB_COLUMNS system table:
SQL> select column_name from all_tab_columns where table_name = 'DUAL';
DUMMY
Or you could DESCRIBE the table if you are using SQL*PLUS:
SQL> desc dual
Name Null? Type
----------------------------------------------------- -------- ---------------------- -------------
DUMMY VARCHAR2(1)
The other answers sufficiently answer the question, but I thought I would share some additional information. Others describe the "DESCRIBE table" syntax in order to get the table information. If you want to get the information in the same format, but without using DESCRIBE, you could do:
SELECT column_name as COLUMN_NAME, nullable || ' ' as BE_NULL,
SUBSTR(data_type || '(' || data_length || ')', 0, 10) as TYPE
FROM all_tab_columns WHERE table_name = 'TABLENAME';
Probably doesn't matter much, but I wrote it up earlier and it seems to fit.
For Oracle
SELECT column_name FROM user_tab_cols WHERE table_name=UPPER('tableName');
describe YOUR_TABLE;
In your case :
describe EVENT_LOG;
Even this is also one of the way we can use it
select * from product where 1 != 1
select column_name,* from information_schema.columns
where table_name = 'YourTableName'
order by ordinal_position
For MySQL, use
SELECT column_name
FROM information_schema.columns
WHERE
table_schema = 'Schema' AND table_name = 'Table_Name'
For SQL Server:
SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = object_id('TABLE_NAME')
SELECT COLUMN_NAME 'all_columns'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='user';
You could also try this, but it might be more information than you need:
sp_columns TABLE_NAME
SELECT A.COLUMN_NAME, A.* FROM all_tab_columns a
WHERE table_name = 'Your Table Name'
AND A.COLUMN_NAME = 'COLUMN NAME' AND a.owner = 'Schema'
Mysql
SHOW COLUMNS FROM a_table_named_users WHERE Field REGEXP 'user_id|user_name|user_pass'
This will return a result something like this:
Field | Type | Null | Key | Default | Extra
user_id int(8) NO PRI NULL auto_increment
user_name varchar(64) NO MUL NULL
user_pass varchar(64) NO NULL
Then to pull out the values you can simply
fetch row[0]
This is also great for passing input dynamically since the REGEXP needs the '|' for multiple inputs, but is also a way to keeps data separated and easy to store/pass to classes/functions.
Try throwing in dummy data as well for security when sending it out and compare what was returned when receiving any errors.
In Oracle, there is two views that describe columns:
DBA_TAB_COLUMNS describes the columns of all tables, views, and
clusters in the database.
USER_TAB_COLUMNS describes the columns of the tables, views, and
clusters owned by the current user. This view does not display the
OWNER column.
The answer is here: http://php.net/manual/en/function.mysql-list-fields.php
I'd use the following code in your case:
$result = mysql_query("SHOW COLUMNS FROM sometable");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$fields = array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$fields[] = $row['Field'];
}
}
you can run this query
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%%' --if you want to find specific column write here
ORDER BY schema_name, table_name;
Try this
select * from sys.all_columns c join sys.objects o on c.object_id=o.object_id where o.name = 'TABLENAME' and c.name like '%COLUMN NAME%'
Just select first row from the table , for oracle : select * from <table name> where rownum = 1;
Came across this question looking for access to column names on Teradata, so I'll add the answer for their 'flavour' of SQL:
SELECT ColumnName
FROM DBC.Columns
WHERE DatabaseName='DBASE_NAME'
AND TableName='TABLE_NAME';
The info is stored in the DBC dbase.
Getting data types is a little bit more involved:
Get column type using teradata system tables
I did it like this
SELECT
TOP 0
*
FROM
Posts
It works even in http://data.stackexchange.com whose service tables I am not aware of!
SELECT COLUMN_NAME
FROM YourDatabase.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTableName'