MySQL 不允许 VARCHAR 作为主键?

发布于 2024-12-17 19:55:02 字数 444 浏览 5 评论 0原文

我正在尝试创建一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戒ㄋ 2024-12-24 19:55:02

您需要指定 VARCHAR(N),其中 N 是最大字符串长度

You need to specify the VARCHAR(N), where N is the maximum string length

数理化全能战士 2024-12-24 19:55:02

您需要提供 VARCHAR 列的长度。即 VARCHAR(255)

You need to provide the length of the VARCHAR column. i.e. VARCHAR(255)

提赋 2024-12-24 19:55:02

mySQL 你,它试图鼓励你做正确的事情;)

不要对抗正确的事情。有两个字段。

在您的姓名字段中使用 UNIQUE。

就这样使用主键。将其命名为 idstudent_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 or student_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

-小熊_ 2024-12-24 19:55:02

您必须指定该字段将采用多少个字符,例如 VARCHAR(255)

You must specify how many chars the field will take like VARCHAR(255)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文