Renaming tables - sql

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

Related

Syntax for explicitly referencing a column name

I'm trying to make a join to a date table on a column called 'date'.
The issue I have is that the column name is the same as the table and it seems to be making a referencing to the table rather than the column (hence the yellow highlighting). I've found a workaround which is to rename the table - though does anyone know how to explicitly reference the column in the join rather than the table if they have the same name?
You can give aliases to the table/column and use that instead. Otherwise, you can use the table name before the column with a . as a separator, i.e., TableName.date = date.

MariaDB unable to drop column named "`"

Not sure how this happened, but a column got created named: "`". (just the back tick). When I attempt to drop this column, I end up with a syntax error.
Outside of taking a SQL dump and fixing the dump in a text editor, Does anybody have a suggestion to fix this query?
ALTER TABLE tableName DROP COLUMN "`";
You need to escape the inner backtick with another backtick, so:
alter table tableName drop column ````;

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.
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' )

Duplicate value in a postgresql table

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.

Can we have the table name as "option" in MySQL?

I am very, very new to MYSQL.I tried to create a table named "option".
My SQL Query is :
create table option(
id int not null primary key auto_increment,
choice varchar(30)
)
While executing this query it shows the following error
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option(
id int not null primary key auto_increment,
choice varchar(30)
)' at line 1
(0 ms taken)
If I try with the table name as "choice" it is working.
can we have the table name as "option" in mysql?
thanks
If you want to have a table name Option, you should be able to, just remember that whenever you use the table in a query, you will have to encase it in ` symbols. Like this.
`option`
The ` key on the top left of your keyboard, with the tilde.
Pick a different name (one that isn't a reserved word in your RDBMS) and save yourself and whoever else might work on it many headaches.
option is a reserved word in Mysql.we can use a reserved word by using the word inside a single quotes.
Better you select the other tablename.Ohterwise maintaining our code will be difficult.
You can use SQL keywords as table names in MySQL if you escape them with back-quotes.
CREATE TABLE `option` (
...
)
It's not normally a good idea to do so, though.
option is a reserved word in MySQL. Save yourself a world of pain and use choice for your table name.
See the MySQL documentation on this. You can do it as follows:
create table `option` (
...
)
Yes you can definitely create a table named option but in every query you will have to use
`option`
instead of plain option. Better improvise a little and create a table named options to save from trouble. Restrain from using mysql reserved words as table name or column name or procedure names.