I have about 8 entities that all have a one to one relationship with a common entity.
The client is not choosing from pre-defined data so it is not necessary to apply a FK to be used as a constraint.
The main table in question is call a finish table and it contains four unknown hex colors that are sent from the client to the server.
For example, when a door is built the colors for the different parts of the door and it's outer parts can all have different colors. So, all of these tables in question, their data is always fresh from the client and not chosen from a drop down, or from some other pre-defined data that I am given to the client to choose from.
My question is, what would be the best way to association this finish entity with the other entities that need a way to express their finish?
I am adding a screen shot of a diagram that I am working on, these are not all the entities and are just the ones in question right now and ones that will help articulate to others what I am needing a solution for.
I have also included some script as well.
/****** Object: Table [dbo].[Finish] Script Date: 06/22/2012 15:08:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Finish](
[ID] [int] NOT NULL,
[Left] [varchar](30) NULL,
[Right] [varchar](30) NULL,
[Top] [varchar](30) NULL,
[Bottom] [varchar](30) NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Finish] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Horizontal] Script Date: 06/22/2012 15:08:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Horizontal](
[ID] [int] IDENTITY(11,1) NOT NULL,
[Name] [varchar](15) NOT NULL,
[Floor] [smallint] NOT NULL,
[SizeID] [int] NOT NULL,
[GlassPocket] [decimal](5, 3) NULL,
[IsFiller] [bit] NOT NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Horizontal] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Door] Script Date: 06/22/2012 15:08:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Door](
[ID] [int] IDENTITY(421,1) NOT NULL,
[BayID] [int] NOT NULL,
[Position] [tinyint] NOT NULL,
[HasJamb] [bit] NOT NULL,
[HasThreshold] [bit] NOT NULL,
[IsAutoShowroom] [bit] NOT NULL,
[IsSingle] [bit] NOT NULL,
[Type] [varchar](10) NOT NULL,
[SizeID] [int] NOT NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Door] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Leaf] Script Date: 06/22/2012 15:08:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Leaf](
[ID] [int] IDENTITY(21,1) NOT NULL,
[DoorID] [int] NOT NULL,
[Position] [tinyint] NOT NULL,
[Stile] [varchar](10) NOT NULL,
[Bottomrail] [decimal](5, 3) NOT NULL,
[Hand] [varchar](5) NOT NULL,
[IsActive] [bit] NOT NULL,
[Swing] [varchar](5) NOT NULL,
[SizeID] [int] NOT NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Leaf] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Bay] Script Date: 06/22/2012 15:08:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Bay](
[ID] [int] IDENTITY(1213,1) NOT NULL,
[ElevationID] [int] NOT NULL,
[Position] [tinyint] NOT NULL,
[SizeID] [int] NOT NULL,
[Note] [varchar](150) NULL,
CONSTRAINT [PK_Bay] 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
SET ANSI_PADDING OFF
GO
Each one of the entities below need to be associated with the finish table. Each entity new record has exactly one finish relationship.
Is it possible to associate these and still be able to do a Cascade On DELETE, with-out having a circular reference issues?
Yes, it's possible.
You would add a reference to Finish entity in each of the tables that has a relationship to it.
You would define the column with the same datatype, (and normally) as the referenced_table_name and id. In our shop, the column_name would be [finish_id]. (I see here that you are using a CamelCase style.)
At any rate, I would recommend you define this as a foreign key.
You say there is no need to, but from what you describe, this is exactly the kind of situation that calls for a foreign key constraint.
You need to decide on the action when the id in the finish table is updated or deleted. (Do you want to disallow the update or delete? Do you want to preserve existing relationship?) I expect you would want the default ON DELETE RESTRICT. You could allow for updates, and preserve the relationships, with ON UPDATE CASCADE.)
I don't see any potential problem with circular references, as long as the finish table will be the parent (all the other tables reference it, and the finish table doesn't have any references to the other tables.)
Related
I want to create this table with the following constrains, is it possible to do with SQL Server Management Studio?
Id
ProductId [Nullable column]
CompanyId [Nullable column]
Username [unique for every productId - IF DeletedAt is not NULL]
Identifier [Nullable column] [unique for every companyId - IF DeletedAt is not NULL]
DeletedAt [Nullable column]
Update:
My table create query is the following:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ProductId] [int] NULL,
[CompanyId] [int] NULL,
[Username] [nvarchar](max) NOT NULL,
[Identifier] [nvarchar](max) NULL,
[DeletedAt] [datetime2](7) NULL,
CONSTRAINT [PK_User] 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
You'll want to use a unique filtered index. Here you go for the table:
/****** Object: Table [dbo].[the_table] Script Date: 7/26/2018 4:04:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[the_table](
[id] [int] NOT NULL,
[productid] [varchar](50) NULL,
[companyid] [varchar](50) NULL,
[username] [varchar](50) NULL,
[identifier] [varchar](50) NULL,
[deleteat] [varchar](50) NULL,
CONSTRAINT [PK_the_table] 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
And the index for username:
SET ANSI_PADDING ON
GO
/****** Object: Index [UIDX_USERNAME] Script Date: 7/26/2018 4:03:31 PM ******/
CREATE NONCLUSTERED INDEX [UIDX_USERNAME] ON [dbo].[the_table]
(
[username] ASC
)
WHERE ([DELETEAT] IS NOT NULL)
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
It would be the same index logic for the other column, but I don't want to clutter this answer by posting more! I would not mess around with putting the logic in triggers or stored proc's, bad data could still sneak in. If you keep nice constraints, nobody can clutter your data up :)
You can use filtered indexes. The syntax looks like this:
create unique index unq_thetable_username on the_table(username)
where deleteAt is not null;
I don't know if you can point-and-click your way to this solution. I would just write the logic as above.
Create a unique index on ProductId and Username, and use a where clause to restrict that index to WHERE DeletedAt IS NOT NULL. Answer showing an example of a unique filtered index.
You may also need the clause AND ProductId IS NOT NULL, although that wasn't stated as a requirement, but since that column is nullable you ought to think about this.
Similar for Identifier.
You might also want to think about the value of a table where it is valid to have a row where all the columns except Id are null...
Here is a screenshot of the error I'm getting.
Here is the snippet I am tying to run
USE [Warehouse_PCL]
GO
/****** Object: Table [dbo].[RouteTable] Script Date: 2/19/2018 2:27:57 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[RouteTable](
[BranchID] [int] NOT NULL,
[StoreID] [int] NOT NULL,
[WaveGroup] [int] NOT NULL,
[Wave] [int] NOT NULL,
[Aisle] [int] NOT NULL,
[Lane] [int] NOT NULL,
CONSTRAINT [PK_RouteTable] PRIMARY KEY CLUSTERED
(
[BranchID] ASC,
[StoreID] ASC,
[WaveGroup] ASC,
[Wave] ASC,
[Aisle] 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 am unsure what is causing this, any thouhts? Let me know if there is any other information I can provide to help.
===================================
Cannot execute script.
===================================
Object reference not set to an instance of an object. (Microsoft.VisualStudio.Editor.Implementation)
------------------------------
Program Location:
at Microsoft.VisualStudio.Editor.Implementation.VsTextViewAdapter.SetScrollPosition(Int32 iBar, Int32 iFirstVisibleUnit)
at Microsoft.SqlServer.Management.UI.VSIntegration.Editors.VSTextEditorTabPage.Clear()
at Microsoft.SqlServer.Management.UI.VSIntegration.Editors.DisplaySQLResultsControl.Clear()
at Microsoft.SqlServer.Management.UI.VSIntegration.Editors.DisplaySQLResultsControl.PrepareForExecution(Boolean prepareForParse)
at Microsoft.SqlServer.Management.UI.VSIntegration.Editors.ScriptAndResultsEditorControl.StandardPrepareBeforeExecute(QEStatusBarKnownStates newStatusBarState)
at Microsoft.SqlServer.Management.UI.VSIntegration.Editors.SqlScriptEditorControl.StandardPrepareBeforeExecute(QEStatusBarKnownStates newStatusBarState)
at Microsoft.SqlServer.Management.UI.VSIntegration.Editors.ScriptAndResultsEditorControl.OnExecScript(Object sender, EventArgs a)
This thread suggests that the problem is related to the .NET Framework update KB4055532. Try the workarounds suggested here.
I have a relatively simple application that pulls SQL data via a .NET Web API using EF6. There are three tables in the database:
Product
CREATE TABLE [dbo].[Product]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](255) NOT NULL,
[FullName] [varchar](max) NOT NULL,
[Description] [varchar](max) NOT NULL,
[BranchID] [int] NOT NULL,
[ProgramID] [int] NOT NULL,
[TechnologyPlatformID] [int] NOT NULL,
[StatusID] [int] NOT NULL,
[FunctionID] [int] NOT NULL,
[ProgramManagerID] [int] NOT NULL,
[TypeID] [int] NOT NULL,
[Vendor] [varchar](max) NOT NULL,
[VendorPOC] [varchar](max) NOT NULL,
[URL] [varchar](255) NULL,
[Code] [varchar](50) NULL,
[CreatedBy] [int] NOT NULL,
[CreateDate] [datetime] NOT NULL,
[ModifiedBy] [int] NOT NULL,
[ModifiedDate] [datetime] NOT NULL,
CONSTRAINT [PK_Product]
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].[Product] WITH CHECK
ADD CONSTRAINT [FK_Product_Function]
FOREIGN KEY([FunctionID]) REFERENCES [dbo].[Function] ([ID])
GO
ALTER TABLE [dbo].[Product] CHECK CONSTRAINT [FK_Product_Function]
GO
ALTER TABLE [dbo].[Product] WITH CHECK
ADD CONSTRAINT [FK_Product_Program]
FOREIGN KEY([ProgramID]) REFERENCES [dbo].[Program] ([ID])
GO
ALTER TABLE [dbo].[Product] CHECK CONSTRAINT [FK_Product_Program]
GO
Program:
CREATE TABLE [dbo].[Program]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](255) NOT NULL,
CONSTRAINT [PK_Program]
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
SET ANSI_PADDING OFF
GO
Function:
CREATE TABLE [dbo].[Function]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](255) NOT NULL,
[Description] [varchar](max) NOT NULL,
CONSTRAINT [PK_Function_1]
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
My EDMX looks normal:
And here's how I'm accessing the data in my repo:
Suddenly I'm getting the following error when calling GetProducts():
An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module.
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I can't seem to figure out what's causing this. Here's what I have tried:
Made sure "Suppress JIT optimization..." was unchecked in VS
Repaired .NET Framework on my local machine
Neither worked...however, if I delete the Function table in SQL and update my EDMX, the issue is resolved. Why would this table be causing the issue?
Appreciate any input, thank you!
Try turning proxy generation off, and see if you still get the same problem.
My initial guess would be that you are including the Program directly via .Include(x => x.Program), but Function navigation property is being proxied into a lazy property. And when WebAPI tries to serialize it, it throws an error.
I am trying to create replica of my database from SQL server to another.
For that I am generating script from original server and trying to run in another server. I've created database manually with the same name.
Here is the screenshot of original database
When I generate script, following script is created which I am trying in another server
USE [ContactsApp]
GO
/****** Object: Table [common].[BU] Script Date: 2/12/2016 3:02:29 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [common].[BU](
[ID] [int] IDENTITY(1,1) NOT NULL,
[IndustryID] [int] NOT NULL,
[BU] [varchar](50) NOT NULL,
[Code] [varchar](2) NOT NULL,
[Active] [bit] NOT NULL,
[CreatedBy] [uniqueidentifier] NOT NULL,
[CreateDate] [date] NOT NULL,
[CreateTime] [time](3) NOT NULL,
[ModifiedBy] [uniqueidentifier] NULL,
[ModifyDate] [date] NULL,
[ModifyTime] [time](3) NULL,
CONSTRAINT [PK_BU] 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],
CONSTRAINT [UK_BU_Code] UNIQUE NONCLUSTERED
(
[Code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UK_BU_Name] UNIQUE NONCLUSTERED
(
[BU] 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 [common].[BU] ADD CONSTRAINT [DF_BU_CreateDate] DEFAULT (CONVERT([date],getdate())) FOR [CreateDate]
GO
ALTER TABLE [common].[BU] ADD CONSTRAINT [DF_BU_CreateTime] DEFAULT (CONVERT([time],getdate())) FOR [CreateTime]
GO
ALTER TABLE [common].[BU] WITH CHECK ADD CONSTRAINT [FK_BU_Industry] FOREIGN KEY([IndustryID])
REFERENCES [common].[Industry] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [common].[BU] CHECK CONSTRAINT [FK_BU_Industry]
GO
When I try to run this script, I get following error
The specified schema name "common" either does not exist or you do
not have permission to use it.
I don't know what is the meaning of common here.Thanks
Your tables are 'grouped' (for want of better word) in schemas (google for it). You should run
CREATE SCHEMA common
And likewise for all other schemas.
I'm learning about and how to use SQL triggers. I'm building a test database that isn't part of my classes so I can practice.
The problem is I'm getting
The object 'dbo.StudentInfo' does not exist or is invalid for this
operation
1) I have permissions, I'm the administrator.
2) I'm absolutely positive I'm in the correct database.
3) I have tried dbo.StudentInfo, StudentInfo, [dbo.StudentInfo] (which I know is wrong anyway) and [dbo].[StudentInfo], and all of them give me the same error.
What else could be wrong? What other information would you need to help?
Use Test3
GO
CREATE TRIGGER tr_high_date
ON [dbo].[StudentInfo]
FOR INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
END
GO
Table Information:
USE [test3]
GO
/****** Object: Table [dbo].[StudentInfo] Script Date: 9/22/2015 9:39:53 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[StudentInfo](
[StudentID] [char](8) NOT NULL,
[LastName] [varchar](20) NOT NULL,
[FirstName] [varchar](20) NOT NULL,
[NickName] [varchar](20) NOT NULL,
[PhoneNumber] [char](10) NULL,
[Email] [varchar](40) NOT NULL,
[DateAdded] [date] NOT NULL,
[DateExpectedGraduation] [date] NOT NULL,
CONSTRAINT [PK_StudentInfo] PRIMARY KEY CLUSTERED
(
[StudentID] 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
Are you sure you are using the correct database? Maybe you're in the master database and your tables are in a different database or vice versa.