Rename a column that is a Reserved Word [duplicate] - sql

This question already has answers here:
How do I escape a reserved word in Oracle?
(5 answers)
Closed 9 years ago.
I have a table called CONTACT_DATA in Oracle, and it has a column called NUMBER. I would like to rename this field to PHONE_NUMBER however, the following does not work because NUMBER is a reserved word in Oracle:
ALTER TABLE CONTACT_DATA RENAME COLUMN NUMBER TO PHONE_NUMBER;
I've looked on these forums, and found how to select and order by a column that is a reserved word. However, in this instance I'd prefer to rename the column instead. Also, I'd like to keep the existing data so dropping the table and re-creating it is not an option. I'm using Oracle version 11.2 Can anyone help?

Have you tried:
ALTER TABLE CONTACT_DATA RENAME COLUMN "NUMBER" TO PHONE_NUMBER;
Oracle uses the double quotes as an escape character for this purpose.

Related

When do we use double quotes for table names in SQL? [duplicate]

This question already has answers here:
When do Postgres column or table names need quotes and when don't they?
(2 answers)
Are PostgreSQL column names case-sensitive?
(5 answers)
Closed 3 months ago.
A SQL coding challenge provided a database; every table was accessible when passed as a string using double quotes and not when passed as a word as I am normally used to.
This did not work:
SELECT * FROM Athletes;
Error message: relation does not exist.
But this worked and I don't understand why:
SELECT * FROM "Athletes";
Was this defined during the database creation? Or is this from PostgreSQL?

SQL require table name in values section [duplicate]

This question already has answers here:
postgres column "X" does not exist
(1 answer)
Postgres error updating column data
(2 answers)
PostgreSQL "column "foo" does not exist" where foo is the value
(1 answer)
Closed 2 years ago.
I am trying to store the data in table by using insert query (I am using PostreSQL), but there is it.
ERROR: column "Fleet" does not exist
LINE 1: insert into appuser values("Fleet")
Postresql require column name in values section, but what is that? Why? (There is only test, my 'appuser' table has 2 columns, one of it is primary key auto-generated, so do not get confuse)
Your table has two columns and you are providing a value for only one, so you need to tell the database which one it is. Also, literal strings must be surrounded with single quotes (double quotes stand for column identifiers).
Assuming that the target column is mycol, that would be:
insert into appuser (mycol) values('Fleet')

To see whether a column of a table is been used somewhere in the database [duplicate]

This question already has answers here:
How can we figure out that a column in my oracle table is being populated/updated by a trigger of another table?
(3 answers)
Closed 5 years ago.
How to check whether a particular column of a table is used in any other object or not in Oracle? For example I have a table EMPLOYEE which has a column BONUS. I need to check whether this column of this table is been used by any other objects like View, Package etc in the database.
You can try the following SQL to check for the table and column used within the object definition
SELECT 1
FROM all_source
WHERE type IN ('PROCEDURE','PACKAGE BODY','FUNCTION')
AND text LIKE '%EMPLOYEE%'
AND text LIKE '%BONUS%';

How can I select a column named 'date' in Oracle? [duplicate]

This question already has answers here:
How do I escape a reserved word in Oracle?
(5 answers)
Closed 8 years ago.
I have a problem with SELECT query in ORACLE SQL. There is a table named 'Battles' with columns 'name' and 'date'. When I try to do:
SELECT date FROM Battles -
there is an error: (936) ORA-00936: missing expression. I guess the problem is name of column 'date' is similar with datatype name. But how can I deal with it?
Yes you guessed it correct. date is a reserve word in Oracle (in fact it's a datatype) and you should escape it using double quote "" like below.
SELECT "date" FROM Battles
That's the very same reason you should never choose column/table names to be reserve word. Even though almost all the RDBMS provides a way/mechanism to bypass this but it's a never a good practice.
In order to quote a identifier, Oracle uses the double quotes. Be aware, that this also makes them case sensitive (you said the column is named date in lowercase, so):
select "date" from Battles;
See Quoted Identifiers in the Oracle Doc.

How to change the length of a column in a SQL Server table via T-SQL [duplicate]

This question already has answers here:
What is the SQL to change the field length of a table column in SQL Server
(6 answers)
Closed 9 years ago.
I am looking to find out how to alter a fields properties contained in an SQL Server 2008 table via an SQL script.
I'm looking to change the 'Length' property specifically.
Does anybody know how to do this?
Thanks
So, let's say you have this table:
CREATE TABLE YourTable(Col1 VARCHAR(10))
And you want to change Col1 to VARCHAR(20). What you need to do is this:
ALTER TABLE YourTable
ALTER COLUMN Col1 VARCHAR(20)
That'll work without problems since the length of the column got bigger. If you wanted to change it to VARCHAR(5), then you'll first gonna need to make sure that there are not values with more chars on your column, otherwise that ALTER TABLE will fail.