0

我目前正在使用 popsql ide 学习 SQL。我正在尝试运行命令来创建表,但我一直在获取

ER_PARSE_ERROR: 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 '--auto increment automatically adds 1 to the primary key-- name VARCHAR(20) ' at line 2

我究竟做错了什么?下面是sql代码

--writing table in all caps differentiates it from other phrases
--the keyword phrase NOT NULL indicates that the column has to have a value in it at every row
--the UNIQUE keyphrase indicates that the column values have to be unique 

CREATE TABLE student (
    student_id INT AUTO_INCREMENT,--auto increment automatically adds 1 to the primary key
    name VARCHAR(20) NOT NULL,
    major VARCHAR (20) UNIQUE,
    age INT DEFAULT 'undecided', --the DEFAULT keyword will populate a particular row with phrase in quotes if there is no info entered
    PRIMARY KEY(student_id)
); 
--constraints are rules that we set in our db to control what happens to info.
--creating a rule such that a particular keyword should populate a row when no data is in it is a constraint  
DESCRIBE student;
--the keyword 'DROP' deletes any item that we specify--

DROP TABLE student;

--THIS LINE ADDS THE GPA COLUMN TO OUR TABLE--
ALTER TABLE student ADD gpa DECIMAL(3,2);

--this command displays the info that's currently in the table
SELECT * FROM student;

--INSERTING DATA INTO THE TABLE
--the order in which the tables were created should be the order in whcich they should be inserted into the table
--INSERT INTO student VALUES (2,'Rose', 'Chemistry',3.23);

--you can specify what values to add to the database if you don't have a particular key 
INSERT INTO student(name, major) VALUES('John', 'Chemistry');
4

1 回答 1

0

错误信息是正确的:有疑问时应查阅手册。

来自评论的 MySQL 文档

从一个--序列到行尾。在 MySQL 中,-- (双破折号)注释样式要求第二个破折号后跟至少一个空格或控制字符(例如空格、制表符、换行符等)。此语法与标准 SQL 注释语法略有不同,如第 1.7.2.4 节“'--' 作为注释的开始”中所述。

您没有这样做,因此您的语法无效。

于 2020-11-08T02:42:09.903 回答