Why is there a staff_typ_id attribute? - sql

I'v created a ER model in data modeler and one of the entity is automatically setting a primary key even though I dont want a primary key to that particular entity.. here is a screenshot As you can see on logical model there is no staff_type_id
but the relational model has staff_type_id
why is this happening? its creating a problem while i implement the database. Please help :(

I think there's no problem in that, if you notice you are linking the two tables with a relation of 1:N and you can't link entities that don't have primary key. it's the best to create a primary key for every linked entity. How you can link between entities without foreign key and primary key?!
generally, it's recommended to give every linked entity primary key.
I don't understand how this is a problem! can you explain more?

Related

It's possible to detect a many-to-many relationship on a rlinq file?

I am working on a project manually mapping entities from a SQL db. My primary source of information about columns, keys, relationships, and others is the rlinq generate by Telerik from the db.
I wonder if it's possible to detect a many-to-many relationship based on only in this file.

Entity Framework one-to-one relationship between multiple tables

I have three entities that need to be linked. In my scenario, I have three tables users, stores and accounts.
Both users and stores can have zero or one account and each account should be either for a store or a user (see image below).
I need a one-to-one relationship between user-account and store-account. Since one-to-one relationship force the model to use one key, the user and store Ids might have conflict.
Is there any solution for this in Entity Framework or do I have a flaw in my design?
It would be much easier to use intermediate tables user_account and store_account, that would store only keys. This way you can enforce any logic you want.

many-to-many database

I'm trying to create a database to analyze the configurations of my servers.
But i have many services that can run on many servers (for failover/load balancing). Also, the configuration of a same service can change from one server to an other, this is why I can't just have a service table
I tried to link the different tables using a single table that tie them all together. I think i'm in NF3 but i'm not 100% sure.
Is that a "valide" database design? database design
I fear that the request to find stuff in the database are going to be a bit complicated.
thank you
It would help more if you showed the actual db design. But...
If you have two tables associated in a many to many relationship, you will need a table in between them that represents the relationship. Tables normally represent entities in the real world, and foreign key represent the relationships. But in a many to many relationship you need a table to handle that complexity.
That table represents and could be called ServiceRunningOnServer and it's primary key should be the combination of a ServiceId (with a foreign key to Service.Id), and a ServerId (with a foreign key to Server.Id).
Any setting for a service that is across the board (not server specific) is an attribute of the service entity and so belongs in the Service table. But any setting that is specific to the server it is running on, is an attribute of the relationship between that service and that server and so it belongs in the ServiceRunningOnServer table.
Yes, this is a perfectly normalized db design. And actually it is the design with the most optimized complexity. Meaning that other designs might make some things easier, but they will also make other things harder. In the end, and in the total sum of things, other designs will over-complicate things. This design will keep the total complexity of adding, updating, reading and deleting data from your database to a minimum.

Foreign Keys cascading in JPA or database Schema

I have a separate script that creates the database and tables for each database that we are supporting. I am using JPA to manipulate the data in the database, but JPA does not create the database or the tables.
I want to add a foreign key to a new table with a cascade property so that when a row is deleted in the parent table, the corresponding rows in the child table are also deleted.
I am aware of the annotations necessary to do this in JPA, however I can create the foreign keys and the cascade statements in the script I am using to create the databases.
My question is, since I am using a separate script to create the database tables, can I just add the foreign key / cascade statements in the script and then ignore all of the JPA relationship annotations? Is there advantages/disadvantages to adding this information in both the database script as well as in the JPA code?
You should always have a 2 level check. if you do not use the features of JPA, then it's a big waste of the functionality JPA provides. you should actually make sure that you JPA relations match your DB relations as closely as possible. It will help you a lot as JPA can cache data and even prevent unnecessary calls to DB.
eg if u have a not null constraint and you persist with no JPA constraint, your DB has to do all the work and throw the exception back.
normally in an application, the network and DB are the slowest factors in the app. so you should try mimicking the constraints in JPA to avoid unnecessary overhead.
also using such constraints you can form bidirectional relationships and have collections of associated entities and many more such advantages.

SQL Relationship Error

I have a database and for the sake of simplicity, lets say it has two tables. One is Employee and the other is EmployeeType. In the Database diagram i am trying to build a relationship of the two tables but it gives me the following error....
'EmployeeType (HumanResources)' table saved successfully
'Employee (HumanResources)' table
- Unable to create relationship 'FK_Employee_EmployeeType'.
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Employee_EmployeeType". The conflict occurred in database "TheDB", table "HumanResources.EmployeeType", column 'iID'.
I don't know what is wrong with it. please help...
Ouch, so many downvotes. At least he bothered to post the error message. As Daniel A. White said above, you likely already have the foreign key you're trying to make. Your tag says you're using SQL Server 2008, so I'm going to assume you Microsoft SQL Server Management Studio. If that is the case, one mistake I used to make (and sometimes still do!), is I go to create a new relationship by right-clicking any of the columns in the Design view of the table I want to make a foreign key, and hit Relationships. In the Foreign Key Relationships menu that appears, I hit "Add" at the bottom, and then expand the "Tables and Columns Specification" section. Then I hit the "..." next to "Tables and Columns Specification," which lets you choose what columns you want to relate.
Now the problem here that I sometimes run into is, if you don't finalize this relationship right here, you're setting yourself up for situations like this. The reason is, is that when you hit "Add" a few steps back, you already created a foreign key on this table. Even if you cancel out and close everything, the table retains that foreign key. If you experimented around with this, you may have already accidentally created the relationship, you just didn't know it then.
I realized how often I made this mistake when I first started, because I looked back at my very first project a year after I began working with SQL Server 2008, and the Foreign Key Relationships are funny, because I have a ton of foreign key entries pointing to no where in every table, where I obviously was just experimenting as I was learning.
Another thing, just to cover some more bases here, is that you mentioned you were doing this in the Database Diagram view. Don't think there's anything wrong with that, but I've found that sometimes doing things via the GUI, it can be kind of flakey. By that, what I mean is, sometimes I will want to make a really small and minor change, but the GUI will tell me that I can't do that kind of change after the table has been made. But then if I just do exactly what I want to do via a typed out query, it runs just fine.
The ultimate test to try to see if you have your desired functionality is to simply try putting in a record that would violate the foreign key restraint. If it goes through, then the relationship isn't there or it's not set up properly. If it fails, then you know you've already got what you want.
I think you got some downvotes because this isn't a very complicated problem/should usually be easily solvable on your own, especially with Google. Since it's not, but you bothered to post anyway, I'm assuming you're somewhat new to this, so I just wanted to throw out my advice as a fellow newbie. Don't let the downvotes get you down, just maybe next time ask a little more concrete question other than "what's wrong?" For instance, "This error seems to be telling me the key already exists. Can anyone tell me how to check and see what relationships already exist? I don't remember making this relationship, how could this have happened?" etc.
A simple answer: you have data in your tables which would break this relationship, so you cannot create the relationship.
In this case, check the Employee table and look for rows where the id for EmployeeType (EmployeeTypeId?) is either NULL or illegal (not to be found in the EmployeeType table). Fix these rows and the relationship will save OK.