MySQL 不允许 VARCHAR 作为主键?
我正在尝试创建一个名为 Students 的表,其中只有一个字段,student_name。
Student_name 应该是 VARCHAR 和主键,因为我想通过学生的姓名来唯一标识他们。
CREATE TABLE 'swinters'.'Students' (
'student_name' VARCHAR NOT NULL,
PRIMARY KEY ('student_name')
)
ENGINE = InnoDB;
<代码> MYSQL 错误号 1064
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 2 行“NOT NULL, PRIMARY KEY ('student_name') ) ENGINE = InnoDB”附近使用的正确语法。
I'm attempting to create a table called Students, with only one field, student_name.
student_name should be a VARCHAR and a primary key, because I want to uniquely identify students by their name.
CREATE TABLE 'swinters'.'Students' (
'student_name' VARCHAR NOT NULL,
PRIMARY KEY ('student_name')
)
ENGINE = InnoDB;
MYSQL Error Number 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 'NOT NULL, PRIMARY KEY ('student_name') ) ENGINE = InnoDB' at line 2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要指定
VARCHAR(N)
,其中N
是最大字符串长度You need to specify the
VARCHAR(N)
, whereN
is the maximum string length您需要提供 VARCHAR 列的长度。即
VARCHAR(255)
You need to provide the length of the VARCHAR column. i.e.
VARCHAR(255)
mySQL 爱你,它试图鼓励你做正确的事情;)
不要对抗正确的事情。有两个字段。
在您的姓名字段中使用 UNIQUE。
就这样使用主键。将其命名为
id
或student_id
。您可以在这里看到更多信息:
<一href="https://stackoverflow.com/questions/8232943/why-should-i-create-an-id-column-when-i-can-use-others-as-key-fields/8235024#8235024 ">https://stackoverflow.com/questions/8232943/why-should-i-create-an-id-column-when-i-can-use-others-as-key-fields/8235024#8235024
mySQL loves you and it is trying to encourage you to do this right thing ;)
don't fight the right thing. Have two fields.
Use UNIQUE on your name field.
Use the primary key as just that. Call it either
id
orstudent_id
.You can see a lot more info here:
https://stackoverflow.com/questions/8232943/why-should-i-create-an-id-column-when-i-can-use-others-as-key-fields/8235024#8235024
您必须指定该字段将采用多少个字符,例如
VARCHAR(255)
You must specify how many chars the field will take like
VARCHAR(255)