Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 February SQL Server 2016: Create a FOREIGN KEY in Memory optimized Table (In-Memory OLTP)

SQL Server 2016: Create a FOREIGN KEY in Memory optimized Table (In-Memory OLTP)

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

If you don’t know about the Hekaton engine of SQL Server 2014, please visit below an article.

SQL Server 2014: What is Hekaton?

I am very surprised with the feature list of SQL Server 2016 because it came with lots of features and improvements. Whenever I explored SQL Server 2016, I got something new.
SQL Server 2016 already enhanced In-Memory OLTP by introducing the list of features. Let me share one of the features here.

In the previous version, you cannot create a FOREIGN KEY in memory optimized table. Now, SQL Server 2016 introduced a feature to store the reference of a table in memory so you can create a FOREIGN KEY in memory optimized tables.

You can create a reference between memory optimized tables; you cannot give reference of a Non-memory optimized table to a memory optimized table.

Below is a full demonstration on this.

If you do not configure Memory optimized file group and data file, use below script to configure it.

Add Memory optimized file group:

1
2
ALTER DATABASE Database_Name
ADD Filegroup OLTP_InMemory CONTAINS MEMORY_OPTIMIZED_DATA

Add data file into created Memory optimized file group:

1
2
3
ALTER DATABASE Database_Name
ADD FILE (NAME = OLTP_InMemory, FILENAME = N'C:\SQLBackup\Hekaton')
TO FILEGROUP OLTP_InMemory

Create a sample In-Memory table:

1
2
3
4
5
6
CREATE TABLE tbl_Departments
(
DeptID INT PRIMARY KEY NONCLUSTERED
,DeptName VARCHAR(20)
)WITH ( MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA)
GO

Create a another In-Memory child table:

1
2
3
4
5
6
7
8
CREATE TABLE tbl_Employee
(
EmpID INT PRIMARY KEY NONCLUSTERED
,EmpName VARCHAR(20)
,DeptID INT
,CONSTRAINT fk_tbl_Employee_EmpID FOREIGN KEY (DeptID) REFERENCES tbl_Departments(DeptID)
) WITH ( MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA);
GO

Try to give reference of Non-Memory optimized table to Memory optimized table:

1
2
3
4
5
6
7
8
9
CREATE TABLE tbl_EmpSalary
(
EmpSalaryID INT PRIMARY KEY
,EmpID INT
,EmpSalary INT
,EmpSalaryMonth INT
,CONSTRAINT fk_tbl_EmpSalary_EmpID FOREIGN KEY (EmpID) REFERENCES tbl_Employee(EmpID)
)
GO

You will get below errors:

1
2
3
4
Msg 10778, Level 16, State 0, Line 1
Foreign key relationships between memory optimized tables and non-memory optimized tables are not supported.
Msg 1750, Level 16, State 1, Line 1
Could not create constraint or index. See previous errors.

Feb 23, 2017Anvesh Patel
SQL Server: Script to monitor the Corrupt Database PagesSQL Server: Script to find Open Connections and CPU Usage of each Client Programs
Anvesh Patel

Database Engineer

February 23, 2017 SQL ServerAnvesh Patel, database, database research and development, dbrnd, foreign key, Hekaton, In-Memory OLTP, Memory Optimized Table, MEMORY_OPTIMIZED_DATA, SQL Query, SQL Server, SQL Server 2014, SQL Server 2016, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....