Strange Issue with Primary Key Addition - sql-server

I am using SQL server 2008 r2.
I was creating a table implicitly i.e without writing queries and tried to add primary key to one of the column by right clicking and selecting primary key.
But i could not add the primary key as it was not reflected there, i refreshed the table , the database and also closed the connection and re-connected but primary key was still not reflected on that column name. I tried this thrice.
Then I added primary key using query (Table was empty)
alter table [Table Name]
Add Primary key ([column name])
and then it got reflected in the table.
I tried searching for reason but could not find any convincing reason.
If anybody knows reason for this please reply.

ALTER TABLE [TableName]
ADD CONSTRAINT [PrimaryKeyName] PRIMARY KEY CLUSTERED ([ExistingColumn])
Make sure the column is NOT NULL i.e doesnt allow NULL values to be inserted.

Please check that you have "UNCHECKED" checkbox which says Prevent saving changes that require the table re-creation. You can follow below instruction to uncheck it.
Open SQL Server Management Studio (SSMS).
On the Tools menu, click Options.
In the navigation pane of the Options window, click Designers.
Select or clear the Prevent saving changes that require the table
re-creation check box, and then click OK.

Related

Creating a foreign key relationship through the UI in VS2015 SQL Server Database Project

I added a new SQL Server database project to my solution in VS2015. I added all of my tables to this project.
I am now trying to figure out how to add foreign keys to the project through the UI preferably .
I am in the table designer and on the right side I click foreign keys and then add new. This adds a new foreign key, but when I go to the properties of it I am unable to set the columns and tables. Everything is grayed out.
The T-SQL looks like this:
CONSTRAINT [FK_Product_ToTable]
FOREIGN KEY ([Column]) REFERENCES [ToTable]([ToTableColumn])
I know I can just simply replace the params with the correct info in the T-SQL pane, but is there any way to completely do it through the UI and let the T-SQL update on its own?
double click your table in solution explorer to edit the columns
add FK column to the table, optionally check "Allow Nulls"
to the right of the table column list, right click "Foreign Keys"
a menu will appear with an item "Add New Foreign Key"
click that item, and you will see it add a new FK constraint under the "Foreign Keys" list. Rename the FK to your liking, then press ENTER.
This will add a FK SQL statement in the "T-SQL" window below the table column list.
edit the SQL script to define the "Column", "ToTable", and "ToTableColumn" for your foreign key.
enter image description here

Make a varchar(50) column unique

I have a column (which represents an e-mail) in a SQL Server database with varchar(50) as data type and I would like to make it unique (do not allow the same two e-mail addresses). I cannot find a way to make such column unique in SQL Server Management Studio.
How to do that?
In T-SQL it would be
ALTER TABLE MyTable WITH CHECK
ADD CONSTRAINT UQ_MyTable_Email UNIQUE (EmailAddress)
Or as an explicit index
CREATE UNIQUE INDEX IXU_Email ON MyTable (EmailAddress)
Edit: I can't see how to create a constraint in the SSMS GUI: other answers show how to manage indexes. I do only use SQL though, never the GUI for this kind of work
In the Object Explorer under the table right-click the Indexes folder and choose New Index....
In the window that appears enter Index name:, tick the Unique checkbox and add your email field from the Add... button then click OK.
Try this:
ALTER TABLE [dbo].[TableName] ADD CONSTRAINT UNQ__TableName__ColumnName UNIQUE ([ColumnName])
From this:
http://msdn.microsoft.com/en-us/library/ms191166.aspx

How to remove identity column from table

I have created a table with employee id as identity column. Now, I want to remove identity and replace datatype as bigint. I am using Sql Compact edition. How to achieve this?
I don't believe you can, using TSQL, remove the IDENTITY property from a column.
Instead:
Add a new BIGINT column to the table
Copy the data from the current IDENTITY field into the new field
Drop the existing column
Rename the new column to the correct name
You can do it in SSMS in the Design view for the table. I believe behind the scenes it does something like above.
Update:
To confirm, in SSMS 2K8 when you try to remove the IDENTITY property from a column and then save it, it will actually recreate the table (you can see what it does exactly by monitoring in SQL Profiler). In order to do it in SSMS, you need to ensure you have the "Prevent saving changes that require table re-creation" option turned OFF in Tools-> Options -> Designers -> Table and Database Designers. I think it defaults to ON, which would result in an error message when you try to do it otherwise.
In "real" SQL Server, you'd have to do these steps - not sure if SQL Server CE allows the same, but give it a try! I'm assuming you probably have your PRIMARY KEY constraint on that column, too - right? If not, you don't need to do the first and last step. And I'm assuming you want to have the IDENTITY on the column again, right?
-- DROP the primary key constraint (if you have that on your column)
ALTER TABLE dbo.Employees
DROP CONSTRAINT PK__Employees__3214EC274222D4EF
-- ALTER the datatype into BIGINT
ALTER TABLE dbo.Employees
ALTER COLUMN Employee_ID BIGINT
-- set PK constraint again
ALTER TABLE dbo.Employees
ADD CONSTRAINT PK_Employees PRIMARY KEY(Employee_ID)
This should help - http://www.sqlmag.com/Files/23/22081/Listing_03.txt

SQL Server Create multiple foreign keys

The table contains a PRIMARY KEY column and another column which is FOREIGN KEY. This works fine. When I attempt to add another column as a FOREIGN KEY I get the following message:
Link to pic
This is unrelated to the PKs or FKs.
There is a setting in SQL Server 2008 SSSM to prevent table rebuilds via the designer.
Tools..Options..Designers... clear the option “Prevent saving changes that require table re-creation”.

How does the constraints GUI work in SQL Server Management Studio?

In my TSQL table I added a constraint with the following sQL statement
alter table disabledqualities
add constraint uc_uIdQualCode
unique (userId, qualitycode)
I did it by hand because I just can't work out how the GUI is supposed to work.
You add a constraint and then you get a window where you should "define" your constraint. It's basically just a textbox. And no matter what I seem to enter, it never validates..
What am I supposed to enter in the box?
you would use new index not new constraint to add a unique constraint (read index)
new constraint is to add check constraints
in the new index window check unique
Example, column must be between 0 and 1,
((0)<=[TABLE].[COLUMN] AND [TABLE].[COLUMN]<=(1))
When adding unique constraints, it's actually an index, like primary key, so you click on indexes/keys.

Resources