0

I have a table name fruits.

FRUITS     ITEMS
______
APPLE        6
ORANGE       7
GRAPES       4

What is the sql query if I don't want to have a duplicate values in fruits. Example if I want to add another values whichs is APPLE 5 and it should not be added

Newboy11
  • 1,592
  • 3
  • 18
  • 28
  • 3
    make the `FRUTS` field `UNIQUE`. – prava Mar 22 '16 at 11:27
  • 1
    Make the FRUITS Field UNIQUE. and then you may use query like this "" Insert into table_name (fruits,item) values ('Apple',' 5') ON DUPLICATE KEY UPDATE items = '5';"" – Priyanshu Mar 22 '16 at 11:45

2 Answers2

0

You should set column fruits to unique. In this way if you try to add duplicate fruits sql won't let you do that.

Daskus
  • 879
  • 1
  • 10
  • 23
0

you should have a unique column, otherwise you can use this query:

INSERT INTO tablename(FRUITS, ITEMS) VALUES('APPLE','5')
WHERE NOT EXISTS
   (SELECT FRUITS
    FROM tablename
    WHERE FRUITS = 'APPLE')
Luthando Ntsekwa
  • 3,914
  • 5
  • 19
  • 49