Sybase null datetime - datetime

I am attempting to insert a row into a table while leaving two datetime columns null.
Error (for both columns):
The column column_name in table conflict does not allow null values.
The only constraint the table table has is a primary key constraint. The constraint is not on the datetime columns.
Is it possible to have a null datetime in Sybase?

You could try casting the NULL value as a datetime datatype and see if that works.
For example:
INSERT INTO tbl_conflict (date_column) VALUES (CAST(NULL AS datetime))

I did not realize that Sybase defaulted columns to not null. I need to set the columns as null.

Related

Sqlite INSERT INTO ... SELECT using ORDER BY

I have a table as below:
CREATE TABLE IF NOT EXISTS TRACE_TABLE ([TRACE_NUM] INTEGER NOT NULL PRIMARY KEY [TRACE_ID] INTEGER NOT NULL [TRACE_TIME_DELTA] TEXT NOT NULL [TRACE_TIME_HEX] INTEGER NOT NULL [TRACE_TIME_AHB] INTEGER NOT NULL [TRACE_PARAM_TEXT] TEXT NOT NULL [TRACE_PARAM_TEXT_DECODED] TEXT);
Now I want to sort this table using a column. To do this I do following:
Create a new table TRACE_TABLE_TEMP using above statement if not exists.
Then delete all rows (in case any exists) by earlier operations
Then copy all rows from TRACE_TABLE to TRACE_TABLE_TEMP but in sorted order using a column.
I try to execute the statement in Sqlite DB browser but I am not getting the expected result. Please see below, the TRACE_NUM column is not sorted as DESC.
How do I copy the table to another in sorted order?
The documentation says:
If a SELECT statement that returns more than one row does not have an ORDER BY clause, the order in which the rows are returned is undefined.
So it does not make much sense to change the order in which rows are stored, because you'd have to put the same ORDER BY on the queries used to read the data later.
Anyway, the error is that 'TRACE_NUM' is a constant string. To refer to the contents of the column, use TRACE_NUM.

In SQLite, how do I alter a table to add a column with default null and foreign key constraint?

The SQLite doc says:
It is not possible to use the "ALTER TABLE ... ADD COLUMN" syntax to
add a column that includes a REFERENCES clause, unless the default
value of the new column is NULL.
I can't get my ALTER TABLE statement to work, it looks like this:
ALTER TABLE Customers ADD COLUMN AddressID INTEGER DEFAULT NULL FOREIGN KEY REFERENCES MasterAddress (AddressID);
I can see references to workarounds that create a constraint at CREATE TABLE time and bouncing the data through a new table but I would prefer altering my existing table. Is this possible?
I have set PRAGMA foreign_keys = ON.
DEFAULT NULL is the default; you do not need to specifiy it.
FOREIGN KEY is used to introduce a table constraint, but ALTER TABLE supports only column constraints. The syntax for a column constraint does not have the FOREIGN KEY:
ALTER TABLE Customers ADD AddressID INTEGER REFERENCES MasterAddress(AddressID);

Create Blob column in Sqlite

I am trying to create a table in SQLite with blob type column (Content):
create table [Files]
(
Id int identity not null
constraint PK_File_Id primary key,
MimeType nvarchar (400) not null,
Content varbinary (max) null
constraint DF_File_Content default (0x),
);
However the following is not being accepted:
Content varbinary (max) null
constraint DF_File_Content default (0x),
Why?
"Max" is the name of a standard SQLite3 function, so is not available as part of a type name.
See the syntax reference for the CREATE TABLE statement and data types. A type name can include numbers in parentheses (which are ignored), but not the word “MAX”.
It looks like you're trying to use MS SQL Server syntax, and there are several errors in your code:
As mentioned above, (max) is not accepted as part of a type name. Since value lengths are unconstrained by default, simply omit it.
varbinary gives the column “numeric affinity”. While such a column can store a blob, you'll probably want to declare it as blob instead.
0x is not a valid blob literal. The correct way to write an empty blob is x''.
identity is called autoincrement. And in order to use it, the type name must be integer rather than int. The not null is redundant: If you try to insert a null value into such a column, you'll get the auto-incremented ROWID instead.
Note: If you simply need Id to have unique values at any given time and don't care if previously-deleted values get re-used, then you can simply declare the column as integer primary key, and inserting null will fill in the column with an unused integer. The autoincrement keyword prevents the re-use of ROWIDs over the lifetime of the database, more closely matching the semantics of MS SQL's identity keyword. See the discussion at the link above.
While it's syntactically legal to declare a name for a column constraint, it's not necessary.
Try this:
create table [Files]
(
Id integer primary key autoincrement,
MimeType nvarchar (400) not null,
Content blob null default (x'')
);
Note that this does not enforce a length limit on a MimeType column. If you need to, add the constraint check (length(MimeType) <= 400).

How to insert data in Asp.net page when the table has an identity column

I'm getting error when I'm inserting data in database into a table with an identity specification.
SQL
string query = "insert into student values('"+txtname.Text+"','"+name_image+"','"+txtage.Text+"','"+txtaddress.Text+‌​"')";
Error is
Column name or number of supplied values does not match table definition.
You should get in the habit of always specifying the explicit list of columns you want to insert values into - that way, you can omit the identity column and prevent this error:
INSERT INTO dbo.Student(Col1, Col2, ...., ColN)
VALUES(Val1, Val2, ...., ValN)
Just saying INSERT INTO dbo.Students VALUES(...) means you have to provide a value for every column - including the identity column - which you cannot provide a value for!
Dont Try to Fill the auto-Increment column. Provides the column value other than auto-increment column.
suppose Column-1 is Auto-incremented then the query will be
Insert into tableName(Column-2,column-3,..) Values(val-2,val-3,..);
Leave the auto increment column

Sqlite setting the default value in create table

I wrote something like
create table if not exists QuickTest (
id integer primary key NOT NULL,
a TEXT DEFAULT #0,
b TEXT,
c TEXT);
I get an error on #0. Is there a way to insert parameters here or do i need to hardcode the value in? I typically like using parameters when setting values.
You have to use string constant or NULL.
http://www.sqlite.org/syntaxdiagrams.html#column-constraint
The DEFAULT constraint specifies a default value to use when doing an INSERT. The value may be NULL, a string constant, a number, or a constant expression enclosed in parentheses. The default value may also be one of the special case-independant keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP. If the value is NULL, a string constant or number, it is inserted into the column whenever an INSERT statement that does not specify a value for the column is executed. If the value is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the current UTC date and/or time is inserted into the columns. For CURRENT_TIME, the format is HH:MM:SS. For CURRENT_DATE, YYYY-MM-DD. The format for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".
You can't use parameters in a ddl (data definition language) statement, only in dml (data manipulation language) statement and select statements are parameters allowed.
CREATE TABLE test (id INTEGER DEFAULT -1, name VARCHAR DEFAULT na)
Above sql will create a table test with two colums id and name
While executing insert for this table if no values are specified for id and name then it will insert -1 for id column and na will be inserted for name column

Resources