illegal usage of identity column teradata - teradata

Getting a error while creating the volatile table illegal usage of identity column..
CREATE VOLATILE TABLE t1 (
ID1 INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 MINVALUE 0 MAXVALUE 100 NO CYCLE),
NoSec BigInt
) ON COMMIT PRESERVE ROWS;

Well, when you read the error messages it clearly indicates that IDENTITY columns are not supported for Volatile Tables.
This is a cut&paste from the Messages manual:
5784 Illegal usage of Identity Column %VSTR.
Explanation: User attempted to define an invalid identity column or use an identity column incorrectly. The error is returned if:
1) an identity column is defined as
a) part of a composite index
b) a join index or hash index
c) a primary partition index
d) a value-ordered index.
2) the input parameter of an INSERT into identity column is a using field (e.g., :F1) which is part of an expression, e.g. :F1+:F2 or :F1+2.
3) an identity column is defined in a temporary or **volatile table. It may
only be defined in a permanent table.
4) a USING statement contains multiple INSERT statements that insert into different
identity column tables.
5) the input parameter of an INSERT into identity column of type BY DEFAULT is a using field (e.g., :F1) that is being reused in another parameter in the insert statement, e.g. USING(F1 INT, F2 INT) INS tab(:F1,:F1);

Related

Creating index with unique constraint for new blank field

A new column was added to an existing DB table (PA0023).
DB: HANA
The column should be unique, therefore i tried to create a unique index constraint via SE11.
Activation succeeded. However, while creating the index via Utilities... Database Utility, an error showed up:
Request: Create Index PA0023-Z01
...
sql:
CREATE UNIQUE INDEX 'PA0023~Z01' ON 'PA0023'
('MANDT',
'RECORD_KEY')
cannot CREATE UNIQUE INDEX; duplicate key found [5] Several documents with the same ID exist in the index;SAPABAP1:PA0023.$uc_PA0023~Z01$ content not unique, cannot define unique constraint. rowCount != distinctCount.
There aren't rows with the same value filled in that column. There are rows with blank value, which are considered duplicates. After replacing blanks in development environment, index was created well. It's less possible in production, because there are many records with an empty value in that new field.
So my question is: Is there a way to create the unique constraint without replacing the blanks?
You cannot create a unique constraint if the existing data does not provide uniqueness. So no you can't do this if you have multiple NULL values for the key. You would need to ensure the data is unique before creating the constraint.
This is normal database practice, it's not HANA specific.
While it is true that a compound primary key cannot contain any nullable columns it is permitted for a compound unique/candidate key to be defined with nullable columns. The only golden rule is that when adding or updating a record if any column in the unique key contains a NULL value then the index entry is NOT written to the database.
MySQL does this by default.
SQL Server will do this provided that you add "WHERE columnX IS NOT NULL" to the key's definition.
ORACLE is the same as SQL Server, except that the syntax is more complicated.

How to handle date type in SQLite?

I've created a table, where I have "Date of birth" column of date type. The problem is that I can insert anything and it's successfully done. I want that field to restrict opportunities like inserting strings and not related stuff.
insertions
wrongResults
I've searched for the solution, but I could only find codes for getting the current time in different formats. I also don't get how exactly modifiers work (https://www.sqlite.org/lang_datefunc.html).
Bar the rowid column or an alias of the rowid column, any type of value can be stored in an type of column. That is the type of column does not restrict/constrain the data that can be stored.
p.s. there is no DATE type rather due to SQLite's flexibility DATE actually has a type (type affinity) of NUMERIC (not that that matters that much). You might find Datatypes In SQLite Version 3 an interesting read or perhaps this How flexible/restricive are SQLite column types?.
the rowid and, therefore an alias thereof, column MUST be an integer. Although typically you allow SQLite to assign the value.
You should either check the data programatically or alternately use a CHECK constraint when defining the column in the CREATE TABLE SQL.
A CHECK constraint may be attached to a column definition or specified
as a table constraint. In practice it makes no difference. Each time a
new row is inserted into the table or an existing row is updated, the
expression associated with each CHECK constraint is evaluated and cast
to a NUMERIC value in the same way as a CAST expression. If the result
is zero (integer value 0 or real value 0.0), then a constraint
violation has occurred. If the CHECK expression evaluates to NULL, or
any other non-zero value, it is not a constraint violation. The
expression of a CHECK constraint may not contain a subquery.
SQL As Understood By SQLite - CREATE TABLE
Example
Consider the following code :-
DROP TABLE IF EXISTS mychecktable ;
CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'));
INSERT INTO mychecktable VALUES('14-03-1900');
INSERT INTO mychecktable VALUES('1900-03-14'); -- ouch 3rd char not -
The is will result in :-
DROP TABLE IF EXISTS mychecktable
> OK
> Time: 0.187s
CREATE TABLE IF NOT EXISTS mychecktable (mycolumn BLOB CHECK(substr(mycolumn,3,1) = '-'))
> OK
> Time: 0.084s
INSERT INTO mychecktable VALUES('14-03-1900')
> Affected rows: 1
> Time: 0.206s
INSERT INTO mychecktable VALUES('1900-03-14')
> CHECK constraint failed: mychecktable
> Time: 0s
i.e. the first insert is successful, the second insert fails.
Usually you would enforce the correct format in your application, but you can also add constraints to your table definition to prevent this, e.g.,
CREATE TABLE users(...,
DoB TEXT CHECK(DATE(DoB) NOT NULL AND DATE(DoB)=DoB)
)

Does Teradata reuse values when an identity column is defined as GENERATED BY DEFAULT... NO CYCLE?

I need to delete rows from a Teradata Table that has an IDENTITY column defined as:
Some_Id INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY
(START WITH 1
INCREMENT BY 1
MINVALUE 0
MAXVALUE 1000000000
NO CYCLE)
I want to know if Teradata will use again for new rows the values that the rows deleted had. I understood from the Teradata documentation that the NO CYCLE won't allow this, but I'm not really sure from what I've read in other posts and how it affects the GENERATED BY DEFAULT option.
I know that since it is defined as GENERATED BY DEFAULT someone could insert a row with one of the old numbers. I'm asking just for the values automatically generated by Teradata when the column value is not provided.

Limiting the number of rows a table can contain based on the value of a column - SQLite

Since SQLite doesn't support TRUE and FALSE, I have a boolean keyword that stores 0 and 1. For the boolean column in question, I want there to be a check for the number of 1's the column contains and limit the total number for the table.
For example, the table can have columns: name, isAdult. If there are more than 5 adults in the table, the system would not allow a user to add a 6th entry with isAdult = 1. There is no restriction on how many rows the table can contain, since there is no limit on the amount of entries where isAdult = 0.
You can use a trigger to prevent inserting the sixth entry:
CREATE TRIGGER five_adults
BEFORE INSERT ON MyTable
WHEN NEW.isAdult
AND (SELECT COUNT(*)
FROM MyTable
WHERE isAdult
) >= 5
BEGIN
SELECT RAISE(FAIL, "only five adults allowed");
END;
(You might need a similar trigger for UPDATEs.)
The SQL-99 standard would solve this with an ASSERTION— a type of constraint that can validate data changes with respect to an arbitrary SELECT statement. Unfortunately, I don't know any SQL database currently on the market that implements ASSERTION constraints. It's an optional feature of the SQL standard, and SQL implementors are not required to provide it.
A workaround is to create a foreign key constraint so isAdult can be an integer value referencing a lookup table that contains only values 1 through 5. Then also put a UNIQUE constraint on isAdult. Use NULL for "false" when the row is for a user who is not an adult (NULL is ignored by UNIQUE).
Another workaround is to do this in application code. SELECT from the database before changing it, to make sure your change won't break your app's business rules. Normally in a multi-user RDMS this is impossible due to race conditions, but since you're using SQLite you might be the sole user.

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

Resources