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.
Related
I'm trying to select mobile phone numbers that are beginning with +385 and on next position could be any number except 9. Please help. Thank you.
You are lucky, I was looking for an occasion to spin up my lubuntu SQL server VM , check this testing code should answer the question
CREATE TABLE [dbo].[Alex](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Phone] [nvarchar](20) NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
insert into Alex (Phone) values ('+3329855870421');
insert into Alex (Phone) values ('+3850125611333');
insert into Alex (Phone) values ('+3859125611333');
GO
select * from Alex where Phone like '+385[0-8]%'
I am getting below error and if I don't use computed column then it works fine .
Msg 213, Level 16, State 1, Procedure test1tri, Line 3 [Batch Start Line 24]
Column name or number of supplied values does not match table definition.
this is sample I am using:
drop table test2
CREATE TABLE test2 ([ID] [INT], [NAME] [VARCHAR](100), [ADDRESS] [VARCHAR](100)
CONSTRAINT [PK_test2] 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]
ALTER TABLE test2
ADD full_name AS (name + ' ' + address) persisted;
drop table test3
CREATE TABLE test3 ([$delete] [bit] NULL,[ID] [INT], [NAME] [VARCHAR](100), [ADDRESS] [VARCHAR](100)
CONSTRAINT [PK_test3] UNIQUE 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 test3
ADD full_name AS (name + ' ' + address) persisted;
CREATE TRIGGER [test1tri] ON [test2]
INSTEAD OF INSERT AS
INSERT INTO [test3] SELECT 0,* FROM inserted
GO
While you want to insert new record to table you should not provide value for the computed column. So you can rewrite your trigger as below:
CREATE TRIGGER [test1tri] ON [test2]
INSTEAD OF INSERT AS
INSERT INTO [test3] SELECT 0,
Inserted.ID,
Inserted.NAME,
Inserted.ADDRESS FROM inserted
GO
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
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]