Oracle SQL adding multi-line table comment or column comment - sql

I want to add multi-line table/column comment.
Normally this is used;
COMMENT ON TABLE USERS IS 'User table has the user data'
What I need is a way to insert the new-line inside the single quotation marks like;
COMMENT ON TABLE USERS IS 'User table has the user data <smthg_here_for_new_line> 1- Name column has name <smthg_here_for_new_line> 2- Number Column has the id'
So that table comments will be seen like;
User table has the user data
1- Name column has name
2- Number Column has the id
Anybody knows how add multi-line table/column comments?

You can simply put line feeds inside the single-quotes of your comment declaration, for example:
COMMENT ON COLUMN MYTABLE.MYCOLUMN
IS
'Line 1
Line 2.
Line 3';
Note, however, that in SQL Developer (and perhaps other tools) this will not always display as expected. With the following query ...
SELECT *
FROM USER_COL_COMMENTS
WHERE
TABLE_NAME = 'MYTABLE'
AND COMMENTS IS NOT NULL;
... you'll get exactly what you're looking for in Script Output (i.e., highlight the query, right-click, select "Run Script"):
TABLE_NAME COLUMN_NAME COMMENTS
---------- ----------- --------------
MYTABLE MYCOLUMN Line 1
Line 2
Line 3
MYTABLE OTHERCOLUMN Other comments
But in a Query Result (i.e., highlight the query, right-click, select "Run Statement"), or when opening the table and looking at the Columns tab, the full comment will be run together on a single line.
Note: The tables in which these comments can be queried are:
Comments on tables: USER_TAB_COMMENTS
Comments on columns: USER_COL_COMMENTS

In SQLPlus you can use concat with chr(10) (or chr(13) || chr(10) in Microsoft Windows environment):
'User table has the user data' || chr(10) || '1- Name column has name...'
Also, it should be possible to just interpret newlines by setting SQLBLANKLINES to ON:
SET SQLBLANKLINES ON
COMMENT ON TABLE USERS IS 'User table has the user data
1- Name column has name
2- Number Column has the id'

Related

Sqlplus formatting width, as specified

How can you execute the following Oracle/pl-sql code to display two columns side by side?
SELECT table_name, user_cons_columns.column_name
FROM user_cons_columns;
Currently, this is my output formatting:
This is the formatting I hope to see:
Solutions tried:
set long 1000
set linesize 200
Where long and linesize have been changed from 20 to 2000, unsuccessfully. I suspect it's just improper SQL code...but unsure. Thank you in advance!
This has nothing to do with the SQL code (and you should NOT change it, for example by truncating the strings in the SQL query, just to fix a formatting problem).
The issue is that the columns, in the table, are declared of a certain width, say VARCHAR2(1000), and then that's what SQL Plus will reserve by default. You can change that in SQL Plus itself, with SQL Plus commands. In this case, the COLUMN command.
SQL> column column_name format a30
SQL> column table_name format a30
These are SQL Plus commands, so don't end them in semicolon ( ; )
Change a30 to a40 if you want 40 characters per column. Etc.
It is not clear why, if in the output you wanted the table name to appear first, in the query you have the column name first. You should be able to fix that yourself. Also, if you select from just one table, there is no need to prefix column names with the table name. However, if you do, be consistent - do it for both columns. And if you do, it is better to give an alias to the table in the FROM clause, and use the alias in SELECT. These are all unrelated to your original question.
Select only the first N (20) characters from the column_name field.
SELECT SUBSTR(column_name, 1, 20) column_name, table_name
FROM user_cons_columns;

How to find the name of a table based upon a column name and then access said table

I have a column name "CustomerIDClass" and I need to find the table it's associated with within an entire Oracle database.
I've run this to determine the owner and name of the table where this column name appears:
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CustomerIDClass%';
and I'm getting this response:
I don't have enough reputation to post the image, so here's the link: http://i.imgur.com/a7rcKoA.png
I have no idea how to access this (BIN$Csew==) table. When I try to use it as a table name I get errors or messages saying that no rows were returned.
My main goal here is to write a simple statement that lets me search the database for the "CustomerIDClass" and view the table that contains this column name.
This table is in the recycle bin. You have to issue FLASHBACK TABLE "Customer1"."BIN$Csew==$0" TO BEFORE DROP command, given you have the appropriate privileges.
Doc: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9012.htm
Do note that in oracle the column names are stored in capital but you are using mixed case in your like statement therefore the select clause will not return any result
Try the below
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CUSTOMERIDCLASS%';

Selecting column names and table names of a select statement

How can I select the column name and table name from a SQL?
I tried something like this but it didn't work:
select column_name, table_name from (select * from users);
This might sound silly, but I have a list of different SQLs and I need to extract their columns and tables into a list. So some of the statements could me:
select username, password from users
select createdate from userlog
select * from dept
...
If I can select the column name and table name of a select statement, then I should get, say for the first statement, username and password for columns and users for table name. And createdate for column and userlog for table name in the second statement.
Then if it all works, I can then loop through the list of select statements and extract their column and table names.
The below query worked for Oracle database.
SELECT COLUMN_NAME,TABLE_NAME FROM ALL_TAB_COLUMNS
You can see more about information-schema
Edit:
You may try like this:
SELECT COLUMN_NAME,TABLE_NAME FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME IN (SELECT ColumnName FROM users)
You need to parse the SQL statement so the SQL engine figures out the columns and datatypes of the columns that the statement returns.
How you do it best depends on what environment you are using. In some programming languages when you create a SqlPreparedStatement or OraCommand or whatever the object may be called, that object may have a metadata collection populated with column information after parsing.
If you are doing it in the database itself, parsing your statement with DBMS_SQL can get you the information you need. See Example 8 in the documentation at this link:
http://docs.oracle.com/database/121/ARPLS/d_sql.htm#ARPLS68205
--
Oh, and this gives you column names of the select statement. The table names I do not know of any way to get easily.

Mysql Query data filled check

I had created the table with 200 columns and i had inserted data
Now i need to check that specific 100 columns in one row are filled or not,how can we check this using mysql query .the primary key is defined .please help me out how to resolve this.
select * from tablename where column1 != null or column2 != null ......
That is a lot of columns so at the risk of being mysql server version specific you can use the information schema to get the column names and then write a SQL procedure or something in your chosen shell / language that iterates over them performing a test.
select distinct COLUMN_NAME as 'Field', IS_NULLABLE from information_schema.columns where TABLE_SCHEMA="YourDatabase" and TABLE_NAME="YourTableName" and TABLE_NAME not like "%view%" escape '!' ;
The example above will tell you the column name as "Field" and tell you if it can hold a NULL. Having the field name may give you a better way of automating a field name specific test.

SQL to add column and comment in table in single command

I am using Oracle 11g for my web application. I want to add a column and a comment to an existing table. I can do that easily with the below commands
ALTER TABLE product ADD product_description VARCHAR2(20)
and
COMMENT ON COLUMN product.product_description
IS 'This is comment for the column';
But I want to do above task in single command. I searched on internet for a command to add a column and comment in a single command but I couldn't find. I wonder if this is possible. Any suggestions would be highly appreciated.
No, you can't.
There's no reason why you would need to. This is a one-time operation and so takes only an additional second or two to actually type and execute.
If you're adding columns in your web application this is more indicative of a flaw in your data-model as you shouldn't need to be doing it.
In response to your comment that a comment is a column attribute; it may seem so but behind the scenes Oracle stores this as an attribute of an object.
SQL> desc sys.com$
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJ# NOT NULL NUMBER
COL# NUMBER
COMMENT$ VARCHAR2(4000)
SQL>
The column is optional and sys.col$ does not contain comment information.
I assume, I have no knowledge, that this was done in order to only have one system of dealing with comments rather than multiple.
You can use below query to update or create comment on already created table.
SYNTAX:
COMMENT ON COLUMN TableName.ColumnName IS 'comment text';
Example:
COMMENT ON COLUMN TAB_SAMBANGI.MY_COLUMN IS 'This is a comment on my column...';
Query to add column with comment are :
alter table table_name
add( "NISFLAG" NUMBER(1,0) )
comment on column "ELIXIR"."PRD_INFO_1"."NISPRODGSTAPPL" is 'comment here'
commit;
Table Comments -RightClick on Table Name GoTo Properties Add to Extended Properties MS_Description as property name and add comments.
Add comments for two different columns of the EMPLOYEE table :
COMMENT ON EMPLOYEE
(WORKDEPT IS 'see DEPARTMENT table for names',
EDLEVEL IS 'highest grade level passed in school' )