Error creating In-Memory Tables - sql

I am trying to create a table, in-memory but it keeps giving me errors like:
Msg 10794, Level 16, State 85, Line 11 The index option 'pad_index' is
not supported with indexes on memory optimized tables.
and another one saying the same thing about Primary Key Clustered.
Those two things above (pad_index and PK Clustered) are needed, unless there is another way to get it to work...
[DEMO-Training1]
GO
/****** Object: Table [dbo].[wtAssetChildren] ******/
SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER ON GO
CREATE TABLE [dbo].[wtAssetChildren] (
[wtAssetChildrenID] [int] IDENTITY(1,1) NOT NULL,
[wtGUID] [uniqueidentifier] NOT NULL,
[CallingAssetID] [int] NOT NULL,
[AssetID] [int] NOT NULL,
[Processed] [bit] NOT NULL,
CONSTRAINT [PK_wtAssetChildren] PRIMARY KEY CLUSTERED ([wtAssetChildrenID] 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].[wtAssetChildren]
ADD CONSTRAINT [DF_wtAssetChildren_Processed] DEFAULT ((0)) FOR [Processed] GO

In-Memory OLTP Tables have a bunch of restrictions relative to the rest of what generally ships with MS SQL. Check out http://msdn.microsoft.com/en-us/library/dn246937.aspx for a full list.
Clustered PKs are not supported, for example. You'll have to use nonclustered primary key indices instead.

Below is an example of how you can create this table in a memory-optimized SQL Server 2014. Note that just because you can, doesn't mean you should. You mention you are beginner and memory-optimized tables are more of an advanced option.
CREATE DATABASE MemoryOptimizedTest;
GO
ALTER DATABASE MemoryOptimizedTest
ADD FILEGROUP MemoryOptimizedTest_MemoryOptimized
CONTAINS MEMORY_OPTIMIZED_DATA;
GO
ALTER DATABASE MemoryOptimizedTest
ADD FILE (
NAME = 'MemoryOptimizedTest__MemoryOptimized1'
, FILENAME = 'D:\SqlDataFiles\MemoryOptimizedTest__MemoryOptimized1')
TO FILEGROUP MemoryOptimizedTest_MemoryOptimized;
GO
ALTER DATABASE MemoryOptimizedTest
SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT=ON;
GO
USE MemoryOptimizedTest;
GO
CREATE TABLE [dbo].[wtAssetChildren](
[wtAssetChildrenID] [int] IDENTITY(1,1) NOT NULL
, [wtGUID] [uniqueidentifier] NOT NULL
, [CallingAssetID] [int] NOT NULL
, [AssetID] [int] NOT NULL
, [Processed] [bit] NOT NULL
CONSTRAINT [DF_wtAssetChildren_Processed] DEFAULT ((0))
, CONSTRAINT [PK_wtAssetChildren] PRIMARY KEY NONCLUSTERED( [wtAssetChildrenID] ASC )
)
WITH (MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_AND_DATA);

Related

SQL Azure is recommending Indexes that are already on my Table

In the Azure Portal you see recommendations from SQL Azure. The weird thing is that these indexes already exist on my Table.
A query on MemberId on the Tip table also returns within I <250ms on a table of 7.5 million records so I have the feeling the index is working.
We do seem having performance problems during high traffic.
What could be going on?
TABLE
CREATE TABLE [dbo].[Tip](
[Id] [int] IDENTITY(1,1) NOT NULL,
[MemberId] [int] NOT NULL,
[Slot] [varchar](25) NOT NULL,
[PeriodKey] [varchar](25) NOT NULL,
[LastChanged] [datetime2](7) NOT NULL,
[Invalid] [bit] NOT NULL,
[Value] [nvarchar](50) NULL,
[ListingIndex] [tinyint] NOT NULL,
CONSTRAINT [PK_Tip] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY],
CONSTRAINT [IX_Tip_Unique_Key] UNIQUE NONCLUSTERED
(
[MemberId] ASC,
[PeriodKey] ASC,
[Slot] ASC,
[ListingIndex] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tip] ADD CONSTRAINT [DF_Tip_Invalid] DEFAULT ((0)) FOR [Invalid]
GO
ALTER TABLE [dbo].[Tip] ADD CONSTRAINT [DF_Tip_ListingIndex] DEFAULT ((0)) FOR [ListingIndex]
GO
ALTER TABLE [dbo].[Tip] WITH NOCHECK ADD CONSTRAINT [FK_Tip_Member] FOREIGN KEY([MemberId])
REFERENCES [dbo].[Member] ([Id])
GO
ALTER TABLE [dbo].[Tip] NOCHECK CONSTRAINT [FK_Tip_Member]
GO
This is a really simple query on MemberId. Normal queries might be a bit more complicated:
This is the Index that it tries to create in the Azure portal:

How to come up with a script for modifying an existing table in the database without losing the existing information?

I have an application with a database which they are working well and everything is fine.
Now, I just need to modify one table in the database by adding more columns to it. I am using SQL Server and the database administrator asked me to provide him with the script of modifying that table.
So how to do that?
I am using SQL Server Management Studio and when I click on the table right-click, I used to select script to create, and Management Studio will give me the script. Now, this table has information, so I think I should not use create script for this table.
The new columns should allow null values.
So what should I use?
For your information, here is the script for creating the table:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Test](
[ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[No] [char](20) NOT NULL,
[Date] [smalldatetime] NULL,
[ProjectType] [char](500) NULL,
[ProjectPhase] [char](300) NULL,
[rejected_reason] [varchar](max) NULL,
[archived_reason] [varchar](max) NULL,
[forward_to] [varchar](max) NULL,
[forward_type] [varchar](max) NULL,
[forward_concern] [varchar](max) NULL,
CONSTRAINT [PK_Test] 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] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Test] ADD CONSTRAINT [DF_LLATestB_No] DEFAULT ('*') FOR [No]
GO
ALTER TABLE [dbo].[Test] ADD CONSTRAINT [DF_LLATestB_Status] DEFAULT ((2)) FOR [Status]
GO
ALTER TABLE [dbo].[Test] ADD CONSTRAINT [DF_LLATestB_AID] DEFAULT ((0)) FOR [AID]
GO
ALTER TABLE [dbo].[Test] ADD CONSTRAINT [DF_LLATestB_Hit] DEFAULT ((0)) FOR [Hit]
GO
ALTER TABLE [dbo].[Test] ADD CONSTRAINT [DF_LLATestB_Sent] DEFAULT ((0)) FOR [Sent]
GO
ALTER TABLE [dbo].[Test] ADD CONSTRAINT [DF_LLATestB_SentTo] DEFAULT ((0)) FOR [SentTo]
GO
ALTER TABLE [dbo].[Test] ADD CONSTRAINT [DF_LLa_Added_To_Cart] DEFAULT ((0)) FOR [Added_To_Cart]
GO
Examples of the new columns that should be added to this table are:
[rejected_reason] [varchar](max) NULL,
[archived_reason] [varchar](max) NULL,
[forward_to] [varchar](max) NULL,
[forward_type] [varchar](max) NULL,
When you've studied SQL Server Books Online on MSDN, it should be a piece of cake - use ALTER TABLE:
ALTER TABLE dbo.LLa
ADD [rejected_reason] [varchar](max) NULL
ALTER TABLE dbo.LLa
ADD [archived_reason] [varchar](max) NULL
ALTER TABLE dbo.LLa
ADD [forward_to] [varchar](max) NULL
ALTER TABLE dbo.LLa
ADD [forward_type] [varchar](max) NULL
The question is more: do you REALLY need up to 2 GByte of data (VARCHAR(MAX)) for each of your columns here?? Seems like a bit of overkill!
You should always use the appropriate data type - if that column will only ever hold up to 100 characters - why not use VARCHAR(100) instead of using the max datatype??

How to create two or more Unique Columns in SQL Azure?

I want to select two columns in my table and make them unique but I don't know how to do it in SQL Azure database. As you can see in the image below, it doesn't show any option to modify the table properties, so everything is done using sql queries:
Here is the generated script of the table:
USE [mydbase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[clientaccess](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[ModuleName] [nvarchar](50) NOT NULL,
[ClientAuthenticationId] [bigint] NOT NULL,
[HasAccess] [bit] NOT NULL,
CONSTRAINT [PK_clientaccess_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)
)
GO
ALTER TABLE [dbo].[clientaccess] WITH CHECK ADD CONSTRAINT [CAI_caID] FOREIGN KEY([ClientAuthenticationId])
REFERENCES [dbo].[clientauthentication] ([ID])
GO
ALTER TABLE [dbo].[clientaccess] CHECK CONSTRAINT [CAI_caID]
GO
This is the preview where I encountered the problem, it contains duplicate records:
Hope someone understand my explanation.
Sometimes GUIS have limitations (or not but you haven't discovered yet how all functionalities work). You can always add a unique constraint with ALTER TABLE:
ALTER TABLE [dbo].[clientaccess]
ADD CONSTRAINT Module_Client_UQ --- choose a name
UNIQUE (ModuleName, ClientAuthenticationId) ;

Error creating a table : "There is already an object named ... in the database", but not object with that name

I'm trying to create a table on a Microsoft SQL Server 2005 (Express).
When i run this query
USE [QSWeb]
GO
/****** Object: Table [dbo].[QSW_RFQ_Log] Script Date: 03/26/2010 08:30:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[QSW_RFQ_Log](
[RFQ_ID] [int] NOT NULL,
[Action_Time] [datetime] NOT NULL,
[Quote_ID] [int] NULL,
[UserName] [nvarchar](256) NOT NULL,
[Action] [int] NOT NULL,
[Parameter] [int] NULL,
[Note] [varchar](255) NULL,
CONSTRAINT [QSW_RFQ_Log] PRIMARY KEY CLUSTERED
(
[RFQ_ID] ASC,
[Action_Time] 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 got this error message
Msg 2714, Level 16, State 4, Line 2
There is already an object named
'QSW_RFQ_Log' in the database. Msg
1750, Level 16, State 0, Line 2 Could
not create constraint. See previous
errors.
but if i try to find the object in question using this query:
SELECT *
FROM QSWEB.sys.all_objects
WHERE upper(name) like upper('QSW_RFQ_%')
I got this
(0 row(s) affected)
What is going on????
You are trying to create a table with the same name as a constraint (QSW_RFQ_Log). Your query doesn't find the object because the table creation fails so the object doesn't exist after the error. Pick a new name for the constraint and it will work, e.g.:
CONSTRAINT [QSW_RFQ_Log_PK] PRIMARY KEY CLUSTERED
try this:
CONSTRAINT [PK_QSW_RFQ_Log] PRIMARY KEY CLUSTERED
add this ^^^
you are trying to add the Primary key the same name as the table, make the PK have a different name.
You should not name the primary key constraint like your datatable ;-)

How do you add Foreign Key Relationships?

I'm working with an existing SQL 2005 database that was not implemented with FK relationships between tables. I tried to add the relationships with a database diagram and my application immediately blew up trying to edit or insert any data that is tied to the new FK.
dbo.person [person_id | firstname | lastname | dateofbirth]
dbo.campaign [campaign_id | campaign_description]
dbo.disposition [disposition_id | disposition_description]
dbo.person_campaigns [person_campaign_id | person_id | campaign_id | disposition_id]
The person_campaigns table is where a person, campaign, and disposition are tied together. Can you please provide the appropriate SQL syntax for adding the proper FK relationships between these entities?
EDIT
CREATE TABLE [dbo].[person_campaigns](
[person_campaigns_id] [int] IDENTITY(1,1) NOT NULL,
[person_id] [int] NOT NULL,
[d_campaign_id] [int] NOT NULL,
[d_physician_disposition_id] [int] NULL,
CONSTRAINT [PK_person_campaigns] PRIMARY KEY CLUSTERED
(
[person_campaigns_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
CREATE TABLE [dbo].[d_campaign](
[d_campaign_id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[year] [int] NULL,
[isactive] [bit] NOT NULL,
CONSTRAINT [PK_d_campaign] PRIMARY KEY CLUSTERED
(
[d_campaign_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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[d_campaign] ADD CONSTRAINT [DF_d_campaign_isactive] DEFAULT ((1)) FOR [isactive]
GO
CREATE TABLE [dbo].[d_disposition](
[d_disposition_id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[isactive] [bit] NOT NULL,
CONSTRAINT [PK_d_disposition] PRIMARY KEY CLUSTERED
(
[d_disposition_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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[d_disposition] ADD CONSTRAINT [DF_d_disposition_isactive] DEFAULT ((1)) FOR [isactive]
GO
CREATE TABLE [dbo].[person](
[person_id] [int] IDENTITY(1,1) NOT NULL,
[firstname] [varchar](30) NULL,
[lastname] [varchar](30) NULL,
[dateofbirth] [datetime] NULL
CONSTRAINT [PK__person__0BC6C43E] PRIMARY KEY CLUSTERED
(
[person_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
the easiest way to do it is through the database diagram editor; do them one at a time and save the diagram to affect the tables after each connection is made. If it "blows up" it is most likely because the tables contain foreign-key values that do not exist; you'll have to clean these up first.
If you have to add them after the table is created the syntax is
create table person (person_id int primary key
,firstname varchar(10)
, lastname varchar(10)
, dateofbirth varchar(10))
create table campaign (campaign_id int primary key
, campaign_description varchar(10))
create table disposition (disposition_id int primary key
,disposition_description varchar(10))
create table person_campaigns(person_campaign_id int
,person_id int, campaign_id int ,disposition_id int)
go
alter table person_campaigns add Constraint
fk_person_campaigns_person_id
Foreign Key (person_id) References person(person_id)
GO
alter table person_campaigns add Constraint
fk_person_campaigns_campaign_id
Foreign Key (campaign_id) References campaign(campaign_id)
GO
alter table person_campaigns add Constraint
fk_person_campaigns_disposition_id
Foreign Key (disposition_id) References disposition(disposition_id)
GO
Suppose I had two tables that should have had a foreign key but did not. The first thing to do is check to see if there will be a data problem if I set a foreign key.
something like the below code would get you the records in the child table that do not have a match in the parent table.
select t2.FKField, t2.PKfield from table2 t2
left join Table1 t1 on t2.fkfield = t1.pkfield
where t1.pkfield is null
Once you can see what is wrong with the existing data, then you need to create a way to fix it. The fix will vary depending on what data you have that has no relationship to the Parent table and what the tables represent. Suppose your parent table contained a VIN number for automobiles as the PK. If your child table contains the cars that were worked on by the shop, you would want to fix the issue by adding the nonexisting VINS to the primary table becasue you wouldn't want to lose the history of what was worked on. There are other structures where you might want to simply delete the records that don't match in child table because they are meaningless. In other circumstances you might want to update those records to some default value (perhaps a customer in the customer table called unknown). In still other circumstances, you might need to go to audit tables or backups to find the value of the PK that was deleted without the associated child records being deleted. The actual way to fix this problem is highly dependent on what the data is used for and how important it is to retain all historical records. Since you should never delete any record that might be related to a financial transaction for legal (and accounting) reasons, you need to be most careful with those.
After fixing all the data, then you run the code to create the FK constraint.
Since I don't have SQL Server on this PC and I don't memorize the syntax, the easiest thing to do is to create two new test tables, create TableA with an ID field, TableB with a field that is a FK of TableA.ID, and then script out TableB to see the ADD CONSTRAINT syntax. Do this using SQL Server Management Studio via a database diagram.
However, if you were able to successfully create the FKs in a data diagram, and you only can't add or update records, I believe something else is wrong. Script out the person_campaigns table and post the code.