Tuesday, 9 February 2016

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...