Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Let' say I Have 2 tables, webpage table, and a keywords table. It's a many to many relationshiop, right? One webpage can contain more than one keyword, and one keyword can be part of more than one webpage.
Webpage table contain id field as PK, and few other fields. Keywords table contain id as a PK, and also a few other fields. Third table, a child table, should contain id fields from both parent tables? Is it posible to track many to many relationship, with no foreign keys, just declaring this 2 id fields in child table as UNIQUE?
With or without FK's, when inserting new keywords for example through PHP, how should I refer, to which webpage this new keyword belongs, webpage id in a webpage table, or a id in a child table?
I would do something like this...
Table1
Table_WebPage
PageID, PageName, Url,...........
Table2
Table_KeyWords
WordID, Word, .........
Table3
Table_PageKeyWords
ID, PageID, WordID
Dont know why you want to do it without FK, Having FKs will enforce the data integrity and stop garbage data coming into your tables.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm a beginner in Laravel and generally in relational tables and I'm not sure about using Laravel relations.
Imagine that we have a table which contains cars. each car has a country of origin. I think that I can insert name of the country in every record (car) and also I can create another table for countries and make a many to one relation in Laravel and set a country for each car in my table. what is the problem with the first choice?
I was thinking that the second one could be a better way to search throw cars by countries but we can do it with a 'like' statement in SQL.
I answer as an sql question since I don't use Laravel.
You need 2 tables: car and country. If you insert the name of countries on car table you have duplication of the name and in case of typo you have to update the country record misspelled and all car records with the same mistake. Moreover if you delete a country you could end up with an inconsistency: the country deleted may remain on car table.
You can use the like statement after doing a join of the two tables:
SELECT cars.*, country.name
FROM cars, country
WHERE cars.id_country = country.id
AND country.name LIKE 'A%'
(you find all the country starting by 'A').
I imagine the two table as:
cars(..., int id_country, ...) <- no country name here!
country(counter id, varchar(64) name, ...)
where cars.id_country references country(id): id_country is a 'foreign key' for 'primary key' country.id (you can find everywhere this definitions if you need them and how to declare them in every dbms).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to insert a column in a database table automatically using a variable.
I have a table named tbl_category, I want to let the user choose the sub category field by their own choice. So, from the dashboard, the user will insert 3/4 (or how many fields they would like to create) sub category fields.
I will take this value as $insert_sub_cat_count. So, when this info will hit the function named function save_category_info($data), the function will receive the value $insert_sub_cat_count as $data.
After that, this info have to implement in the table tbl_category and add 3/4 fields automatically according to the value $data.
If the user inputs 2, this will insert two columns automatically:
If the user inputs 3, this will insert 3 columns:
Is this possible? I don't know how to extend columns automatically or if there's any other way to do so.
This is a bad idea from the very start. As Raymond Nijland said above ("the same column names with increment are a SQL anti pattern.. you should check table normalization instead"), you shouldn't allow users to create columns with their desired name in your database. You should have the following tables:
user - id, name
categories - id, id_user, name
subcategories - id, id_category, id_user, name
So you will be able to link the category and subcategory to the user that created it. You don't need to create a separate column for each user.
If you're worried about speed you should add index for the subcategories table, for the column: id_user. In this way the search will work fast enough.
Insert is used to insert variables into an already existing column, you need to first create your column before filling it:
ALTER TABLE `tbl_category` ADD `sub_category_one` VARCHAR(100) NOT NULL;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to set up tables in a mysql database so that a member can be assigned a position based on their section, group and role.
So if i had a form with three drop down boxes in html like so:
<form>
<select name="section"></select>
<select name="group"></select>
<select name="role"></select>
<button name="setPosition">Set Position</button>
</form>
How do I set up the sql tables so that each drop down can show whats available from there respective table based on the previous selection, and how would the member be assigned to that position in the database?
Thanks,
Edit: Sorry from not being clear, I know how to use ajax to get query's from a database and populate the selects with that data. What I need is the sql tables to be set up so that the selects can be context based, so for example, If I select a value from 'section' the 'group' shows all the groups in that section, which it gets from said table.
The standard way to set this up would be like this, with a members table that holds the id's for the other related tables, it's called "normalizing" the data.
table name columns
--------------------------------------------------
members section_id, group_id, role_id
sections id, section_name
groups id, group_name
roles id, role_name
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm Using php and Mysql database.
which one is the faster way for select data from database
Input category id/*name* to each row of product table, then
select * from product table WHERE category name = something
or
Make a relationships table, search in this table first, then select
matched result from product table
and what about ID or NAME? Is it necessary to search for IDs, then convert them to NAMEs
which way is better?
Using php to convert category id to category name
Using category-name table to convert id to category name
Get category name from the prosuct's row in product table
Unless there is an obvious performance reason, you should stick to a normalized database design. In your specific case, you will probably have a worse performance by repeating the category name because you will end up with a larger table and will have to compare strings instead of a 4 or 8 byte integer.
Please don't worry about "fastest" without having first done some sort of measurement that it matters.
Rather than worrying about fastest, think about which way is clearest.
In databases especially, think about which way is going to protect you from data errors.
It doesn't matter how fast your program is if it's buggy or gives incorrect answers.
Ideally, you would build a table for you categories, and then use the primary key (id field) from that table in the product table for the category_id. You can then still do your search on the category_id field, but it will be closer to being normalized and you can index that integer field with better performance results than trying to index a text field.
It is faster to have a separate category table that lists the names. You put the category id in the product table that corresponds to the same category id in the category table. You don't need to "search in this table." You just use joins:
SELECT product_table.*, category_table.CategoryName
FROM product_table
INNER JOIN category_table ON category_table.CategoryID = product_table.CategoryID
WHERE CategoryName = ?
If you can select by CategoryID instead of CategoryName, it will be faster, because CategoryID would be your primary key.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a MYSQL Db that has a PROFILE-TABLE as well as a KEYWORD-TABLE which holds profiles and the other holds keywords associated with those categories.
Profile-Table
UserID
UserName
UserDept
UserPhoto
UserKeyword > indexes KeywordName (from Keyword-Table)
UserAssociations
Keyword-Table
KeyID
KeywordName
I need to make an association with the categories/keywords.
I want to add a hidden field (UserAssociations) onto my profile form which will display a hidden association where as when you click on a category via a link on the page, it will index first those that are associated. I have written this in PHP and use MYSQLI database.
I have never created associations before needing this. What would be the easiest way to achieve this functionality?
From what I gather, you wish to associate a user profile with keyword. What you need is another table to represent the relationship, something like this:
profile_keywords ( <UserID>, <KeyID> )
Hence, if UserID 4 has associated himself with keyword ID 3, you would have an entry in profile_keywords like this:
UserID, KeyID
---------------
3 4