Question

I am new to mysql views,just came along this problem I have 3 tables

personal(name,age,village)
office(id,company,location)
entertainment(sport,team,music)

Now I create a view using the following syntax

mysql> CREATE VIEW person AS
    ->  SELECT personal.name,personal.age,personal.village,office.id,office.comp
any,office.location,entertainment.sport,entertainment.team,entertainment.music
    -> FROM personal,office,entertainment;

Next I insert into person view

 INSERT INTO person(name,age,village) VALUES ('jay','40','Pune');
 INSERT INTO person(id,company,location) VALUES ('36234','AZcD','Mumbai');
 INSERT INTO person(sport,team,music) VALUES ('football','KKR','POP');

I get the output with this properly.(I had to insert sepeartely or else it gives me an error)

 +------+------+---------+-------+---------+----------+----------+------+-------+

| name | age  | village | id    | company | location | sport    | team | music |

+------+------+---------+-------+---------+----------+----------+------+-------+

| jay  |   40 | Pune    | 36234 | AZcD    | Mumbai   | football | KKR  | POP   |

+------+------+---------+-------+---------+----------+----------+------+-------+

The problem arises now,when I try to insert the same insert queries again with different values ,I get the following output

+-------+------+---------+-------+---------+----------+------------+------+-----
------+
| name  | age  | village | id    | company | location | sport      | team | musi
c     |
+-------+------+---------+-------+---------+----------+------------+------+-----
------+
| jay   |   40 | Pune    | 36234 | AZcD    | Mumbai   | football   | KKR  | POP
      |
| Rohit |   42 | Goa     | 36234 | AZcD    | Mumbai   | football   | KKR  | POP
      |
| jay   |   40 | Pune    | 86234 | YZcD    | Kolkata  | football   | KKR  | POP
      |
| Rohit |   42 | Goa     | 86234 | YZcD    | Kolkata  | football   | KKR  | POP
      |
| jay   |   40 | Pune    | 36234 | AZcD    | Mumbai   | basketball | CSK  | Boll
ywood |
| Rohit |   42 | Goa     | 36234 | AZcD    | Mumbai   | basketball | CSK  | Boll
ywood |
| jay   |   40 | Pune    | 86234 | YZcD    | Kolkata  | basketball | CSK  | Boll
ywood |
| Rohit |   42 | Goa     | 86234 | YZcD    | Kolkata  | basketball | CSK  | Boll
ywood |
+-------+------+---------+-------+---------+----------+------------+------+-----
------+

I get 7 new records instead of just 1.And if you see closely the last record is the right record,I mean the record i inserted. What am I doin wrong? The tables are proper after the above queries:

mysql> select * from personal;
+-------+------+---------+
| name  | age  | village |
+-------+------+---------+
| jay   |   40 | Pune    |
| Rohit |   42 | Goa     |
+-------+------+---------+
2 rows in set (0.00 sec)

mysql> select * from office;
+-------+---------+----------+
| id    | company | location |
+-------+---------+----------+
| 36234 | AZcD    | Mumbai   |
| 86234 | YZcD    | Kolkata  |
+-------+---------+----------+
2 rows in set (0.00 sec)

mysql> select * from entertainment;
+------------+------+-----------+
| sport      | team | music     |
+------------+------+-----------+
| football   | KKR  | POP       |
| basketball | CSK  | Bollywood |
+------------+------+-----------+
2 rows in set (0.00 sec)

Please help.

Was it helpful?

Solution

I think you have a conception problem in your db.

There is no relationships between your tables. They contain all different data, with no reference to other table. For example, i think personal table should contain an office_id, which would be a foreign key (ie reference) of office table.

For example :

mysql> select * from personal;
+-------+------+---------+-----------+
| name  | age  | village | office_id |
+-------+------+---------+-----------+
| jay   |   40 | Pune    | 36234     |
| Rohit |   42 | Goa     | 86234     |
+-------+------+---------+-----------+

And then, you could get a person with his corresponding office with this request :

SELECT personal.name,personal.age,personal.village,office.id,office.company,office.location
    FROM personal,office WHERE personal.office_id = office.id;

OR (with a real JOIN) :

SELECT personal.name,personal.age,personal.village,office.id,office.company,office.location
    FROM personal JOIN office ON personal.office_id = office.id;

You should think your database structure again, then adapt your view by adding joins between tables.

Be careful with insert statements using views : you can have weird behaviours. I always prefer to make insert statements directly into tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top