I'm using Entity Frame Core Code First with an ASP MVC project I'm working on.
I have a table with two foreign keys that reference the same table. I followed this great example: Entity Framework Code First - two Foreign Keys from same table
How can I enforce it so that the value combinations can only exist once?
i.e.
Id Value1 Value
1 1 2 --This is ok
2 1 3 --This is ok
3 1 2 --This is bad. Duplicate combination
4 3 1 --This is bad. This is just the reverse of Id 2
Thank you.
Related
I'm trying to create a new table on my DB, the table has 2 important columns
id_brands (This is an FK from the table brands)
id_veiculo
What I would like to have is something like this:
id_brands
id_veiculo
1
1
1
2
2
1
2
2
3
1
1
3
3
2
I create the table but I'm trying to find a way to make this condition with a trigger but without success, I don't know if it's possible or if a trigger is the best way to do that.
What you are probably trying to do, by the pattern of the example table, is setting up an auxiliary N to N relationship table.
In this case, by having another table, for id_veiculo and its properties, you will be able to have both ids as FKs. As for the primary key in this auxiliary table, it would be both id_brands and id_veiculo:
PRIMARY KEY (id_veiculo, id_brands);
Here's another Stackoverflow question about NxM/NxN relationships.
Also, it isn't very clear what you're trying to do with the table, but if it's the population/seeding of data, then yes, a Trigger is an viable solution.
I need to enforce uniqueness on specific data in a table (~10 million rows). This example data illustrates the rule -
For code=X the part# cannot be duplicate. For any other code there can be duplicate part#. e.g ID 8 row can't be there but ID 6 row is fine. There are several different codes in the table and part# but uniqueness is desired only for one code=X.
ID CODE PART#
1 A R0P98
2 X R9P01
3 A R0P98
4 A R0P44
5 X R0P44
6 A R0P98
7 X T0P66
8 X T0P66
The only way I see is to create a trigger on the table and check for PART# for code=X before insert or update. However, I fear this solution may slow down inserts and updates on this table.
Appreciate your help!
In Oracle, you can create a unique index on an expression for this:
create unique index myidx
on mytable (case when code = 'X' then part# end);
Sorry but I don't know how to call in the Title what I need.
I want to create an unique key where each two digits of the number identify other table PK. Lets say I have below Pks in this 3 tables:
Id Company Id Area Id Role
1 Abc 1 HR 1 Assistant
2 Xyz 2 Financial 2 Manager
3 Qwe 3 Sales 3 VP
Now I need to insert values in other table, I know that I may do in 3 columns and create a Composite Key to reach integrity and uniqueness as below:
Id_Company Id_Area Id_Role ...Other_Columns.....
1 2 1
1 1 2
2 2 2
3 3 3
But I was thinking in create a single column where each X digites identify each FK. So the above table 3 first columns become like below (suposing each digit in an FK)
Id ...Other_Columns.....
121
112
222
333
I don't know how to call it and even if it's stupid but it makes sense for me, where I can select for a single column and in case of need some join I just need to split number each X digits by my definition.
It's called a "smart", "intelligent" or "concatenated" key. It's a bad idea. It is fragile, leads to update problems and impedes the DBMS. The DBMS and query language are designed for you to describe your application via base tables in a straightforward way. Use them as they were intended.
I've read plenty of supertype/subtype threads and I'm pretty sure I am not asking the same one.
I have the following tables in my database. Note that:
1. Some security types only need Type but require no SubType, such as stocks and bonds.
2. Securties.TypeId is a foreign key pointing to Type.ID.
3. Securties.SubTypeId has no foreign key relationship to BondType or DerivativeType tables. And currently the data integrity is maintained by C# code.
Since lacking of foreign key relationship is bad, I want to refactor this DB to have it. Given that this DB is already in production, what's the best way to improve it while limiting the software risk? i.e., one way to do it is to combine all XXXType tables into a single table and have all SubTypeIds rearranged, but clearly that involves updating tons of records in the Securites table. So it's considered a more risky approach than another one which doesn't require changing values.
[Securites]
ID Name TypeId SubTypeId
1 Stock1 2 NULL
2 Fund1 3 NULL
3 Bond1 1 3
4 Deriv1 4 3
[Type]
ID Name
1 Bond
2 Stock
3 ETF
4 Derivative
[BondType]
ID Name
...
2 GovermentBond
3 CorporateBond
4 MunicipalBond
...
[DerivativeType]
ID Name
...
2 Future
3 Option
4 Swap
...
First at all, sorry for my english. I've tried to find an answer to this question but I don't really how to express myself.
If it's a duplicate, please close it and let me know the answer.
I have a table to store the data for each item of customer's shopping cart. The structure is:
Table - tmpShoppingCartItem
tmpShoppingCart_ID int FK from tmpShoppingCart
ID int PK, Ident 1,1
StoreSKU_ID int FK from StoreSku
Quantity int
Enabled bit
The column ID is a identity with seed = 1. But when I start inserting data, it looks like:
tmpShoppingCart_ID ID ....
1 1
1 2
1 3
1 4
until here, its ok but when it's a new shopping cart, it looks like:
tmpShoppingCart_ID ID ....
2 5
2 6
3 7
4 8
the ID columns still seeding 1.
I want to know if (and how) can I reset the seed counter when the tmpShoppingCart_ID changes, like:
tmpShoppingCart_ID ID ....
1 1
1 2
1 3
1 4
2 1
2 2
3 1
3 2
3 3
3 4
3 5
4 1
Thanks for your time.
Identity columns when used as a primary key should be sequential and not repeat in a table. First, you need to make both temShoppingCart_ID and ID the unique PK to prevent duplicatation. Secondly, you will not be able to use the IDENTITY function, but rather a counter in your application for each row that's inserted for a given tempShoppingCart_ID.
In my opinion, keep the ID as an identity column like it currently is. Add a second column called LineID and make that increment per record.
You cannot do that with an auto incrementing field. If you want to do that you will have to write a trigger to popluate the field based on a process you write. The process will have to make sure it includes multiple row inserts and can handle race conditions. WHy do you want to do this? Are you planning on letting customers reorder the shopping cart ids. THey don't need to ever see the id so it should not matter if the first on is 1 or 7.
I do want to point out that while DBCC CHECKIDENT ([Table_Name], RESEED, 0) might technically work, it requires the user to be a sys_admin or db_owner or db_ddladmin. None of these are roles that should ever be assigned to the user who logs in from an application to do data entry.