How to insert columns in between in table in sql server 2008 - vb.net

I want to add or update columns using alter table if i am adding a new column i want show error. I am using the code below
alter table Personal_Details alter columns DOB datetime
if i uncheck the NULL to not NULL then it will shows column does not allow nulls; update fails;
i want to insert the fields in between columns not at end.
Plese fix my bug,
Thanks in advance.

The position of the column in the table declaration has nothing to do with its being NULL or NOT NULL.
If you are adding a column (of any type) which you want to be NOT NULL, i.e. you want to prohibit NULL values in that column, and the table already contains some rows, you must also provide some default value. For example:
ALTER TABLE Personal_Details
ADD COLUMN DOB datetime NOT NULL DEFAULT (GETDATE())
Otherwise the engine will attempt to add that column with NULLs as its values, which will violate the NOT NULL property, and the change, therefore, will be reverted.
Basically, the same applies when you want to set an existing column's NOT NULL property on while the column already contains NULLs. But in this case you must explicitly eliminate the NULLs before the change by either replacing them with values or removing the respective rows.
Source:
ALTER TABLE (Transact-SQL). (The particular section related to your problem is just above this code snippet.)

1)For ur adding column with not null problem
Use
ALTER TABLE Personal_Details ADD COLUMN DOB datetime NULL
Update the DOB column with the required dates and make sure there is no null in the column
then alter the column using
ALTER TABLE Personal_Details ALTER COLUMN DOB datetime not NULL
2)For your column going to the end problem...
you should not be worried...the order in which the columns are arranged doesnt matter...unless u are using a pathetic way of accessing data by column order..in which case again..u should stop accessing it by column order...
If the column order really matters you can change it using design option in the sql management table(rightclick on table >design and drag the column to its required place.)

Related

Add a column with a value computed from other columns in SQL without using the default value

I can add a column this way:
ALTER TABLE MyTable
ADD MyColumn BIT NOT NULL
CONSTRAINT MyConstraint
DEFAULT 0
Now, instead of the constant 0 I want to put a different value depending on my other columns in the MyTable. I.e. it is a one time computation during the column addition. How could I do that?
Also, I do not want my column to have a default value.
E.g. I have another BIT column called MyOldColumn and I want to just copy its value into the MyColumn.
Looks like you just want to add the column, then run an update using some calculation.
ALTER TABLE MyTable ADD MyColumn BIT;
UPDATE MyTable
SET MyColumn = SomeCalculation;
ALTER TABLE MyTable ALTER COLUMN MyColumn BIT NOT NULL;
Why set the column to null first then not null? Because it will be more performant. Setting it to null first means it's a meta-data only operation, and will not change the data pages. The final ALTER will also not change any pages (although it will read them all), because none of them are null.
Whereas if you set it to not null first then all pages get changed, and then changed again.

Add column to table with existing data in SQL Server

I have a table Rates with data in it and I need to add new column to the table however I get the error:
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'CreatedOn' cannot be added to non-empty table 'RateIncreases' because it does not satisfy these conditions
How can I do this, I have disabled prevent saving changes that required table re-creation
What part of the error do you not understand? When a column is added to an existing table, there are rows. The column is assigned to each of those rows. SQL Server has to give those rows a value. How does it do this?
It can assign the default value for the column.
It can assign NULL.
In your case, you have defined the column as NOT NULL but not provided a default value. Hence, the database does not know what to do, so it returns an error.
The simplest solution is to remove the NOT NULL constraint in the definition. Very close behind is assigning a default value.

The EntityEntries property will return null because a single entity cannot be identified as the source of the exception

I am using Code first approach. how can i fix this problem?
I am assuming "Modified" column is a date time field in the table which is mandatory column.
You can resolve this issue in two ways
You can pass the "Modified" column field each time you do a insert.
Alter the column to accept null values if that field is not mandatory. Following is
sql query you can run to change the column to accept null values.
ALTER TABLE myTable ALTER COLUMN myColumn {DataType} NULL

Filling in a datetime field as empty

I need to add a new record to a database and one of the columns in my table is 'paymentDate', which is a "DateTime" field. Now the record I need to add is one in which the paymentDate is not known. I can't put in 'NULL' as SQL says 'Column 'paymentDate' cannot be null'. Also the '""' doesn't work in a datetime field.
If you have a paymentDate column and you don't know the date, then the column should allow NULL values. So, you should fix the data model:
alter table t alter column paymentDate datetime;
This will remove the not-NULL constraint, so you can add the data that you have.
I would be a little cautious, though. Why are you trying to add a row with an unknown payment date, if the designer of the table thought the value should never be NULL?

Insertion SQL and NOT NULL values

I've created a table schema and specified that for some attributes, values cannot be null. For one column of this table, values are to be imported from a column of some another table but the problem i am facing is that when i use insert statement to copy values from that column of another table to the column of this newly created table, the attributes of this new column start screaming because they kind of have a constraint on them that while insertion their values cannot be NULL!
How do i cope with this?
One solution is that for other attributes, just for time being, i can state that null values can be accommodated so that i can successfully import values from column of other table and then later on put condition on the remaining attributes that values are not be NULL. But how do i do do this?
You need to convert NULL to some DEFAULT values while importing.
I am not sure which DB engine you are using, in mysql:
Use something like IFNULL(column_name, "").
Reference
You may simply be looking for the default clause. When you define a column, you can specify;
intcol int not null default 0
If the column is not specified for an insert, then it will default to 0. In some databases, if a NULL value is supplied, it will also get the default value.