Google BigQuery: how to create a new column with SQL - sql

I would like to add an column to an already existing table without using legacy SQL.
The basic SQL syntax for this is:
ALTER TABLE table_name
ADD column_name datatype;
I formatted the query for Google BigQuery:
ALTER TABLE `projectID.datasetID.fooTable`
ADD (barColumn date);
But than the syntax is incorrect with this error:
Error: Syntax error: Expected "." or keyword SET but got identifier "ADD" at [1:63]
So how do I format the SQL properly for Google BigQuery?

Support for ALTER TABLE ADD COLUMN was released on 2020-10-14 per BigQuery Release Notes.
So the statement as originally proposed should now work with minimal modification:
ALTER TABLE `projectID.datasetID.fooTable`
ADD COLUMN barColumn DATE;

BigQuery does not support ALTER TABLE or other DDL statements, but you could consider submitting a feature request. For now, you either need to open in the table in the BigQuery UI and then add the column with the "Add New Field" button, or if you are using the API, you can use tables.update.

my_old_table
a,b,c
1,2,3
2,3,4
CREATE TABLE IF NOT EXISTS my_dataset.my_new_table
AS
SELECT a,b,c,
"my_string" AS d, current_timestamp() as timestamp
FROM my_dataset.my_old_table
my_new_table
a,b,c,d,timestamp
1,2,3,my_string,2020-04-22 17:09:42.987987 UTC
2,3,4,my_string,2020-04-22 17:09:42.987987 UTC
schema:
a: integer
b: integer
c: integer
d: string
timestamp: timestamp
I hope all is clear :)
With this you can easily add new columns to an existing table, and also specify the data types. After that you can delete the old table, if necessary. :)

Related

Alter Table in SQL [duplicate]

I have a PostgreSQL (9.0) database with a column card_id which is currently of type integer
I need to change this to type text
What is the most best way to achieve this?
The only solution I can find involves creating a temporary column, dropping the original then renaming, I thought they might be a better method??
Have you tried what the fine manual suggests:
ALTER TABLE table ALTER COLUMN anycol TYPE anytype;
Depending on the current and the new type you may need to add USING ... to this statement.
But in your specific case that should not be necessary I believe.
ALTER TABLE table ALTER COLUMN card_id SET DATA TYPE text;

how to fix this please

I Created the db with phpmyadmin and created 8 columns i forgot to create date column i tried to create it with this code and i got this
ALTER TABLE "users" ADD "cdate" DATE
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near '"users" ADD "cdate" DATE' at line 1
You can Use phpmyadmin to add a new column without any code
Or Use this :
ALTER TABLE `users` ADD `cdate` DATE
You should use this ` not this "
you can add columns to existing table using phpmyadmin
below the table structure you can find add [num] columns
and since you asked
ALTER TABLE `users` ADD `cdate` DATE
Read about this in the documentation https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
ALTER TABLE table_name ADD column_name column_definition
you dont need any qoute
ALTER TABLE users ADD cdate DATE
Backticks are to be used for table and column identifiers, but are only necessary when the identifier is a reserve word of mysql

Custom SQL with ALTER TABLE

In Tableau, I'm trying to add a column through Alter Table in a custom SQL
It looks like this:
ALTER TABLE (SELECT [W$].[W], [W$].[SDate], [W$].[EDate], [Date E$].[Date]
FROM [W$],[Date E$]
WHERE [Date E$].[Date]
BETWEEN [W$].[SDate] AND [W$].[EDate])
ADD [Type] string NOT NULL DEFAULT "P1"
But I get an error saying Syntax Error in FROM clause.
I also get this error if I use ALTER TABLE [Date E$]
ALTER TABLE [Date E$]
ADD [Type] string NOT NULL DEFAULT "P1"
error message:
Database error 0x80040E14: Syntax error in FROM clause.
; The query: SELECT TOP 1 * FROM ( ALTER TABLE [Date E$]
ADD [Type] string NOT NULL DEFAULT "P1" ) [TableauSQL]
This is my first time using Alter Table so I'm not sure where the FROM clause is being used, or if I'm doing this incorrectly. I'm following the instructions I found here:
Add a column with a default value to an existing table in SQL Server
Is it possible that Tableau doesn't allow Alter Table to be used when creating a Custom SQL?
I am using Tableau Desktop 2018.1.2
Tableau custom SQL will not allow DDL statements such as Alter table. It creates a read-only connection to the database.
You might be able to do this using the initial sql function that is available when you setup your connection. Really Tableau is the wrong tool for this. If you want to alter tables you should use a SQL client thats appropriate for your database.

Changing the data type "varchar'' of a column to "DATE" in SQL/ORACLE

I have a table in a database created in oracle 10G. It contains a column of type 'VARCHAR' and stores date as string in this format-> 'dd-mon-yyyy' eg: '12-aug-2008'. Now I want to change the datatype of this column from VARCHAR to DATE. but when i perfrom this query->
ALTER TABLE sales_order
MODIFY COLUMN delivery_date DATE;
I get following error
ORA-00905: missing keyword
I have also tried :
ALTER TABLE sales_order
ALTER COLUMN delivery_date DATE;
I got the error :
ORA-01735: invalid ALTER TABLE option
However when i try to add a fresh column with DATE datatype it works fine.
example :
ALTER TABLE sales_order
ADD delivery DATE;
So, can anybody suggest me a way to change the datatype without deleting the column and its data.
It's the first one, with a slight modification:
ALTER TABLE sales_order MODIFY (delivery_date DATE);
But I'm not sure that will work for those particular datatypes and it also may not work depending on the current data.
You may find it necessary in that case to:
create a new column X of date type.
populate X based on the old column (may need several passes of data fix-ups to work).
delete old column.
rename X to old column name.
Although its a pretty old question, I'll put my solution here for people seeking for a solution:
Here's my solution and it works perfectly.
ALTER TABLE `sales_order` CHANGE `delivery_date` `delivery_date` DATE;
Thank you
modify a column then syntax is:-
alter table table_name modify column_name datatype;
but when you modify the column datatype column must be empty
Thanks for the hints! This got it for me.
alter table Table_Name
Alter column Column_Name datatype
GO
I too was needing to change from a VARCHAR to a date. I am working in SQL 2008 R2. I have found that if I bring in dates as a char or varchar and then change the type to date or datetime, I can catch time/date problems more easily.
THIS STATEMENT WILL FAIL IF YOUR DATA HAS ANY BAD DATES. (I break the date down into sections to find the bad date so I can correct and then I can alter the column type.)
Alternatively, you could create a new column, in which the data type is DATE. then pass the data in your varchar as a date .then drop your initial column and finally rename your new column to what it was initially...code below.
ALTER TABLE my_table ADD (new_col DATE);
UPDATE my_table SET new_col=TO_DATE(old_col,'MM/DD/YYYY');
ALTER TABLE my_table DROP (old_col);
ALTER TABLE my_table RENAME COLUMN new_col TO old_col;
alter table employee add (DOB varchar(10));
if you add a column with datatype varchar and if you want to modify the datatype of DOB then you can use this command ->
alter table employee modify(DOB date);
Now the table is modified.

date syntax issue in sql

I keep getting this error message when trying to change the data type of my column:
alter table x modify column order_date date NOT NULL;
ERROR at line 1
ORA-00905 missing keyword
I not sure where I am going wrong, as I am aware there are many types of dates in sql?
Many thanks
The MODIFY clause does not take COLUMN as a keyword. This will work:
alter table x modify order_date date NOT NULL;
The syntax is documented in the Oracle SQL reference. Find out more.
We only need to include COLUMN with commands which have several different possibilities. For instance, with the ALTER TABLE ... DROP command, because we can drop columns, constraints or partitions....
alter table x drop column order_date ;
"when I tried entering NOT NULL, it said the table needed to be empty"
You should be able to apply a NOT NULL constraint, providing all the rows in the table have a value in the order_date column. The error message you get is quite clear:
ORA-01758 table must be empty to add mandatory (NOT NULL) column
This means your column has some rows without values. So, you need to update the table and populate those rows with some value; what you will use as a default depends on your business rules.