I have a situation where I am trying to get a value stored in one table, but have to go through several other tables on the same database to get the correct result.
Table 1 has a PK ID I can query, this PK will give me a new FK in table 2, this will give me a key to table 3 and table 3 will give me key to table 4 that has the value stored against the PK in table 1. If this made sense? I can not do something with the tables or the database, so I need to find a way to select the value in table 4 from the primary key I got in table 1.
Any ideas?
Edit 1 :
I will try to explain better. I have a filepath located in table 4. To get the correct filepath I need to first find the a project id in table 1. That same project id is a FK in table 2. In table 2 I need to find another id (let's call it "customer id") using project id as FK, the customer id is a FK in table 3. In table 3 I need to find purchase id using customer id as FK, the purchase id is a FK in table 4. With the correct FK (purchase id) in table 4, I am able to get the correct filepath. The filepath that coresponds with the project id from table 1.
I am using ASP.NET (Entity Framework) and a SQL database. I was thinking to use Linq, but is somewhat confused about how to do it. Join several tables or try to get the filepath that way?
Did this make it more clear?
use Linq join Query,..
One example, See here
Entity Framework Join 3 Tables
or search LINQ join in Google,you can find the solution yourself.
Related
i'm not really an mysql guy more like a php guy :)
I have an issue with copying values from one table column to another table column.
The trick is that the data should be copied only if condition is matched. So basically i want to transfer categoryID from one table to another if postIDs are the same.
i have two tables news and news_in_category
in news table i have the following columns (id, title, categoryID)
in news_in_category i have the following columns (newsId, newsCategoryId)
So i want to move newsCategoryId to categoryID if newsId is the same as id.
For example if news table id=99 it should look up in news_in_category table find the newsId with value 99 and copy the newsCategoryId value to news table categoryID with id 99
Hope it makes sense to you :)
Thank you!
Try:
UPDATE news n
JOIN news_in_category nic ON n.id = nic.newsId
SET n.categoryID = nic.newsCategoryId
It looks like you might have a redundant functional dependency: newsId -> categoryId. If so, you could drop the news_in_category table without any loss of information, after running the query above.
I have a problem with this very simple sql statement:
SELECT * FROM video NATURAL JOIN user
I get a warnig ins phpMyAdmin, that there is no unique column in the selection and i cannot edit any values.
But the id column should be unique...
My database structure:
structure 1
structure 2
id in video is a primary key.
id in all other tables are foreign keys pointing to video.id
Could you tell me what is wrong in my structure?
Thanks!
Benjamin
SOLVED:
I created a view from my SQL selection. Inside the view I was able to do changes.
I am creating a site that is sort of ecommerce-ish. I want to give my users a perfect search ability using specific attributes that differ from product to product. I plan to create 1 products table storing the basic information that is shared among products i.e Name, Description, Price and a few others. Then I plan to create several "details" table say categories_computers with columns Processor, HDD, RAM, etc and another table say table_shoes with columns MATERIAL, SIZE, GENDER, etc.
I am new to Mysql but not to the concept of Databases. I don't think I will have a problem storing this data to each table. My issue comes about from reads. It won't be hard to query a product id but I think it would be extremely wasteful to query all details tables to get the details of the product since 1 product can only have 1 details.
So my question is how can I store a reference to a table in a column so that a product has say ID, Name, Description, Price, Details_Table_ID or something similar to save on queries. Do tables have unique ids in Mysql? Or how does the Stackoverflow community suggest I go about this? Thanks.
EDIT
Silly me, I have just remembered that every table name is uniques so I can just use that, so my question changes to how I can write a query that contains one cell in a table A to be used as a reference to a Table name.
Don't use separate details tables for each category, use a generic details table that can store any attribute. Its columns would be:
Product_ID INT (FK to Products)
Attribute VARCHAR
Value VARCHAR
The unique key of this table would be (Product_ID, Attribute).
So if Product_ID = 1 is a computer, you would have rows like:
1 Processor Xeon
1 RAM 4GB
1 HDD 1TB
And if Product_ID = 2 is shoes:
2 Material Leather
2 Size 6
2 Gender F
If you're worried about the space used for all those attribute strings, you can add a level of indirection to reduce it. Create another table Attributes that contains all the attribute names. Then use AttributeID in the Details table. This will slow down some queries because you'll need to do an additional join, but could save lots of space
Think about just having a single ProductDetails table like this:
ProductDetailID (PK)
ProductID (foreign key to your Products table)
DetailType
DetailValue
this way you do not have to create new columns every time you add a new product detail type. and you'll have many ProductDetail rows for each productid, which is fine and will query ok. Just be sure to put an index on ProductDetails.ProductID !
Since this is an application so you must be generating the queries. So lets generate it in 2 steps. I assume you can add a column product_type_id in your Product table that will tell you which child table to user. Next create another table Product_type which contains columns product_type_id and query. This query can be used as the base query for creating the final query e.g.
Product_type_id | Query
1 | SELECT COMPUTERS.* FROM COMPUTERS JOIN PRODUCT ON COMPUTERS.PRODUCT_ID = PRODUCT.PRODUCT_ID
2 | SELECT SHOES.* FROM SHOES JOIN PRODUCT ON COMPUTERS.PRODUCT_ID = PRODUCT.PRODUCT_ID
Based on the product_id entered by the user lookup this table to build the base query. Next append your where clause to the query returned.
We have a unique database structure and would like to use the Entity Frame.
Currently we have a large number of tables that contain the same schema. The table name has a reference to the primary key in another table. Take for instance
Table: Customer
Primary Key: CustomerId
Table: User_[CustomerId]
For example the Table Customer has the Primary Key of CustomerId and say has data
CustomerId
1
2
3
4
5
The are multiple User tables (1000's of them) in the database. The table name would be
User_00001
User_00002
User_00003
User_00004
User_00005
each of the user tables have the same columns.
This is something that we cannot change.
We would prefer not to import to import the thousand plus tables into the designer.
Is there a way in the Entity Framework that we could map a single entity to a specific table either in the linq call or in an individual context reference?
For instance something like this?
var myUser = db.User(1020).FirstOrDefault(i=>i.UserId == 134);
or
db.UserID = 1020;
What would the best practice using an ORM be for this?
I have two tables:
Friends :
id name
1 jhon
2 peter
Teammates:
id name
3 juan
i am looking for a way two auto increment the id of the second table (teammates) according to the first table ( Friends ).
When I add a new register to Teammates it never match with an id of Friends
I think this is not good practice. If you do so, you are introducing an implicit functional dependency between both tables outside of the declared design. If you want to it anyway, you can use a trigger to asign the value instead of making the column autoincrement.
I would suggest to have a table for all people with the real autoincrement id, then you can use several approaches:
i) Make your two actual tables take id values as foreign keys of this new table, with the corresponding integrity constraint.
ii) Simply create 2 views of the table: One for friends, other for teammates.
Table_Friends: (id, name, role)
View_Friends: Select id, name from table_Friends where role = value_for_friend_role
View_Mates: Select id, name from table_Friends where role = value_for_teammate_role