Showing posts with label sql table. Show all posts
Showing posts with label sql table. Show all posts

Tuesday, August 20, 2013

[Resolved]Msg 8111 Cannot define PRIMARY KEY constraint on nullable column in table 'tablename' | SQL Server Add Primary key constraint to table

To Resolve such an error :

Msg 8111
Cannot define PRIMARY KEY constraint on nullable column in table 'tablename'.
Msg 1750
Could not create constraint. See previous errors.

Steps:

1. First Remove all the null values from the column on which primary key has to be created or update such fields with unique values.

2.Then Make that column as not Nullable using this query:

ALTER TABLE tablename ALTER COLUMN columnName NOT NULL

3. Now We can add the primary key constraint for the table.

ALTER TABLE tablename 
ADD CONSTRAINT PK primary key (columnName

Monday, August 5, 2013

changing column size in sql | Query to increase or decrease column length in sql server

Changing Column Size :

Suppose you want to change the size of a column having datatype as NVARCHAR of length 100.
We will increase it length to 200.

Query :

ALTER TABLE CUSTOMER_TABLE
ALTER COLUMN CUSTOMERNAME nvarchar(200)

Friday, March 29, 2013

Table variable and Temp table

Difference between table variable and temp table :

Table Variable Temp Table
1 It cannot be rollbacked.

It can be rollbacked.
2 It gets destroyed after the particular code gets executed(Like Stored procedures) It uses tempdb to store the temp tables. Thus it can be accessed till the sql connection exists

3 Less Recompilation

More Recompilation
4 It can only have cluster index on the table It can have both cluster and non cluster indexes
5 It can be used with Stored procedures, Functions, Triggers.

It can be used with Stored procedures, Triggers.
6 It Should be used when there is no transaction  to be used for table variables. It can be used within the transaction block.

Examples:

Table variable :


Temp Variable: