alter table with space in name - sql

I have a table with a space in the name generated by a system.
I am trying to alter the table name to remove the space so that it can be processed by a library the pre-exists.
I am trying:
ALTER TABLE 'My Table'
RENAME TO 'MyTable';
I have also tried double quotes, no luck.
Any pointers?

[This will not work in MS-Access. Tables cannot be renamed in Access. Not clear if original question applied to MS Access.]
Square brackets:
ALTER TABLE [My Table]
RENAME TO [MyTable];
Square brackets can't enclose the entire object "path" so this won't work:
ALTER TABLE [MyDatabase.dbo.My Table]
but this will
ALTER TABLE [MyDatabase].[dbo].[My Table]

For MySQL, single quotes, double quotes or brackets did NOT work for me. Only backticks (aka backquotes) worked.
So, try this:
ALTER TABLE `My Table`
RENAME TO MyTable;

[My Table]
You can use square brackets in SQL to get around this.
It has many functions, you can use keywords in tables, put spaces and periods in table names or schemas, etc.
For example you can have the schema [Work.Employees]. With the square bracket it would be [Work.Employees].Addresses (schema, table). However, if you forget the brackets it will attempt to find the database Work -> schema Employees -> Table Addresses.
However, it is generally good practice to avoid doing any of the above :)

This is one of those things that is a lot easier to achieve using the Access GUI!
To do the same in SQL DDL you must 'clone' the table, for which you must already have knowledge of all the attribute names, types, constraints, etc, noting it may have features that are not creatable via SQL DDL e.g. Validation Rules. Then you need to populate it using the original table. The you drop the original. Phew!

Related

How to represent double quotes in dynamic PL/pgsql ALTER TABLE statement

I find myself in a position that I'm sure many others have before. I generated my PostgreSQL schema using a generator and now I'm in production(!), I realise I should really try to remove the double quotes from the table and column names that the generator added i.e.
CREATE TABLE "myschema"."mytable" (....
should have been
CREATE TABLE myschema.mytable (....
I'm writing a PL/pgsql function to loop through information_schema.tables and information_schema.columns and then to execute ALTER TABLE statements to drop the doube quotes.
What I can't grasp from the documentation and searhcing is how to issue that statement.
I know it's an EXECUTE I need but can someone help me with the syntax to rename "mytable" to mytable using that. I'm lost with $$ and format() and quote_ident() and think I'm overcomplicating how to indicate that my original table names will have double quotes.
The double-quotes in your examples are not part of the names, just decorators to preserve original spelling of identifiers. See:
Are PostgreSQL column names case-sensitive?
While working with lower-case, legal identifiers (like your examples suggest), those double-quotes are purely optional. Add them or leave them.
If you've been unwise enough to create objects with illegal names or with mixed case, those double-quotes are required at all times. The cure is to stick to legal, lower-case identifiers.
Start by reading the manual about identifiers.

Rename column in existing table in sql server where column name is blank

I want to rename column in sql server because I forgot to give name to one column. I have also tried the query below but it is not working either:
EXEC sp_RENAME 'TableName.[]' , '[Frequency of compliance]', 'COLUMN'
Using SQL Server Management Studio interactive interface, expand the DB and expand the tables and then right click on the table name to display the options, and then choose design
you can rename, change the type, add columns, including delete columns, and many others that you can do to design your table
After you finish designing, just CTRL + S to save the change, this is really simple and easiest way to design your tables
I am sure this is not possible in SQL server.
You should instead do this to find the actual column name:
select c.name from sys.columns c
join sys.tables t on c.object_id=t.object_id and t.name='TableName'
This will give you the name of all the columns from table.
You should seek the right name and then rerun sp_rename procedure.
Empty columnnames are not allowed in sql server, but they can only consist of spaces.
To rename it you can add space(s) between [].
Also you need to remove the square brackets from the new columnname, otherwise the columnname will be [Frequency of compliance] including the brackets.
But I would recommend to completely not use spaces inside columnnames.
EXEC sp_RENAME 'TableName.[ ]' , 'Frequency of compliance', 'COLUMN'
Another possibility is to use the design-mode in SSMS.

SQL Server : drop schema with special characters

I've got an old schema that needs to be removed. Problem is it's got the special character \ in the name (believe me I'm no fan of special characters in schema names). I have tried commands like the following
DROP SCHEMA databasename."COMPANY\user1"
where COMPANY\user1 is the name of the schema.
However, I end up getting errors like the following
SQL Error: Incorrect syntax near '.'.`
I've dropped all of the tables inside of the schema, so I don't think there should be any objects remaining. I had success dropping tables with the following command
DROP TABLE databasename."COMPANY\user1".persontable;
Any idea why my attempt to drop the schema is failing? I'm sure it's something obvious I'm missing in the syntax.
Have you tried using square braces?
drop schema databasename.[COMPANY\user1]
Actually, this doesn't work, because drop schema doesn't accept the database (as sort of implied by the syntax in the documentation). Just go into the database and do:
drop schema [COMPANY\user1]
This works for me with names that have unusual characters.

Oracle create table with column comments

Is there a column comment syntax that allows me to specify a column comment directly where I declare the column in the create table statement (i.e. inline)? The 11g spec does not mention anything, on another page something is mentioned but I could not get it to work. There is a way to specify comments after creating the table, but I think it is annoying that the comment is separated from the field definition. I am looking for something like this (which does not work):
create table whatever (
field number(15,0) primary key comment 'primary key generated from sequence pkseq',
...
)
I'm afraid the "annoying" COMMENT ON syntax is the only way of doing this. SQL Server, PostgreSQL and DB2 use the same syntax (even though, as far as I know, there is no ANSI standard syntax for adding comments to database objects).
MySQL supports the way you would like it to work. I agree it would be a nicer mechanism, but in my experience so few people use comments at all that I doubt Oracle will ever change it.
I'm afraid it can only be done after table creation, using the comment on column ... is '' syntax.
A workaround to this annoying syntax is also to view and edit the tables in Oracles SQLExplorer. It contains a wizard that allows you to edit the comments right next to the columns. It even allows easy creation of alter table scripts.
My procedure when editing tables is to enter the changes in the wizard without actually executing them, then go to its DDL tab and retrieve the SQL from there (as update, not full create script) and press cancel on the wizard. Then I put the created SQL into the SQL script I am writing. Only when I am finished with the script I execute everything; I do never make any changes with the wizard itself.
Test on sqlplus (or similar), but the syntax is as follows:
-- assuming you have privileges
COMMENT ON COLUMN SCHEMA1.TABLE1.COL1
IS 'My comment'
-- then you can double check like this
SELECT * FROM all_col_comments WHERE
(OWNER, TABLE_NAME, COLUMN_NAME)
IN (('SCHEMA1','TABLE1','COL1'));
Note that the comment will now show in SQLDeveloper (or Toad or whatever env you have) until you reopen said table's properties.
Similar syntax can be used to annotate tables, indexes and materialized views. [source: https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_4009.htm]
I understand similar syntax exists for MySQL and others, but it is not proper ANSI. It's very useful, though.

Question about delimiter characters for SQL Server 2008 queries and truncate syntax.

I'm new to SQL, so I apologize if this is a dumb question.
When do I need to use the brackets '[' and ']' around schema and table names? Could I just use them always?
Now, suppose I did the following to create a table
CREATE TABLE [dbo].[table1] ([ID] VARCHAR(5))
This worked. I verified it by running a select statement. Suppose time goes on and I added a bunch of rows. Now if I want remove all the rows, should I be using delete or truncate? It seems I should be doing this
TRUNCATE [dbo].[table1]
But I keep getting incorrect syntax near 'dbo' or incorrect syntax near keyword TRUNCATE when I do this via jtds jdbc driver.
What am I missing with the syntax?
TRUNCATE [dbo].[table1]
should be
TRUNCATE TABLE [dbo].[table1]
brackets are used to allow for invalid column names like then ones that start with a number or have spaces in them
example
CREATE TABLE [ ]([ ] INT)
INSERT [ ] VALUES(1)
SELECT [ ] FROM [ ]
you can always use brackets, they are not required but guard against problematic column names
I think you wanted TRUNCATE TABLE ... there is no TRUNCATE on its own.
TRUNCATE TABLE [dbo].[table1];
The [] are required around table and column names that are reserved words, or include spaces. MS tends to put them around everything, but most of the time they are not necessary.
And as Aaron said, you want TRUNCATE TABLE.
In answer to the fist part of your question:
When do I need to use the brackets '[' and ']' around schema and table names? Could I just use them always?
Use brackets when the names clash with SQL keywords (usually you can see this by the colours in your IDE) and Yes - you can always use them.
Truncate Table and Delete From are not the same though they often appear to do the same thing.
Truncate Table requires some heightened permissions and Truncate Table "Removes all rows from a table without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources."
This means that Truncate always removes all of the rows in a table and cannot be rolled back as part of a transaction.
Delete From is almost always what you want to do as part of transactional processing. Truncate Table is appropriate while developing and testing as a standalone, adhoc command. If you feel an urge to put Truncate Table in a stored procedure, check yourself and see if Delete might be more appropriate.