sybase create table syntax error - sql

I am trying to create a table in Sybase and i get getting the same syntax error. Which is ASA Error -131: Syntax error near '(' on line 1
Here is my create table script:
CREATE TABLE tablename
(NUM_PO BIGINT DEFAULT AUTOINCREMENT,
MNT NUMERIC(9) NULL,
QTY_PROD NUMERIC(9) NULL,
NUMERIC(14) NULL
PRIMARY KEY (NUM_PO)
);

It appears you are trying to specify the database the table should be created in. In SQL-server, sometimes it would nag if you didn't put the owner of the table creating it within the database. I looked at SyBase's create table, and I think you just need to make a slight shift
create table IDW.tablename
to
create table IDW..tablename
The "IDW" appears to be your database. the extra period via ".." would imply that whoever you are connected as is the table owner, or just goes to default owner value, THEN the table name.
Hope this helps.

Unless I am missing something the definition of the last column is incomplete. Only the data type is provided.
See if the error disappears once you fix this. Otherwise the table DDL looks perfect. I verified with the Sybase manual too.

Related

Create a table in dbshell with django

here is my code :
frontgreat=> CREATE TABLE contact_titlemessagesuggestion;
ERROR: syntax error at or near ";"
LINE 1: CREATE TABLE contact_titlemessagesuggestion;
i don't understand why it's not working and why it's an syntax error.
frontgreat=> DROP TABLE contact_titlemessagesuggestion;
ERROR: table "contact_titlemessagesuggestion" does not exist
have no syntax error and work fine.
Regards
You can normally not create a table without any columns. Therefore one often either has to list the columns, or use for example a query that provides both data (but meta-data as well) to construct the columns in the table.
For example:
CREATE TABLE contact_titlemessagesuggestion (
pk INT
);

Oracle SQL alter table add column with current user name

It was a 2 part question and I got the timestamp correctly.
I'm on 12C and trying to do something like:
ALTER TABLE customers
ADD modified_by (USER FROM DUAL);
Basically just columns in a table that show who modified the table and at what time they did so.
I also tried
ALTER TABLE customers
ADD last_modified TIMESTAMP;
ALTER TABLE customers
ADD modified_by USER;
and other combinations of keywords that I found on this site and other sites but none of them work.
We only learned dual in class but I'm looking for any way to do these.
Edit:
I now understand what was taught to me by the user with almost 1 million points.
Still unsure how to do the username.
Read this:
https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2114.htm#REFRN20302
and tried:
ALTER TABLE customers
ADD modified_by USERNAME;
doesn't work get invalid datatype.
Then saw this: https://www.techonthenet.com/oracle/questions/find_users.php
and tried:
ALTER TABLE customers
ADD modified_by USERNAME FROM DBA_USERS;
but getting invalid alter table option. SQL is hard.
After you edited the question, it seems that you are somewhat closer to what you want. This:
ALTER TABLE customers ADD modified_by VARCHAR2(30);
^^^^^^^^^^^^
datatype goes here, not what you'd like
to put into this column
Then, during inserts or updates of that table, you'd put USER in there, e.g.
insert into customers (id, modified_by) values (100, USER);
Or, probably even better, set it to be default, as #a_horse_with_no_name suggested:
ALTER TABLE customers ADD modified_by VARCHAR2(30) DEFAULT USER;
so - if you don't explicitly put anything into that column, it'll be populated with the USER function's value.
If you read through the Oracle documentation on virtual columns, you will find this:
Functions in expressions must be deterministic at the time of table creation, but can subsequently be recompiled and made non-deterministic
A deterministic function is one that returns the same value when it is passed the same arguments. The expressions that you are using contain non-deterministic functions. Hence, they are not allowed.
The error that I get when I try this is:
ORA-54002: only pure functions can be specified in a virtual column expression
For some unknown reason, Oracle equates "pure" with "deterministic" in the error message. But this is saying that the function needs to be deterministic.

Google BigQuery: how to create a new column with 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. :)

Can a table and a column within it use the same name in Sybase?

I am using a Sybase database and would like to know if it's valid to have a table contain a column with the same name as that of the table, e.g.:
CREATE TABLE foo
(
foo int not null,
etc...
)
Valid? Sure. Recommended? No.
Yes, we can surely do that but as said it is not recommended. But why are we able to do so? Well, because a table name entry goes into the system table sysobjects however column entries go in the system table syscolumns.
Therefore there is no restriction in using table name as column name, however using table name as column name usually is not recommended because it makes your table structure a bit confusing and also it adds bad practice to database design.

Adding a column after another column within SQL

How do I add a column after another column within MS SQL by using SQL query?
TempTable
ID int,
Type nvarchar(20),
Active bit
NewTable
ID int,
Type nvarchar(20),
Description text,
Active bit
That is what I want, how do I do that
Assuming MySQL (EDIT: posted before the SQL variant was supplied):
ALTER TABLE myTable ADD myNewColumn VARCHAR(255) AFTER myOtherColumn
The AFTER keyword tells MySQL where to place the new column. You can also use FIRST to flag the new column as the first column in the table.
It depends on what database you are using. In MySQL, you would use the "ALTER TABLE" syntax. I don't remember exactly how, but it would go something like this if you wanted to add a column called 'newcol' that was a 200 character varchar:
ALTER TABLE example ADD newCol VARCHAR(200) AFTER otherCol;
In a Firebird database the AFTER myOtherColumn does not work but you can try re-positioning the column using:
ALTER TABLE name ALTER column POSITION new_position
I guess it may work in other cases as well.