I am trying to make generalized master search for my MS-SQL data. Let me explain what I had done till now:
Firstly, I fill one combo box with all the table names.
Then, fill second combo box with columns of selected table from first combo box.
I run general database query like select * from combobox1.text where combobox2.text='someValue'. Everything works fine for me, but I am not able to get foreign key data which is primary in another table.
What i need
suppose Table A has columns: 1 primary key, 2 name of id. And Table 2 with: 1 as primary key column, 2 as address column, and 3 as foreign key column. When I run select * from table2
I get result of foreign key in terms of id, I want value of table1.2 column value.
Why i am asking
This because with this type of system, I can query each and every row in database without manual coding for each table.
Images to explain better
Employee Master Table
Attendance Table
Result i want
Related
First, let me say that I am a newbie and that I have read many other posts with the same problem. I have a table called "AllPeople", and in that table I have an integer column called "Ethnicity", which I want to be a foreign key that points at a record in my "RefEthnicities" table. I keep getting the following message:
Unable to create relationship 'FK_AllPeople_RefEthnicities'.
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_AllPeople_RefEthnicities". The conflict occurred in database "MVC-Cemeteries-Dev", table "dbo.RefEthnicities", column 'ID'.
I set up the relationship in the "AllPeople" table and told it that the primary key is the ID column in the "RefEthnicities" and the foreign key is the "Ethnicity" column in the "AllPeople" table. What am I doing wrong? My RefEthnicities table is new; no data in it.
While in design mode I set the ID field in the "RefEthnicities" table set as primary key by clicking the small box to the left of the name "ID", and down below in this same window in the column properties tab, I told it to set the index specification to "yes".
I am sure it is something simple that I am doing but I can't figure it out.
Error Message
Constraint Folder
Setting Up PK FK Link
As my limited information in the question, there 2 possibilities
NULL or Blank '' value for column Ethnicity in table AllPeople
SELECT A.Ethnicity,A.*
FROM dbo.AllPeople A
WHERE ISNULL(A.Ethnicity,'')=''
Some values column Ethnicity in table AllPeople don't have parent in column ID in table RefEthnicities
SELECT A.Ethnicity,R.ID, *
FROM dbo.AllPeople A
LEFT JOIN RefEthnicities R
ON A.Ethnicity=R.ID
WHERE R.ID IS NULL
If you get any rows in two queries, then you need to fix data in column Ethnicity in table AllPeople.
Read
Ok this still makes no sense. If I create two brand new tables with the following:
table1
ID primary key int not nullable
value varchar
table2FK
table2
ID primary key int not nullable
value varchar
and in table1 I make a relationship between table2FK and Table2.ID, it works perfect with no data saved in the tables. If I use the exact same process in my AllPeople and RefEthnicicties tables, I get the error. This makes no sense. What am I missing?
adam
That fixed it. many thanks. I had a record in my AllPeople table for ethnicity that had a value of 0. Since I didn't have a record in the RefEthnicity Table with an ID of 0, it was telling me that I couldn't do this.
adam
I created a new table called Expirations that has it's own unique id, with indentity incrementing, as it's primary key. I also want this table to pull in data from two other tables, so I created a column for the InsuranceId and the LicenseId, making both columns foreign keys to connect them to the data (aka ID) from their respective tables (Insurance and License).
Any idea why my data is not automatically filling in for my Expirations Table? I believe it was created as "many to one" for all of these columns. Not sure if that is correct either, as I want the Expiration table to list all insurance id's and licence id's.
Anyone know what I am doing wrong here?
Foreign keys don't mean that a table gets filled automatically.
Let's say you have a person table and a company table and a company_person table to show which person works in which company. Now you insert three companies and four persons. That doesn't mean that the company_person table gets filled with 3 x 4 = 12 records (i.e. all persons work in all companies). That would make no sense.
Foreign keys merely guarantee that you cannot insert invalid data (i.e. a person that doesn't exist or a company that doesn't exist) into the table in question. You must insert the records yourself.
Basically your expirations table is like a fact table and you want to load data from the dim table which is Insurancetable ( InsuranceId as primarykey) and Licencetable ( Licenceid as PK).
But if you do not have any combination of InsuranceId and licenseId how do you know which insuranceid belongs to which LicenseId.
If your expirations table is empty then you need to first do cross join between the insurancetable and licensetable which is cartesian result but you do not want to do that as it does not make sense in real world.
Hope my explanation helps.
Hello i have a question i assume mainly about SQL server functionality. Im building a test database and i have stumbled a problem when i try to insert data into my tables.
picture 1 shows the error message i get when trying to add rows.
https://i.stack.imgur.com/GW3C2.png
Picture 2 shows all relations in the database
//i.stack.imgur.com/7BhHa.png
picture 3 shows the table i am currently trying to update
//i.stack.imgur.com/3JqtA.png
In the table i have a combined primary key ("SDat" and "Kurs") The error message i get implyes that primary key must be uniqe, but what dont understand is since i have a third column "Elev" which makes the row uniqe, why wont SQL server me insert this row to table? I have tried making the same database in Acess and it works so i assume problem is something in SQL server
Regards Robert
A Primary Key by definition means the value must be unique. So if you have a combined primary key on 2 fields, then that value on those 2 fields needs to be unique meaning it can only have 1 row. If you need to enforce unique values on the combination of 3 fields (SDat, Kurs, and Elev) then your PK needs to include all 3 fields.
If you really need to enforce a unique constraint across alot of fields in a table, I wouldn't use a PK to enforce that, but instead use a UNIQUE constraint.
ALTER TABLE tablename ADD CONSTRAINT constraintname UNIQUE (column1, ..., columnn)
Then you can create a different column for your Primary Key so that as you add columns and need to require those additional columns be unique, you don't have to edit your PK and rebuild your table.
I have a table say Table A
Col A is the primary key (identity).
Col b is a foreign key from some other table.
Now, for example I want to select the rows where colB = 2 , and insert the rows back into the table again but with a different value of col B say 5 . This can be done easily. Next once the rows are inserted again, I need to find the relation between the copies, especially.
Say new rows are inserted with primary key 6,7,8. Now I want to check which parent row (row with primary key value 1/2/3) has the same data as row with primary key 6. Similarly, I need to find relative for the row with primary key 7, 8. One way is to do the insert one row at a time so that we can say that row with primary key 1 is the copy of row with primary key 6. Another way to do is to join all the columns. I am trying to avoid all these to see if there is any easy solution possible.
Use SCOPE_IDENTITY() to get last primary key inserted value on your table. If another process could be modifying the table then do your update & query in one transaction.
I would like to have some explaination about the characteristics of a primary key in the table of such database.This is for vb 2008 express edition,since im new to this language,if this is true, as far as i understand about the characteristic in setting the primary key on each field for true.My question is if you are doing an updates/edit records in your table using the DataContext,if you setup the primary key for true of one of your field in the table it would edit all records in one datarow but if you put the primary key for true in all fields except one of them,all the records in the data column of that field which primary key is false could be edited.Basically its impossible to edit all records in the datarow and all the records in the datacolumn of the table in such one event.
Is there any further explaination about the characteristics of primary key in the table?
The purpose of the primary key in a database table is to identify the field (or fields) that make up a value that uniquely identifies each record on the table. A typical examples are CustomerID in a Customer table; each customer is given a unique ID, and this ID can be used to link the customer into other tables (such as an order table).
Sometimes there are tables where not one single field will contain a unique value for each record. In such cases more than one field can be set as the primary key. In those cases, the combination of values in the primary key fields should always be unique.
So, on the database level, this is not related to the possibility to edit the field or not.
Of course, wikipedia has some content on the subject.