user1200819
user1200819

Reputation: 63

Advise on creating relationship in mysql with phpmyadmin

I was wondering if anyone could help me with this as I am novice and very confused

I want to able to assign customer to user (1:M relationship) but I cant figure how to do this in mysql. What I would like to do is in the customer table add the multiple id's of the user and then create a table where you can assign a new customer to a certain user or search how many customer a particular user has etc...

I have been looking at creating relationship but I keep getting error in phpmyadmin

Can anyone advise how to do this properly in the db please

Thanks for all your help

Upvotes: 6

Views: 10651

Answers (4)

Mateus Schneiders
Mateus Schneiders

Reputation: 4903

If your problem is creating the relationship in PHPMyAdmin, i should recommend you the following:

  • Create the user table with the user_id as PK.
  • Create the customer table adding the user_id column and defining it as an INDEX.
  • Go to Customer table structure, and click on "View Relations".
  • On the user_id field, choose the User_table.user_id.

This way, an user can have zero or many customers and a customer will always belong to a single user.

Upvotes: 6

Jonathan Weatherhead
Jonathan Weatherhead

Reputation: 1580

You might be interested in trying MySQL Workbench It's a visual MySQL database modelling tool created and supported by Oracle/MySQL guys. Not only does it tidy away messy SQL code (but you should still understand what's going on) but seeing your database model visually is often a big help.

Upvotes: 0

Randy
Randy

Reputation: 16677

user
------
user_id
other_stuff

customer
--------
customer_id
other_stuff


user_customer
-------------
user_id
customer_id

Upvotes: 0

Ofir Baruch
Ofir Baruch

Reputation: 10356

What about the following

> +Users
> -id
> -email
> -full_name
> 
> 
> +Customers
> -id
> -user_id
> -name
> -phone

So user_id will contain the id of the user which this customer belongs to.

Upvotes: 1

Related Questions