Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL not accepting my comments

I am trying to run this sql query in PHPMyAdmin:

--create a mysql contact table
--delete contact table if already exists
DROP TABLE IF EXISTS contact;

--create new table named contact with fields as specified
CREATE TABLE contact(
    contactID int PRIMARY KEY,
    name VARCHAR(50),
    company VARCHAR(30),
    email VARCHAR(50)
);

--add these to the table
INSERT INTO contact VALUES (0, 'Bill Gates', 'Microsoft', '[email protected]');
INSERT INTO contact VALUES (1, 'Larry Page', 'Google', '[email protected]');

--displays whats in this
SELECT * FROM contact;

I thought that in sql this is considered a comment: --I'm a comment

However PHPMyAdmin isn't accepting it.

I get this error:

SQL query:

--create a mysql contact table

--delete contact table if already exists DROP TABLE IF EXISTS contact; 


MySQL said: 

Documentation

1064 - You have an error in your SQL syntax; 

Check the manual that corresponds to your MySQL server version for the right syntax to 

use near '--create a mysql contact table --delete contact table if already exists 

DROP T' at line 1

I get the same error with the same code on these sql checkers also:

http://www.piliapp.com/mysql-syntax-check/ http://sqlfiddle.com/

like image 653
Zachooz Avatar asked Aug 11 '14 21:08

Zachooz


People also ask

How do I comment in a MySQL query?

MySQL Server supports three comment styles: From a # character to the end of the line. From a -- sequence to the end of the line. In MySQL, the -- (double-dash) comment style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on).

How do I view comments in SQL?

You can view the comments on a particular table or column by querying the data dictionary views USER_TAB_COMMENTS , DBA_TAB_COMMENTS , or ALL_TAB_COMMENTS or USER_COL_COMMENTS , DBA_COL_COMMENTS , or ALL_COL_COMMENTS . Specify the name of the operator to be commented.


2 Answers

You need an interval/space after the --

Otherwise it's not considered a valid comment in MySQL.

like image 197
peter.petrov Avatar answered Sep 22 '22 02:09

peter.petrov


You need a space if you use "--" style comments per the manual. Also add a ";" after your create.

-- create a mysql contact table

-- delete contact table if already exists DROP TABLE IF EXISTS contact;

-- create new table named contact with fields as specified CREATE TABLE contact( contactID int PRIMARY KEY, name VARCHAR(50), company VARCHAR(30), email VARCHAR(50) );

-- add these to the table INSERT INTO contact VALUES (0, 'Bill Gates', 'Microsoft', '[email protected]'); INSERT INTO contact VALUES (1, 'Larry Page', 'Google', '[email protected]');

-- displays whats in this SELECT * FROM contact;

like image 35
drewk Avatar answered Sep 21 '22 02:09

drewk