Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 932
Sunil Chavan Avatar asked Jul 03 '12 09:07

Sunil Chavan


People also ask

How do you add a comment to a column in SQL?

Use the COMMENT statement to add a comment about a table, view, materialized view, or column into the data dictionary. To drop a comment from the database, set it to the empty string ' '. See Also: "Comments" for more information on associating comments with SQL statements and schema objects.

Can you add a column to a table in SQL?

In SQL, create new column using the ALTER TABLE statement. This program helps to add column in SQL. You can also modify, or delete columns in the existing table. It can also be used to add or drop various constraints on the existing table.

How do you add a column to a specific position in an existing table in SQL?

To add a column at a specific position within a table row, use FIRST or AFTER col_name . The default is to add the column last. You can also use FIRST and AFTER in CHANGE or MODIFY operations to reorder columns within a table.


2 Answers

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.

like image 60
Ben Avatar answered Sep 22 '22 12:09

Ben


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...'; 
like image 37
Ravi Sambangi Avatar answered Sep 20 '22 12:09

Ravi Sambangi