Im building a shop and each product has a unique set of attributes.
the product db looks like this:
products_id, relation_id, product_name, description,
price, glass, shipping, img, cat, subcat, model
Since every product has several (~40) different attributes unique to that product only ive created a second table to store them.
products_id, att_name, att_val, att_head, att_standard, att_order
This works fine, because there will never be two unique rows.
The problem, however, is when i need to modify the attributes content.
using MySQL Workbench i can modify a row using something like
UPDATE product_attributes SET att_val='1500' WHERE products_id='112' AND att_head='threshold'
This however, doesn't seem to work when i update from my php script.
Is there an easy way to modify the table to support updating?
Im well aware of the stupidity not having an unique column.
But im not sure how to make the two tables relate.
Where should i store the unique id of the attributes?
One choice,
add a primary key "auto_incremented" into the product_attributes table...
ALTER TABLE `product_attributes` ADD `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
This Id is just for CRUD (Create Read Update Delete) task.
The only relation you can have between your two tables is the products_id wich allow you to have few product_attributes for one product
Since 1 product has more than 1 unique attributes that u store in a second table, you should use the ID of the table product and store it in the second table with the attributes.
Hope this is what u need?
Related
I have been looking around for answers for countless hrs and could not find much.I am making a food project and I was wondering if there is a way to prevent 2 items with the same name from colliding each other with their 2nd table.To make things clear I have 1 table which is
CREATE TABLE cuisine (id int,dish_name varchar(32));
//2nd table CREATE TABLE ingrediant (id int,dish_name varchar(32),ingrediant);
I was wondering if lets say 2 people posted dish_name = pizza with different ingrediants,when i left join how would the tables know which one to join because the only thing that are matching is the dish_name.
I know i could add the ingrediants to the first table but lets say i am adding up to 50 ingrediants and other items it is too much stuff.
An example i can use is Ebay.
If 2 people post items with the exact same name,when the item is clicked how does it know which info it is for.Hope it is clear
If you would model your relations correctly, you don't have that problem.
You would have to define a foreign key in the 2nd table, and the foreign key would have to refer to the primary key (or at least some other uniquely indexed field or combination of fields) in the 1st table. If you plan on using dish_name as a foreign key in the 2nd table, that would necessarily mean that dish_name would have to be unique in the 1st table.
This is probably a bad idea, so it's better to create a dish_id foreign key column in your 2nd table and get rid of the dish_name column.
In my database I have one table that contains a complete list of products, and another table that contains the same list of products on the x-axis, with a list of customers on the y-axis, where the value for each product can be 1 or 0 depending on whether that customer can view that product. My SQL looks like this:
SELECT products.product_code, products.product_type, products.product_category, products.product_title, products.product_description
FROM product_lists
INNER JOIN products
ON product_lists.product_code=products.product_code
WHERE product_lists.customer="1"
ORDER BY products.product_code
My problem is that I would like to create a view of this result for each customer to use as that customers product table, however when I create it I get the message "This table does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available." even though the product_code field is set as a primary key in both the products table and the product_lists table.
How can I create a join/view that uses the primary key from the table(s) it was created from? In short I would like the product_code field to become the primary key of my view.
Thanks!
I think the problem is the join. You can fix this by moving the condition to the where clause. MySQL doesn't allow subqueries in the from, but it does in the where:
SELECT p.product_code, p.product_type, p.product_category, p.product_title, p.product_description
FROM products p
WHERE EXISTS (SELECT 1
FROM product_lists pl
WHERE pl.product_code = p.product_code AND
pl.customer = 1
)
ORDER BY p.product_code;
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm pretty new to PHP and databases and I'm having some problems creating the database I need.
I'm hoping that someone here can help me! Probably a very stupid question...
What I'm trying to do is create a very simple movie library and creating tables from within phpmyadmin. I have not started writing any PHP code for this yet, I just want to make sure I created the database tables correctly.
I´m doing one table (MOVIES) with the fields:
ID, TITLE, DIRECTOR, YEAR and CATEGORY_ID.
This table should have a relation to another table for categories. So the CATEGORY_ID field in the first table should be connected to a table that stores the categories.
I have problems with the CATEGORY_ID field. Since the first ID field is set as primary and also Auto indent I can´t have that on the CATEGORY_ID field so when I add movies as a test from within phpmyadmin I just get the Id number 0 on category id.
Just to show the table layout that I've done:
MOVIE TABLE - ID (primary key, auto indent), TITLE, DIRECTOR, YEAR, CATEGORY_ID
CATEGORY TABLE - CATEGORY_ID, CATEGORY
You'd need to make a little more study to get complete help. I'd suggest you do some googling and read articles like this one:
http://www.sitepoint.com/mysql-foreign-keys-quicker-database-development/
[EDIT]
Thanks for taking the time to edit your question and making necessary changes:
Sample Database Schema
CATEGORY TABLE
CATEGORY_ID int auto_increment (11) (primary key)
CATEGORY
MOVIES TABLE:
ID int auto_increment (Primary Key)
TITLE,
DIRECTOR,
MOVIE_YEAR
CATEGORY_ID int(11) (Foreign Key)
Now you see what I mean by a foreign key; The above schema is based on your question and sample. I've only renamed the year column. I'm careful words or names that might conflict with reserved words. I just called it MOVIE_YEAR. TITLE field is not unique. There isn't a need for that. To prevent duplicates if you want, you could query the database for a match. But seeing two movies could have same names, their ids should set them apart.
Mysql
You don't need two auto increment columns in a table. Begin by creating category table and loading the values for categories. Id column could be auto increment or not. If it isn't, you'd have to manually provide the ids. Then create the movies table. You are doing a one to many relationship. Meaning, one category could match many movies in the movie table. So having 0 there isn't an error. That issue would be solved by your PHP script. The column is possibly set to not null, so a default value is provided.
The PHP Scripting
In PHP, create your code to pull values from category table into something like a dropdown select or clickable links and pass them to a hidden form element. Using a select could be quicker (see example below). The select and other form elements needed to add a new movie would be in one html form. So when you submit the form, it adds the data into the movies table. That data would carry with it the category_id that the user is inserting. This would replace the 0 you now have.
Creating the Select Dropdown
0 would happier if you had set the category_id column to not null. Nothing strange. During updates, the same process is repeated. Please note that the select would have a special format as shown below:
<?php while($row = mysqli_fetch_assoc($connection, $recordset)){ ?>
<select name='category_id' id='categories_id'>
<options value='<?php echo $row['category_id'] ?>'><?php echo $row['category_name'] ?></option>
</select>
<?php } ?>
These value should come from your db and populates the select menu. This should get you started. So try working on the PHP side now with the database you currently have. If you have a PHP code, you could show it so we all tweak it.
Forget the up and down votes, we're not in a competition in here. It's called reputation. We seem have a lot of it. :)
Displaying Records
Displaying is the tricky part. There you would need a join. Something in the line of:
$sql='SELECT * FROM movies LEFT JOIN category ON movies.category_id = category.id';
$recordset_all = mysqli_query($connection, $sql);
Something like that would give you all the records on movies and matching records on category. That way, you'd have the category name to work with all movies. And since you don't seem to have column name conflicts, everything should be fine. If you want only matching rows, take out the word LEFT, so movies that don't have category_id matching the foreign key in movies would not be selected.
Sub Headings
Your case would get complex if you intend to load rows in subheadings of category names. In which case, you would have to do two database selects and create a function to call and return corresponding movies. There could be a better way. We'd fix that when you come to that part if necessary.
Here's is an example of a very common database design of a one-to-many relationship. Notice CATEGORIES has a primary key that is used to relate MOVIES, which employs the FOREIGN KEY statement. Notice also that TITLE must be unique (I am presuming).
create table CATEGORIES (
ID int not null auto_increment,
Description varchar(100),
PRIMARY KEY (ID)
);
create table MOVIES(
ID int not null auto_increment,
TITLE varchar(30),
DIRECTOR varchar(30),
YEAR datetime,
CATEGORY_ID int,
PRIMARY KEY (ID),
UNIQUE KEY (TITLE),
FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORIES(ID)
)
you may be looking for INNER JOIN
SELECT ... FROM table1 t1
INNER JOIN table2 t2 ON t1.key = t2.key
WHERE ...
more information on table joining can be found Here
Remember to prefix your column names with the alias of the table (in this case the aliases are t1 and t2
Hope this helps.
Edit: here is a sample update and select query that you can use to update categoryID and later select using inner join
UPDATE MovieTable
SET categoryID = (number here)
WHERE (use unique identifier like ID or Title if it is unique) = (value)
SELECT my.Title FROM MovieTable mt
INNER JOIN CategoryTable ct ON mt.categoryID = ct.categoryID
WHERE ct.categoryID = (value)
I'm using phpactiverecord for this project, I have this db structure:
Tables: Tickets, Labels
I'm currently using a has_many association for these two tables: "Tickets has many labels", however I need to assign each label to different ticket, which is not possible at the moment without having duplicated rows with different id and ticket_id values.
Any idea on how to achieve this ?
Cheers
You need create additional table
table Label2Tickets (`label_id`, `ticket_id`, PRIMARY KEY (`label_id`, `ticket_id`)
\* there you may store date of create, status and etc *\
`created`, `status`, `user_id` ... )
And update existing AR and create new AR for this table:
Ticket HAS MANY Label2Tickets where Label2Tickets.ticket_id = Tickets.id
Label HAS MANY Label2Tickets where Label2Tickets.label_id = Labels.id
tbl_product
Name | Creator | UID | Salerank
tbl_price
Supplier | Price | UID
I want to insert a product and then insert multiple prices into a seperate table. How is it possible to ensure that both tables had the same UID ideally an auto increment field? I will be using PHP alongside MySQL.
Thanks,
J
Make UID an auto_increment primary key on the products table, but just a regular primary key on the prices table (no auto_increment). After you insert itnto products, use the PHP command mysql_insert_id(). This will get the ID generated from the last query, which will be your UID generated on the products table. Assign it a variable and use it in your insert statement on the prices table.
http://php.net/manual/en/function.mysql-insert-id.php
Use a GUID for the UID, or better, insert your products and the insert the prices using e.g. the name of the product (assuming unique) to look up the relevant product UID.