IDJosh
IDJosh

Reputation: 109

SQL - Multiple owned items

I am making a MySQL Database. I've got a table containing basic information of people looking like this:

MySQL

create table TBL_Pers (
Clientnr        int,
Name            varchar,
Post            char(6),
Cont            char(1),
Contdr          tinyint,
p_p_a           smallmoney)

Moving on, I have got a table with info about the printers in the database like so:

MySQL

create table TBL_Printer (
PNr             int,
PBrand          varchar,
PName           varchar,
Serialnr        varchar)

So here comes my question:

Let's say we have 3 different kinds of printers in TBL_Printer and a person owns 2 of them. How do I fill in that information in TBL_Pers so I can extract that information with PHP (To generate the owned printers in a overview on a webpage)?

Upvotes: 0

Views: 52

Answers (2)

user2764220
user2764220

Reputation: 21

You'd better add a third table to connect these two tables.But if you only have three kind of printer you just need to add three colums to the TBL_Pers for each printer.If he or she owns one "1" to the column else fill "0".

Upvotes: 1

juergen d
juergen d

Reputation: 204766

Use another table to relate both

CREATE TABLE person_printer
(
   person  int not null,
   printer int not null,
   PRIMARY KEY (person, printer)
)

Upvotes: 3

Related Questions