SQL Server - How to change table column only having some specific values? - sql-server

TABLE Family(
BrothersName varchar(30)
);
I have added some names (values) into BrothersName, but now I want it to only have 2 specific names 'Alex' and 'Tom'. However later it should also accept other names. What is the best way to handle this problem?

Add a check constraint stating BrothersName should accept only 'Alex' and 'Tom'. In future when you don't need it you can drop the constraint
ALTER TABLE Family
ADD CONSTRAINT chk_BrothersName CHECK (BrothersName in ('Alex','Tom'))
To Drop the Check Constraint
ALTER TABLE Family
DROP CONSTRAINT chk_BrothersName

Related

trying to create a column but it also creates a default constraint with useless name

I am using the alter command to add a column with not null and default value to the sql,
but when i run it it also creates the constraint with some silly words, how can i use the alter command to add the column also and also add the constraint with it with the name i specify
my alter is like this
ALTER TABLE users
ADD role bit not null default 0;
Should the combined constraint with same alter command will work, not sure how to add it
You can include the constraint name within the statement as follows:
ALTER TABLE users ADD role bit NOT NULL CONSTRAINT DF_Users__Role DEFAULT (0);
There are examples in the docs online, albeit a long way down the page: B. Adding a column with a constraint

Add Check constraints in SQL Server: ADD CONSTRAINT vs ADD CHECK

I want use a check constraint for a column in any table in SQL Server 2008.
I would like give a qualified name to the check constraint.
I have seen several syntax version on how to create it:
ALTER TABLE [dbo].[Roles2016.UsersCRM] WITH CHECK
ADD CHECK (([Estado]=(4) OR [Estado]=(3) OR [Estado]=(2) OR [Estado]=(1)))
ALTER TABLE [dbo].[Roles2016.UsersCRM] WITH CHECK
ADD CONSTRAINT [CK_UsuariosCRM_Estado]
CHECK (([Estado]=(4) OR [Estado]=(3) OR [Estado]=(2) OR [Estado]=(1)))
What's difference ADD CHECK and ADD CONSTRAINT for a check constraint?
It is possible, but a very bad habit to add constraints without a name:
CREATE TABLE tbl(SomeColumn VARCHAR(10) DEFAULT('test'))
will create a CONSTRAINT with a random name. Better use this
CREATE TABLE tbl(SomeColumn VARCHAR(10) CONSTRAINT DF_YourTable_SomeColumm DEFAULT('test'))
This will do the same, but will name the constraint like you want it.
This is extremely important if you run upgrade scripts in deployed environments. Just imagine, you want to change a constraint later and the name of this constraint is all different on your customers machines... That's a real pain!
So: Always name your constraints!

Drop a Unique Constraint

I need to drop a Unique constraint on a previously existing table and create a new unique constraint that will include extra column. Can I use the name of the constraint to drop it? or will the name of the unique constraint change based on Datasource. I need to execute the script on multiple instances of same DB (eg. dev,test,prod)
Yes, you can use the name of the constraint, provided that you previously deleted and it was a named contraint.

default keyword with unique default value in mssql

I want to add a Column to a table in my Database. As several data already exist in that particular table , MSSQL does not allow me to add a not Null field. So I can use a default keyword to solve this problem and run the below mentioned Query -
Alter table ServiceDetails
Add OCF_Internal_ID varchar(50) not null default 'OCF';
But now I want to make the "OCF_Internal_ID" field Primary key and I need to insert unique values to the every record of that particular filed. Please give me any suggestion.
How can I add Unique values to all the existing records ?
You can use one of these options
Alter table ServiceDetails
Add OCF_Internal_ID integer NOT NULL IDENTITY (1,1);
OR
Alter table ServiceDetails
Add OCF_Internal_ID uniqueidentifier NOT NULL default newid();
Then run this do define a primary key constraint
ALTER TABLE ServiceDetails
ADD CONSTRAINT PK_ServiceDetails_OCF_Internal_ID PRIMARY KEY CLUSTERED (OCF_Internal_ID);
GO

How do I set default value for a foreign key column in sql server?

I am adding a new column in an existing table with preloaded data. This column uses a primary key from another table and I want to default this to 5. I have tried the following code:
ALTER TABLE group
ADD group_type INT
GO
ALTER TABLE group
ADD CONSTRAINT FK_group_type DEFAULT 5 FOR group_type
GO
I was expecting on alter of the group table then all the values will be filled with 5 but instead its NULL. What am I doing wrong?
First of all, adding a DEFAULT constraint (in it's own SQL statement) to a column does not effect existing data in that column. It only effects new INSERTS to that table which do not provide a value for that column.
Second, you haven't created a FOREIGN KEY constraint here.
EDIT:
Here would be one way to create the FK correctly
ALTER TABLE group
ADD group_type_id INT
GO
ALTER TABLE group
ADD CONSTRAINT fk_groupType FOREIGN KEY (group_type_id)
REFERENCES group_type (group_type_id)
This worked for me, it set the foreign key constraint, default value, and updated existing table records all in a single ALTER TABLE statement. I'm using a SQL Azure database (via SQL Management Studio), so, YMMV.
ALTER TABLE Group
ADD GroupTypeId int NOT NULL
CONSTRAINT DK_GroupTypeId DEFAULT (5) WITH VALUES
CONSTRAINT FK_GroupTypeId FOREIGN KEY
REFERENCES [dbo].[GroupType] (GroupTypeId)
GO
It took a while to run, but a subsequent select, showed the rows had the correct default value for those columns.
Disclaimer: I edited the above query from my table / key names to yours without re-testing it, so you may want to double check it for any typos or other mismatches; The syntax should be the same though.
You can use:
alter table [group]
add group_type int constraint df_group_type default (5) with values
However, it doesn't seem a good idea to use constant as a default value for a column, which is supposed to be FK column.
It seems, that may be what actually you are trying to do is following:
alter table [group] add column group_type int
GO
update [group] set group_type = (select id from group_type where desc ='typeA')
GO
alter table [group] add constraint FK_group_grouptype foreign key (group_type) references group_type (id)
GO
Adding default constraint will affect existing rows if you add new not nullable column to table.
ALTER TABLE group
ADD group_type INT NOT NULL
CONSTRAINT DK_group_type DEFAULT 5
GO

Resources