Update foreign key column from not null to null - sql

How I can update a foreign key column in a table from not null to null without having to recreate a table?
I tried to use this:
update tblProduct
set ConsumerID not null
Please advise, thank you

did you try something like this?
ALTER TABLE [Table] ALTER COLUMN [Column] INTEGER NULL

ALTER TABLE tblProduct MODIFY ConsumerID BIGINT NOT NULL;
For more details about altering a MySQL table please visit ALTER TABLE SYNTAX

This is altering data about the table and not data in the table, thus you need to alter the data schema.
So look at create and alter table statements
e.g. for Sybase (and MS Sql server)
alter table tblProduct modify ConsumerID integer NULL

Related

How do i set an attribute in a table to have the constraint not null

I have an attribute A (which is a foreign key that references attribute A in table C) with datatype int in my table B
I tried
ALTER TABLE B ALTER COLUMN A int not null;
but it doesn't work it gives me an error
I tried
ALTER TABLE B MODIFY (A INT NOT NULL);
well but it also gives an error as well.
How do i go about doing this?
First, either delete all rows that currently have NULL values in your column or update the value of that column to, for example, 0.
After that you can continue with altering your column to NOT NULL.
I don't know your DBMS.
For Oracle, it would be:
ALTER TABLE B MODIFY A NUMBER NOT NULL
For SQL Server:
ALTER TABLE B ALTER COLUMN A INTEGER NOT NULL
I hope I helped!

How to add Identity Column in SQL server?

Here is my table and Id is unique constraint...Now I want to add Identity Column in ID .Can anyone help me how to do it with TSQL.
This is existing table.
You can only have 1 Identity Column per Table. So if you don't have one already, Just alter the table and add the Column. Like this
ALTER TABLE YourTableName
ADD IdCol INT IDENTITY(1,1)
You can rename your previous ID column to keep those values if you need them in your application
EXEC sp_rename 'TableName.id', 'oldid', 'COLUMN';
Then add new ID column with Unique constraint as follows
Alter table TableName Add id int identity(1,1) unique not null

How i can modify the Parent table for my Sql server 2008 r2 FK

I have a FK inside my table but i want to modify the parent table for the FK . so is there an alter command that can achieve this ? or i need to remove the FK and create a new one ?
Thanks
Add this to your PK and it will automatically update all FKs for you:
ON UPDATE CASCADE
For full details, you can read this article.
EDIT Based on your comment, if you want to change the PK data type, it depends on the change:
If the old type can be implicitly casted to the new type without any loss:
Change the PK type first.
Change the FK type to the same.
If the old type cannot be implicitly casted to the new type without any loss:
Break the relationship first (i.e. remove the FK restriction/index).
Convert the PK. If the data needs to be modified, save both the old values and the new ones in a temporary table.
Convert the FK. If the PK data was changed in previous step, update the FK using the mapped values from the temporary table.
Create the relationship again (i.e. create the FK restriction/index).
To modify the data type, use the ALTER command, the syntax is:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
Examples:
ALTER TABLE table_name
ALTER COLUMN id NUMBER(10,2);
ALTER TABLE table_name
ALTER COLUMN id VARCHAR(20);
For full details, you can read this article.
Looks like you are looking for alter statement but since you didn't mention exactly what you are looking to modify; I assume that you want to change column data type size. You can do something like this (an example; say you want to change size from 10 to 15)
alter table sample3
alter column name varchar(15)
EDIT:
In that case this is what you should be doing. You need to drop the existing constraint and recreate the constraint to point to TableC
alter table TableA
drop constraint your_FK_constraint_name
alter table TableA
add constraint constraint_name
FOREIGN KEY (column_name) references TableC(some other column name)
An Example:
alter table sample2
drop constraint FK__sample2__realnam__09DE7BCC
alter table sample2
add constraint FK__sample2__realnam
FOREIGN KEY (realname) references sample1(name)
Based on this comment, "now my current FK inside TableA is referring to another table primary key TableB. but i need my modify my current FK to refer to tableC instead of tableB ... this what i need (to modify the parent table for my FK)– "
The parent table is TableB. No action is required on that table.
On TableA, you have to:
Drop the existing foreign key constraint.
Update as necessary so that all values in the applicable column have a matching value in TableC.
Add a new foreign key constraint.
in that order.
Edit Starts Here
Here is a link to the syntax,

How to add a PK to an existing table in DB2?

I am facing one problem. I have a table already create in DB2.
CREATE TABLE "DDL12"
(
"D4_1" decimal(10,0),
"D4_2" decimal(10,0),
);
I am trying to create a PK on this table as :-
ALTER TABLE "DDL12" ADD CONSTRAINT "Key4" PRIMARY KEY ("D4_1");
But while running the command, I am getting the error saying D4_1 is NULLABLE.
Now, how can I create a PK on this table?
Thanks
Yes, this is due the fact, that your database "could have" rows having NULL value in that non PK column right now.
So first set the column to NOT NULL (+ make sure having a unique value in all rows) and then set the primary key with the command above.
You can change a column to not NULL like this:
ALTER TABLE "DDL12"
MODIFY "D4_1" decimal(10,0) NOT NULL;

DELETE complete column from table

[Name] [major_version] [minor_version] [revision] [install_failures]
s 23 1 NULL 0
This is my table. How can I delete the revision column (not the value, the complete revision column) from table?
ALTER TABLE `table_name` DROP COLUMN `revision`;
You can do this with the following sql
ALTER TABLE table_name
DROP COLUMN revision
Here can you read simple documentation on that.
Use ALTER statement
ALTER TABLE table_name DROP COLUMN revision;
The ALTER TABLE statement is used to
add, delete, or modify columns in an
existing table.
Syntax :
ALTER TABLE table_name
DROP COLUMN column_name
You have to use the alter table statement to delete the revision column from your table.
The ALTER TABLE statement is used to
add, delete, or modify columns in an
existing table.
your delete column syntax will look like this
ALTER TABLE table_name
DROP COLUMN revision
This is very simple...use this
ALTER TABLE table_name DROP COLUMN column_name
Here are some links that will help you.
techonthenet, w3schools, php.about, sqlzoo.