Should I use AI on these fields in MySQL? - php

I have this db below. I wonder how I should use the ID to identify each record.
Everything is connected from the classified_table!
Two Questions:
Should I use AI on every PK in this case?
Could somebody give me the FULL code for selecting an entire classified from only an ad_id ("bmw_330ci_8939483" for example)?
I am new to normalized db and making a good database work, so detailed instructions is very much appreciated... Also if you notice any 'wrongs' in this db please let me know.
category table:
cat_id (PK)
cat_name
category_options table:
option_id (PK)
cat_id (FK)
option_name
option_values table:
value_id (PK)
option_id (FK)
value
classifieds table:
classified_id (PK)
ad_id (VARCHAR) something like "Bmw330ci_28238239832" which will appear in URL
poster_id (FK)
cat_id (FK)
area_id (FK)
headline
description
price
etc....
posters table:
poster_id (PK)
name
email
tel
password
area table:
area_id (PK)
area
community
Thanks

I would auto-increment (AI) on fields that I would do majority of searching by. AI makes it easier to return results, but there are performance issues where it can slow down the database.
In regards to the query, I am not exactly sure what you would want to return, but this query returns the classified_id by the given ad_id
SELECT classified_id FROM classifieds_table WHERE ad_id = "bmw_330ci_8939483"
To perform a single insert into your classifieds table and column ad id the value audi a4 would be:
INSERT INTO classifieds_table ad_id VALUES "audi_a4"
Or multiple inserts using the same table, multiple fields and multiple values would be:
INSERT INTO classifieds_table (ad_id, poster_id) VALUES ("audi_a4", 10)
Notice I left out classified_id because if you choose to auto-increment it will automatically assign a value without you explicitly assigning one.
Check out MySQL :: Building a Database-Driven Website using PHP and MySQL for more tutorials.

Using auto-increment for your PKs sounds sensible, because it sounds like you already want to use a surrogate key and auto-increment makes the inserts very straightforward. Worth taking a look at this discussion about how to pragmatically choose what primary key to use.

Related

Users History and Users saved database schema design

I want to store into database the visited users and favourite users (when a user clicks on hearth icon). So, I could create 1 table, users_activities like so:
Users_activities:
id_users (pk)
id_users2 (pk)
activity (varchar or enum, it'll contain "history", "favourite") (pk)
created (datetime)
For the history.php or favourites.php pages, I'll select all, where id_users = X and activity = "history/favourite", join users table. For insert and update, I have a composite primary key, users, users2 and activity. If I'll find a users X, users2 Y and activity Z, update the row..else insert.
Otherwise, I could create 2 separated table, users_history and users_favourite, like so:
Users_history
id_users (pk)
id_users2 (pk)
created
users_favourite
id_users (pk)
id_users2 (pk)
created
it's a simple design and SQL is tiny simple than first form. Which is better? I use php and mysql.
"History" sounds like a log of things. "Favorites" sounds like a current set of values. They have two different sets of usages and properties; do not put them in the same table.

MySQL: Ignore insert if match two columns

I need a help with a PHP/MySQL issue. I have a table named users and other named relationships.
users
--------------
id (PK)
name
email
etc
relashionships
--------------
id (PK)
id_user (FK to users.id)
id_friend (FK to users.id)
rating
I'm trying to INSERT multiple relationships but I don't want duplicated entries. I want to ignore the current row if the row is duplicated. I can't use the IGNORE statement because the id_user and the id_friend columns aren't unique. A user/friend may have multiple relationship rows.
Any tip?
You can create a unique key on the id_user/id_friend tuple. Neither of them are unique, but their combination is.
See multiple column indexes on the documentation.
Thanks amenadiel, I found that solution here and worked for me!
CREATE UNIQUE INDEX relation ON relationship (id_user, id_friend)

How to link a row of MySQL database table to another row in another table

I know it makes little sense... and i'm new to using MySQL...
What i'm trying to do here is, link one tables row to another tables row...
for an example there are two tables..
one table is for user registration and same table is used for login as well...
and the next table is for user posts.. like status updates and all...
here is how i want it...
user_log_info:-
id ( primary )
firstname
lastname
username
email
password
posts:-
id ( primary )
userposts
posted_by
date_post
so as you can see, i want the user_log_info tables username to be automatically copied to posts posted_by row... And i have no idea how i can archive this...
You haven't given nearly enough information to give a full answer, but I'll do my best with what you've given.
Tables
+-----------------+ +-----------------+
| users_log_info | | posts |
+-----------------+ +-----------------+
| int ID (primary)| | int ID (primary)|
+-----------------+ | int posted_by |
+-----------------+
(I left off fields that are irrelevant to what you seem to want to do, I'm just simplifying it)
posted_by is an unofficial foreign key, or referencing the primary key of another table.
To insert, what you can do is along the lines of this:
INSERT INTO posts(...., posted_by) VALUES (...., user.ID)
Where .... is referencing all of your other information to insert
Then, to find information on someone who posted something:
SELECT * FROM users_log_info WHERE ID = Post.posted_by
Or if you want to find all posts by a user:
SELECT * FROM posts WHERE posted_by = user.ID
So, if Bob, who is User ID 3 wants to post "Hi", you might be able to do:
INSERT INTO posts(content, posted_by) VALUES('Hi', bob.ID)
And then when you are outputting the post you might do this:
post = (however you choose the post to put on the page)
userPosted = SELECT * FROM users_log_info WHERE ID = post.posted_by
print post.content + " posted by: " userPosted.Name
Essentially, the field "posted_by" is, to "posts" an arbitrary number, but you know that it links to, or references, a user. It does that by referencing "ID", which is the primary key of users_log_info, so that when you want to get information from users_log_info, is all you need to do is select the entry which has the ID that corresponds to "posted_by". I do recommend naming it something like posterID, however, for easier identification.

Insert Mysql with PHP - problem with foreign key insert

[project_select]
UserID (fk) | project_id (fk) | project_category_id (fk)
[project_category]
project_category_id | category
[projects]
project_id | projectname
[project_user]
UserID | Name
How can I insert Data with php in the tables project_category, projects and project_user, to get automatically the values in the project_select table with the FK's?
Update:
How can I merge this 3 queries into one line?
INSERT INTO project_category
VALUES (1,'Fruits')
INSERT INTO projects
VALUES (4,'Apple')
INSERT INTO project_user
VALUES (2,'Adam')
and get this values with this one query in the project_select table:
[project_select]
UserID (fk) | project_id (fk) | project_category_id (fk)
2 4 1
You can use the mysql_insert_id function to retrieve the id you just inserted.
mysql_query("INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')");
$person_id = mysql_insert_id();
So, do inserts on your tables, call mysql_insert_id() after each insert, store inserted IDs and finally use the IDs in the mysql command to create the join table.
I doubt that I understood your problem correctly. But if you want to have the values entered automatically in project_select table? Then how about using MySql triggers? But be sure you are well aware of the database internals before using it.
What are triggers?, Setting up triggers
But, please describe this -
problem with foreign
key insert?

MySQL database setup help

I am making a classifieds website...
I have these 6 tables:
Every category has sub-categories (or options) which you can see below.
Lets say the user wants to post a classified, and has entered all info into the forms necessary, and I am at the stage where I have to create the PHP code to actually INSERT the data into the database.
I am thinking something like this:
mysql_query("INSERT INTO classifieds (classified_id, ad_id, poster_id, cat_id, area_id, headline, description) VALUES ($classified_id, '$ad_id', $poster_id, $cat_id, $area_id, '$headline', '$description')");
But I don't know where to take it from here...
I think the posters table should not be like this, because how should I determine what the poster_id should be? Or should I set it to auto-increment?
Remember this, posters may not log in or anything, so there is no problem with one person having multiple poster_table records if you know what I mean.
classified_id is a random unique value generated by PHP so that is always unique.
Please guide me! I don't know how to link the tables together correctly.
If you have any Q let me know and I will update this Q!
category table:
cat_id (PK)
cat_name
category_options table:
option_id (PK)
cat_id (FK)
option_name
option_values table:
value_id (PK)
option_id (FK)
value
classifieds table:
classified_id (PK)
ad_id (VARCHAR) something like "Bmw330ci_28238239832" which will appear in URL
poster_id (FK)
cat_id (FK)
area_id (FK)
headline
description
price
etc....
posters table:
poster_id (PK)
name
email
tel
password
area table:
area_id (PK)
area
community
You've got the right idea already. When someone creates a post, and enters their personal info, FIRST insert the "poster" record into the posters table. The "poster_id" primary key for that table should be an auto_increment field.
Next, get the ID of the new poster you just created using PHP's "mysql_insert_id". That integer value will be the number you put in the "poster_id" foreign key field in the "classifieds" table.
You should usually set the primary key to an auto-increment field.
When you have linked tables and you need to join on the id, you can first insert into the main table and then use the function mysql_insert_id to retrieve the id of the element you just inserted. You can then insert into the other table using this value as the foreign key.
This is a very standard way to do things, so it should be fine for you.

Categories