MS SQL Server is giving the following error message on the WITH that preceeds the index options for the dbo.Calendar table:
"Incorrect syntax near the word 'WITH'".
When the FK declaration is disabled then the error goes away.
CREATE TABLE dbo.Scenario
(
ScenarioKey int NOT NULL IDENTITY(1,1),
ScenarioName varchar(60) NOT NULL
CONSTRAINT [PK-C_dbo.Scenario] PRIMARY KEY CLUSTERED (ScenarioKey)
WITH (
PAD_INDEX = OFF,
FILLFACTOR = 100,
IGNORE_DUP_KEY = OFF,
STATISTICS_NORECOMPUTE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
)
ON [PRIMARY]
);
GO
Works. But the following fails
CREATE TABLE dbo.Calendar
(
ScenarioKey int NOT NULL,
Bucket smalldatetime NOT NULL,
BucketEnd smalldatetime NOT NULL,
CONSTRAINT [PK-C_dbo.Calendar] PRIMARY KEY CLUSTERED (ScenarioKey, Bucket),
CONSTRAINT [FK_dbo.Calendar_dbo.Scenario] FOREIGN KEY (ScenarioKey)
REFERENCES dbo.Scenario (ScenarioKey)
ON DELETE CASCADE
ON UPDATE CASCADE
WITH (
PAD_INDEX = OFF,
FILLFACTOR = 100,
IGNORE_DUP_KEY = OFF,
STATISTICS_NORECOMPUTE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
)
ON [PRIMARY]
);
GO
What is wrong with the syntax?
The
WITH ( PAD_INDEX = OFF, /*... */ ALLOW_PAGE_LOCKS = ON )
defines options for the index associated with the PK constraint not the foreign key. So it needs to go as part of the PK constraint definition. You are trying to include it as part of the FK definition. It should be
CREATE TABLE dbo.Calendar
(
ScenarioKey INT NOT NULL,
Bucket SMALLDATETIME NOT NULL,
BucketEnd SMALLDATETIME NOT NULL,
CONSTRAINT [PK-C_dbo.Calendar]
PRIMARY KEY CLUSTERED (ScenarioKey, Bucket)
WITH ( PAD_INDEX = OFF,
FILLFACTOR = 100,
IGNORE_DUP_KEY = OFF,
STATISTICS_NORECOMPUTE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON ),
CONSTRAINT [FK_dbo.Calendar_dbo.Scenario]
FOREIGN KEY (ScenarioKey)
REFERENCES dbo.Scenario (ScenarioKey) ON DELETE CASCADE
ON UPDATE CASCADE
)
ON [PRIMARY]
Related
I need to delete all indexes (other than clustered index) of a table and rebuild them all.
For example, I have the following structure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SP6](
[SP6_REGCLI] [char](10) NOT NULL,
[SP6_REGEMP] [char](4) NOT NULL,
[SP6_FILIAL] [char](2) NOT NULL,
[SP6_CODDEP] [char](9) NOT NULL,
[SP6_DESCRI] [char](40) NULL,
[SP6_FOLGA] [char](1) NULL,
[SP6_HESOAU] [char](1) NULL,
[SR_RECNO] [numeric](15, 0) IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK__SP6] PRIMARY KEY NONCLUSTERED
(
[SR_RECNO] 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
CREATE CLUSTERED INDEX [SP6_SR] ON [dbo].[SP6]
(
[SR_RECNO] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
CREATE NONCLUSTERED INDEX [SP601_1_REG] ON [dbo].[SP6]
(
[SP6_REGCLI] ASC,
[SP6_REGEMP] ASC,
[SP6_FILIAL] ASC,
[SP6_CODDEP] ASC,
[SR_RECNO] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
CREATE NONCLUSTERED INDEX [SP602_2_REG] ON [dbo].[SP6]
(
[SP6_REGCLI] ASC,
[SP6_REGEMP] ASC,
[SP6_FILIAL] ASC,
[SP6_DESCRI] ASC,
[SR_RECNO] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
In this structure I have to delete all the NONCLUSTERED indexes (i.e. the index SP6_SR won't be deleted).
I need to do this because I have to change the size of some columns that is part of some indexes. For example, if I try to change the size of the colum SP6_DESCRI to Char(50) an error occurs because the column is part of index SP602_2_REG.
I think that if I delete all indexes of a table, change the size of columns desired and then rebuild all indexes, everything will be fine.
Can anyone help me please?
Thank you so much!
I am a beginner in SQL,
I have a table with GroupRole column and Age column.
CREATE TABLE [Persons](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[FullName] [varchar](70) NULL,
[Age] [int] NULL,
[GroupRole ] [varchar](30) NULL
CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED
(
[PersonID] 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 want to limit the value range that can be placed in an age column lower 30
and GroupRole column equal 'Admin'.
I do not want to do this in the c# code.
How can I do it?
this have easy way:
ALTER TABLE Persons
ADD CONSTRAINT CHK_PersonAge CHECK (Age>=30 AND GroupRole ='Admin');
You need to use Check Constraints. They are part of your table's metadata and define conditions to be checked every time, when the data is modified or inserted in the table. If these conditions are not met, the DML operation will fail with an error.
You need to modify your CREATE TABLE statement as follows:
CREATE TABLE [Persons](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[FullName] [varchar](70) NULL,
[Age] [int] NULL CONSTRAINT CHK_Age CHECK ([Age] < 30),
[GroupRole] [varchar](30) NULL CONSTRAINT CHK_GroupRole CHECK ([GroupRole] in ('Admin'))
CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED
(
[PersonID] 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 have a table: Sales_Table with columns:
ProductKey Int not null,
UnitPrice decimal 18,2,
SalesAmount decimal 18,2
I am trying to add a primary key to the ProductKey column using alter table
ALTER TABLE SALES_TABLE
ADD PRIMARY KEY (ProductKey)
after I run the code and I get the following error:
Msg 1505, Level 16, State 1, Line 9 The CREATE UNIQUE INDEX statement
terminated because a duplicate key was found for the object name
'dbo.Sales_Table' and the index name 'PK__Sales_Ta__A15E99B36F3FE24F'.
The duplicate key value is (604).
What can I do to overcome this issue? Thanks in Advance.
Try this
IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='Product')
BEGIN
CREATE TABLE [dbo].[Product](
[ProductKey] [int] NOT NULL,
[UnitPrice] [decimal](18, 2) NULL,
[SalesAmount] [decimal](18, 2) NULL,
) ON [PRIMARY]
END
IF NOT EXISTS(SELECT 1 FROM sys.indexes WHERE name='Pk_Product')
BEGIN
ALTER TABLE [dbo].[Product] ADD CONSTRAINT [Pk_Product] PRIMARY KEY CLUSTERED
(
[ProductKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
END
I have the following two tables (only relevant key columns shown)
CREATE TABLE [dbo].[Jobs](
[ID] [int] IDENTITY(1,1) NOT NULL,
[JobNo] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Jobs] 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]
CREATE TABLE [dbo].[Items](
[JobID] [int] NOT NULL,
[BarcodeNo] [nvarchar](50) NOT NULL,
[SerialNo] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Items_1] PRIMARY KEY CLUSTERED
(
[JobID] ASC,
[BarcodeNo] ASC,
[SerialNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I need to make sure that when an item is entered, the JobID matches an ID in the Jobs table. It seems, I can't use a FK because the PK in the items table is a composite key of 3 columns.
TIA
Mark
What kind of constraint can I use for this?
A foreign key works fine.
There's no problem with having the single column [dbo].[Items].[JobID] reference the PK of jobs.
CREATE TABLE [dbo].[Jobs]
(
[ID] [INT] IDENTITY(1, 1) NOT NULL,
[JobNo] [NVARCHAR](50) NOT NULL,
CONSTRAINT [PK_Jobs] 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]
CREATE TABLE [dbo].[Items]
(
[JobID] [INT] NOT NULL REFERENCES [dbo].[Jobs]([ID]), /*<-- FK declaration*/
[BarcodeNo] [NVARCHAR](50) NOT NULL,
[SerialNo] [NVARCHAR](50) NOT NULL,
CONSTRAINT [PK_Items_1] PRIMARY KEY CLUSTERED ( [JobID] ASC, [BarcodeNo] ASC, [SerialNo] ASC )
WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
ON [PRIMARY]
I've created a compound unique index on my table:
CREATE TABLE [dbo].[SearchIndexWord](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CatalogID] [int] NOT NULL,
[Word] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_SearchIndexWord] 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]
CREATE UNIQUE NONCLUSTERED INDEX [IX_SearchIndexWord] ON [dbo].[SearchIndexWord]
(
[Word] ASC,
[CatalogID] ASC
)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
The collation for the entire database is set to SQL_Latin1_General_CP1_CI_AS. When I run the following T-SQL, it prints 'Does not equal':
IF 'm3/h' = 'm³/h'
PRINT 'Equals'
ELSE
PRINT 'Does not equal'
Then, if I try the following insert statement:
INSERT INTO [SearchIndexWord] (Word, CatalogID) VALUES ('m3/h', 1), ('m³/h', 1)
I get the following error message:
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.SearchIndexWord' with unique index 'IX_SearchIndexWord'.
Why is this? I couldn't find it in the docs, but I assume the condition of two keys being duplicate is examined using the configured collation.
I've checked the table, column and index collation by the way, and they're all equal to the database collation.
Try this:
IF CAST('m3/h' AS NVARCHAR(100)) = CAST('m³/h' AS NVARCHAR(100))
PRINT 'Equals'
ELSE
PRINT 'Does not equal'
For me, this returns 'Equals' which explains why you're getting the duplicate key row error.
I suspect the values in the code IF 'm3/h' = 'm³/h' are created as VARCHAR.