Mysql創建表過程中報1064錯誤

我在自己搭建的mysql服務中,在使用create table創建表時報了1064錯誤,嘗試網上找了各種解決方法,最後還是被自己試着解決了。解決的有的稀裏糊塗的,畢竟我自己對數據庫知識還沒個很清晰的認知。廢話不多說了,下面看我的解決歷程吧。

自己創建表的初衷:想要從無到有的嘗試

set names utf8
set foreign_key_checks=0
drop table if exists `own_reimbursement`;
create table `own_reimbursement` (
    `id` int(10) not null AUTO_INCREMENT,
    `start_time` date not null default,
    `end_time` date not null default,
    `travel_time` int(3) not null default,
    `place_name` char(30) default comment '地名',
    `project_name` char(30) default comment '項目名稱',
    `venue_name` char(30) default comment '場館名稱',
    `personnel_name` char(30) default comment '人員',
    `hotel_expense` float(7) default comment '住宿費',
    `taxi_fare` float(7) not null default comment '打的費',
    `travel_allowance` float(7) not null default comment '出差補助',
    `road_fee` float(7) not null default comment '路費',
    `subsidy` float(7) not null default comment '報銷合計',
    primary key (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8;

BEGIN;
INSERT INTO `own_reimbursement` VALUES ('1', '2018-08-22', '2018-08-31', '10', '江西景德鎮','xx項目','xxxx運動中心','王思聰','1830','195.8','750','738','3513.8');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;

在執行時一直報1064錯誤,讓我百思不得其解,還傻傻的以爲真是version問題,還特意找了相關的version說明看(下了英文版的一臉懵逼的),無賴直接簡單粗暴的在網上搜mysql 創建表示報1064錯誤,還真看到不少解決方法,但沒一條適用的。

[SQL]set names utf8
set foreign_key_checks=0
drop table if exists `own_reimbursement`;
[Err] 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 'set foreign_key_checks=0
drop table if exists `own_reimbursement`' at line 2

網上解決版本:
1.查看create table 語句裏面的表、列、索引都要反斜槓符號也可以不使用,但不能寫成 '單引號。不然執行就會報1064錯誤了
2.不要使用mysql的保留字

我的錯誤是因爲 沒搞清楚default。去掉default後就成功了。

set names utf8;
set foreign_key_checks = 0;
drop table if exists `own_reimbursement`;
create table `own_reimbursement` (
    `id` int(10) not null AUTO_INCREMENT,
    `start_time` date not null ,
    `end_time` date not null ,
    `travel_time` int(3) not null ,
    `place_name` char(30) NOT NULL  comment '地名',
    `project_name` char(30) NOT NULL  comment '項目名稱',
    `venue_name` char(30) NOT NULL comment '場館名稱',
    `personnel_name` char(30) NOT NULL  comment '人員',
    `hotel_expense` float(7)  comment '住宿費',
    `taxi_fare` float(7) not null  comment '打的費',
    `travel_allowance` float(7) not null  comment '出差補助',
    `road_fee` float(7) not null  comment '路費',
    `subsidy` float(7) not null  comment '報銷合計',
    primary key (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS = 1;

原因:
default 修飾符
可以使用 DEFAULT 修飾符爲字段設定一個默認值。
如果一個字段中沒有指定 DEFAULT 修飾符,MySQL 會依據這個字段是 NULL 還是 NOT NULL 自動設置默認值。
如果指定字段可以爲 NULL,則 MySQL 爲其設置默認值爲 NULL。
如果是 NOT NULL 字段,MySQL 對於數值類型插入 0,字符串類型插入空字符串,
時間戳類型插入當前日期和時間,ENUM 類型插入枚舉組的第一條。

如果創建表時要使用default修飾符,那不要忘記在default後面加個默認值。
例如:

CREATE TABLE `websites` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL DEFAULT '' COMMENT '站點名稱',
  `url` varchar(255) NOT NULL DEFAULT '',
  `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
  `country` char(10) NOT NULL DEFAULT '' COMMENT '國家',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章