Category Archives: SQL Server

Script to Create Foreign Key on the Compound Primary Key

Compound Primary key is a primary key which is created on more than one column. Now the questions is how to create the foreign key for the compound primary key where it references more than one column.

Check the below example.

create table employee
(
	empID int not null,
	SSN int not null,
	name varchar(20)
)


ALTER TABLE [employee]
ADD CONSTRAINT pk_employee PRIMARY KEY (empID, SSN)


create table EmpDetail
(
		empID int,
		SSN int,
		address varchar(20),
		city varchar(20),
		pin varchar(20)
)

ALTER TABLE dbo.empDetail
   ADD CONSTRAINT FK_Employee
   FOREIGN KEY(empID, SSN)
   REFERENCES dbo.employee(empID, SSN)


SELECT
    tc.TABLE_NAME,
    tc.CONSTRAINT_NAME, 
    ccu.COLUMN_NAME
FROM 
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN 
    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu 
      ON ccu.TABLE_NAME = tc.TABLE_NAME AND ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
WHERE
    tc.TABLE_NAME IN ('employee','employeeDetail')

Primary Key, Unique Key Constraints – Clustered Index and Non Clustered Index

You can use the below script to create the Primary Key on the already existing tables. Primary key enforces a uniqueness in the column and created the clustered index as default.

Primary key will not allow NULL values.

-- Adding the NON NULL constraint
ALTER TABLE [TableName]	 
ALTER COLUMN PK_ColumnName int NOT NULL

--Script to add the primary key on the existing table
ALTER TABLE [TableName]
ADD CONSTRAINT pk_ConstraintName PRIMARY KEY (PK_ColumnName)

If you want to define or create the non-clustered index on the existing table, you can use the below script. If the data in the column is unique, you can create the Unique Constraint as well.

Unique Key enforces uniqueness of the column on which they are defined. Unique Key creates a non-clustered index on the column. Unique Key allows only one NULL Value.

--script to create non-clustered Index
create index IX_ColumName on TableName(ColumnName)
--script to create Unique constraint on the existing table
ALTER TABLE TableName ADD CONSTRAINT ConstraintName UNIQUE(ColumnName)

Again Awarded as MVP for 2012-2013

Dear Friends,

I want to share a good news with you all, I am again 3rd time awarded as MVP in SQL Server. Thanks for reading my site, I will keep posting the best article and always there to solve your queries.

Thanks,
Jugal Shah

You will see the 2012 in below award shortly -:)

Bug Fix: Property IsLocked is not available for Login ‘[sa]’. This property may not exist for this object, or may not be retrievable due to insufficient access rights

Problem: Property IsLocked is not available for Login ‘[sa]’. This property may not exist for this object, or may not be retrievable due to insufficient access rights. (Microsoft.SqlServer.Smo)

 

Solution: You will get the above error, in case of one of the below issue.

  1. SQL Server authentication mode is Windows Only
  2. SA Account is disable
  3. SA account is locked

SQL Server authentication mode is Windows Only

You can check the SQL Server authentication mode using below query.

SELECT
CASE
SERVERPROPERTY(‘IsIntegratedSecurityOnly’)

WHEN 1 THEN
‘Windows Authentication’

WHEN 0 THEN
‘Windows and SQL Server Authentication’

END
as [Authentication Mode]

If the authentication mode Windows, you have to change the authentication mode is Mix Mode. You can change the authentication mode by right click on Server Properties -> Security tab.
This requires SQL Server Service restart.

SA Account is locked/Disable

You can execute below query to check whether SA account is locked or not. If the account is locked it will return 1 and 0 for un-locked.

SELECT
LOGINPROPERTY(‘sa’,
‘IsLocked’)

You can investigate whether SA account is locked by bad Password using below query. It will return the count of consecutive failed login attempts

SELECT
LOGINPROPERTY(‘sa’,
‘BadPasswordCount’);

GO

Right click on SA account and check the SA account properties, check the below properties of the SA login whether it is disabled or locked.

Execute the below script to unlock and enable the SA account

ALTER
LOGIN [sa] WITH
PASSWORD=N’Password’,

DEFAULT_DATABASE=[master],

DEFAULT_LANGUAGE=[us_english],

CHECK_EXPIRATION=OFF,

CHECK_POLICY=OFF

GO

ALTER
LOGIN [sa] ENABLE

GO