Sunday, August 26, 2018

mysql - SQL query on PHPMyAdmin to insert data not working




Following is my SQL query:



Attempt 1



INSERT INTO `product`(`productid`, `title`, `category`, `description`) 
VALUES (NULL,`iMac`,`Desktop`,`With its enhanced, big and beautiful display, the new Apple iMac M-D093-B/A 21.5 Desktop Computer renders your movies, photos, web pages and other graphics in truly jaw-dropping detail.`)


Attempt 2




INSERT INTO `product`(`productid`, `title`, `category`, `description`) 
VALUES(` `,`iMac`,`Desktop`,`With its enhanced, big and beautiful display, the new Apple iMac M-D093-B/A 21.5 Desktop Computer renders your movies, photos, web pages and other graphics in truly jaw-dropping detail.`)


I keep getting an error of : MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0003 sec )



I don't understand what am i doing wrong. Here is my column name list



1  productid int(11)    

2 title varchar(100)
3 category varchar(100)
4 description varchar(2000)


table name:product


Answer



For the values, use the ' character, not this one: ``` (sorry, I'll have to find out how to put a single backtick into an inline code block in Markdown...)



INSERT INTO `product`(`productid`, `title`, `category`, `description`) 

VALUES (NULL,'iMac','Desktop','With its enhanced, big and beautiful display, the new Apple iMac M-D093-B/A 21.5 Desktop Computer renders your movies, photos, web pages and other graphics in truly jaw-dropping detail.')


And this should work... Here is the SQLfiddle for it.



EDIT
This is the solution as per zour table definition:



INSERT INTO `product`(`title`, `category`, `description`) 
VALUES ('iMac','Desktop','With its enhanced, big and beautiful display, the new Apple iMac M-D093-B/A 21.5 Desktop Computer renders your movies, photos, web pages and other graphics in truly jaw-dropping detail.')



You had two things you forgot to mention:
* productid is a PRIMARY KEY (and hence, automatically NOT NULL) column -- any inserts with a NULL in that column will fail
* productid is an AUTO_INCREMENT column -- you don't even have to include it in the INSERT statement, it will get filled with an unique value each time you insert a row



The SQL fiddle for this


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...