I do have a problem connecting two tables on MSSQL Management studio.
My goal is connect tables by foreign key and if I delete user I want 2nd table entry will be deleted automatically. I plan to use DELETE Cascade method for that.
User:
CREATE TABLE [dbo].[Users](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Email] [nvarchar](89) NOT NULL,
[Name] [nvarchar](25) NOT NULL,
[Midname] [nvarchar](25) NOT NULL,
[Surname] [nvarchar](25) NOT NULL,
[Phone] [varchar](15) NOT NULL,
[Country] [smallint] NOT NULL,
[Manager] [nvarchar](89) NOT NULL,
[Referrer] [nvarchar](89) NOT NULL,
[Rank] [tinyint] NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[Email] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
Email is Primary key
Payments:
CREATE TABLE [dbo].[Investments](
[ID] [bigint] NOT NULL,
[Investor] [nvarchar](89) NOT NULL,
[Sum] [decimal](19, 4) NOT NULL,
[Currency] [smallint] NOT NULL,
[Credit] [decimal](19, 4) NOT NULL,
[CreditRate] [decimal](19, 4) NOT NULL,
[Rate] [tinyint] IDENTITY(1,1) NOT NULL,
[Date] [smalldatetime] NOT NULL,
[Comment] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_Investments] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ID is Primary key
My FK should be like USER->PAYMENTS or PAYMENTS->USER?
When I am trying to connect User -> Payments using foregn key by Email -> Investor, it tell me such error:
The columns in table 'Payments' do not match an existing primary key or UNIQUE constraint.
Could you please explain me where problem is? And what I am doing wrong?
Change your structure to:
CREATE TABLE [dbo].[Users](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Email] [nvarchar](89) NOT NULL,
[Name] [nvarchar](25) NOT NULL,
[Midname] [nvarchar](25) NOT NULL,
[Surname] [nvarchar](25) NOT NULL,
[Phone] [varchar](15) NOT NULL,
[Country] [smallint] NOT NULL,
[Manager] [nvarchar](89) NOT NULL,
[Referrer] [nvarchar](89) NOT NULL,
[Rank] [tinyint] NOT NULL);
ALTER TABLE [Users]
ADD CONSTRAINT PK_UsersID PRIMARY KEY (ID);
and then
CREATE TABLE [dbo].[Investments](
[ID] [bigint] NOT NULL,
[UserID] [bigint] NOT NULL,
[Sum] [decimal](19, 4) NOT NULL,
[Currency] [smallint] NOT NULL,
[Credit] [decimal](19, 4) NOT NULL,
[CreditRate] [decimal](19, 4) NOT NULL,
[Rate] [tinyint] IDENTITY(1,1) NOT NULL,
[Date] [smalldatetime] NOT NULL,
[Comment] [nvarchar](max) NOT NULL);
ALTER TABLE Investments
ADD CONSTRAINT PK_InstestmentsID PRIMARY KEY (ID);
ALTER TABLE Investments
ADD CONSTRAINT FK_UsersInvestments
FOREIGN KEY (UserID)
REFERENCES Users(ID);
Then join Users.ID on Investments.UserID
I searched for the error message you are seeing (without the table name) and the general consensus
seems to be that a PRIMARY KEY or UNIQUE contraint has not been correctly set in your tables. The error also tells me you are (probably) using SQL Server.
From technet.microsoft.com:
The columns on the primary key side of a foreign key relationship must
participate in either a Primary Key or a Unique Constraint. After
setting up a Primary Key or a Unique constraint for one of the tables
you've selected, you can then define other relationships for that
table.
Check your PRIMARY KEYS in both tables. Without the actual DDL of your tables it's difficult to be of more help.
EDIT: You have Email as the PRIMARY KEY in your Users table but I do not see an Email field in the Investments table. Which fields are you joining on in your contraint?
Related
Error
There are no primary or candidate keys in the referenced table
'dbo.CMS_User' that match the referencing column list in the foreign
key 'FK_CMS_UserSettings_User
Query:
ALTER TABLE [dbo].[CMS_UserSettings] WITH CHECK ADD CONSTRAINT [FK_CMS_UserSettings_UserSettingsUserGUID_CMS_User] FOREIGN KEY([UserSettingsUserGUID])
REFERENCES [dbo].[CMS_User] ([UserGUID])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[CMS_UserSettings] CHECK CONSTRAINT [FK_CMS_UserSettings_UserSettingsUserGUID_CMS_User]
GO
I am getting the above error while i execute statements
Could you please help me how to create a contraint here, what going wrong while i create it?
CREATE TABLE [dbo].[CMS_UserSettings](
[UserSettingsID] [int] IDENTITY(1,1) NOT NULL,
[UserNickName] [nvarchar](200) NULL,
[UserPicture] [nvarchar](200) NULL,
[UserSignature] [nvarchar](max) NULL,
[UserURLReferrer] [nvarchar](450) NULL,
[UserCampaign] [nvarchar](200) NULL,
[UserMessagingNotificationEmail] [nvarchar](200) NULL,
[UserCustomData] [nvarchar](max) NULL,
[UserRegistrationInfo] [nvarchar](max) NULL,
[UserPreferences] [nvarchar](max) NULL,
[UserActivationDate] [datetime2](7) NULL,
[UserActivatedByUserID] [int] NULL,
[UserTimeZoneID] [int] NULL,
[UserAvatarID] [int] NULL,
[UserBadgeID] [int] NULL,
[UserActivityPoints] [int] NULL,
[UserForumPosts] [int] NULL,
[UserBlogComments] [int] NULL,
[UserGender] [int] NULL,
[UserDateOfBirth] [datetime2](7) NULL,
[UserMessageBoardPosts] [int] NULL,
[UserSettingsUserGUID] [uniqueidentifier] NOT NULL CONSTRAINT [DEFAULT_CMS_UserSettings_UserSettingsUserGUID] DEFAULT ('00000000-0000-0000-0000-000000000000'),
[UserSettingsUserID] [int] NOT NULL CONSTRAINT [DEFAULT_CMS_UserSettings_UserSettingsUserID] DEFAULT ((0)),
[WindowsLiveID] [nvarchar](50) NULL,
[UserBlogPosts] [int] NULL,
[UserWaitingForApproval] [bit] NULL CONSTRAINT [DEFAULT_CMS_UserSettings_UserWaitingForApproval] DEFAULT ((0)),
[UserDialogsConfiguration] [nvarchar](max) NULL,
[UserDescription] [nvarchar](max) NULL,
[UserUsedWebParts] [nvarchar](1000) NULL,
[UserUsedWidgets] [nvarchar](1000) NULL,
[UserFacebookID] [nvarchar](100) NULL,
[UserAuthenticationGUID] [uniqueidentifier] NULL,
[UserSkype] [nvarchar](100) NULL,
[UserIM] [nvarchar](100) NULL,
[UserPhone] [nvarchar](26) NULL,
[UserPosition] [nvarchar](200) NULL,
[UserLinkedInID] [nvarchar](100) NULL,
[UserLogActivities] [bit] NULL,
[UserPasswordRequestHash] [nvarchar](100) NULL,
[UserInvalidLogOnAttempts] [int] NULL CONSTRAINT [DEFAULT_CMS_UserSettings_UserInvalidLogOnAttempts] DEFAULT ((0)),
[UserInvalidLogOnAttemptsHash] [nvarchar](100) NULL,
[UserAvatarType] [nvarchar](200) NULL,
[UserAccountLockReason] [int] NULL CONSTRAINT [DEFAULT_CMS_UserSettings_UserAccountLockReason] DEFAULT ((0)),
[UserPasswordLastChanged] [datetime2](7) NULL,
[UserDataComUser] [nvarchar](200) NULL,
[UserDataComPassword] [nvarchar](200) NULL,
[UserShowIntroductionTile] [bit] NULL,
[UserDashboardApplications] [nvarchar](max) NULL,
[UserDismissedSmartTips] [nvarchar](max) NULL,
CONSTRAINT [PK_CMS_UserSettings] PRIMARY KEY CLUSTERED
(
[UserSettingsID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[CMS_UserSettings] WITH CHECK ADD CONSTRAINT [FK_CMS_UserSettings_UserActivatedByUserID_CMS_User] FOREIGN KEY([UserActivatedByUserID])
REFERENCES [dbo].[CMS_User] ([UserID])
GO
ALTER TABLE [dbo].[CMS_UserSettings] CHECK CONSTRAINT [FK_CMS_UserSettings_UserActivatedByUserID_CMS_User]
GO
ALTER TABLE [dbo].[CMS_UserSettings] WITH CHECK ADD CONSTRAINT [FK_CMS_UserSettings_UserAvatarID_CMS_Avatar] FOREIGN KEY([UserAvatarID])
REFERENCES [dbo].[CMS_Avatar] ([AvatarID])
GO
ALTER TABLE [dbo].[CMS_UserSettings] CHECK CONSTRAINT [FK_CMS_UserSettings_UserAvatarID_CMS_Avatar]
GO
ALTER TABLE [dbo].[CMS_UserSettings] WITH CHECK ADD CONSTRAINT [FK_CMS_UserSettings_UserBadgeID_CMS_Badge] FOREIGN KEY([UserBadgeID])
REFERENCES [dbo].[CMS_Badge] ([BadgeID])
GO
ALTER TABLE [dbo].[CMS_UserSettings] CHECK CONSTRAINT [FK_CMS_UserSettings_UserBadgeID_CMS_Badge]
GO
ALTER TABLE [dbo].[CMS_UserSettings] WITH CHECK ADD CONSTRAINT [FK_CMS_UserSettings_UserSettingsUserGUID_CMS_User] FOREIGN KEY([UserSettingsUserGUID])
REFERENCES [dbo].[CMS_User] ([UserGUID])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[CMS_UserSettings] CHECK CONSTRAINT [FK_CMS_UserSettings_UserSettingsUserGUID_CMS_User]
GO
ALTER TABLE [dbo].[CMS_UserSettings] WITH CHECK ADD CONSTRAINT [FK_CMS_UserSettings_UserSettingsUserID_CMS_User] FOREIGN KEY([UserSettingsUserID])
REFERENCES [dbo].[CMS_User] ([UserID])
GO
ALTER TABLE [dbo].[CMS_UserSettings] CHECK CONSTRAINT [FK_CMS_UserSettings_UserSettingsUserID_CMS_User]
GO
ALTER TABLE [dbo].[CMS_UserSettings] WITH CHECK ADD CONSTRAINT [FK_CMS_UserSettings_UserTimeZoneID_CMS_TimeZone] FOREIGN KEY([UserTimeZoneID])
REFERENCES [dbo].[CMS_TimeZone] ([TimeZoneID])
GO
ALTER TABLE [dbo].[CMS_UserSettings] CHECK CONSTRAINT [FK_CMS_UserSettings_UserTimeZoneID_CMS_TimeZone]
GO
CREATE TABLE [dbo].[CMS_User](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](100) NOT NULL CONSTRAINT [DEFAULT_CMS_User_UserName] DEFAULT (''),
[FirstName] [nvarchar](100) NULL,
[MiddleName] [nvarchar](100) NULL,
[LastName] [nvarchar](100) NULL,
[FullName] [nvarchar](450) NULL,
[Email] [nvarchar](254) NULL,
[UserPassword] [nvarchar](100) NOT NULL CONSTRAINT [DEFAULT_CMS_User_UserPassword] DEFAULT (N''),
[PreferredCultureCode] [nvarchar](10) NULL,
[PreferredUICultureCode] [nvarchar](10) NULL,
[UserEnabled] [bit] NOT NULL CONSTRAINT [DEFAULT_CMS_User_UserEnabled] DEFAULT ((0)),
[UserIsExternal] [bit] NULL CONSTRAINT [DEFAULT_CMS_User_UserIsExternal] DEFAULT ((0)),
[UserPasswordFormat] [nvarchar](10) NULL,
[UserCreated] [datetime2](7) NULL,
[LastLogon] [datetime2](7) NULL,
[UserStartingAliasPath] [nvarchar](200) NULL,
[UserGUID] [uniqueidentifier] NOT NULL,
[UserLastModified] [datetime2](7) NOT NULL,
[UserLastLogonInfo] [nvarchar](max) NULL,
[UserIsHidden] [bit] NULL CONSTRAINT [DEFAULT_CMS_User_UserIsHidden] DEFAULT ((0)),
[UserVisibility] [nvarchar](max) NULL,
[UserIsDomain] [bit] NULL CONSTRAINT [DEFAULT_CMS_User_UserIsDomain] DEFAULT ((0)),
[UserHasAllowedCultures] [bit] NULL,
[UserTokenID] [uniqueidentifier] NULL,
[UserMFRequired] [bit] NULL,
[UserTokenIteration] [int] NULL,
[CountryCode] [nvarchar](2) NULL CONSTRAINT [DEFAULT_CMS_User_CountryCode] DEFAULT (N'US'),
[UserPrivilegeLevel] [int] NULL,
[UserSecurityStamp] [nvarchar](72) NULL,
CONSTRAINT [PK_CMS_User] PRIMARY KEY CLUSTERED
(
[UserID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
I am using the code below:
Insert into JV(PurchaseID)
select PurchaseID from PurchaseReturnDetail
But when I execute the query it gives an error:
Cannot insert the value NULL into column 'column1', table 'database.table1'; column does not allow nulls.
Then my INSERT fails. What could be the issue?
The error says the table has a non-null constraint for the column. To change the column to allow nulls, run this query (for Oracle DB):
alter table TABLE_NAME modify (COLUMN_NAME null);
If first columns is identity column, make sure that auto increment is enabled for that column .
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[JV](
[JVID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[JVInvoiceNo] [nvarchar](50) NULL,
[JVDate] [datetime] NULL,
[StoreID] [numeric](18, 0) NULL,
[BusinessID] [numeric](18, 0) NULL,
[UserID] [numeric](18, 0) NULL,
[Currency] [nvarchar](50) NULL,
[Rate] [decimal](18, 10) NULL,
[DueDate] [datetime] NULL,
[Reference] [nvarchar](50) NULL,
[RefID] [numeric](18, 0) NULL,
[Narration] [nvarchar](max) NULL,
[InvoiceRef] [nvarchar](50) NULL,
[Status] [nvarchar](50) NULL,
[Type] [nvarchar](50) NULL,
[PurchaseID] [numeric](18, 0) NULL,
CONSTRAINT [PK_JV] PRIMARY KEY CLUSTERED
(
[JVID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
I can't set the foreign key for my advertisement table or should I say how do I add datatype for my computed column in employer table ? error details below
CREATE Table Employer
(
No Int NOT NULL IDENTITY(1,1),
EmployerID AS 'EID'+ CAST(No as VARCHAR(50)) PERSISTED NOT NULL ,
CONSTRAINT PK_Employer PRIMARY KEY CLUSTERED(EmployerID),
EUsername CHAR(20) NOT NULL,
EPassword CHAR(20) NOT NULL,
Name VARCHAR(20) NOT NULL,
Contact_Number INT NOT NULL,
Email CHAR(20) NOT NULL,
Company_Name CHAR(30) NOT NULL,
Current_Position VARCHAR(30) NULL
);
CREATE Table Advertisement
(
No Int NOT NULL IDENTITY(1,1),
AdvertisementID AS 'AID'+ CAST(No as VARCHAR(10)) PERSISTED ,
CONSTRAINT PK_Advertisement PRIMARY KEY CLUSTERED(AdvertisementID) ,
Employer_ID VARCHAR(10) Foreign Key References Employer(EmployerID) NOT NULL,
Company_Name VARCHAR(30) NOT NULL,
Company_Location CHAR(30) NULL,
Job_Position VARCHAR(30)NOT NULL,
Job_Description VARCHAR(100) NULL,
Skills_Requirement VARCHAR(100) NULL,
Education_Requirement CHAR(50) NOT NULL,
Salary CHAR(20) NOT NULL
);
This is the error I keep getting :
Msg 1753, Level 16, State 0, Line 27
Column 'Employer.EmployerID' is not the same length or scale as referencing column 'Advertisement.EmployerID' in foreign key 'FK__Advertise__Emplo__07C12930'. Columns participating in a foreign key relationship must be defined with the same length and scale.
Msg 1750, Level 16, State 0, Line 27
Could not create constraint or index. See previous errors.
note that the foreign key relationship must be defined with the same length and scale for both foreign key and primary key
so you must set EmployerID field to varchar(50) in advertisement table
Its recommended to use BIGINT auto increment field type for EmployerID.
and use Employer table's EmployerID field as you foreign key.
CREATE TABLE [Employer](
[EmployerID] [bigint] IDENTITY(1,1) NOT NULL,
[EUsername] [char](20) NOT NULL,
[EPassword] [char](20) NOT NULL,
[Name] [varchar](20) NOT NULL,
[Contact_Number] [int] NOT NULL,
[Email] [char](20) NOT NULL,
[Company_Name] [char](30) NOT NULL,
[Current_Position] [varchar](30) NULL,
CONSTRAINT [PK_Employer] PRIMARY KEY CLUSTERED
(
[EmployerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [Advertisement](
[AdvertisementID] [bigint] IDENTITY(1,1) NOT NULL,
[fk_Employer_ID] [bigint] NOT NULL,
[Company_Name] [varchar](30) NOT NULL,
[Company_Location] [char](30) NULL,
[Job_Position] [varchar](30) NOT NULL,
[Job_Description] [varchar](100) NULL,
[Skills_Requirement] [varchar](100) NULL,
[Education_Requirement] [char](50) NOT NULL,
[Salary] [char](20) NOT NULL,
CONSTRAINT [PK_Advertisement] PRIMARY KEY CLUSTERED
(
[AdvertisementID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [Advertisement] WITH CHECK ADD CONSTRAINT [FK_Advertisement_Employer] FOREIGN KEY([fk_Employer_ID])
REFERENCES [Employer] ([EmployerID])
ALTER TABLE [Advertisement] CHECK CONSTRAINT [FK_Advertisement_Employer]
I know this is a common topic but i stil feel my scenario requires some custom advice. I was selecting from a table the other day and it took me an AGE to select on a datetime column that has not been indexed. I want to index this, only problem ebing is that the "production" 2005 box i am unfamiliar with and how it will handle the index creation. With that in mind i am wondering what the safest way for me to create an index on the table is. All the details i think people will need are below (i hope) :
Index to be created on field 5 (possibly field4 also)
Table 1 definition:
CREATE TABLE [dbo].[TABLE 1](
[ID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_BLA] DEFAULT (newid()),
[field1] [nvarchar](7) NULL,
[field2] [nvarchar](10) NULL,
[field3] [nvarchar](2) NULL,
[field4] [datetime] NULL,
[field5] [datetime] NULL,
[field6] [smallint] NULL,
[field7] [nvarchar](1) NULL,
[field8] [nvarchar](7) NULL,
[field9] [nvarchar](60) NULL,
[field10] [smallint] NULL,
[field11] [nvarchar](15) NULL,
[field12] [datetime] NULL,
CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Total count of rows in Table 1 : 2926836
I am executing the query on SQL server on hosting and it is taking 1 minute and 35 seconds. And the no, of rows of retrieval are 18000. Still it is taking too much time. Query is
select ID,
FirstName,
LastName,
Branch,
EnquiryID,
Course,
College,
Mobile,
ExamID,
EntranceID,
Entrance,
Venue,
RegNo,
VenueID,
Exam,
Gender,
row_number() over (partition by EnquiryID order by ID asc) as AttemptNO
from AGAM_View_AOPList
order by EnquiryID
TABLE SCHEMAS
CREATE TABLE [dbo].[AGAM_AceOFPace](
[ID] [int] IDENTITY(1,1) NOT NULL,
[EnquiryID] [int] NULL,
[FirstName] [nvarchar](100) NULL,
[MiddleName] [nvarchar](100) NULL,
[LastName] [nvarchar](100) NULL,
[BranchID] [int] NULL,
[Branch] [nvarchar](100) NULL,
[CourseID] [int] NULL,
[ExamID] [int] NULL,
[Exam] [nvarchar](200) NULL,
[EntranceID] [int] NULL,
[Entrance] [nvarchar](200) NULL,
[RegNo] [nvarchar](200) NULL,
[EntranceCode] [nvarchar](100) NULL,
[ExamDate] [nvarchar](50) NULL,
[UserID] [nvarchar](100) NULL,
[EntranceFees] [numeric](18, 2) NULL,
[VenueID] [int] NULL,
[Venue] [nvarchar](max) NULL,
[ChequeNumber] [nvarchar](50) NULL,
[Bank] [nvarchar](100) NULL,
[CreatedDate] [datetime] NULL,
CONSTRAINT [PK_AGAM_AceOFPace] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[AGAM_AceOFPace] WITH CHECK ADD CONSTRAINT [FK_AGAM_AceOFPace_AGAM_Inquiry] FOREIGN KEY([EnquiryID])
REFERENCES [dbo].[AGAM_Inquiry] ([ID])
GO
ALTER TABLE [dbo].[AGAM_AceOFPace] CHECK CONSTRAINT [FK_AGAM_AceOFPace_AGAM_Inquiry]
GO
SECOND TABLE
CREATE TABLE [dbo].[AGAM_Inquiry](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RegNo] [nvarchar](200) NULL,
[BranchID] [int] NULL,
[Category] [nvarchar](100) NULL,
[CourseID] [int] NULL,
[EntranceFees] [numeric](18, 2) NULL,
[EntranceID] [int] NULL,
[UserID] [nvarchar](50) NULL,
[Status] [nvarchar](50) NULL,
[ReminderDate] [datetime] NULL,
[Reminder] [nvarchar](150) NULL,
[Mobile] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
[FirstName] [nvarchar](50) NULL,
[MiddleName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Landline] [nvarchar](50) NULL,
[Address] [nvarchar](100) NULL,
[DOB] [datetime] NULL,
[Gender] [nvarchar](50) NULL,
[PfBatchTime] [nvarchar](50) NULL,
[SourceOfInquiry] [nvarchar](50) NULL,
[ExStudentID] [int] NULL,
[InquiryDate] [datetime] NULL,
[ReceiptNumber] [nvarchar](50) NULL,
[RawID] [int] NULL,
[Deleted] [int] NULL,
[CreatedBy] [nvarchar](50) NULL,
[CreatedDate] [datetime] NULL,
[LastModifiedBy] [nvarchar](50) NULL,
[LastModifiedDate] [datetime] NULL,
[College] [nvarchar](150) NULL,
[Qualification] [nvarchar](150) NULL,
[RptNo] [nvarchar](100) NULL,
CONSTRAINT [PK_AGAM_Inquiry] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[AGAM_Inquiry] WITH CHECK ADD CONSTRAINT [FK_AGAM_Inquiry_AGAM_Branch] FOREIGN KEY([BranchID])
REFERENCES [dbo].[AGAM_Branch] ([ID])
GO
ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Branch]
GO
ALTER TABLE [dbo].[AGAM_Inquiry] WITH CHECK ADD CONSTRAINT [FK_AGAM_Inquiry_AGAM_Course] FOREIGN KEY([CourseID])
REFERENCES [dbo].[AGAM_Course] ([ID])
GO
ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Course]
GO
ALTER TABLE [dbo].[AGAM_Inquiry] WITH CHECK ADD CONSTRAINT [FK_AGAM_Inquiry_AGAM_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[AGAM_Users] ([UserID])
GO
ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Users]
GO
Can you try with changing the view to this?
SELECT TOP (100) PERCENT
AP.ID,
AP.FirstName,
AP.LastName,
AP.Branch,
AP.EnquiryID,
AC.Name,
AI.College,
AI.Mobile,
AP.ExamID,
AP.EntranceID,
AP.RegNo,
AP.VenueID,
AP.Exam,
AI.Gender,
AP.BranchID,
AP.CourseID,
AP.CreatedDate,
AI.Status,
AP.Entrance,
AP.Venue
FROM dbo.AGAM_AceOFPace AS AP
INNER JOIN dbo.AGAM_Inquiry AS AI ON AI.ID = AP.EnquiryID
INNER JOIN dbo.AGAM_Course as AC on AC.ID = AP.CourseId
ORDER BY AP.EnquiryID
Do you have an index on EnquiryId and CourseID?
Seeing as you are joining, ordering and partitioning by it you really should.
CREATE INDEX IDX_AGAM_AceOFPace_EnquiryID
ON AGAM_AceOFPace (EnquiryID)
CREATE INDEX IDX_AGAM_AceOFPace_CourseID
ON AGAM_AceOFPace (CourseID)