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' )
Related
I keep getting a syntax error in PSQL and I can't figure out why. Any help would be greatly appreciated
SELECT page_code, developer
FROM page
WHERE data_approved=null and approved_by=null
ALTER TABLE page
RENAME COLUM page_code TO unapproved;
You are, probably, looking for alias, not for altering table:
SELECT page_code AS unapproved,
developer
FROM page
WHERE data_approved IS null
AND approved_by IS null
Note, that page table will stay intact while its page_code field will be represented in this particular query as unapproved. Please, be very careful with altering tables since doing this you change the rules for all the users.
Another issue is = null which returns null (neither true nor false). IS null is the right syntax.
ALTER TABLE table_name RENAME new_table_name;
this command will help you to change table name
Rename is used to change the table name and
change is used to rename column name
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'
Is it possible to create a table with column name containing space? If so how can I create and use it?
It is possible, but it is not advisable. You need to enclose the column name in double quotes.
create table my_table ("MY COLUMN" number);
But note the warning in the documentation:
Note: Oracle does not recommend using quoted identifiers for database
object names. These quoted identifiers are accepted by SQL*Plus, but
they may not be valid when using other tools that manage database
objects.
The name will be case-sensitive, and you wil have to enclose the name in double quotes every time you reference it:
select "MY COLUMN" from my_table;
So... don't, would be my advice...
Yes, it's possible. You just have to be sure to quote the column name. For instance:
CREATE TABLE Fubar ("Foo Bar" INT);
INSERT INTO Fubar VALUES (1337);
SELECT "Foo Bar" FROM SpaceMonster
Even though it's possible, it doesn't make it a good idea. You'll probably save yourself from a lot of pain if just replace all you spaces with underscores.
it is possible by naming the column between two "
example: "My columN" , the column name becomes case sensitive which means.
SELECT "my column" from table; --NOT OK
SELECT "My columN" from table; --OK
You can (see the Oracle doc) if you quote these appropriately. However I suspect this is not a good idea, since you'll have to quote everything. Generally db naming standards / conventions (e.g. here or here) favour using underscores (which don't require quoting) over whitespace.
This is the columns rule defined for oracle
Columns (for tables)
All columns are in form {alias}_{colname}. For example prs_id,
prs_name, prs_adr_id, adr_street_name. This guarantees that column
names are unique in a schema, except denormalized columns from
another table, which are populated using triggers or application
logic.
All columns are in singular. If you think you need a column name in plural think twice whether it is the right design? Usually it
means you are including multiple values in the same column and that
should be avoided.
All tables have surrogate primary key column in form {alias}_id, which is the first column in the table. For example, prs_id, mat_id,
adr_id.
you can always have alias for column name bu using ""
Did you Google for this? I did - the 6th link was this, in which I find the following:
create table employee (
"First Name" varchar2(20),
"Last Name" varchar2(20),
Address varchar2(60),
Phone varchar2(15),
Salary number,
EmpID number,
DeptID number
);
... which works fine when I tried it in 10g.
I get an error when i try to do this using SQL. ftp.server is the key whose value i would like to change:
UPDATE OL_PREF SET ftp.server='dev.isinet.com'
If the column name is really ftp.server, then the correct Oracle syntax for updating it should be
UPDATE OL_PREF SET "ftp.server" ='dev.isinet.com'
In Oracle, double quotes are the correct way to handle column names that have non-standard characters, including lower-case letters and the period. Note that you must have the exact correct column name for this to work, including case.
You can verify the column name with:
SELECT column_name FROM user_tab_columns WHERE table_name='OL_PREF' ORDER BY column_name;
If what you really mean is that you have a table that stores key-value pairs, and that 'ftp.server' is a key, then you probably want an update like the one in Mark Wilkins' answer.
UPDATE OL_PREF SET [ftp.server]='dev.isinet.com'
or
UPDATE OL_PREF SET [ftp].[server]='dev.isinet.com'
if ftp is a schema and server is the fieldname.
You can't put the column name in quotes.
You could use brackets, which have the same functionality as quotes.
UPDATE OL_PREF SET [ftp.server] ='dev.isinet.com'
Also, it's a good idea to use "two part naming", specifying the schema as well:
UPDATE dbo.OL_PREF SET [ftp.server] ='dev.isinet.com'
UPDATE OL_PREF
SET ftp.server ='dev.isinet.com'
no '' for column names
SQL Update
'ftp.server' is in quotes. (and that should not be)
Are you getting the error ORA-00904 string : invalid identifier?
If so read this
I did a quick test on this
SQL> create table t_tab_1 ( tname varchar2(3), "ftp.service" varchar2(20) );
Table created.
SQL> insert into t_tab_1(tname,"ftp.service") values('21','ftp://fila');
1 row created.
SQL> update t_tab_1 set "ftp.service"='fila.real.com';
1 row updated.
If there is more than I row in your table remember to use the where clause in your update statement.
I'm trying to modify a table inside my PostgreSQL database, but it says there is duplicate! what is the best way to find a duplicate value inside a table? kinda a select query?
Try Like This
SELECT count(column_name), column_name
from table_name
group by column_name having count(column_name) > 1;
If you try to change a value in a column that is part of the PRIMARY KEY or has a UNIQUE constraint and get this error there, then you should be able to find the conflicting row by
SELECT *
FROM your_table
WHERE conflicting_column = conflicting_value;
If conflicting_value is a character type, put it in single quotes (').
EDIT: To find out which columns are affected by the constraint, check this post.
First of all, determine which fields in your table have to be unique. This may be something marked as a Primary Key, a unique index based on one or more fields or a check constraint, again based on one or more fields.
Once you've done that, look at what you're trying to insert and work out whether it busts any of the unique rules.
And yes, SELECT statements will help you determine what's wrong here. Use those to determine whether you are able to commit the row.