简体   繁体   English

在 mysql 中创建表和数据库时出现错误 1064 (42000)

[英]ERROR 1064 (42000) while creating a table and database in mysql

I am getting following error while executing my mysql query via a.sql script, but couldn't tell what is causing this error.通过 a.sql 脚本执行 mysql 查询时出现以下错误,但不知道是什么导致了此错误。

I am using mysql Ver 8.0.23 for macos10.15 on x86_64 (MySQL Community Server - GPL)我在 x86_64 上为 macos10.15 使用 mysql Ver 8.0.23(MySQL 社区服务器 - GPL)

Error:错误:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax;第 1 行的错误 1064 (42000):您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '/Users/shivamshekhar/Documents/Others/chat-app-backend/scripts/mysql/db.sql' at line 1检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“/Users/shivamshekhar/Documents/Others/chat-app-backend/scripts/mysql/db.sql”附近使用正确的语法

.sql file .sql 文件

CREATE DATABASE IF NOT EXISTS `chat_app`;  

USE `chat_app`;  

CREATE TABLE IF NOT EXISTS `user_details` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    `name` VARCHAR(255) NOT NULL, 
    `password` VARCHAR(255) NOT NULL, 
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP, 
    `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    INDEX `idx_name` (`name`), 
    CONSTRAINT `uc_name` UNIQUE (`name`)
);

Terminal command:终端命令:

mysql -u admin -ppassword -e ~/Documents/Others/chat-app-backend/scripts/mysql/db.sql

-e command means "execute the command", not "load and execute source file". -e command 表示“执行命令”,而不是“加载并执行源文件”。

Send the source file to the client via stdin:通过标准输入将源文件发送给客户端:

mysql -u admin -ppassword < ~/Documents/Others/chat-app-backend/scripts/mysql/db.sql

Or provide according command to the client:或者向客户端提供相应的命令:

mysql -u admin -ppassword -e "SOURCE ~/Documents/Others/chat-app-backend/scripts/mysql/db.sql"

The syntax is INT UNSIGNED , not UNSIGNED INT .语法是INT UNSIGNED ,而不是UNSIGNED INT In addition, NOT NULL is redundant on a PRIMARY KEY .此外, NOT NULLPRIMARY KEY上是多余的。 So try this:所以试试这个:

CREATE TABLE IF NOT EXISTS `user_details` (
    `id`  INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    `name` VARCHAR(255) NOT NULL, 
    `password` VARCHAR(255) NOT NULL, 
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP, 
    `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    INDEX `idx_name` (`name`), 
    CONSTRAINT `uc_name` UNIQUE (`name`)
);

As already highlighted, it has to be int unsigned.如前所述,它必须是 int unsigned。 In case there is any partition column in the data base, that could be added at the end, specifying the compression type.如果数据库中有任何分区列,可以在最后添加,指定压缩类型。 (Depending upon the context) (视上下文而定)

CREATE TABLE IF NOT EXISTS user_details ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_name ( name ), CONSTRAINT uc_name UNIQUE ( name ) ) partitioned by () stored as parquet (compression =<"compression type">); CREATE TABLE IF NOT EXISTS user_details ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_name ( name ), CONSTRAINT uc_name UNIQUE ( name ) ) 由 () 分区存储为 parquet (compression =<"compression type">);

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

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