How do I add comments to existing table DDL in Teradata? - ddl

I am adding some new columns to a table and want to add documentation to the table DDL for future developers. How does one go about this?

Generic Syntax:
COMMENT ON {OBJECT} {OBJECTNAME} AS '{255 characters of text};
Detailed Syntax Examples:
COMMENT ON TABLE {DATABASE}.{TABLENAME} AS '{255 characters of text}';
COMMENT ON COLUMN {DATABASENAME}.{TABLENAME}.{COLUMNNAME} AS '{255 characters of text}';
COMMENT ON USER {USERNAME} AS '{255 characters of text}';
COMMENT ON DATABASE {DATABASENAME} AS '{255 characters of text}';

In addition to adding object COMMENTs, you can add in-line comments to view definitions. Since most access is always through a view, that's how we communicate the DBA responsible for the table and document changes. For example:
replace view VIEWDB.vmy_table as
locking DATADB.my_table for access
select *
from DATADB.my_table
/* This is a comment */
/* Created by Bob */
The nice thing about this technique is that the comments are shown when you do a SHOW SELECT * FROM VIEWDB.vmy_table.

Related

Why does "UPDATE Users SET Password=? WHERE Username=?" give a syntax error? [duplicate]

One of my columns is called from. I can't change the name because I didn't make it.
Am I allowed to do something like SELECT from FROM TableName or is there a special syntax to avoid the SQL Server being confused?
Wrap the column name in brackets like so, from becomes [from].
select [from] from table;
It is also possible to use the following (useful when querying multiple tables):
select table.[from] from table;
If it had been in PostgreSQL, use double quotes around the name, like:
select "from" from "table";
Note: Internally PostgreSQL automatically converts all unquoted commands and parameters to lower case. That have the effect that commands and identifiers aren't case sensitive. sEleCt * from tAblE; is interpreted as select * from table;. However, parameters inside double quotes are used as is, and therefore ARE case sensitive: select * from "table"; and select * from "Table"; gets the result from two different tables.
These are the two ways to do it:
Use back quote as here:
SELECT `from` FROM TableName
You can mention with table name as:
SELECT TableName.from FROM TableName
While you are doing it - alias it as something else (or better yet, use a view or an SP and deprecate the old direct access method).
SELECT [from] AS TransferFrom -- Or something else more suitable
FROM TableName
Your question seems to be well answered here, but I just want to add one more comment to this subject.
Those designing the database should be well aware of the reserved keywords and avoid using them. If you discover someone using it, inform them about it (in a polite way). The keyword here is reserved word.
More information:
"Reserved keywords should not be used
as object names. Databases upgraded
from earlier versions of SQL Server
may contain identifiers that include
words not reserved in the earlier
version, but that are reserved words
for the current version of SQL Server.
You can refer to the object by using
delimited identifiers until the name
can be changed."
http://msdn.microsoft.com/en-us/library/ms176027.aspx
and
"If your database does contain names
that match reserved keywords, you must
use delimited identifiers when you
refer to those objects. For more
information, see Identifiers (DMX)."
http://msdn.microsoft.com/en-us/library/ms132178.aspx
In Apache Drill, use backquotes:
select `from` from table;
If you ARE using SQL Server, you can just simply wrap the square brackets around the column or table name.
select [select]
from [table]
I have also faced this issue.
And the solution for this is to put [Column_Name] like this in the query.
string query= "Select [Name],[Email] from Person";
So it will work perfectly well.
Hi I work on Teradata systems that is completely ANSI compliant. Use double quotes " " to name such columns.
E.g. type is a SQL reserved keyword, and when used within quotes, type is treated as a user specified name.
See below code example:
CREATE TABLE alpha1
AS
(
SEL
product1
type_of_product AS "type"
FROM beta1
) WITH DATA
PRIMARY INDEX (product1)
--type is a SQL reserved keyword
TYPE
--see? now to retrieve the column you would use:
SEL "type" FROM alpha1
I ran in the same issue when trying to update a column which name was a keyword. The solution above didn't help me. I solved it out by simply specifying the name of the table like this:
UPDATE `survey`
SET survey.values='yes,no'
WHERE (question='Did you agree?')
The following will work perfectly:
SELECT DISTINCT table.from AS a FROM table
Some solid answers—but the most-upvoted one is parochial, only dealing with SQL Server. In summary:
If you have source control, the best solution is to stick to the rules, and avoid using reserved words. This list has been around for ages, and covers most of the peculiarities. One tip is that reserved words are rarely plural—so you're usually safe using plural names. Exceptions are DIAGNOSTICS, SCHEMAS, OCTETS, OFFSETS, OPTIONS, VALUES, PARAMETERS, PRIVILEGES and also verb-like words that also appear plural: OVERLAPS, READS, RETURNS, TRANSFORMS.
Many of us don't have the luxury of changing the field names. There, you'll need to know the details of the RDBM you're accessing:
For SQL Server use [square_braces] around the name. This works in an ODBC connection too.
For MySQL use `back_ticks`.
Postgres, Oracle and several other RDBMs will apparently allow "double_quotes" to be used.
Dotting the offending word onto the table name may also work.
You can put your column name in bracket like:
Select [from] from < ur_tablename>
Or
Put in a temprary table then use as you like.
Example:
Declare #temp_table table(temp_from varchar(max))
Insert into #temp_table
Select * from your_tablename
Here I just assume that your_tablename contains only one column (i.e. from).
In MySQL, alternatively to using back quotes (`), you can use the UI to alter column names. Right click the table > Alter table > Edit the column name that contains sql keyword > Commit.
select [from] from <table>
As a note, the above does not work in MySQL
Judging from the answers here and my own experience. The only acceptable answer, if you're planning on being portable is don't use SQL keywords for table, column, or other names.
All these answers work in the various databases but apparently a lot don't support the ANSI solution.
Simple solution
Lets say the column name is from ; So the column name in query can be referred by table alias
Select * from user u where u.from="US"
In Oracle SQL Developer, pl/sql you can do this with double quotes but if you use double quotes you must type the column names in upper case. For example, SELECT "FROM" FROM MY_TABLE

How to add 'WITH READ ONLY' constraint in views in Oracle

I have created a simple view and have forgotten to add WITH READ ONLY while creating the view.
Now I want to alter the view and add WITH READ ONLY option. What will be the query for it?
alter view x read only;
was added in 11.2, but unfortunately only for editioning views.
So there is a chance that in some future version this will be extended to regular views:)
Until that use simple create or replace view
create or replace view x as
select * from dual /* your query */
with read only;
or
create or replace view x as
select * from dual /* your query */
with check option;

Where/How are Oracle Column Aliases Declared?

I am trying to trace some information/primary keys in tables, and the normal mechanism we use to do this is through views that are nothing more than semantic layers on top of tables that have extremely cryptic column names.
As an example, the table looks like:
create table F3002_WH
(
ixtbm CHAR(9) not null,
ixkit NUMBER not null,
ixmmcu CHAR(36) not null,
ixcpnt NUMBER not null,
ixsbnt NUMBER not null,
ixbqty NUMBER not null,
ixcoby CHAR(3) not null
);
But the views turn each of these fields into readable names, such as "TYPE_BILL" instead of "ixtbm."
However, when I look at the DDL for the view:
CREATE OR REPLACE VIEW F3002_VIEW AS
SELECT
ixtbm, ixkit, ixmmcu, ixcpnt, ixsbnt, ixbqty, ixcoby
from F3002_WH;
I don't see the alias. I would have expected to see ixtbm as type_bill.
So my question is where does this alias come from, and how can I tie it to the original field name without turning this into a science project? I don't understand how it can be rendered when querying the view without being within the view code.
For what it's worth, I am using PL/SQL Developer, by All Around Automation. I strongly suspect it is not the culprit, but in the interest of full disclosure I thought to mention it.
CREATE VIEW myView (alias1, alias2) as select ixtbm, ixkit from f3002_wh;
I have to say though, if you can see the underlying view query then the obfuscation is a waste of time!
And what I assume is happening is that the DDL generated by your tool doesn't by default include the column list which is why you don't see it. If you dropped the view and rebuilt it with their generated DDL instead of using the script that originally created it and you would lose the aliases.
Their may be options on the DDL generator to include the column name list in views, but as I don't have that tool I cannot verify that. To do that they need to query ALL_TAB_COLUMNS rather than just showing the query from within the TEXT field of ALL_VIEWS.

When using Toad to create a view in Oracle, how can I store the formatted script also?

This question may be Toad specific. I have no idea how Oracle stores views, so I'll explain what happens when I use Toad. If I get an answer that is Oracle specific, so much the better.
I have created a rather complex view. To make it clearer, I have formatted the code nicely, and entered some comments where needed. When I need to make changes to the view, I use Toad's "describe objects" window, where I can find a script to recreate the view. The only problem is that all my formatting is gone. Comments before the select keyword (but after "create view xxx as") will also disappear.
If I enter this script to create a view:
create or replace view TestViewFormatting as
-- Here I have a long comment explaining the role of the
-- view and certain things to be aware of if changing it.
-- Unfortunately this comment will disappear...
select
name, --This comment will be kept
accountnumber --This also
from
debtable
where
name like 'S%';
Toad will display this when I describe it later:
DROP VIEW XXX.TESTVIEWFORMATTING;
/* Formatted on 04.07.2012 09:35:45 (QP5 v5.185.11230.41888) */
CREATE OR REPLACE FORCE VIEW XXX.TESTVIEWFORMATTING
(
NAME,
ACCOUNTNUMBER
)
AS
select name, --This comment will be kept
accountnumber --This also
from debtable
where name like 'S%';
Note that the first comment has disappeared, and that the format is totally different.
I suspect that Oracle doesn't store the code of the view, just some parsed version, and when Toad brings up the script, it reverses this parsed version and generates a script on the fly.
What will I have to do to make Toad/Oracle keep the original formatting?
(PS: I know I can change the settings for Toad's code formatter, but this is not what I want to do. Due to some questionable choices in my past, this particular view has several levels of inline views, and I need a very specific formatting to make it clear what happens)
select text from user_views
where view_name = 'YOUR_VIEW_NAME';
I've tested with:
create view z_v_test as
select
-- te
--st
* from
dual;
and it keeps even the blank line.
Another way is to use DBMS_METADATA:
select dbms_metadata.get_ddl('VIEW', 'YOUR_VIEW_NAME', user) from dual
This works not only for views, but also for (nearly) all kind of database objects (tables, triggers, functions, ...).

How to create a comment to an oracle database view

I would really like to create a comment to a view with a short description of its purpose. Unfortunately it is not possible to create comments to views in oracle. This feature is only for tables, columns and materialized views available. I would like to know how you do describe your database views?
Try:
comment on table <name> is 'text';
The command works on views. For example:
CREATE OR REPLACE VIEW MY_VW AS
SELECT 1 FROM DUAL;
COMMENT ON TABLE MY_VW IS 'Clever comment.';
Its possible to create a comment on VIEWs too :
COMMENT ON TABLE view_name ;
Simply use :
Comment on table 'view Name' is ' Comment ...';
If use view in place of SQL , Oracle throws error invalid object category for COMMENT command".
Materialized View : we can simply comment using:
Command on Materialized view "View name" is 'Comment ...';

Resources