sybase default while table creation null or not null? - sybase

I am creating a table in sybase with proeprty
"overriddenBy varchar(32)".
If i don't define it "null" or "not null" while table creation, will it allow null or not?
I am having issue
org.springframework.dao.DataIntegrityViolationException:
PreparedStatementCallback; SQL [insert into EntityPnlData(nodeId,
hierarchyViewId, entityPnlCriteriaId, receivedFeedId, pnlValue,
overridden) values (?, ?, ?, ?, ?,?)]; The column overriddenBy in
table EntityPnlData does not allow null values.
But it was working fine earlier. I am not sure what could have changed

If you set the database option 'allow nulls by default', then columns will always allow NULL except when explicitly specified otherwise.
You can also add the NULL keyword to allow NULLs, i.e. "create table mytab(mycol varchar(32) null)".
Or if the table is already created you can do "alter table mytab modify mycol varchar(32) [not] null". The latter may work only n ASE 15.7 or later.

Related

Error creating entity Microsoft SQL server IDENTITY_INSERT

I'm using JHipster 4. I've create a simple entity just with a "name" property and when I try to create a entity from UI I get the folowing error. (I'm using Microsoft SQL Server).
In think the important part of the error is :
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'tenant' when IDENTITY_INSERT is set to OFF.
Direct answer appears to be that you just need to configure jhipster to have identity_insert on, per this link: https://jhipster.github.io/tips/004_tip_using_ms_sql_server.html
Adding the identityInsertEnabled="true" is the same as wrapping your Inserts with IDENTITY_INSERT ON and IDENTITY_INSERT OFF which will allow you to insert the project autogenerated identities directly. This is why we are using the MS SQL Server Liquibase for.
The error tells you everything you need to know. You are trying to insert a value in to an identity column. This is not allowed, as the column is supposed to be automatically populated unless you explicitly turn it off temporarily.
Documentation on what identity_insert is here: https://msdn.microsoft.com/en-us/library/ms188059.aspx
You can turn it off using the command set identity_insert SchemaName.TableName off though you had better be very confident you are inserting the correct data.
I would recommend you do some investigation with whoever manages your database as to why that column is an identity column and whether or not you should be inserting into it at all.
Assume a table:
create table test(ID int identity(1,1)
,Name nvarchar(100)
)
ALL of these insert statements will throw an error:
insert into test(ID,Name)
select ID
,Name
from OtherTable
insert into test(ID,Name)
select null as ID
,'TestName' as Name
insert into test(ID,Name)
values(99
,'Name'
)
Whereas these will work, automatically generating a new, unique value for the ID column:
insert into test(Name)
select Name
from OtherTable
insert into test(Name)
select 'TestName' as Name
insert into test(Name)
values('Name')
In short, you need to insert nothing into the identity column. Not a blank string, not a null value, but absolutely nothing at all.

SQL Server - Why does it allow me to insert blanks into a non-nullable column?

I have a table in SQL Server with a column that is a varchar(50) and set as a non-null column. This column is not a primary key.
If I try to do a null insert, I get the error:
Cannot insert the value NULL into column. Column does not allow nulls.
INSERT fails.
Which is expected.
I would expect the same to be true if I tried to insert a '' but for some reason it is allowing blanks without an issue. Why? How can I make the table not allow blanks into this specific column? I don't want a default value, I want the insert to fail the same way that a null fails.
Thank you
An empty string is something different from a NULL value. NULL really means not available, not known.
You can add a CHECK constraint to implement the functionality you are after:
ALTER TABLE mytable ADD CONSTRAINT CHK_EmptyString CHECK ( myCol <> '')
[I] would expect the same to be true if I tried to insert a '' but for some reason it is allowing blanks without an issue. Why?
Because an empty string is not a null!
If you want to stop empty strings, you could use a check constraint.
Because an empty string is not a NULL value.
You can add a CHECK constraint on the table/column to get your desired result.
create table #a(name nvarchar(20))
alter table #a add check (name != '')
Incidentally, that check will also prevent strings of only spaces as well.
insert into #a(name) values ('')
insert into #a(name) values (' ')
insert into #a(name) values (' ')
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the CHECK constraint "CK__#a__________name__1CA18AD3". The conflict occurred in database "tempdb", table "dbo.#a__________________________________________________________________________________________________________________0000000BF04A", column 'name'.
The statement has been terminated.
EDIT: Constraint to allow a space-only insert.
ALTER TABLE #a ADD CHECK (name != '' or charindex(' ',name) = 1)
I would expect the same to be true if I tried to insert a ''
I want a steak, but vegetarian. IT MAKES NO SENSE - not in the term of "the same" (i.e. the same error) as null is not space.
Similar - ok. But then - there are cases a space may be wanted. Not ok with you? Why do you not set up a check constraint? That is the way to limit values to a particular value.

SQL Server Invalid Column Name Error

I have an invalid Column Name error when inserting a record to my SQL Server table. Here's the definition:
CREATE TABLE [dbo].[myTable]
(
[id] int IDENTITY(1, 1) NOT NULL,
[person_name] varchar(255) NOT NULL,
[modified_By] varchar(255) NOT NULL
)
ON [PRIMARY] WITH (DATA_COMPRESSION = NONE);
GO
And insert
INSERT INTO myDB.dbo.myTable (id, person_name, modified_By)
VALUES (1, 'Aishwarya', 'admin')
But I get the following error upon execution:
Lookup Error - SQL Server Database Error: Invalid column name
'modified_BY'.
I can SELECT from the column fine. The only thing I've noticed in error is that "BY" is capitalized, contrary to the table definition. Any ideas?
UPDATE: Updating all the syntax errors, apologeez
Is your database set to a case sensitive collation? You can check by running this query:
SELECT DATABASEPROPERTYEX('<Insert Database Name>', 'Collation') SQLCollation;
Case insensitive collations usually have CI in the name, such as SQL_Latin1_General_CP1_CI_AS.
Where as case sensitive collation might be something like Latin1_General_BIN.
If the database has a case sensitive collation, then the capitalization in the T-SQL must match the column definition.
Your post here doesn't include the modified_BY capitalization in the insert statement, so double check that. If it's capitalized there properly, then check if there are any triggers on the table that might have the improper spelling.
In your INSERT statement do not supply a value for your IDENTITY column "Id". It should look like:
INSERT INTO myDB.dbo.myTable
(person_name, modified_By)
VALUES ('Aishwarya', 'admin')
Not an answer anyone wants to hear, but our DBA dropped and recreated the table (which had triggers set on it) and this has resolved the issue.

How do I insert data in specific columns in SQL Server 2012?

I am using the AdventureWorks2012 database...
I created a backup table using the SELECT INTO clause. However, I got an error message when I tried to insert data into some columns using the query below:
INSERT INTO Sales.salesorderdetails_backup
(SalesOrderID ,
OrderQty,
ProductId,
SpecialOfferId,
UnitPrice,
UnitPriceDiscount,
LineTotal)
OUTPUT inserted.*
VALUES (57123,
45,
712,
1,
15.89,
0,
45.89)
I got the error msg:
Cannot insert the value NULL into column 'rowguid', table
'AdventureWorks2014.Sales.salesorderdetails_backup'; column does not
allow nulls. INSERT fails...
When you define your table, you can specify a "not null" constraint for any column or columns. Which means that you MUST put a value in that column for any insert.
This is generally a Good Thing. For example, "not null is the default for a primary key column.
You have two choices:
1) Make sure the column in question has a value before you "Insert" (PREFERRED)
2) Do an "alter table" to drop the "not null" constraint:
ALTER TABLE MyTable ALTER COLUMN MyColumn columnType NULL
ALSO: As you probably know, NEWID() is the MSSQL function to generate a GUID for your "insert".
I finally succeeded after I added DEFAULT constraints:
ALTER TABLE Sales.salesorderdetails_backup
ADD CONSTRAINT df_date DEFAULT GETDATE() for ModifiedDate;
ALTER TABLE Sales.salesorderdetails_backup
ADD CONSTRAINT df_guid DEFAULT NEWID() FOR rowguid;

Altering a column: null to not null

I have a table that has several nullable integer columns. This is undesirable for several reasons, so I am looking to update all nulls to 0 and then set these columns to NOT NULL. Aside from changing nulls to 0, data must be preserved.
I am looking for the specific SQL syntax to alter a column (call it ColumnA) to "not null". Assume the data has been updated to not contain nulls.
Using SQL server 2000.
First, make all current NULL values disappear:
UPDATE [Table] SET [Column]=0 WHERE [Column] IS NULL
Then, update the table definition to disallow "NULLs":
ALTER TABLE [Table] ALTER COLUMN [Column] INTEGER NOT NULL
I had the same problem, but the field used to default to null, and now I want to default it to 0. That required adding one more line after mdb's solution:
ALTER TABLE [Table] ADD CONSTRAINT [Constraint] DEFAULT 0 FOR [Column];
You will have to do it in two steps:
Update the table so that there are no nulls in the column.
UPDATE MyTable SET MyNullableColumn = 0
WHERE MyNullableColumn IS NULL
Alter the table to change the property of the column
ALTER TABLE MyTable
ALTER COLUMN MyNullableColumn MyNullableColumnDatatype NOT NULL
For Oracle 11g, I was able to change the column attribute as follows:
ALTER TABLE tablename MODIFY columnname datatype NOT NULL;
Otherwise abatichev's answer seemed good. You can't repeat the alter - it complains (at least in SQL Developer) that the column is already not null.
this worked for me:
ALTER TABLE [Table]
Alter COLUMN [Column] VARCHAR(50) not null;
As long as the column is not a unique identifier
UPDATE table set columnName = 0 where columnName is null
Then
Alter the table and set the field to non null and specify a default value of 0
In case of FOREIGN KEY CONSTRAINT... there will be a problem if '0' is not present in the column of Primary key table. The solution for that is...
STEP1:
Disable all the constraints using this code :
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
STEP2:
RUN UPDATE COMMAND (as mentioned in above comments)
RUN ALTER COMMAND (as mentioned in above comments)
STEP3:
Enable all the constraints using this code :
exec sp_msforeachtable #command1="print '?'", #command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
this seems simpler, but only works on Oracle:
ALTER TABLE [Table]
ALTER [Column] NUMBER DEFAULT 0 NOT NULL;
in addition, with this, you can also add columns, not just alter it.
It updates to the default value (0) in this example, if the value was null.
In my case I had difficulties with the posted answers. I ended up using the following:
ALTER TABLE table_name CHANGE COLUMN column_name column_name VARCHAR(200) NOT NULL DEFAULT '';
Change VARCHAR(200) to your datatype, and optionally change the default value.
If you don't have a default value you're going to have a problem making this change, as default would be null creating a conflict.
Making column not null and adding default can also be done in the SSMS GUI.
As others have already stated, you can't set "not null" until all
the existing data is "not null" like so:
UPDATE myTable SET myColumn = 0
Once that's done, with the table in design view (right click on
table and click "design view"), you can just uncheck the Allow
Nulls columns like so:
Still in design view with the column selected, you can see the
Column Properties in the window below and set the default to 0 in there as well like so:
Let's take an example:
TABLE NAME=EMPLOYEE
And I want to change the column EMPLOYEE_NAME to NOT NULL. This query can be used for the task:
ALTER TABLE EMPLOYEE MODIFY EMPLOYEE.EMPLOYEE_NAME datatype NOT NULL;
For the inbuilt javaDB included in the JDK (Oracle's supported distribution of the Apache Derby) the below worked for me
alter table [table name] alter column [column name] not null;
You can change the definition of existing DB column using following sql.
ALTER TABLE mytable modify mycolumn datatype NOT NULL;
First make sure the column that your changing to not does not have null values
select count(*) from table where column's_name is null
Impute the missing values. you can replace the nulls with empty string or 0 or an average or median value or an interpolated value. It depends on your back fill strategy or forward fill strategy.
Decide if the column values need to be unique or non-unique. if they need to be unique than add an unique constraint. Otherwise, see if performance is adequate or if you need to add an index.

Resources