简体   繁体   English

尝试通过终端在MySQL中创建表时出错

[英]Error trying to create table in MySQL via terminal

I am trying to create a table in my database via terminal. 我试图通过终端在数据库中创建一个表。

Here is my syntax: 这是我的语法:

CREATE TABLE `users` (
   PRIMARY KEY(id) NOT NULL AUTO_INCREMENT,
   `last_name` VARCHAR NOT NULL,
   `first_name` VARCHAR NOT NULL,
   `gender`  VARCHAR NOT NULL,
   `fav_color`  VARCHAR NOT NULL,
   `birthdate`  DATE NOT NULL
);

I am getting this error: 我收到此错误:

ERROR 1064 (42000): 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 AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHA' at line 2

What am i dong wrong here? 我在这里错了什么?

Besides the issues that Jens and Marc pointed out, You have to declare the length of your Varchar fields in order for this statement to work, like so: 除了Jens和Marc指出的问题外,还必须声明Varchar字段的长度才能使该语句起作用,如下所示:

CREATE TABLE `test`.`users` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `last_name` VARCHAR(45) NOT NULL,
  `first_name` VARCHAR(45) NOT NULL,
  `gender` VARCHAR(45) NOT NULL,
  `fav_color` VARCHAR(45) NOT NULL,
  `birthdate` DATE NOT NULL,
  PRIMARY KEY (`id`));

the syntax of your create statement is wrong: 您的create语句的语法错误:

the correct one is this: 正确的是这样的:

CREATE TABLE `users` (
   `id` int NOT NULL AUTO_INCREMENT,
   `last_name` VARCHAR(255) NOT NULL,
   `first_name` VARCHAR(255) NOT NULL,
   `gender`  VARCHAR(255) NOT NULL,
   `fav_color`  VARCHAR(255) NOT NULL,
   `birthdate`  DATE NOT NULL,
   PRIMARY KEY(`id`)
);

For more Information see the offical documentation . 有关更多信息,请参见官方文档

it should be 它应该是

id int primary key auto_increment not null

You're trying to define a primary key on a field that doesn't exist. 您正在尝试在不存在的字段上定义主键。 Keys cannot be "not null" and definitely cannot be "auto_increment". 键不能为“非null”,并且绝对不能为“ auto_increment”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM